toosoon-utils 3.1.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/classes/_pool.d.ts +2 -2
- package/lib/classes/_pool.js +22 -50
- package/lib/classes/color-scale.d.ts +1 -1
- package/lib/classes/color-scale.js +67 -88
- package/lib/classes/frame-rate.js +19 -27
- package/lib/colors.d.ts +1 -1
- package/lib/colors.js +85 -123
- package/lib/constants.js +6 -9
- package/lib/dom.js +10 -17
- package/lib/files.js +8 -15
- package/lib/functions.d.ts +1 -1
- package/lib/functions.js +27 -40
- package/lib/geometry.d.ts +11 -11
- package/lib/geometry.js +30 -43
- package/lib/maths.js +22 -51
- package/lib/prng.js +43 -70
- package/lib/query.js +9 -15
- package/lib/random.d.ts +1 -1
- package/lib/random.js +30 -62
- package/lib/strings.js +17 -30
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.d.ts +6 -6
- package/lib/types.js +1 -2
- package/package.json +3 -1
- package/tsconfig.json +17 -11
package/lib/functions.js
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.now = exports.defer = exports.throttle = exports.debounce = exports.wait = exports.noop = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* No-op function
|
|
6
3
|
*/
|
|
7
|
-
|
|
8
|
-
exports.noop = noop;
|
|
4
|
+
export const noop = () => { };
|
|
9
5
|
/**
|
|
10
6
|
* Promise wrapped setTimeout
|
|
11
7
|
*
|
|
12
8
|
* @param {number} [delay=0] Time to wait (in milliseconds)
|
|
13
9
|
* @returns {Promise}
|
|
14
10
|
*/
|
|
15
|
-
function wait(delay) {
|
|
16
|
-
|
|
17
|
-
return new Promise(function (resolve) { return setTimeout(resolve, delay); });
|
|
11
|
+
export function wait(delay = 0) {
|
|
12
|
+
return new Promise((resolve) => setTimeout(resolve, delay));
|
|
18
13
|
}
|
|
19
|
-
exports.wait = wait;
|
|
20
14
|
/**
|
|
21
15
|
* Create a debounced function that delays the execution of `callback` until a specified `delay` time has passed since the last call
|
|
22
16
|
*
|
|
@@ -24,19 +18,14 @@ exports.wait = wait;
|
|
|
24
18
|
* @param {number} delay Delay (in milliseconds)
|
|
25
19
|
* @returns {Function} Debounced function
|
|
26
20
|
*/
|
|
27
|
-
function debounce(callback, delay) {
|
|
28
|
-
|
|
29
|
-
return
|
|
30
|
-
var args = [];
|
|
31
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
32
|
-
args[_i] = arguments[_i];
|
|
33
|
-
}
|
|
21
|
+
export function debounce(callback, delay) {
|
|
22
|
+
let timeout = null;
|
|
23
|
+
return (...args) => {
|
|
34
24
|
if (timeout)
|
|
35
25
|
clearTimeout(timeout);
|
|
36
|
-
timeout = setTimeout(
|
|
26
|
+
timeout = setTimeout(() => callback(...args), delay);
|
|
37
27
|
};
|
|
38
28
|
}
|
|
39
|
-
exports.debounce = debounce;
|
|
40
29
|
/**
|
|
41
30
|
* Create a throttled function that limits the execution of `callback` to once every `limit` time
|
|
42
31
|
*
|
|
@@ -44,54 +33,52 @@ exports.debounce = debounce;
|
|
|
44
33
|
* @param {number} limit Minimum interval between two calls (in milliseconds)
|
|
45
34
|
* @returns {Function} Throttled function
|
|
46
35
|
*/
|
|
47
|
-
function throttle(callback, limit) {
|
|
48
|
-
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
52
|
-
args[_i] = arguments[_i];
|
|
53
|
-
}
|
|
54
|
-
var time = (0, exports.now)();
|
|
36
|
+
export function throttle(callback, limit) {
|
|
37
|
+
let lastTime = 0;
|
|
38
|
+
return (...args) => {
|
|
39
|
+
const time = now();
|
|
55
40
|
if (time - lastTime >= limit) {
|
|
56
41
|
lastTime = time;
|
|
57
|
-
callback
|
|
42
|
+
callback(...args);
|
|
58
43
|
}
|
|
59
44
|
};
|
|
60
45
|
}
|
|
61
|
-
exports.throttle = throttle;
|
|
62
46
|
/**
|
|
63
47
|
* Deferred promise implementation
|
|
64
48
|
*
|
|
65
|
-
* @returns {
|
|
49
|
+
* @returns {object}
|
|
66
50
|
*/
|
|
67
|
-
function defer() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
51
|
+
export function defer() {
|
|
52
|
+
let resolve;
|
|
53
|
+
let reject;
|
|
54
|
+
const promise = new Promise((_resolve, _reject) => {
|
|
71
55
|
resolve = _resolve;
|
|
72
56
|
reject = _reject;
|
|
73
57
|
});
|
|
74
|
-
return { promise
|
|
58
|
+
return { promise, resolve, reject };
|
|
75
59
|
}
|
|
76
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Polyfill for `now()` functions
|
|
62
|
+
*/
|
|
63
|
+
export let now;
|
|
77
64
|
// In node.js, use `process.hrtime`
|
|
78
65
|
if (typeof process !== 'undefined' && process.hrtime) {
|
|
79
|
-
|
|
66
|
+
now = function () {
|
|
80
67
|
// Convert [seconds, nanoseconds] to milliseconds
|
|
81
|
-
|
|
68
|
+
const time = process.hrtime();
|
|
82
69
|
return time[0] * 1000 + time[1] / 1000000;
|
|
83
70
|
};
|
|
84
71
|
}
|
|
85
72
|
// In a browser use `performance` or `Date`
|
|
86
73
|
else if (typeof performance !== 'undefined') {
|
|
87
74
|
// This must be bound, because directly assigning this function leads to an invocation exception in Chrome
|
|
88
|
-
|
|
75
|
+
now = performance.now.bind(performance);
|
|
89
76
|
}
|
|
90
77
|
else if (typeof Date.now !== 'undefined') {
|
|
91
|
-
|
|
78
|
+
now = Date.now;
|
|
92
79
|
}
|
|
93
80
|
else {
|
|
94
|
-
|
|
81
|
+
now = function () {
|
|
95
82
|
return new Date().getTime();
|
|
96
83
|
};
|
|
97
84
|
}
|
package/lib/geometry.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Vector3 } from './types';
|
|
1
|
+
import type { Vector3 } from './types';
|
|
2
2
|
/**
|
|
3
3
|
* Convert a radians value into degrees
|
|
4
4
|
*
|
|
@@ -59,30 +59,30 @@ export declare function diagonal(width: number, height: number): number;
|
|
|
59
59
|
* @returns {Vector3}
|
|
60
60
|
*/
|
|
61
61
|
export declare function radToSphere(radius: number, phi: number, theta: number, target?: Vector3): Vector3;
|
|
62
|
-
export
|
|
62
|
+
export type FitInput = {
|
|
63
63
|
width: number;
|
|
64
64
|
height: number;
|
|
65
|
-
}
|
|
66
|
-
export
|
|
65
|
+
};
|
|
66
|
+
export type FitOutput = {
|
|
67
67
|
left: number;
|
|
68
68
|
top: number;
|
|
69
69
|
width: number;
|
|
70
70
|
height: number;
|
|
71
71
|
scale: number;
|
|
72
|
-
}
|
|
72
|
+
};
|
|
73
73
|
/**
|
|
74
74
|
* Make a target fit a container (cover mode)
|
|
75
75
|
*
|
|
76
|
-
* @param {
|
|
77
|
-
* @param {
|
|
78
|
-
* @returns {
|
|
76
|
+
* @param {object} target Dimensions of the target
|
|
77
|
+
* @param {object} container Dimensions of the container
|
|
78
|
+
* @returns {object}
|
|
79
79
|
*/
|
|
80
80
|
export declare function cover(target: FitInput, container: FitInput): FitOutput;
|
|
81
81
|
/**
|
|
82
82
|
* Make a target fit a container (contain mode)
|
|
83
83
|
*
|
|
84
|
-
* @param {
|
|
85
|
-
* @param {
|
|
86
|
-
* @returns {
|
|
84
|
+
* @param {object} target Dimensions of the target
|
|
85
|
+
* @param {object} container Dimensions of the container
|
|
86
|
+
* @returns {object}
|
|
87
87
|
*/
|
|
88
88
|
export declare function contain(target: FitInput, container: FitInput): FitOutput;
|
package/lib/geometry.js
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.contain = exports.cover = exports.radToSphere = exports.diagonal = exports.distance = exports.closestAngle = exports.angle = exports.toRadians = exports.toDegrees = void 0;
|
|
4
|
-
var constants_1 = require("./constants");
|
|
1
|
+
import { PI } from './constants';
|
|
5
2
|
/**
|
|
6
3
|
* Convert a radians value into degrees
|
|
7
4
|
*
|
|
8
5
|
* @param {number} radians Angle in radians
|
|
9
6
|
* @returns {number} Angle in degrees
|
|
10
7
|
*/
|
|
11
|
-
function toDegrees(radians) {
|
|
12
|
-
return (radians * 180) /
|
|
8
|
+
export function toDegrees(radians) {
|
|
9
|
+
return (radians * 180) / PI;
|
|
13
10
|
}
|
|
14
|
-
exports.toDegrees = toDegrees;
|
|
15
11
|
/**
|
|
16
12
|
* Convert a degrees value into radians
|
|
17
13
|
*
|
|
18
14
|
* @param {number} degrees Angle in degrees
|
|
19
15
|
* @returns {number} Angle in radians
|
|
20
16
|
*/
|
|
21
|
-
function toRadians(degrees) {
|
|
22
|
-
return (degrees *
|
|
17
|
+
export function toRadians(degrees) {
|
|
18
|
+
return (degrees * PI) / 180;
|
|
23
19
|
}
|
|
24
|
-
exports.toRadians = toRadians;
|
|
25
20
|
/**
|
|
26
21
|
* Calculate the angle from a point to another
|
|
27
22
|
*
|
|
@@ -31,10 +26,9 @@ exports.toRadians = toRadians;
|
|
|
31
26
|
* @param {number} y2 Y value of the second point
|
|
32
27
|
* @returns {number} Angle
|
|
33
28
|
*/
|
|
34
|
-
function angle(x1, y1, x2, y2) {
|
|
29
|
+
export function angle(x1, y1, x2, y2) {
|
|
35
30
|
return Math.atan2(y2 - y1, x2 - x1);
|
|
36
31
|
}
|
|
37
|
-
exports.angle = angle;
|
|
38
32
|
/**
|
|
39
33
|
* Find the closest angle between to angles
|
|
40
34
|
*
|
|
@@ -42,11 +36,10 @@ exports.angle = angle;
|
|
|
42
36
|
* @param {number} target Target angle in radians
|
|
43
37
|
* @returns {number} Closest angle
|
|
44
38
|
*/
|
|
45
|
-
function closestAngle(source, target) {
|
|
46
|
-
|
|
47
|
-
return delta >
|
|
39
|
+
export function closestAngle(source, target) {
|
|
40
|
+
const delta = target - source;
|
|
41
|
+
return delta > PI ? target - 2 * PI : target < -PI ? delta + 2 * PI : target;
|
|
48
42
|
}
|
|
49
|
-
exports.closestAngle = closestAngle;
|
|
50
43
|
/**
|
|
51
44
|
* Calculate the distance between two points
|
|
52
45
|
*
|
|
@@ -56,12 +49,11 @@ exports.closestAngle = closestAngle;
|
|
|
56
49
|
* @param {number} y2 Y coord of the second point
|
|
57
50
|
* @returns {number} Computed distance
|
|
58
51
|
*/
|
|
59
|
-
function distance(x1, y1, x2, y2) {
|
|
60
|
-
|
|
61
|
-
|
|
52
|
+
export function distance(x1, y1, x2, y2) {
|
|
53
|
+
const dx = x1 - x2;
|
|
54
|
+
const dy = y1 - y2;
|
|
62
55
|
return Math.sqrt(dx * dx + dy * dy);
|
|
63
56
|
}
|
|
64
|
-
exports.distance = distance;
|
|
65
57
|
/**
|
|
66
58
|
* Calculate the length of the diagonal of a rectangle
|
|
67
59
|
*
|
|
@@ -69,10 +61,9 @@ exports.distance = distance;
|
|
|
69
61
|
* @param {number} height Height of the rectangle
|
|
70
62
|
* @returns {number} Diagonal length
|
|
71
63
|
*/
|
|
72
|
-
function diagonal(width, height) {
|
|
64
|
+
export function diagonal(width, height) {
|
|
73
65
|
return Math.sqrt(width * width + height * height);
|
|
74
66
|
}
|
|
75
|
-
exports.diagonal = diagonal;
|
|
76
67
|
/**
|
|
77
68
|
* Convert radians to a 3D point on the surface of a unit sphere
|
|
78
69
|
*
|
|
@@ -82,26 +73,24 @@ exports.diagonal = diagonal;
|
|
|
82
73
|
* @param {Vector3} target Target vector
|
|
83
74
|
* @returns {Vector3}
|
|
84
75
|
*/
|
|
85
|
-
function radToSphere(radius, phi, theta, target) {
|
|
86
|
-
if (target === void 0) { target = { x: 0, y: 0, z: 0 }; }
|
|
76
|
+
export function radToSphere(radius, phi, theta, target = { x: 0, y: 0, z: 0 }) {
|
|
87
77
|
target.x = radius * Math.sin(phi) * Math.sin(theta);
|
|
88
78
|
target.y = radius * Math.cos(phi);
|
|
89
79
|
target.z = radius * Math.sin(phi) * Math.cos(theta);
|
|
90
80
|
return target;
|
|
91
81
|
}
|
|
92
|
-
exports.radToSphere = radToSphere;
|
|
93
82
|
/**
|
|
94
83
|
* Make a target fit a container
|
|
95
84
|
*
|
|
96
|
-
* @param {
|
|
97
|
-
* @param {
|
|
98
|
-
* @param {string} mode
|
|
99
|
-
* @returns {
|
|
85
|
+
* @param {object} target Dimensions of the target
|
|
86
|
+
* @param {object} container Dimensions of the container
|
|
87
|
+
* @param {string} mode Can be 'contain' | 'cover'
|
|
88
|
+
* @returns {object}
|
|
100
89
|
*/
|
|
101
90
|
function fit(target, container, mode) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
91
|
+
const ratioWidth = container.width / target.width;
|
|
92
|
+
const ratioHeight = container.height / target.height;
|
|
93
|
+
let scale;
|
|
105
94
|
if (mode === 'contain') {
|
|
106
95
|
scale = ratioWidth < ratioHeight ? ratioWidth : ratioHeight;
|
|
107
96
|
}
|
|
@@ -113,28 +102,26 @@ function fit(target, container, mode) {
|
|
|
113
102
|
top: (container.height - target.height * scale) >> 1,
|
|
114
103
|
width: target.width * scale,
|
|
115
104
|
height: target.height * scale,
|
|
116
|
-
scale
|
|
105
|
+
scale
|
|
117
106
|
};
|
|
118
107
|
}
|
|
119
108
|
/**
|
|
120
109
|
* Make a target fit a container (cover mode)
|
|
121
110
|
*
|
|
122
|
-
* @param {
|
|
123
|
-
* @param {
|
|
124
|
-
* @returns {
|
|
111
|
+
* @param {object} target Dimensions of the target
|
|
112
|
+
* @param {object} container Dimensions of the container
|
|
113
|
+
* @returns {object}
|
|
125
114
|
*/
|
|
126
|
-
function cover(target, container) {
|
|
115
|
+
export function cover(target, container) {
|
|
127
116
|
return fit(target, container, 'cover');
|
|
128
117
|
}
|
|
129
|
-
exports.cover = cover;
|
|
130
118
|
/**
|
|
131
119
|
* Make a target fit a container (contain mode)
|
|
132
120
|
*
|
|
133
|
-
* @param {
|
|
134
|
-
* @param {
|
|
135
|
-
* @returns {
|
|
121
|
+
* @param {object} target Dimensions of the target
|
|
122
|
+
* @param {object} container Dimensions of the container
|
|
123
|
+
* @returns {object}
|
|
136
124
|
*/
|
|
137
|
-
function contain(target, container) {
|
|
125
|
+
export function contain(target, container) {
|
|
138
126
|
return fit(target, container, 'contain');
|
|
139
127
|
}
|
|
140
|
-
exports.contain = contain;
|
package/lib/maths.js
CHANGED
|
@@ -1,36 +1,30 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.damp = exports.average = exports.sum = exports.parabola = exports.smoothstep = exports.pingPong = exports.modAbs = exports.snap = exports.map = exports.normalize = exports.expLerp = exports.triLerp = exports.lerp = exports.clamp = exports.sign = exports.toPowerOf2 = exports.isPowerOf2 = exports.isOdd = exports.isEven = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Check if a number is even
|
|
6
3
|
*
|
|
7
4
|
* @param {number} value Value to check
|
|
8
5
|
* @returns {boolean} True if the given number is even, false otherwise
|
|
9
6
|
*/
|
|
10
|
-
function isEven(value) {
|
|
7
|
+
export function isEven(value) {
|
|
11
8
|
return !(value & 1);
|
|
12
9
|
}
|
|
13
|
-
exports.isEven = isEven;
|
|
14
10
|
/**
|
|
15
11
|
* Check if a number is odd
|
|
16
12
|
*
|
|
17
13
|
* @param {number} value Value to check
|
|
18
14
|
* @returns {boolean} True if the given number is odd, false otherwise
|
|
19
15
|
*/
|
|
20
|
-
function isOdd(value) {
|
|
16
|
+
export function isOdd(value) {
|
|
21
17
|
return !!(value & 1);
|
|
22
18
|
}
|
|
23
|
-
exports.isOdd = isOdd;
|
|
24
19
|
/**
|
|
25
20
|
* Check if a number is a power of 2
|
|
26
21
|
*
|
|
27
22
|
* @param {number} value Value to check
|
|
28
23
|
* @returns {boolean} True if the given number is a power of 2, false otherwise
|
|
29
24
|
*/
|
|
30
|
-
function isPowerOf2(value) {
|
|
25
|
+
export function isPowerOf2(value) {
|
|
31
26
|
return (value & (value - 1)) === 0;
|
|
32
27
|
}
|
|
33
|
-
exports.isPowerOf2 = isPowerOf2;
|
|
34
28
|
/**
|
|
35
29
|
* Find closest power of 2 that fits a number
|
|
36
30
|
*
|
|
@@ -38,25 +32,22 @@ exports.isPowerOf2 = isPowerOf2;
|
|
|
38
32
|
* @param {string} [mode='ceil'] Can be 'floor' | 'ceil' | 'round'
|
|
39
33
|
* @returns {number} Power of 2
|
|
40
34
|
*/
|
|
41
|
-
function toPowerOf2(value, mode) {
|
|
42
|
-
if (mode === void 0) { mode = 'ceil'; }
|
|
35
|
+
export function toPowerOf2(value, mode = 'ceil') {
|
|
43
36
|
return Math.pow(2, Math[mode](Math.log(value) / Math.log(2)));
|
|
44
37
|
}
|
|
45
|
-
exports.toPowerOf2 = toPowerOf2;
|
|
46
38
|
/**
|
|
47
39
|
* Return the sign (positive or negative) of a number
|
|
48
40
|
*
|
|
49
41
|
* @param {number} value Value to check
|
|
50
42
|
* @returns {number} 1 if the given number is positive, -1 if it is negative, otherwise 0
|
|
51
43
|
*/
|
|
52
|
-
function sign(value) {
|
|
44
|
+
export function sign(value) {
|
|
53
45
|
if (value > 0)
|
|
54
46
|
return 1;
|
|
55
47
|
else if (value < 0)
|
|
56
48
|
return -1;
|
|
57
49
|
return 0;
|
|
58
50
|
}
|
|
59
|
-
exports.sign = sign;
|
|
60
51
|
/**
|
|
61
52
|
* Clamp a value between two bounds
|
|
62
53
|
*
|
|
@@ -65,12 +56,9 @@ exports.sign = sign;
|
|
|
65
56
|
* @param {number} [max=1] Maximum boundary
|
|
66
57
|
* @returns {number} Clamped value
|
|
67
58
|
*/
|
|
68
|
-
function clamp(value, min, max) {
|
|
69
|
-
if (min === void 0) { min = 0; }
|
|
70
|
-
if (max === void 0) { max = 1; }
|
|
59
|
+
export function clamp(value, min = 0, max = 1) {
|
|
71
60
|
return Math.min(max, Math.max(min, value));
|
|
72
61
|
}
|
|
73
|
-
exports.clamp = clamp;
|
|
74
62
|
/**
|
|
75
63
|
* Linear interpolation between two values (lerping)
|
|
76
64
|
*
|
|
@@ -79,10 +67,9 @@ exports.clamp = clamp;
|
|
|
79
67
|
* @param {number} max Maximum value
|
|
80
68
|
* @returns {number} Lerped value
|
|
81
69
|
*/
|
|
82
|
-
function lerp(value, min, max) {
|
|
70
|
+
export function lerp(value, min, max) {
|
|
83
71
|
return min + (max - min) * value;
|
|
84
72
|
}
|
|
85
|
-
exports.lerp = lerp;
|
|
86
73
|
/**
|
|
87
74
|
* Triangular interpolation between two values
|
|
88
75
|
*
|
|
@@ -92,11 +79,10 @@ exports.lerp = lerp;
|
|
|
92
79
|
* @param {number} target Triangle target value
|
|
93
80
|
* @returns {number} Interpolated value
|
|
94
81
|
*/
|
|
95
|
-
function triLerp(value, min, max, target) {
|
|
96
|
-
|
|
82
|
+
export function triLerp(value, min, max, target) {
|
|
83
|
+
const x = Math.pow(1 + Math.abs(target - max) / Math.abs(target - min), -1);
|
|
97
84
|
return value <= x ? min - (min - target) * (value / x) : target - (target - max) * ((value - x) / (1 - x));
|
|
98
85
|
}
|
|
99
|
-
exports.triLerp = triLerp;
|
|
100
86
|
/**
|
|
101
87
|
* Exponential interpolation between two values
|
|
102
88
|
*
|
|
@@ -107,11 +93,10 @@ exports.triLerp = triLerp;
|
|
|
107
93
|
* @param {number} targetMax Upper bound of the value's target range
|
|
108
94
|
* @returns {number} Interpolated value
|
|
109
95
|
*/
|
|
110
|
-
function expLerp(value, currentMin, currentMax, targetMin, targetMax) {
|
|
96
|
+
export function expLerp(value, currentMin, currentMax, targetMin, targetMax) {
|
|
111
97
|
return (targetMin *
|
|
112
98
|
Math.pow(targetMax / targetMin, (clamp(value, currentMin, currentMax) - currentMin) / (currentMax - currentMin)));
|
|
113
99
|
}
|
|
114
|
-
exports.expLerp = expLerp;
|
|
115
100
|
/**
|
|
116
101
|
* Normalize a value between two bounds
|
|
117
102
|
*
|
|
@@ -120,10 +105,9 @@ exports.expLerp = expLerp;
|
|
|
120
105
|
* @param {number} max Maximum boundary
|
|
121
106
|
* @returns {number} Normalized value
|
|
122
107
|
*/
|
|
123
|
-
function normalize(value, min, max) {
|
|
108
|
+
export function normalize(value, min, max) {
|
|
124
109
|
return (value - min) / (max - min);
|
|
125
110
|
}
|
|
126
|
-
exports.normalize = normalize;
|
|
127
111
|
/**
|
|
128
112
|
* Re-map a number from one range to another
|
|
129
113
|
*
|
|
@@ -134,10 +118,9 @@ exports.normalize = normalize;
|
|
|
134
118
|
* @param {number} targetMax Upper bound of the value's target range
|
|
135
119
|
* @returns {number} Re-mapped value
|
|
136
120
|
*/
|
|
137
|
-
function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
121
|
+
export function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
138
122
|
return ((value - currentMin) / (currentMax - currentMin)) * (targetMax - targetMin) + targetMin;
|
|
139
123
|
}
|
|
140
|
-
exports.map = map;
|
|
141
124
|
/**
|
|
142
125
|
* Round a number up to a nearest multiple
|
|
143
126
|
*
|
|
@@ -145,13 +128,11 @@ exports.map = map;
|
|
|
145
128
|
* @param {number} [multiple=1] Multiple to round to
|
|
146
129
|
* @returns {number} Closest multiple
|
|
147
130
|
*/
|
|
148
|
-
function snap(value, multiple) {
|
|
149
|
-
if (multiple === void 0) { multiple = 1; }
|
|
131
|
+
export function snap(value, multiple = 1) {
|
|
150
132
|
if (multiple === 0)
|
|
151
133
|
return value;
|
|
152
134
|
return Math.round(value / multiple) * multiple;
|
|
153
135
|
}
|
|
154
|
-
exports.snap = snap;
|
|
155
136
|
/**
|
|
156
137
|
* Modulo absolute a value based on a length
|
|
157
138
|
*
|
|
@@ -159,13 +140,12 @@ exports.snap = snap;
|
|
|
159
140
|
* @param {number} length Total length
|
|
160
141
|
* @returns {number} Modulated value
|
|
161
142
|
*/
|
|
162
|
-
function modAbs(value, length) {
|
|
143
|
+
export function modAbs(value, length) {
|
|
163
144
|
if (value < 0) {
|
|
164
145
|
return length + (value % length);
|
|
165
146
|
}
|
|
166
147
|
return value % length;
|
|
167
148
|
}
|
|
168
|
-
exports.modAbs = modAbs;
|
|
169
149
|
/**
|
|
170
150
|
* Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0
|
|
171
151
|
*
|
|
@@ -173,11 +153,10 @@ exports.modAbs = modAbs;
|
|
|
173
153
|
* @param {number} length Total length
|
|
174
154
|
* @returns {number} PingPonged value
|
|
175
155
|
*/
|
|
176
|
-
function pingPong(value, length) {
|
|
156
|
+
export function pingPong(value, length) {
|
|
177
157
|
value = modAbs(value, length * 2);
|
|
178
158
|
return length - Math.abs(value - length);
|
|
179
159
|
}
|
|
180
|
-
exports.pingPong = pingPong;
|
|
181
160
|
/**
|
|
182
161
|
* Smooth a value using cubic Hermite interpolation
|
|
183
162
|
*
|
|
@@ -186,13 +165,10 @@ exports.pingPong = pingPong;
|
|
|
186
165
|
* @param {number} [max=1] Maximum boundary
|
|
187
166
|
* @returns {number} Normalized smoothed value
|
|
188
167
|
*/
|
|
189
|
-
function smoothstep(value, min, max) {
|
|
190
|
-
|
|
191
|
-
if (max === void 0) { max = 1; }
|
|
192
|
-
var x = clamp(normalize(value, min, max));
|
|
168
|
+
export function smoothstep(value, min = 0, max = 1) {
|
|
169
|
+
const x = clamp(normalize(value, min, max));
|
|
193
170
|
return x * x * (3 - 2 * x);
|
|
194
171
|
}
|
|
195
|
-
exports.smoothstep = smoothstep;
|
|
196
172
|
/**
|
|
197
173
|
* Re-map the [0, 1] interval into [0, 1] parabola, such that corners are remaped to 0 and the center to 1
|
|
198
174
|
* -> parabola(0) = parabola(1) = 0, and parabola(0.5) = 1
|
|
@@ -201,31 +177,27 @@ exports.smoothstep = smoothstep;
|
|
|
201
177
|
* @param {number} [power=1] Parabola power
|
|
202
178
|
* @returns {number} Normalized re-mapped value
|
|
203
179
|
*/
|
|
204
|
-
function parabola(x, power) {
|
|
205
|
-
if (power === void 0) { power = 1; }
|
|
180
|
+
export function parabola(x, power = 1) {
|
|
206
181
|
return Math.pow(4 * x * (1 - x), power);
|
|
207
182
|
}
|
|
208
|
-
exports.parabola = parabola;
|
|
209
183
|
/**
|
|
210
184
|
* Return the sum of numbers
|
|
211
185
|
*
|
|
212
186
|
* @param {number[]} array Array of numbers
|
|
213
187
|
* @returns {number} Total sum
|
|
214
188
|
*/
|
|
215
|
-
function sum(array) {
|
|
216
|
-
return array.reduce(
|
|
189
|
+
export function sum(array) {
|
|
190
|
+
return array.reduce((previous, current) => previous + current);
|
|
217
191
|
}
|
|
218
|
-
exports.sum = sum;
|
|
219
192
|
/**
|
|
220
193
|
* Return the average of numbers
|
|
221
194
|
*
|
|
222
195
|
* @param {number[]} array Array of numbers
|
|
223
196
|
* @returns {number} Total average
|
|
224
197
|
*/
|
|
225
|
-
function average(array) {
|
|
198
|
+
export function average(array) {
|
|
226
199
|
return sum(array) / array.length;
|
|
227
200
|
}
|
|
228
|
-
exports.average = average;
|
|
229
201
|
/**
|
|
230
202
|
* Smoothly interpolate a number toward another
|
|
231
203
|
*
|
|
@@ -235,7 +207,6 @@ exports.average = average;
|
|
|
235
207
|
* @param {number} delta Delta time (in seconds)
|
|
236
208
|
* @returns {number} Interpolated number
|
|
237
209
|
*/
|
|
238
|
-
function damp(value, target, damping, delta) {
|
|
210
|
+
export function damp(value, target, damping, delta) {
|
|
239
211
|
return lerp(1 - Math.exp(-damping * delta), value, target);
|
|
240
212
|
}
|
|
241
|
-
exports.damp = damp;
|