html画直线箭头


   
   
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset=“UTF-8”>
  5. <title>Text </title>
  6. <script type=“text/javascript”>
  7. window.onload = function(){
  8. arrow2( “myCanvas”, 0, 0, 0, 0, 300, 100)
  9. arrow2( “myCanvas”, 0, 0, 200, 200, 150, 150)
  10. arrow2( “myCanvas”, 0, 0, 20, 200, 150, 150)
  11. arrow2( “myCanvas”, 0, 0, 100, 0, 50, 100)
  12. arrow2( “myCanvas”, 0, 0, 0, 100, 300, 100)
  13. }
  14. function arrow2(canId,ox,oy,x1,y1,x2,y2)
  15. {
  16. //参数说明 canvas的 id ,原点坐标 第一个端点的坐标,第二个端点的坐标
  17. var sta = new Array(x1,y1);
  18. var end = new Array(x2,y2);
  19. var canvas = document.getElementById(canId);
  20. if(canvas == null) return false;
  21. var ctx = canvas.getContext( ‘2d’);
  22. //画线
  23. ctx.beginPath();
  24. ctx.translate(ox,oy, 0); //坐标源点
  25. ctx.moveTo(sta[ 0],sta[ 1]);
  26. ctx.lineTo(end[ 0],end[ 1]);
  27. ctx.fill();
  28. ctx.stroke();
  29. ctx.save();
  30. //画箭头
  31. ctx.translate(end[ 0],end[ 1]);
  32. //我的箭头本垂直向下,算出直线偏离Y的角,然后旋转 ,rotate是顺时针旋转的,所以加个负号
  33. var ang=(end[ 0]-sta[ 0])/(end[ 1]-sta[ 1]);
  34. ang= Math.atan(ang);
  35. if(end[ 1]-sta[ 1] >= 0){
  36. ctx.rotate(-ang);
  37. } else{
  38. ctx.rotate( Math.PI-ang); //加个180度,反过来
  39. }
  40. ctx.lineTo( -5, -10);
  41. ctx.lineTo( 0, -5);
  42. ctx.lineTo( 5, -10);
  43. ctx.lineTo( 0, 0);
  44. ctx.fill(); //箭头是个封闭图形
  45. ctx.restore(); //恢复到堆的上一个状态,其实这里没什么用。
  46. ctx.closePath();
  47. }
  48. </script>
  49. <style type=“text/css”>
  50. canvas{ border: #ccc solid 1px;}
  51. </style>
  52. </head>
  53. <body>
  54. <canvas id=“myCanvas” width=“500” height=“300”> </canvas>
  55. </body>
  56. </html>

转载自https://blog.csdn.net/kongjiea/article/details/23600009#commentBox

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值