0

I load object of my canvas with this function

var objects = canvas.getObjects();

But when I try to read the array is empty but so weird in the browserconsole appears like this

Array[0] 0: klass 1: klass length: 2 __proto__: Array[0]

In my other function to resize canvas the array appears with all the objects I dont why is this happening

2
  • I am sorry, could you provide more relevant code, please? Commented Dec 27, 2016 at 8:18
  • I solved it Waiting one second meanwhile fabric is loading objects, when I do getObjects they arent ready to read. I am not sure if this is the best way. But now is solved. Commented Dec 27, 2016 at 20:39

2 Answers 2

3

I was having a very similar issue, but it would only manifest when my application was in production. The issue is the objects are not available until the images are loaded, so even waiting one second as one of the comments suggest will not always work.

My solution is to observe for the object to be added to the canvas:

 canvas.on('object:added', function(object) {
   // Do something here
 });

There is also a related post from a few years ago here:

https://groups.google.com/forum/#!topic/fabricjs/3RpIRfrJBZ0

Sign up to request clarification or add additional context in comments.

Comments

1

I faced such an issue when I was using the loadFromJSON() function.

It happens because you call getObjects() before all objects are rendered. loadFromJSON() accepts a callback, where you can get all canvas items.

Hope, this will help someone.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.