forked from Flipboard/react-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFrameUtils.js
131 lines (117 loc) · 2.88 KB
/
FrameUtils.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
function Frame (x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
* Get a frame object
*
* @param {Number} x
* @param {Number} y
* @param {Number} width
* @param {Number} height
* @return {Frame}
*/
function make (x, y, width, height) {
return new Frame(x, y, width, height);
}
/**
* Return a zero size anchored at (0, 0).
*
* @return {Frame}
*/
function zero () {
return make(0, 0, 0, 0);
}
/**
* Return a cloned frame
*
* @param {Frame} frame
* @return {Frame}
*/
function clone (frame) {
return make(frame.x, frame.y, frame.width, frame.height);
}
/**
* Creates a new frame by a applying edge insets. This method accepts CSS
* shorthand notation e.g. inset(myFrame, 10, 0);
*
* @param {Frame} frame
* @param {Number} top
* @param {Number} right
* @param {?Number} bottom
* @param {?Number} left
* @return {Frame}
*/
function inset (frame, top, right, bottom, left) {
var frameCopy = clone(frame);
// inset(myFrame, 10, 0) => inset(myFrame, 10, 0, 10, 0)
if (typeof bottom === 'undefined') {
bottom = top;
left = right;
}
// inset(myFrame, 10) => inset(myFrame, 10, 10, 10, 10)
if (typeof right === 'undefined') {
right = bottom = left = top;
}
frameCopy.x += left;
frameCopy.y += top;
frameCopy.height -= (top + bottom);
frameCopy.width -= (left + right);
return frameCopy;
}
/**
* Compute the intersection region between 2 frames.
*
* @param {Frame} frame
* @param {Frame} otherFrame
* @return {Frame}
*/
function intersection (frame, otherFrame) {
var x = Math.max(frame.x, otherFrame.x);
var width = Math.min(frame.x + frame.width, otherFrame.x + otherFrame.width);
var y = Math.max(frame.y, otherFrame.y);
var height = Math.min(frame.y + frame.height, otherFrame.y + otherFrame.height);
if (width >= x && height >= y) {
return make(x, y, width - x, height - y);
}
return null;
}
/**
* Compute the union of two frames
*
* @param {Frame} frame
* @param {Frame} otherFrame
* @return {Frame}
*/
function union (frame, otherFrame) {
var x1 = Math.min(frame.x, otherFrame.x);
var x2 = Math.max(frame.x + frame.width, otherFrame.x + otherFrame.width);
var y1 = Math.min(frame.y, otherFrame.y);
var y2 = Math.max(frame.y + frame.height, otherFrame.y + otherFrame.height);
return make(x1, y1, x2 - x1, y2 - y1);
}
/**
* Determine if 2 frames intersect each other
*
* @param {Frame} frame
* @param {Frame} otherFrame
* @return {Boolean}
*/
function intersects (frame, otherFrame) {
return !(otherFrame.x > frame.x + frame.width ||
otherFrame.x + otherFrame.width < frame.x ||
otherFrame.y > frame.y + frame.height ||
otherFrame.y + otherFrame.height < frame.y);
}
module.exports = {
make: make,
zero: zero,
clone: clone,
inset: inset,
intersection: intersection,
intersects: intersects,
union: union
};