A
A
alkhadidzhaevich2015-02-16 17:05:32
JavaScript
alkhadidzhaevich, 2015-02-16 17:05:32

How to animate on fabric.js?

Hello everyone, I'm trying to learn canvas animation on fabricjs . Everything works, but I can't get good fps in any way. Tell me, please, what am I doing wrong?
First, we create a fabric.canvas object, after which we create a 16x16 array, which I fill with fabric.rect objects:

var canvas = new fabric.Canvas('canvas', { selection: false });

var objects = new Array();
for (var x = 1; x <= 16; x++) {
  objects[x] = new Array();
  for (var y = 1; y <= 16; y++) {
    var rect = new fabric.Rect({
      left: (350/8)*(x-1)+40,
      top: (350/8)*(y-1-8),
      fill: 'red',
      width: 34,
      height: 34
    });
    
    objects[x][y] = {
      type: 'square',
      object: rect
    }
    canvas.add(rect);
  }
}

Then I try to animate them like this:
var startTime = Date.now();
(function animate() {
  var current = Date.now() - startTime;
  for (var x = 1; x <= 16; x++) {
    for (var y = 1; y <= 16; y++) {
      objects[x][y].object.top = fabric.util.ease.easeOutBounce(current, (350/8/5)*(y-1-8), (350/8)*(y-1)+40+((350/8/5)*(y-1-8)*-1), 800);
    }
  }
  if (current > 800) {
    canvas.renderAll();
    return;
  }
    canvas.renderAll();
    fabric.util.requestAnimFrame(animate);
})();

Those. in 800ms, the squares should fall with the easeOutBounce function
Everything seems to be fine, but I just can’t achieve good performance, it still lags on my computer, but I want this animation to always run smoothly, even if the squares are, for example, 32x32
Here is an example on JSFiddle
Tell me what to do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question