- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
loadFromJson 采用 3 个参数,第 3 个参数是应用于每个加载对象的函数。如何将 ClipTo 函数应用于加载的每个图像?
我尝试过这个:
this.canvas.loadFromJSON(this.getJson(), () => {
fabric.Image.fromURL(url.replace(/^https:\/\//i, 'http://').concat('?').concat(Math.random().toString(36).substring(7)), (img) => {
img.scaleToWidth(this.canvas.getWidth() - 4)
this.canvas.add(img)
img.sendToBack()
this.canvas.renderAll()
var image = document.getElementById(this.props.product._id+this.props.type+"-productPreviewImage")
image.src = this.canvas.toDataURL()
}, {
crossOrigin: 'anonymous',
objectCaching: false
});
}, (img) => {
if (img.type == 'image') {
img.clipTo = (ctx) => {
img.setCoords();
var clipRect = this.canvas.getObjects().filter(p => p.clipFor === 'layer')[0]
var scaleXTo1 = (1 / img.scaleX);
var scaleYTo1 = (1 / img.scaleY);
ctx.save();
var ctxLeft = -(img.width / 2) + 1;
var ctxTop = -(img.height / 2) + 1;
var ctxWidth = clipRect.width - clipRect.strokeWidth;
var ctxHeight = clipRect.height - clipRect.strokeWidth;
ctx.translate(ctxLeft, ctxTop);
ctx.scale(scaleXTo1, scaleYTo1);
ctx.rotate((img.angle * -1)* (Math.PI / 180))
ctx.beginPath();
ctx.rect(
clipRect.left - img.oCoords.tl.x,
clipRect.top - img.oCoords.tl.y,
clipRect.width,
clipRect.height
);
ctx.closePath();
ctx.restore()
}
}
})
不幸的是,它会引发有关 img.setCoords()
不是函数的错误。
最佳答案
canvas.loadFromJSON(json, canvas.renderAll.bind(canvas), function(o, object) {
// `o` = json object
// `object` = fabric.Object instance
// ... do some stuff ...
});
如文档 loadFromJSON 中所述,reviver 函数有两个参数。第一个是 json 中的当前对象,第二个是 Fabric 对象实例。所以你需要使用第二个参数。
关于javascript - Fabricjs 在 LoadfromJson 之后应用 ClipTo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48279149/
我有一个 Canvas 可以加载不同的页面,这在 JavaScript 上到底是如何工作的?我可以说包含此数据的 object ,在更改页面后,我正在执行类似 delete object; 的操作,我
即使我在 http://fabricjs.com/kitchensink 上尝试过,loadFromJSON 也无法使用 clipto 参数作为演示给出。 对象在 Canvas 上也显示为圆形,但是当
我正在开发这个图形可视化工具。以下是我正在做的示例代码。首先我创建新的 Canvas ,有一个简单的 jQuery 单击事件将图像对象添加到 Canvas 。在处理此 Canvas 后,我需要从数据库
希望有人能帮我解决这个问题。 我正在从 Json 加载 Canvas 并在回调函数中尝试删除其中一个对象。 canvas.loadFromJSON( json, function()
我正在对对象应用滤镜(在 image filters demo 之后),一切正常,但在我保存并加载 Canvas 后,图像滤镜会更改索引。 目前我有四个过滤器,它们按索引应用(如演示中所示)。 0 :
我在使用 Fabric.js 时遇到问题。 当我尝试像这样保存织物 Canvas 时: var data = JSON.stringify(self.canvas.toJSON(['width', '
我有这个 JSON:https://codepen.io/dhavalsisodiya/pen/RJNNXw 该 json 具有附加属性作为 textAnchor。现在的问题是,每当我尝试将该 jso
loadFromJson 采用 3 个参数,第 3 个参数是应用于每个加载对象的函数。如何将 ClipTo 函数应用于加载的每个图像? 我尝试过这个: this.canvas.load
我正在尝试加载一个包含图像对象的 json 对象。图像对象以 base 64 图像数据作为背景。但是我无法加载 loadFromJSON 方法。 代码: var jsonDataSet = '{"ob
我有一个简单的 Canvas ,我在上面添加形状(三 Angular 形、圆形、矩形)。我使用了支持 rgba 的光谱颜色选择器,并且根据所选颜色设置形状的填充。至此一切正常。现在我将 Canvas
我有 2 个 JSON 对象: jSONObject1 {"type":"group","originX":"left","originY":"top","left":0,"top":0,"objec
我正在尝试将视频设置为 Canvas 上的背景,当我第一次设置它时效果很好。但是在使用 loadFromJSON() 重新加载它时,它在 Canvas 上显示空白背景。 我正在使用 angular 6
我在 fabric.js 中遇到了一个奇怪的序列化问题。 我创建了一个带有几个自定义属性的自定义组对象。我已经实现了 toObject() 方法来处理自定义属性。 var customGrpFiel
我使用fabricjs的image.applyFilters功能。当我第一次在 Canvas 上加载图像并应用图像过滤器时,浏览器会阻塞几秒钟,尤其是对于大于 2 MB 的大尺寸图像。但是,当我清除
当我尝试将 JSON 数据加载到 Canvas 上时,每个形状都会被加载,但填充在形状中的图案只有在我点击形状后才会加载。 有人能解释一下为什么会这样吗? 即使我检查了这个: canvas2.load
我目前正在使用 fabrics.js 开发名片设计平台。 我正在尝试实现自动保存功能,通过在浏览器缓存中保存和恢复设计数据来防止用户丢失工作。 所以我使用了Fabrics.js Canvas对象的lo
我在服务器端使用 Fabric.js + Node.js 生成 PNG 图像时遇到问题。我想知道论坛中没有人发现类似的问题。我完全绝望了。这使得在我们的项目中使用 Fabric.js 面临风险。 Fa
我是一名优秀的程序员,十分优秀!