Ok, por lo que la primera respuesta en esta página me ha ayudado en gran medida cuando yo estaba tratando de entender este problema a mí mismo, a pesar de que otra persona ya se ha dicho, si tiene un ancho de línea mayor que 1 píxel se obtiene formas divertidas . La solución que alguien más sugirió casi funcionó, pero todavía tenía algunos problemas cuando intentaba obtener una flecha de ancho más grueso. Después de varias horas de jugar con él, pude combinar la solución anterior con algunos de mis propios retoques para obtener el siguiente código que dibujará una flecha al grosor que desee sin distorsionar la forma de la flecha.
function drawArrow(fromx, fromy, tox, toy){
//variables to be used when creating the arrow
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var headlen = 10;
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 = 22;
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 = 22;
ctx.stroke();
ctx.fillStyle = "#cc0000";
ctx.fill();
}
Este es ahora el código que estoy usando en mi programa. Lo que descubrí que era la clave para eliminar el problema de la distorsión fue continuar el trazo desde la punta de la flecha a un punto lateral, al otro punto lateral, de vuelta a la punta, y de vuelta al primer punto lateral, y luego hacer un llenar. Esto corrigió la forma de la flecha.
Espero que esto ayude!
que produce una forma extraña, desea deshacerse de ese último movimiento y añadir al final lineTo (tox, juguete) – owook
la función no funciona bien cuando lineWidth no es == 1 –
funciona bien con 2: P '+ 1' –