/
bounds.js
43 lines (35 loc) · 918 Bytes
/
bounds.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
export function boundsSize(bounds) {
const [ x, y, width, height ] = bounds;
return width * height;
};
export function compareBoundsSize(rectA, rectB) {
return boundsSize(rectA) > boundsSize(rectB);
};
export function isInRect(x, y, bounds) {
const [ _x, _y, width, height ] = bounds;
return x >= _x
&& x <= _x + width
&& y >= _y
&& y <= _y + height;
};
export function getNodePathByXY(tree, isIOS, x, y) {
let bestBounds = null;
let bestPath = null;
function walk(node, path) {
let bounds = node.bounds;
let inRect = isInRect(x, y, bounds);
if (inRect) {
if (!bestBounds || compareBoundsSize(bestBounds, bounds)) {
bestBounds = bounds;
bestPath = path;
}
if (node.nodes) {
node.nodes.forEach((child, index) => {
walk(child, path.concat([index]));
});
}
}
}
walk(tree, []);
return bestPath;
};