A
A
annnger2017-08-09 15:48:54
JavaScript
annnger, 2017-08-09 15:48:54

How to make line drawing animation in canvas?

I wrote a function that draws an arrow on the coordinate line from x0 to x1 in the canvas: https://gyazo.com/38ecb65bfec50bf22e25c377430b1e0b . How to make the arrow appear not immediately, but as if it is drawn by hand? I did not find any intelligible tutorials, tell me which way to dig.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
annnger, 2017-08-09
@annnger

function drawCurvedArrow(start, 
                         end, 
                          curveHeigh
) {
  var x0 = 35
  var y0 = 175;
  var divValue=39;
  var arrowWidth = 15;
  var startPointX = x0+start*39;
  var endPointX = x0+end*39
  ctx.save();
  ctx.strokeStyle = 'tomato';
  ctx.lineWidth = 3;
  var curveCenter = (startPointX+endPointX)/2
  var arrowAngle = Math.atan2(curveCenter - endPointX, curveHeigh - y0) + Math.PI;
  ctx.beginPath();
  ctx.moveTo(startPointX, y0);
  ctx.quadraticCurveTo(curveCenter, curveHeigh, endPointX, y0);

  ctx.moveTo(endPointX - (arrowWidth * Math.sin(arrowAngle - Math.PI / 6)), 
             endPointY - (arrowWidth * Math.cos(arrowAngle - Math.PI / 6)));
  ctx.lineTo(endPointX, endPointY);

  ctx.lineTo(endPointX - (arrowWidth * Math.sin(arrowAngle + Math.PI / 6)), 
             endPointY - (arrowWidth * Math.cos(arrowAngle + Math.PI / 6)));
  ctx.stroke();
  ctx.closePath();
  ctx.restore();
}

K
kulaeff, 2017-08-09
@kulaeff

You need to smoke requestAnimationFrame.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question