fabricjs / fabric.js

Javascript Canvas Library, SVG-to-Canvas (& canvas-to-SVG) Parser
http://fabricjs.com
Other
29.06k stars 3.51k forks source link

.loadFromJSON loses backgroundImage property in the lastest version 1.4.0 #1295

Closed manfredjb closed 10 years ago

manfredjb commented 10 years ago

In order to get details of a possible bug please see this recent issue closed:

https://github.com/kangax/fabric.js/issues/1293

kangax commented 10 years ago

1.4.5 is the latest — https://github.com/kangax/fabric.js/releases/tag/v1.4.5

manfredjb commented 10 years ago

Hi Kangax,

I have tested it using and the backgroundImage property still going missed. I used 1.4.5 version:

https://github.com/kangax/fabric.js/blob/master/dist/fabric.min.js  

Thank you in advance

Kienz commented 10 years ago

@manfredjb Can you please create jsfiddle testcase. Thanks a lot!

manfredjb commented 10 years ago

Hello @Kienz , apologies for delay. I haven't seen your replay. Well, It took a long time to reproduce the error:

http://jsfiddle.net/manix/8GWH6/

The possible bug is basically with canvas.toJSON() in onLoad time. Take a look how I have set a background image (example, line 3) Then, you will see that the backgroundImage property is lost.

Thank you for your time.

Kienz commented 10 years ago

@manfredjb canvas.setBackgroundImage is async. If you want to serialize your canvas in the onload event, you have to call canvas.toJSON() in the callback function of canvas.setBackgroundImage.

Something like that:

var canvas = new fabric.Canvas('c');
canvas.isDrawingMode = true;
canvas.setBackgroundImage("https://lh6.googleusercontent.com/-nI_6EJ8gyDU/AAAAAAAAAAI/AAAAAAABQz4/uQHZE0iWnYs/s120-c/photo.jpg", function() {
  canvas.renderAll();
  console.log(canvas.toJSON());
});

Updated jsfiddle: http://jsfiddle.net/Kienz/8GWH6/6/

manfredjb commented 10 years ago

Excellent! Works as expected. Thank you so much for clarify and time.