How to capture the onRendered event from a canvas?

Asked

Viewed 47 times

1

How to know if the canvas is ready?

I’m using the library fabricjs, that creates a complex element structure within a canvas, but I’d like to call a callback function after the canvas had all the complex elements loaded.

html2canvas does something similar, but it doesn’t work with fabricjs:

html2canvas($('#my_canvas'), {
   onrendered: function(canvas) {
   //SIMILAR A ISSO
  }
});

What I can get is the total elements of my canvas:

 var totalItens = canvas.getObjects().length;

And I’m processing a time based on quantity, the problem is that there may be a single very complex item, and the action may not behave well while still being loaded in the browser:

var calc = (totalItens * 400), 
    tempoDoProcesso = (calc) > 6000 ? 6000 : calc

setTimeout({
 //faço a ação...
}, tempoDoProcesso);

The canvas starts to hang when it has many items on the screen... and so it is impossible to do any event before the process has finished.

  • Have you read the documentation? http://fabricjs.com/events

  • canvas.on("after:render", function() {}). Source: https://github.com/fabricjs/fabric.js/wiki/Working-with-events

  • I read yes, but I did not find the information very clear, I had difficulties in finding the solution, a colleague of mine also searched with me here and did not find... if you notice in this link of Events, he is the last of the list, passed beaten. In fact much of the events I have used in lib, I had to hunt around...

1 answer

2


Hello Ivan and for other programmers.

Reading the event documentation on the Fabric.js website will understand everything.

Before using a new javascript library read the documentation to use correctly and there are no problems.

Solution:

var canvas = new fabric.Canvas('my-canvas');
canvas.on("after:render",function() {
  console.log("render on")
});

Source: https://github.com/fabricjs/fabric.js/wiki/Working-with-events

  • Thanks @Anittadeveloper, this is what I was looking for...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.