canvas学习-------画箭头

时间:2024-03-23 07:42:14

原文出处: https://www.w3cplus.com/canvas/drawing-arrow.html

在这篇文章中主要来聊在Canvas中怎么绘制箭头。在Canvas的CanvasRenderingContext2D对象中是没有提供绘制箭头的方法,言外之意,在Canvas中要绘制箭头是需要自己封装函数来处理。那今天的主题就是来看怎么封装绘制箭头的函数。

了解一些基础知识

平常我们常常看到的一些箭头样式如下图所示:

canvas学习-------画箭头

在绘制箭头最关键之处就是处理箭头:

canvas学习-------画箭头

其包括几个部分:

  • 一条直线,从起点P1到终点P2
  • 终点P2向这条直线两侧扩展,将会产生一个P3P4
  • 另外P2P3或者P2P4构成箭头斜线率
  • 箭头斜线和直线有一个夹角thetaθ
  • 指定箭头的长度d

canvas学习-------画箭头

从上图上我们可以看出,控制一个箭头,可以通过这几个参数来控制:

  • 起点P1(fromX, fromY)
  • 终点P2(toX, toY)
  • 斜线长度headlen
  • 夹角thetaθ

对于箭头的P3P4点,我们就需要通过相应的三角函数计算得来。

canvas学习-------画箭头

那么P3的坐标可以轻易计算出来(错误):

 
  1. p3[0] = P2[0] - Math.cos(θ * Math.PI / 180); // P3对应的X坐标

  2. p3[1] = p2[1] - Math.sin(θ * Math.PI / 180); // P3对应的Y坐标

 

个人补充: 上面应该是错误的。根据上图公式求x,y的值应该是: x=cosθ * r, y=sinθ * r, 所以应该是:

 
  1. p3[0] = P2[0] - Math.cos(θ * Math.PI / 180) * headlen; // P3对应的X坐标

  2. p3[1] = p2[1] - Math.sin(θ * Math.PI / 180) * headlen; // P3对应的Y坐标

用同样的方法可以计算出P4坐标(错误):

 
  1. p4[0] = P2[0] - Math.cos(θ * Math.PI / 180); // P4对应的X坐标

  2. p3[1] = p2[1] + Math.sin(θ * Math.PI / 180); // P4对应的Y坐标

 

同样计P4坐标时headlen也要参与运算:

 
  1. p4[0] = P2[0] - Math.cos(θ * Math.PI / 180) * headlen; // P4对应的X坐标

  2. p3[1] = p2[1] + Math.sin(θ * Math.PI / 180) * headlen; // P4对应的Y坐标


除此之外,还有一个关键,就是箭头的角度。获取箭头的角度,可以直接通过atan2(y,x)来获取。这也就涉及到三角函数中的反正切函数

在三角函数中,两个参数的函数atan2是正切函数tan的一个变种。对于任意不同时等于0的实参数xyatan2(y,x)所表达的意思是坐标原点为起点,指向(x,y)的射线在坐标平面上与x轴正方向之间的角的角度。
y>0时,射线与x轴正方向的所得的角的角度指的是x轴正方向绕逆时针方向到达射线旋转的角的角度;
而当y<0时,射线与x轴正方向所得的角的角度指的是x轴正方向绕顺时针方向达到射线旋转的角的角度。 

在几何意义上,atan2(y, x) 等价于 atan(y/x),但 atan2 的最大优势是可以正确处理 x=0 而 y≠0 的情况,而不必进行会引发除零异常的 y/x 操作。

简单的用下图来阐述:

canvas学习-------画箭头

在一个单位圆内atan2函数在各点的取值。圆内标注代表各点的取值的幅度表示。图片中,从最左端开始,角度的大小随着逆时针方向逐渐从增大到,并且角度大小在点位于最右端时,取值为0。另外要注意的是,函数atan2(y,x)中参数的顺序是倒置的,atan2(y,x)计算的值相当于点(x,y)的角度值。

简单的了解了反正切函数,我们回到我们的主题中。

先来看一张图:

canvas学习-------画箭头

通过Math.atan2()函数计算出angle:

angle = Math.atan2(toY - fromY, toX - fromX)

为了和θ的单位值相匹配,将上面的公式进行一下转换:

angle = Math.atan2(toY - fromY, toX - fromX) * 180 / Math.PI;   //弧度转角度

除此之外,还需要计算出箭头两条侧边线的夹角:

canvas学习-------画箭头

 
  1. angle1 = (angle + theta) * Math.PI / 180; //角度转弧度

  2. angle2 = (angle - theta) * Math.PI / 180; //角度转弧度

感觉有点零乱,其实我自己也瞎折腾了好几天。如果上面的内容不太好理解,建议你移步阅读这篇文章

封装绘制箭头函数

通过前面的内容,可能对绘制箭头有一定的理论基础,接下来,我们看如何封装箭头函数。

drawArrow(ctx, fromX, fromY, toX, toY, theta, headlen, width, color)

这里我们传了九个参数:

  • ctx:Canvas绘图环境
  • fromX, fromY:起点坐标(也可以换成p1,只不过它是一个数组)
  • toX, toY:终点坐标 (也可以换成p2,只不过它是一个数组)
  • theta:三角斜边一直线夹角
  • headlen:三角斜边长度
  • width:箭头线宽度
  • color:箭头颜色

根据前面的内容,我们可以这样来写这个函数:

 
  1. function drawArrow(ctx, fromX, fromY, toX, toY,theta,headlen,width,color) {

  2.  
  3. theta = typeof(theta) != 'undefined' ? theta : 30;

  4. headlen = typeof(theta) != 'undefined' ? headlen : 10;

  5. width = typeof(width) != 'undefined' ? width : 1;

  6. color = typeof(color) != 'color' ? color : '#000';

  7.  
  8. // 计算各角度和对应的P2,P3坐标

  9. var angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI,

  10. angle1 = (angle + theta) * Math.PI / 180,

  11. angle2 = (angle - theta) * Math.PI / 180,

  12. topX = headlen * Math.cos(angle1),

  13. topY = headlen * Math.sin(angle1),

  14. botX = headlen * Math.cos(angle2),

  15. botY = headlen * Math.sin(angle2);

  16.  
  17. ctx.save();

  18. ctx.beginPath();

  19.  
  20. var arrowX = fromX - topX,

  21. arrowY = fromY - topY;

  22.  
  23. ctx.moveTo(arrowX, arrowY);

  24. ctx.moveTo(fromX, fromY);

  25. ctx.lineTo(toX, toY);

  26. arrowX = toX + topX;

  27. arrowY = toY + topY;

  28. ctx.moveTo(arrowX, arrowY);

  29. ctx.lineTo(toX, toY);

  30. arrowX = toX + botX;

  31. arrowY = toY + botY;

  32. ctx.lineTo(arrowX, arrowY);

  33. ctx.strokeStyle = color;

  34. ctx.lineWidth = width;

  35. ctx.stroke();

  36. ctx.restore();

  37. }

这个时候,只需要调用这个封装好的函数,我们就可以轻松的绘制一条向右的箭头:

drawArrow(ctx, 150, 100, 400,100,30,30,10,'#f36');

canvas学习-------画箭头

改变不同的坐标,可以得到不同方向的箭头:

 
  1. // 向右箭头

  2. drawArrow(ctx, myCanvas.width / 2, myCanvas.height / 2, myCanvas.width / 2 + 150, myCanvas.height / 2,30,30,4,'#f36');

  3. // 向下箭头

  4. drawArrow(ctx, myCanvas.width / 2, myCanvas.height / 2, myCanvas.width / 2, myCanvas.height / 2 + 150,30,30,4,'#f66');

  5. // 向左箭头

  6. drawArrow(ctx, myCanvas.width / 2, myCanvas.height / 2, myCanvas.width / 2 - 150, myCanvas.height / 2,30,30,4,'#0f6');

  7. // 向上箭头

  8. drawArrow(ctx, myCanvas.width / 2, myCanvas.height / 2, myCanvas.width / 2, myCanvas.height / 2 - 150,30,30,4,'#d6f');

有的时候,我们需要线的两头都要有箭头形状,在上面的基础上,稍加修改,增加一个反项的代码即可:

 
  1. function drawArrow(ctx, fromX, fromY, toX, toY, theta, headlen, width, color) {

  2. theta = typeof(theta) != 'undefined' ? theta : 30;

  3. headlen = typeof(theta) != 'undefined' ? headlen : 10;

  4. width = typeof(width) != 'undefined' ? width : 1;

  5. color = typeof(color) != 'color' ? color : '#000';

  6.  
  7. var angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI,

  8. angle1 = (angle + theta) * Math.PI / 180,

  9. angle2 = (angle - theta) * Math.PI / 180,

  10. topX = headlen * Math.cos(angle1),

  11. topY = headlen * Math.sin(angle1),

  12. botX = headlen * Math.cos(angle2),

  13. botY = headlen * Math.sin(angle2);

  14.  
  15.  
  16. ctx.save();

  17. ctx.beginPath();

  18.  
  19. var arrowX = fromX - topX,

  20. arrowY = fromY - topY;

  21. ctx.moveTo(arrowX, arrowY);

  22. ctx.lineTo(fromX, fromY);

  23. arrowX = fromX - botX;

  24. arrowY = fromY - botY;

  25. ctx.lineTo(arrowX, arrowY);

  26. ctx.moveTo(fromX, fromY);

  27. ctx.lineTo(toX, toY);

  28.  
  29. // Reverse length on the other side

  30. arrowX = toX + topX;

  31. arrowY = toY + topY;

  32. ctx.moveTo(arrowX, arrowY);

  33. ctx.lineTo(toX, toY);

  34. arrowX = toX + botX;

  35. arrowY = toY + botY;

  36. ctx.lineTo(arrowX, arrowY);

  37. ctx.strokeStyle = color;

  38. ctx.lineWidth = width;

  39. ctx.stroke();

  40. ctx.restore();

  41. }

 

调用函数:

drawArrow(ctx, myCanvas.width / 2 - 200, myCanvas.height / 2, myCanvas.width / 2 + 200,myCanvas.height / 2,30,30,5,'#f36');

看到的效果如下:

canvas学习-------画箭头

上面我们看到的仅是一种箭头方式,文章开头,提到箭头方式有多种方式。那么我们可以将drawArrow功强变得更为强大一些。比如@Patrick Horgan在他的文章中提到的方法:

  • drawHead:封装一个专门绘制箭头头部的函数,而且提供了四种样式做为选择
  • drawArrow: 封装直线箭头,并且提供两个方向
  • drawArcedArrow:函数一个曲线箭头

由于代码较多,这里就不展示出来了,不过可以在对应的CodePen示例中查看到代码: