<legend id='51joz'><style id='51joz'><dir id='51joz'><q id='51joz'></q></dir></style></legend>
      1. <tfoot id='51joz'></tfoot>
        • <bdo id='51joz'></bdo><ul id='51joz'></ul>

        <i id='51joz'><tr id='51joz'><dt id='51joz'><q id='51joz'><span id='51joz'><b id='51joz'><form id='51joz'><ins id='51joz'></ins><ul id='51joz'></ul><sub id='51joz'></sub></form><legend id='51joz'></legend><bdo id='51joz'><pre id='51joz'><center id='51joz'></center></pre></bdo></b><th id='51joz'></th></span></q></dt></tr></i><div id='51joz'><tfoot id='51joz'></tfoot><dl id='51joz'><fieldset id='51joz'></fieldset></dl></div>

        <small id='51joz'></small><noframes id='51joz'>

        Html 5 Canvas 完整箭头

        Html 5 Canvas complete arrowhead(Html 5 Canvas 完整箭头)

      2. <small id='3h03p'></small><noframes id='3h03p'>

            <bdo id='3h03p'></bdo><ul id='3h03p'></ul>

                <tbody id='3h03p'></tbody>
              <legend id='3h03p'><style id='3h03p'><dir id='3h03p'><q id='3h03p'></q></dir></style></legend>

                  <i id='3h03p'><tr id='3h03p'><dt id='3h03p'><q id='3h03p'><span id='3h03p'><b id='3h03p'><form id='3h03p'><ins id='3h03p'></ins><ul id='3h03p'></ul><sub id='3h03p'></sub></form><legend id='3h03p'></legend><bdo id='3h03p'><pre id='3h03p'><center id='3h03p'></center></pre></bdo></b><th id='3h03p'></th></span></q></dt></tr></i><div id='3h03p'><tfoot id='3h03p'></tfoot><dl id='3h03p'><fieldset id='3h03p'></fieldset></dl></div>
                  <tfoot id='3h03p'></tfoot>

                  本文介绍了Html 5 Canvas 完整箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 wPaint 插件,并尝试添加更多功能.我需要的是一条以箭头"结尾的画线.我已经尝试了几乎所有我能想到的东西,但我只能得到箭头的一半(想象 <-----,但头部只延伸到底部或顶部,但从来没有两个方向.)

                  I'm using the wPaint plugin and I am attempting to add a few more features. What I need is a drawn line to end with an "arrowhead". I have tried just about everything I could think of, but I can only get half of the arrow ( imagine <-----, but the head only extends to the bottom or the top, but never both directions.)

                  这里是画线的函数(带半箭头):

                  Here is the function for drawing the line (with the half arrowhead):

                    drawArrowMove: function(e, _self)
                    {
                          var xo = _self.canvasTempLeftOriginal;
                          var yo = _self.canvasTempTopOriginal;
                  
                          if(e.pageX < xo) { e.x = e.x + e.w; e.w = e.w * -1}
                          if(e.pageY < yo) { e.y = e.y + e.h; e.h = e.h * -1}
                  
                          _self.ctxTemp.lineJoin = "round";
                          _self.ctxTemp.beginPath();
                          _self.ctxTemp.moveTo(e.x, e.y);
                          _self.ctxTemp.lineTo(e.x + e.w, e.y + e.h);
                  
                          _self.ctxTemp.closePath();
                          _self.ctxTemp.moveTo(e.x, e.y);
                  
                          _self.ctxTemp.lineTo(15,10);                   
                          _self.ctxTemp.stroke();
                    }
                  

                  任何帮助/想法/提示都会有所帮助.

                  Any help/ideas/tips would be helpful.

                  谢谢.

                  推荐答案

                  这是如何创建一个在两端绘制箭头的线对象

                  有趣的部分是这样计算箭头的角度:

                  The interesting part is calculating the angle of the arrowheads like this:

                  var startRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
                  startRadians+=((this.x2>=this.x1)?-90:90)*Math.PI/180;
                  
                  var endRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
                  endRadians+=((this.x2>=this.x1)?90:-90)*Math.PI/180;
                  

                  剩下的只是画线和 2 个三角形作为箭头计算的旋转

                  The rest is just drawing the line and 2 triangles for arrowheads the calculated rotations

                  Line.prototype.drawArrowhead=function(ctx,x,y,radians){
                      ctx.save();
                      ctx.beginPath();
                      ctx.translate(x,y);
                      ctx.rotate(radians);
                      ctx.moveTo(0,0);
                      ctx.lineTo(5,20);
                      ctx.lineTo(-5,20);
                      ctx.closePath();
                      ctx.restore();
                      ctx.fill();
                  }
                  

                  这是代码和小提琴:http://jsfiddle.net/m1erickson/Sg7EZ/

                  <!doctype html>
                  <html>
                  <head>
                  <link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
                  <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
                  
                  <style>
                      body{ background-color: ivory; }
                      canvas{border:1px solid red;}
                  </style>
                  
                  <script>
                  $(function(){
                  
                      var canvas=document.getElementById("canvas");
                      var context=canvas.getContext("2d");
                  
                      function Line(x1,y1,x2,y2){
                          this.x1=x1;
                          this.y1=y1;
                          this.x2=x2;
                          this.y2=y2;
                      }
                      Line.prototype.drawWithArrowheads=function(ctx){
                  
                          // arbitrary styling
                          ctx.strokeStyle="blue";
                          ctx.fillStyle="blue";
                          ctx.lineWidth=1;
                  
                          // draw the line
                          ctx.beginPath();
                          ctx.moveTo(this.x1,this.y1);
                          ctx.lineTo(this.x2,this.y2);
                          ctx.stroke();
                  
                          // draw the starting arrowhead
                          var startRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
                          startRadians+=((this.x2>this.x1)?-90:90)*Math.PI/180;
                          this.drawArrowhead(ctx,this.x1,this.y1,startRadians);
                          // draw the ending arrowhead
                          var endRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
                          endRadians+=((this.x2>this.x1)?90:-90)*Math.PI/180;
                          this.drawArrowhead(ctx,this.x2,this.y2,endRadians);
                  
                  
                      }
                      Line.prototype.drawArrowhead=function(ctx,x,y,radians){
                          ctx.save();
                          ctx.beginPath();
                          ctx.translate(x,y);
                          ctx.rotate(radians);
                          ctx.moveTo(0,0);
                          ctx.lineTo(5,20);
                          ctx.lineTo(-5,20);
                          ctx.closePath();
                          ctx.restore();
                          ctx.fill();
                      }
                  
                      // create a new line object
                      var line=new Line(50,50,150,150);
                      // draw the line
                      line.drawWithArrowheads(context);
                  
                  }); // end $(function(){});
                  </script>
                  
                  </head>
                  
                  <body>
                      <canvas id="canvas" width=300 height=300></canvas>
                  </body>
                  </html>
                  

                  这篇关于Html 5 Canvas 完整箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How does object-fit work with canvas element?(对象适合如何与画布元素一起使用?)
                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)

                    <tbody id='4vT1C'></tbody>
                  <i id='4vT1C'><tr id='4vT1C'><dt id='4vT1C'><q id='4vT1C'><span id='4vT1C'><b id='4vT1C'><form id='4vT1C'><ins id='4vT1C'></ins><ul id='4vT1C'></ul><sub id='4vT1C'></sub></form><legend id='4vT1C'></legend><bdo id='4vT1C'><pre id='4vT1C'><center id='4vT1C'></center></pre></bdo></b><th id='4vT1C'></th></span></q></dt></tr></i><div id='4vT1C'><tfoot id='4vT1C'></tfoot><dl id='4vT1C'><fieldset id='4vT1C'></fieldset></dl></div>
                • <tfoot id='4vT1C'></tfoot>
                • <small id='4vT1C'></small><noframes id='4vT1C'>

                        <bdo id='4vT1C'></bdo><ul id='4vT1C'></ul>

                          1. <legend id='4vT1C'><style id='4vT1C'><dir id='4vT1C'><q id='4vT1C'></q></dir></style></legend>