-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.js
More file actions
33 lines (29 loc) · 1.06 KB
/
Copy pathTriangle.js
File metadata and controls
33 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Triangle Class
Triangle.prototype = new Shape();
Triangle.prototype.constructor = Triangle;
function Triangle(x, y, width, angle, fill) {
this.x = x || 0;
this.y = y || 0;
this.width = width || 1; //equilateral triangle
this.angleDegrees = normalizeDegreeAngle(angle || 0);
this.fill = fill || '#AAAAAA';
this.updatePoints();}
// Draws this shape to a given context
Triangle.prototype.draw = function(ctx) {
ctx.fillStyle = this.fill;
ctx.strokeStyle = this.fill;
ctx.beginPath();
ctx.moveTo(this.points[0].x, this.points[0].y);
this.points.forEach(function (point) {
ctx.lineTo(point.x, point.y);
});
ctx.lineTo(this.points[0].x, this.points[0].y);
ctx.closePath();
//ctx.fill();
ctx.stroke();
};
Triangle.prototype.updatePoints = function(){
const height = (Math.sqrt(3) / 2) * this.width;
this.points = [{x: this.x, y: this.y + height}, {x: this.x + this.width, y: this.y + height}, {x: this.x + this.width / 2, y: this.y}];
this.points = rotatePoints(this.points, this.angleDegrees);
}