我正在尝试在html画布中绘制弯曲的箭头。绘制曲线我没有问题,但我不知道如何将>
放在直线末端(方向)。
ctx.beginPath();
ctx.fillStyle = "rgba(55, 217, 56,"+ opacity +")";
ctx.moveTo(this.fromX,this.fromY);
ctx.quadraticCurveTo(this.controlX, this.controlY, this.toX, this.toY);
ctx.stroke();
我的想法是在最后占据一小部分线并绘制一个三角形。 如何获取线中某点的坐标?
以下是更好理解的图片。
答案 0 :(得分:17)
由于您使用的是二次曲线,因此您知道两条点使得一条线指向箭头的“方向”:
所以抛下一个微微的触发器,你就有了自己的解决方案。这是一个可以为你做的通用函数:
function drawArrowhead(locx, locy, angle, sizex, sizey) {
var hx = sizex / 2;
var hy = sizey / 2;
ctx.translate((locx ), (locy));
ctx.rotate(angle);
ctx.translate(-hx,-hy);
ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(0,1*sizey);
ctx.lineTo(1*sizex,1*hy);
ctx.closePath();
ctx.fill();
ctx.translate(hx,hy);
ctx.rotate(-angle);
ctx.translate(-locx,-locy);
}
// returns radians
function findAngle(sx, sy, ex, ey) {
// make sx and sy at the zero point
return Math.atan2((ey - sy), (ex - sx));
}