因为你正在使用二次曲线,所以你知道两个点可以构成一条指向箭头的线:
所以只需要进行一些三角函数计算就能得到解决方案。这是一个通用的函数,它可以帮助你完成这个任务:
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));
}
atan
更改为atan2
,以支持负角度并防止除以零。 - Variant