-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
101 lines (85 loc) · 1.75 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Expose `Rect`.
*/
module.exports = Rect;
/**
* Initialize a new Rect with the given
* position and dimensions, or an object
* with the same properties.
*
* @param {Number|Object} [left]
* @param {Number} [top]
* @param {Number} [width]
* @param {Number} [height]
* @api public
*/
function Rect(left, top, width, height) {
if ('object' == typeof left) {
var o = left;
left = o.left;
top = o.top;
width = o.width;
height = o.height;
}
this.moveTo(left || 0, top || 0);
this.size(width || 0, height || 0);
}
/**
* Move to (left, top).
*
* @param {Number} left
* @param {Number} top
* @api public
*/
Rect.prototype.moveTo = function(left, top){
this.left = this.ox = left;
this.top = this.oy = top;
this.to(left + this.width, top + this.height);
return this;
};
/**
* Resize to `width` / `height`
*
* @param {Number} width
* @param {Number} height
* @api public
*/
Rect.prototype.size = function(width, height){
this.width = width;
this.height = height;
this.right = this.left + width;
this.bottom = this.top + height;
return this;
};
/**
* Move the second point to (right, bottom).
*
* @param {Number} right
* @param {Number} bottom
* @api public
*/
Rect.prototype.to = function(right, bottom){
if (right < this.ox) {
this.left = right;
right = this.ox;
}
if (bottom < this.oy) {
this.top = bottom;
bottom = this.oy;
}
this.size(right - this.left, bottom - this.top);
return this;
};
/**
* Returns true if two rects overlap.
*
* @param {Object} other
* @return {Boolean}
* @api public
*/
Rect.prototype.intersects = function(other){
return !(other.left > this.right
|| other.right < this.left
|| other.top > this.bottom
|| other.bottom < this.top);
}