-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
rect-helpers.js
266 lines (242 loc) · 5.93 KB
/
rect-helpers.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @param {LH.Artifacts.Rect} rect
* @param {{x:number, y:number}} point
*/
function rectContainsPoint(rect, {x, y}) {
return rect.left <= x && rect.right >= x && rect.top <= y && rect.bottom >= y;
}
/**
* Returns whether rect2 is contained entirely within rect1;
* @param {LH.Artifacts.Rect} rect1
* @param {LH.Artifacts.Rect} rect2
* @return {boolean}
*/
// We sometimes run this as a part of a gatherer script injected into the page, so prevent
// renaming the function for code coverage.
/* c8 ignore start */
function rectContains(rect1, rect2) {
return rect2.top >= rect1.top &&
rect2.right <= rect1.right &&
rect2.bottom <= rect1.bottom &&
rect2.left >= rect1.left;
}
/* c8 ignore stop */
/**
* @param {LH.Artifacts.Rect[]} rects
* @return {LH.Artifacts.Rect[]}
*/
function filterOutTinyRects(rects) {
return rects.filter(
rect => rect.width > 1 && rect.height > 1
);
}
/**
* @param {LH.Artifacts.Rect[]} rects
* @return {LH.Artifacts.Rect[]}
*/
function filterOutRectsContainedByOthers(rects) {
const rectsToKeep = new Set(rects);
for (const rect of rects) {
for (const possiblyContainingRect of rects) {
if (rect === possiblyContainingRect) continue;
if (!rectsToKeep.has(possiblyContainingRect)) continue;
if (rectContains(possiblyContainingRect, rect)) {
rectsToKeep.delete(rect);
break;
}
}
}
return Array.from(rectsToKeep);
}
/**
* @param {LH.Artifacts.Rect} rect
*/
/* c8 ignore start */
function getRectCenterPoint(rect) {
return {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2,
};
}
/* c8 ignore stop */
/**
* @param {LH.Artifacts.Rect} rectA
* @param {LH.Artifacts.Rect} rectB
* @return {boolean}
*/
function rectsTouchOrOverlap(rectA, rectB) {
// https://stackoverflow.com/questions/2752349/fast-rectangle-to-rectangle-intersection
return (
rectA.left <= rectB.right &&
rectB.left <= rectA.right &&
rectA.top <= rectB.bottom &&
rectB.top <= rectA.bottom
);
}
/**
* Returns a bounding rect for all the passed in rects, with padded with half of
* `padding` on all sides.
* @param {LH.Artifacts.Rect[]} rects
* @param {number} padding
* @return {LH.Artifacts.Rect}
*/
function getBoundingRectWithPadding(rects, padding) {
if (rects.length === 0) {
throw new Error('No rects to take bounds of');
}
let left = Number.MAX_VALUE;
let right = -Number.MAX_VALUE;
let top = Number.MAX_VALUE;
let bottom = -Number.MAX_VALUE;
for (const rect of rects) {
left = Math.min(left, rect.left);
right = Math.max(right, rect.right);
top = Math.min(top, rect.top);
bottom = Math.max(bottom, rect.bottom);
}
// Pad on all sides.
const halfMinSize = padding / 2;
left -= halfMinSize;
right += halfMinSize;
top -= halfMinSize;
bottom += halfMinSize;
return {
left,
right,
top,
bottom,
width: right - left,
height: bottom - top,
};
}
/**
* @param {LH.Artifacts.Rect[]} rects
*/
function getBoundingRect(rects) {
return getBoundingRectWithPadding(rects, 0);
}
/**
* @param {{left:number, top:number, right:number, bottom: number}} rect
* @return {LH.Artifacts.Rect}
*/
function addRectWidthAndHeight({left, top, right, bottom}) {
return {
left,
top,
right,
bottom,
width: right - left,
height: bottom - top,
};
}
/**
* @param {{x:number, y:number, width:number, height: number}} rect
* @return {LH.Artifacts.Rect}
*/
function addRectTopAndBottom({x, y, width, height}) {
return {
left: x,
top: y,
right: x + width,
bottom: y + height,
width,
height,
};
}
/**
* @param {LH.Artifacts.Rect} rect1
* @param {LH.Artifacts.Rect} rect2
*/
function getRectOverlapArea(rect1, rect2) {
// https://stackoverflow.com/a/9325084/1290545
const rectYOverlap = Math.min(rect1.bottom, rect2.bottom) - Math.max(rect1.top, rect2.top);
if (rectYOverlap <= 0) return 0;
const rectXOverlap = Math.min(rect1.right, rect2.right) - Math.max(rect1.left, rect2.left);
if (rectXOverlap <= 0) return 0;
return rectXOverlap * rectYOverlap;
}
/**
* @param {LH.Artifacts.Rect} rect
* @param {number} centerRectSize
*/
function getRectAtCenter(rect, centerRectSize) {
return addRectWidthAndHeight({
left: rect.left + rect.width / 2 - centerRectSize / 2,
top: rect.top + rect.height / 2 - centerRectSize / 2,
right: rect.right - rect.width / 2 + centerRectSize / 2,
bottom: rect.bottom - rect.height / 2 + centerRectSize / 2,
});
}
/**
* @param {LH.Artifacts.Rect} rect
*/
/* c8 ignore start */
function getRectArea(rect) {
return rect.width * rect.height;
}
/* c8 ignore stop */
/**
* @param {LH.Artifacts.Rect[]} rects
*/
/* c8 ignore start */
function getLargestRect(rects) {
let largestRect = rects[0];
for (const rect of rects) {
if (getRectArea(rect) > getRectArea(largestRect)) {
largestRect = rect;
}
}
return largestRect;
}
/* c8 ignore stop */
/**
*
* @param {LH.Artifacts.Rect[]} rectListA
* @param {LH.Artifacts.Rect[]} rectListB
*/
function allRectsContainedWithinEachOther(rectListA, rectListB) {
for (const rectA of rectListA) {
for (const rectB of rectListB) {
if (!rectContains(rectA, rectB) && !rectContains(rectB, rectA)) {
return false;
}
}
}
return true;
}
/**
* @param {Array<number>} rect
* @return {LH.Artifacts.Rect}
*/
function traceRectToLHRect(rect) {
const rectArgs = {
x: rect[0],
y: rect[1],
width: rect[2],
height: rect[3],
};
return addRectTopAndBottom(rectArgs);
}
export {
rectContainsPoint,
rectContains,
addRectWidthAndHeight,
addRectTopAndBottom,
getRectOverlapArea,
getRectAtCenter,
getLargestRect,
getRectArea,
getRectCenterPoint,
getBoundingRect,
getBoundingRectWithPadding,
rectsTouchOrOverlap,
allRectsContainedWithinEachOther,
filterOutRectsContainedByOthers,
filterOutTinyRects,
traceRectToLHRect,
};