Modify fabric.Image.fromURL after initial load using Fabric.js

会有一股神秘感。 提交于 2020-01-02 07:27:10

问题


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. 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.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? Or am I using it incorrectly?


回答1:


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. Outside of the image fromURL function Modify After Load Fiddle



来源:https://stackoverflow.com/questions/24439561/modify-fabric-image-fromurl-after-initial-load-using-fabric-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!