-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
65 lines (58 loc) · 1.91 KB
/
util.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
/** Global Parameters Object */
const params = {};
/**
* @param {Number} n
* @returns Random Integer Between 0 and n-1
*/
const randomInt = n => Math.floor(Math.random() * n);
/**
* @param {Number} r Red Value
* @param {Number} g Green Value
* @param {Number} b Blue Value
* @returns String that can be used as a rgb web color
*/
const rgb = (r, g, b) => `rgba(${r}, ${g}, ${b})`;
/**
* @param {Number} r Red Value
* @param {Number} g Green Value
* @param {Number} b Blue Value
* @param {Number} a Alpha Value
* @returns String that can be used as a rgba web color
*/
const rgba = (r, g, b, a) => `rgba(${r}, ${g}, ${b}, ${a})`;
/**
* @param {Number} h Hue
* @param {Number} s Saturation
* @param {Number} l Lightness
* @returns String that can be used as a hsl web color
*/
const hsl = (h, s, l) => `hsl(${h}, ${s}%, ${l}%)`;
/** Creates an alias for requestAnimationFrame for backwards compatibility */
window.requestAnimFrame = (() => {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
/**
* Compatibility for requesting animation frames in older browsers
* @param {Function} callback Function
* @param {DOM} element DOM ELEMENT
*/
((callback, element) => {
window.setTimeout(callback, 1000 / 60);
});
})();
/**
* Returns distance from two points
* @param {Number} p1, p2 Two objects with x and y coordinates
* @returns Distance between the two points
*/
const getDistance = (p1, p2) => {
return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
};
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min); // The maximum is inclusive and the minimum is inclusive
}