How to change the image of the fabric.Image object attribute src?

早过忘川 提交于 2020-08-01 09:52:12

问题


How can I change the image of the fabric.Image object attribute src, if I have already made an animation?


回答1:


You can use .setElement() to change the image.

For example, let's say you have a fabricJS image object called myFabricObect.

Then, if you have an html image element <img id="newImage"> on your page, you can load your myFabricObject with the "newImage" like this:

myFabricObject.setElement(document.getElementById("newImage"));

You could also create a javascript Image() and assign that to myFabricObject:

var img=new Image();
img.onload=function(){
    myFabricObject.setElement(img);
}
img.src="myNewImage.png";



回答2:


This is best option I found

 var activeObject = canvas.getActiveObject();
 activeObject.setSrc(data.url);



回答3:


You can use setSrc():

var activeObject = canvas.getActiveObject();
activeObject.setSrc('images/my-image.png', function(img) {
    canvas.add(img);
});


来源:https://stackoverflow.com/questions/15179976/how-to-change-the-image-of-the-fabric-image-object-attribute-src

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