在画布中旋转时绘制线箭头

时间:2020-03-26 07:18:49

标签: html canvas arrows

在html canvas中绘制箭头的大多数代码都涉及旋转画布上下文并绘制线条。

我的用例是使用三角函数绘制它们而不旋转画布。还是您称之为矢量算法?感谢您的帮助。

这就是我所拥有的(忘记了我获得大部分代码的地方)。根据最后两个布尔值参数arrowStart和arrowEnd在起点和终点绘制2个箭头。

drawLineArrowhead: function(context, arrowStart, arrowEnd) {

        // Place start end points here.
        var x1 = 0;
        var y1 = 0;
        var x2 = 0;
        var y2 = 0;

        var distanceFromLine = 6;
        var arrowLength = 9;
        var dx = x2 - x1;
        var dy = y2 - y1;
        var angle = Math.atan2(dy, dx);
        var length = Math.sqrt(dx * dx + dy * dy);

        context.translate(x1, y1);
        context.rotate(angle);
        context.beginPath();
        context.moveTo(0, 0);
        context.lineTo(length, 0);

        if (arrowStart) {
            context.moveTo(arrowLength, -distanceFromLine);
            context.lineTo(0, 0);
            context.lineTo(arrowLength, distanceFromLine);
        }

        if (arrowEnd) {
            context.moveTo(length - arrowLength, -distanceFromLine);
            context.lineTo(length, 0);
            context.lineTo(length - arrowLength, distanceFromLine);
        }

        context.stroke();
        context.setTransform(1, 0, 0, 1, 0, 0);
    },

1 个答案:

答案 0 :(得分:1)

请看下面的代码,只是一点三角函数。

canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
ctx.lineCap = "round";
ctx.lineWidth = 5;

function drawLineArrowhead(p1, p2, startSize, endSize) {
  ctx.beginPath()
  ctx.moveTo(p1.x, p1.y);
  ctx.lineTo(p2.x, p2.y);
  
  if (startSize > 0) {
    lineAngle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
    delta = Math.PI/6  
    for (i=0; i<2; i++) {
      ctx.moveTo(p1.x, p1.y);
      x = p1.x + startSize * Math.cos(lineAngle + delta)
      y = p1.y + startSize * Math.sin(lineAngle + delta)
      ctx.lineTo(x, y);
      delta *= -1
    }
  }
  if (endSize > 0) {
    lineAngle = Math.atan2(p1.y - p2.y, p1.x - p2.x);
    delta = Math.PI/6  
    for (i=0; i<2; i++) {
      ctx.moveTo(p2.x, p2.y);
      x = p2.x + endSize * Math.cos(lineAngle + delta)
      y = p2.y + endSize * Math.sin(lineAngle + delta)
      ctx.lineTo(x, y);
      delta *= -1
    }
  }
  ctx.stroke();
}

drawLineArrowhead({x:10, y:10}, {x:100, y:20}, 0, 30)
drawLineArrowhead({x:20, y:25}, {x:140, y:120}, 20, 20)
drawLineArrowhead({x:140, y:20}, {x:80, y:50} , 20, 0)

drawLineArrowhead({x:150, y:20}, {x:150, y:90}, 20, 5)
drawLineArrowhead({x:180, y:90}, {x:180, y:20}, 20, 5)

drawLineArrowhead({x:200, y:10}, {x:200, y:140}, 10, 10)
drawLineArrowhead({x:220, y:140}, {x:220, y:10}, 10, 20)
<canvas id="canvas">

如果运行它,应该会看到一些示例。
drawLineArrowhead有4个参数(p1, p2, startSize, endSize) 前两个是该行的起点和终点,后两个是箭头的大小,只是为了使最终用户可以控制这些箭头在末端的大小,如果要删除它们,可以设置到0。