-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRectangle.js
41 lines (37 loc) · 1.36 KB
/
Rectangle.js
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
34
35
36
37
38
39
40
41
//Rectangle Class
Rectangle.prototype = new Shape();
Rectangle.prototype.constructor = Rectangle;
function Rectangle(x, y, w, h, angle, fill) {
this.x = x || 0;
this.y = y || 0;
this.w = w || 1;
this.h = h || 1;
this.angleDegrees = normalizeDegreeAngle(angle || 0);
this.fill = fill || '#AAAAAA';
this.updatePoints();
}
// Draws this shape to a given context
Rectangle.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();
}
// Determine if a point is inside the shape's bounds
Rectangle.prototype.contains = function(mx, my) {
// All we have to do is make sure the Mouse X,Y fall in the area between
// the shape's X and (X + Height) and its Y and (Y + Height)
return (this.x <= mx) && (this.x + this.w >= mx) &&
(this.y <= my) && (this.y + this.h >= my);
}
Rectangle.prototype.updatePoints = function(){
this.points = [{x: this.x, y: this.y}, {x: this.x + this.w, y: this.y}, {x: this.x + this.w, y: this.y + this.h}, {x: this.x, y: this.y + this.h}];
this.points = rotatePoints(this.points, this.angleDegrees);
}