I am new to working with <canvas>and terrible at math. I draw a simple equilateral triangle on my canvas with this function, which takes the code from someone else (don't hate me):
drawTriangle(PosX, PosY, SideLength, Orientation) {
context.beginPath();
var sides = 3;
var a = ((Math.PI * 2) / sides);
context.moveTo(PosX + SideLength, PosY);
for (var i = 1; i < sides + 1; i++) {
context.lineTo(PosX + SideLength * Math.cos(a*i), PosY + SideLength * Math.sin(a*i));
}
context.closePath();
return true;
}
The function will know only the central coordinates of the triangle and the orientation to indicate it, nothing more.
Currently, he draws a triangle successfully, but "points" to the east.
How to rotate a triangle using a parameter Orientation(in degrees) without rotating the entire canvas, as other answers suggest?
source
share