user2636123
user2636123

Reputation: 75

Image does not load when using canvas.loadFromJSON in Fabric.js 1.3.0

I'm using the latest version:

http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.3.0/fabric.min.js

The test code below simulates a canvas you will save after 1 second. Then load it from JSON. This works on older versions, then on 1.3.0 it stopped working. You would have to keep putting some timeout like:

setTimeout( function(){canvas.renderAll()},500);

to make it work at times but not all the time.

var img= "https://www.google.com/images/srpr/logo3w.png";          
fabric.Image.fromURL(img, function(img) {
        img.set({
        left: 200,
        top: 200   
    });   
    canvas.add(img); 
    canvas.renderAll();
}); 
 setTimeout( function(){
    var json=JSON.stringify(canvas);
    canvas.clear();         
    canvas.loadFromJSON(json);
    canvas.renderAll(); 
    //setTimeout( function(){canvas.renderAll()},500) // adding a timeout seems to be the only fix.
 },1000)

how do I make it render without adding some timeouts like before ?

Upvotes: 1

Views: 5369

Answers (1)

John
John

Reputation: 2033

just use

canvas.loadFromJSON(json,canvas.renderAll.bind(canvas));

Upvotes: 10

Related Questions