How to animate line-drawing in Fabric

梦想的初衷 提交于 2021-01-28 05:53:11

问题


It is not very hard to animate line-drawing in clear Canvas, but i don't understand how do it in Fabric . For example i got this line :

var L = new fabric.Line([50, 100, 200, 200], {
    left: 170,
    top: 150,
    stroke: 'red'
})

and i need that one of its end started to continue till next point ( like a pencil drawing the line ). I know that there is a function 'animate' , but with

L.animate('left', 50, {
onChange: canvas.renderAll.bind(canvas),
duration: 3000
});

my line only changes its top-left coordinates , not changing itself


回答1:


You need to pass the animate property as object in first argument

{
  x2: 200,
  y2: 200
}

DEMO

var canvas = new fabric.Canvas('canvas');
var line = new fabric.Line([50,100,50,100],{
  left: 170,
  top: 150,
  stroke: 'red'
});
canvas.add(line);
line.animate({
  x2: 200,
  y2: 200
}, {
  onChange: canvas.renderAll.bind(canvas),
  onComplete: function() {
    line.setCoords();
  },
  duration: 3000
});
canvas{
 border:2px solid #000;
}
<script src="https://rawgit.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id='canvas' width=500 height=400>


来源:https://stackoverflow.com/questions/50199458/how-to-animate-line-drawing-in-fabric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!