forked from kjirou/box-overlap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.js
77 lines (65 loc) · 2.29 KB
/
dist.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
"use strict";
/**
* @param {Object} params
* @param {number} [params.top]
* @param {number} [params.right]
* @param {number} [params.bottom]
* @param {number} [params.left]
* @param {number} [params.x]
* @param {number} [params.y]
* @param {number} [params.width]
* @param {number} [params.height]
* @return {{top, left, bottom, right}}
*/
var normalizeToTopLeftBottomRight = function normalizeToTopLeftBottomRight(params) {
var points = {
top: null,
left: null,
bottom: null,
right: null
};
Object.keys(points).forEach(function (k) {
if (k in params) points[k] = params[k];
});
if ('x' in params && points.left === null) {
points.left = params.x;
}
if ('y' in params && points.top === null) {
points.top = params.y;
}
if (points.top === null || points.left === null) {
throw new Error('`top` or `left` can not be calculated');
}
if ('width' in params && points.right === null) {
points.right = points.left + params.width;
}
if ('height' in params && points.bottom === null) {
points.bottom = points.top + params.height;
}
if (points.bottom === null || points.right === null) {
throw new Error('`bottom` or `right` can not be calculated');
}
if (points.bottom < points.top) {
throw new Error('`bottom` is less than `top`');
} else if (points.right < points.left) {
throw new Error('`right` is less than `left`');
}
return points;
};
/**
* Check whether two boxes overlap
* @param {Object} boxA - A value set like DOMRect (https://www.w3.org/TR/geometry-1/#DOMRect)
* e.g. { top, left, bottom, right } or { x, y, width, height }
* @param {Object} boxB
* @return {boolean}
*/
var areBoxesOverlapping = function areBoxesOverlapping(boxA, boxB) {
var a = normalizeToTopLeftBottomRight(boxA);
var b = normalizeToTopLeftBottomRight(boxB);
return (a.top < b.top && a.bottom > b.top || a.top > b.top && b.bottom > a.top || a.top === b.top && a.top < a.bottom && b.top < b.bottom // It means that both `a` and `b` have thickness
) && (a.left < b.left && a.right > b.left || a.left > b.left && b.right > a.left || a.left === b.left && a.left < a.right && b.left < b.right);
};
module.exports = {
_normalizeToTopLeftBottomRight: normalizeToTopLeftBottomRight,
areBoxesOverlapping: areBoxesOverlapping
};