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

前端 未结 3 516
生来不讨喜
生来不讨喜 2021-02-13 17:23

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

相关标签:
3条回答
  • 2021-02-13 17:43

    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";
    
    0 讨论(0)
  • 2021-02-13 17:52

    You can use setSrc():

    var activeObject = canvas.getActiveObject();
    activeObject.setSrc('images/my-image.png', function(img) {
        canvas.add(img);
    });
    
    0 讨论(0)
  • 2021-02-13 17:57

    This is best option I found

     var activeObject = canvas.getActiveObject();
     activeObject.setSrc(data.url);
    
    0 讨论(0)
提交回复
热议问题