let canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
canvas.width = 400;
canvas.height = 400;
class Triangle {
constructor(ptA, ptB, ptC) {
this.ptA = ptA;
this.ptB = ptB;
this.ptC = ptC;
this.translate = {x: 155, y: 100 };
this.centroid = {
ox: (this.ptA.x + this.ptB.x + this.ptC.x) / 3,
oy: (this.ptA.y + this.ptB.y + this.ptC.y) / 3
};
this.c = "red";
this.a = 0;
this.rotation = this.a * (Math.PI / 180);
this.pts = [];
}
draw() {
let t;
this.a -= 0.5;
this.rotation = this.a * (Math.PI / 180);
const cos = Math.cos(this.rotation)
const sin = Math.sin(this.rotation)
ctx.save();
ctx.beginPath();
ctx.fillStyle = this.c;
ctx.setTransform(cos, sin, -sin, cos, this.translate.x, this.translate.y);
t = ctx.getTransform();
ctx.moveTo(this.ptA.x - this.centroid.ox, this.ptA.y - this.centroid.oy);
ctx.lineTo(this.ptB.x - this.centroid.ox, this.ptB.y - this.centroid.oy)
ctx.lineTo(this.ptC.x - this.centroid.ox, this.ptC.y - this.centroid.oy);
ctx.lineTo(this.ptA.x - this.centroid.ox, this.ptA.y - this.centroid.oy);
ctx.fill();
ctx.closePath();
ctx.restore();
this.updateVertices(t);
}
drawVertices() {
for (let i=0; i < this.pts.length; i++) {
ctx.beginPath();
ctx.fillStyle = "blue";
ctx.arc(this.pts[i].x, this.pts[i].y, 3, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
}
updateVertices(t) {
//Explanation:
//t is a variable for getTransform() passed in from draw() method.
//The 7th and 8th arguments are the original point of where the vertex is drawn for that point. The 9th and 10th arguments are how mush the shap has been translated by.
this.pts[0] = calcVertices(t.a, t.b, t.c, t.d, t.e, t.f, this.ptA.x - this.centroid.ox, this.ptA.y - this.centroid.oy, this.translate.x, this.translate.y)
this.pts[1] = calcVertices(t.a, t.b, t.c, t.d, t.e, t.f, this.ptB.x - this.centroid.ox, this.ptB.y - this.centroid.oy, this.translate.x, this.translate.y)
this.pts[2] = calcVertices(t.a, t.b, t.c, t.d, t.e, t.f, this.ptC.x - this.centroid.ox, this.ptC.y - this.centroid.oy, this.translate.x, this.translate.y)
}
}
let triangle = new Triangle({ x: 0, y: 0 }, { x: 50, y: 60 }, { x: 0, y: 100 })
function calcVertices(a, b, c, d, e, f, pX, pY, cx, cy) {
//pX and pY are the original vertex points
let x, y;
x = (e + pX - cx) * a + (f + pY - cy) * c + (e);
y = (e + pX - cx) * b + (f + pY - cy) * d + (f);
return {x: x, y: y}
}
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
triangle.draw();
triangle.drawVertices();
requestAnimationFrame(animate);
}
animate();
<canvas id="canvas"></canvas>