-
Notifications
You must be signed in to change notification settings - Fork 319
/
helpers.core.js
56 lines (49 loc) · 1.68 KB
/
helpers.core.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
export const EPSILON = 0.001;
export const clamp = (x, from, to) => Math.min(to, Math.max(from, x));
export function clampAll(obj, from, to) {
for (const key of Object.keys(obj)) {
obj[key] = clamp(obj[key], from, to);
}
return obj;
}
export function inPointRange(point, center, radius, borderWidth) {
if (!point || !center || radius <= 0) {
return false;
}
const hBorderWidth = borderWidth / 2;
return (Math.pow(point.x - center.x, 2) + Math.pow(point.y - center.y, 2)) <= Math.pow(radius + hBorderWidth, 2);
}
export function inBoxRange(point, {x, y, width, height}, axis, borderWidth) {
const hBorderWidth = borderWidth / 2;
const inRangeX = point.x >= x - hBorderWidth - EPSILON && point.x <= x + width + hBorderWidth + EPSILON;
const inRangeY = point.y >= y - hBorderWidth - EPSILON && point.y <= y + height + hBorderWidth + EPSILON;
if (axis === 'x') {
return inRangeX;
} else if (axis === 'y') {
return inRangeY;
}
return inRangeX && inRangeY;
}
export function getElementCenterPoint(element, useFinalPosition) {
const {x, y} = element.getProps(['x', 'y'], useFinalPosition);
return {x, y};
}
const isOlderPart = (act, req) => req > act || (act.length > req.length && act.slice(0, req.length) === req);
export function requireVersion(pkg, min, ver, strict = true) {
const parts = ver.split('.');
let i = 0;
for (const req of min.split('.')) {
const act = parts[i++];
if (parseInt(req, 10) < parseInt(act, 10)) {
break;
}
if (isOlderPart(act, req)) {
if (strict) {
throw new Error(`${pkg} v${ver} is not supported. v${min} or newer is required.`);
} else {
return false;
}
}
}
return true;
}