简体   繁体   English

使用Fabric.js初始加载后修改fabric.Image.fromURL

[英]Modify fabric.Image.fromURL after initial load using Fabric.js

I'm using theFabric.js library, and I've seen a lot of examples on how to load an image from a URL using Image.fromURL. 我正在使用theFabric.js库,我已经看到很多关于如何使用Image.fromURL从URL加载图像的示例。 Almost every example assigns the image to a variable, using this general setup: 几乎每个示例都使用以下常规设置将图像分配给变量:

var bgnd = new fabric.Image.fromURL(bgndURL, function(oImg){
    oImg.hasBorders = false;
    oImg.hasControls = false;
    // ... Modify other attributes
    canvas.insertAt(oImg,0);
});

I've found that the attributes of the image can only be modified within the callback function when the image finishes loading. 我发现图像的属性只能在图像完成加载时在回调函数中修改。 Is there a way to modify its attributes at a later time? 有没有办法在以后修改其属性? I tried changing the attributes of the bgnd variable directly but it doesn't do anything. 我尝试直接更改bgnd变量的属性,但它没有做任何事情。

bgnd.set({left: 20, top: 50});
canvas.renderAll();

or 要么

bgnd.rotation = 45;
canvas.renderAll();

they don't do anything. 他们什么都不做。 What's the point of assigning the fabric.Image object to the bgnd variable if this variable can't be accessed at a later time? 如果以后无法访问此变量,那么将bgnd对象分配给bgnd变量有什么意义呢? Or am I using it incorrectly? 还是我错误地使用它?

After you insert the image using 使用后插入图像后

canvas.insertAt(oImg,0);

canvas.setActiveObject(oImg);
myImg= canvas.getActiveObject();

You should be able to change it using 你应该可以使用它来改变它

myImg.rotation = 45;

Remember to create the myImg variable as a global variable. 请记住将myImg变量创建为全局变量。 Outside of the image fromURL function Modify After Load Fiddle 来自URL函数的图像外部修改后加载小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM