Draw arrow on canvas tag

后端 未结 12 910
小鲜肉
小鲜肉 2020-11-27 13:50

I want to draw an arrow using the canvas tag, javascript. I\'ve made it using the quadratic function, but I\'m having problems to calculate the angle of rotation of the arro

12条回答
  •  -上瘾入骨i
    2020-11-27 14:15

    Given a size and the starting position, following code will draw the arrow for you.

    function draw_arrow(context, startX, startY, size) {
      var arrowX = startX + 0.75 * size;
      var arrowTopY = startY - 0.707 * (0.25 * size);
      var arrowBottomY = startY + 0.707 * (0.25 * size);
      context.moveTo(startX, startY);
      context.lineTo(startX + size, startX);
      context.lineTo(arrowX, arrowTopY);
      context.moveTo(startX + size, startX);
      context.lineTo(arrowX, arrowBottomY);
      context.stroke();
    }
    window.onload = function() {
      var canvas = document.getElementById("myCanvas");
      var context = canvas.getContext("2d");
      var startX = 50;
      var startY = 50;
      var size = 100;
      context.lineWidth = 2;
      draw_arrow(context, startX, startY, size);
    };
    body {
      margin: 0px;
      padding: 0px;
    }
    
    #myCanvas {
      border: 1px solid #9C9898;
    }
    
    
    
    
      
    
    
    

提交回复
热议问题