GoldenGonaz
GoldenGonaz

Reputation: 1176

FabricJS animating :selected object on canvas during mouse:up and :down

I am trying to use FabricJS to animate any object when it is selected.

So when selected an object 'shrinks' from center on mouse:down (10px off the width and 10px off the height)

And on mouse:up it returns to the original size and state.

I don't know if I need to use FabricJS built in animation for this. I wanted it to grow and shrink linear though.

Right now my Fiddle does pretty much nothing, I can't get far enough without errors.

Does anyone have any examples of this or similar? I couldn't find anything about changing the appearance of FabricJS objects on mouse:up/:down when selected.

(function() {
  var canvas = this.__canvas = new fabric.Canvas('canvas');
  var touchedObject;

  var rect = new fabric.Rect({
    left: 50,
    top: 50,
    width: 50,
    height: 50,
    fill: 'blue',
    hasBorders: false,
  });
  canvas.add(rect);

  var rect2 = new fabric.Rect({
    left: 190,
    top: 50,
    width: 50,
    height: 50,
    fill: 'red',
    hasBorders: false,
  });
  canvas.add(rect2);

  canvas.on('object:selected', function(evn) {
    /*animate({
      'width': ,
      'height':
    }, {
      duration: 200,
    });*/
  });

  canvas.renderAll();
})();

Upvotes: 1

Views: 1240

Answers (1)

ɢʀᴜɴᴛ
ɢʀᴜɴᴛ

Reputation: 32889

You could accomplish that using the fabric.util.animate() method.

(function() {
    var canvas = this.__canvas = new fabric.Canvas('canvas');
    var touchedObject;
    var rect = new fabric.Rect({
        left: 50,
        top: 50,
        width: 50,
        height: 50,
        fill: '#07C',
        hasBorders: false,
    });
    canvas.add(rect);
		
    // disable controls and set hover-cursor
    canvas.forEachObject(function(o) {
        o.hasBorders = o.hasControls = false;
    });
    canvas.hoverCursor = 'pointer';
		
    // mouse events
    canvas.on('mouse:down', function(e) {
        animate(e, 1);
    });
    canvas.on('mouse:up', function(e) {
        animate(e, 0);
    });

    function animate(e, p) {
        if (e.target) {
            fabric.util.animate({
                startValue: e.target.get('height'),
                endValue: e.target.get('height') + (p ? -10 : 50 - e.target.height),
                duration: 200,
                onChange: function(v) {
                    e.target.setHeight(v);
                    canvas.renderAll();
                },
                onComplete: function() {
                    e.target.setCoords();
                }
            });
            fabric.util.animate({
                startValue: e.target.get('width'),
                endValue: e.target.get('width') + (p ? -10 : 50 - e.target.width),
                duration: 200,
                onChange: function(v) {
                    e.target.setWidth(v);
                    canvas.renderAll();
                },
                onComplete: function() {
                    e.target.setCoords();
                }
            });
            fabric.util.animate({
                startValue: e.target.get('top'),
                endValue: e.target.get('top') + (p && 5),
                duration: 200,
                onChange: function(v) {
                    e.target.setTop(v);
                    canvas.renderAll();
                },
                onComplete: function() {
                    e.target.setCoords();
                }
            });
            fabric.util.animate({
                startValue: e.target.get('left'),
                endValue: e.target.get('left') + (p && 5),
                duration: 200,
                onChange: function(v) {
                    e.target.setLeft(v);
                    canvas.renderAll();
                },
                onComplete: function() {
                    e.target.setCoords();
                }
            });
        }
    }
    canvas.renderAll();
})();
#canvas {
  border: 1px solid lightgrey;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js"></script>
<canvas id="canvas" width="150"></canvas>

Upvotes: 2

Related Questions