Draw arrow on canvas tag

后端 未结 12 909
小鲜肉
小鲜肉 2020-11-27 13:50

I want to draw an arrow using the canvas tag, javascript. I\'ve made it using the quadratic function, but I\'m having problems to calculate the angle of rotation of the arro

12条回答
  •  自闭症患者
    2020-11-27 14:20

    Ok, so the first answer on this page helped me greatly when I was trying to figure this problem out myself, although as someone else already stated, if you have a line width greater than 1px you get funny shapes. The fix that someone else suggested almost worked, but I still had some issues when trying to go for a thicker width arrow. After several hours of playing around with it I was able to combine the above solution with some of my own tinkering to come up with the following code that will draw an arrow at whatever thickness you desire without distorting the arrow shape.

    function drawArrow(fromx, fromy, tox, toy){
                    //variables to be used when creating the arrow
                    var c = document.getElementById("myCanvas");
                    var ctx = c.getContext("2d");
                    const width = 22;
                    var headlen = 10;
                    // This makes it so the end of the arrow head is located at tox, toy, don't ask where 1.15 comes from
                    tox -= Math.cos(angle) * ((width*1.15));
                    toy -= Math.sin(angle) * ((width*1.15));
    
                    var angle = Math.atan2(toy-fromy,tox-fromx);
                    
                    //starting path of the arrow from the start square to the end square and drawing the stroke
                    ctx.beginPath();
                    ctx.moveTo(fromx, fromy);
                    ctx.lineTo(tox, toy);
                    ctx.strokeStyle = "#cc0000";
                    ctx.lineWidth = width;
                    ctx.stroke();
                    
                    //starting a new path from the head of the arrow to one of the sides of the point
                    ctx.beginPath();
                    ctx.moveTo(tox, toy);
                    ctx.lineTo(tox-headlen*Math.cos(angle-Math.PI/7),toy-headlen*Math.sin(angle-Math.PI/7));
                    
                    //path from the side point of the arrow, to the other side point
                    ctx.lineTo(tox-headlen*Math.cos(angle+Math.PI/7),toy-headlen*Math.sin(angle+Math.PI/7));
                    
                    //path from the side point back to the tip of the arrow, and then again to the opposite side point
                    ctx.lineTo(tox, toy);
                    ctx.lineTo(tox-headlen*Math.cos(angle-Math.PI/7),toy-headlen*Math.sin(angle-Math.PI/7));
    
                    //draws the paths created above
                    ctx.strokeStyle = "#cc0000";
                    ctx.lineWidth = width;
                    ctx.stroke();
                    ctx.fillStyle = "#cc0000";
                    ctx.fill();
                }
    

    This is now the code that I am using in my program. What I found to be the key with eliminating the distortion issue was continuing the stroke from the tip of the arrow to one side point, to the other side point, back to the tip, and back over to the first side point, then doing a fill. This corrected the shape of the arrow.

    Hope this helps!

提交回复
热议问题