-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
78 lines (71 loc) · 2.15 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
66
67
68
69
70
71
72
73
74
75
76
77
78
/** Global Parameters Object */
const params = {
debug: false,
hardcore: false,
fps: 60,
totaltime: 0,
cache: [],
};
/**
* @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));
};
/**
* Simple linear interpolation
* @param {Number} start position of original
* @param {Number} end position of target
* @param {Number} amount amount of interpolation between 0.00 and 1.00
* @returns the lerped or percentage in between the start and end values
*/
const lerp = (start, end, amount) => {
return start + (end - start) * amount;
};