在 Canvas 内绘制直线箭头而不旋转

Draw Line Arrowhead Without Rotating in Canvas

在 html canvas 中绘制箭头的大部分代码涉及旋转 canvas 上下文和绘制线条。

我的用例是在不旋转 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);
    },

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

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.