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

Faradey picture Faradey · Mar 2, 2013 · Viewed 11.6k times · Source

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

Answer

markE picture markE · Mar 3, 2013

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";