toosoon-utils 4.3.1 → 4.4.0
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 +36 -13
- package/lib/colors.d.ts +3 -3
- package/lib/colors.js +3 -3
- package/lib/dom.d.ts +2 -2
- package/lib/dom.js +5 -5
- package/lib/extras/colors/Color.d.ts +12 -17
- package/lib/extras/colors/Color.js +12 -17
- package/lib/extras/paths/Path.js +4 -7
- package/lib/extras/paths/PathContext.d.ts +8 -8
- package/lib/extras/paths/PathContext.js +10 -10
- package/lib/files.d.ts +25 -4
- package/lib/files.js +43 -4
- package/lib/functions.d.ts +2 -2
- package/lib/functions.js +2 -2
- package/lib/geometry.d.ts +7 -7
- package/lib/geometry.js +9 -9
- package/lib/maths.d.ts +25 -25
- package/lib/maths.js +25 -25
- package/lib/prng.d.ts +10 -10
- package/lib/prng.js +10 -10
- package/lib/query.d.ts +1 -1
- package/lib/query.js +1 -1
- package/lib/random.d.ts +3 -3
- package/lib/random.js +3 -3
- package/lib/strings.d.ts +5 -5
- package/lib/strings.js +5 -5
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
package/lib/files.d.ts
CHANGED
|
@@ -1,14 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load a file
|
|
3
|
+
*
|
|
4
|
+
* @param {File} file File to load
|
|
5
|
+
* @returns {Promise<string>} Data URL of the file
|
|
6
|
+
*/
|
|
7
|
+
export declare function load(file: File): Promise<string>;
|
|
1
8
|
/**
|
|
2
9
|
* Download a Blob object into user files
|
|
3
10
|
*
|
|
4
|
-
* @param {Blob} blob
|
|
5
|
-
* @param {
|
|
11
|
+
* @param {Blob} blob Blob object to download
|
|
12
|
+
* @param {Object} params Download parameters
|
|
13
|
+
* @param {string} params.filename Downloaded file name
|
|
6
14
|
*/
|
|
7
|
-
export declare function download(blob: Blob, filename
|
|
15
|
+
export declare function download(blob: Blob, { filename }: {
|
|
16
|
+
filename: string;
|
|
17
|
+
}): void;
|
|
8
18
|
/**
|
|
9
19
|
* Upload a file from user files
|
|
10
20
|
*
|
|
11
|
-
* @param {Function} onLoad
|
|
21
|
+
* @param {Function} onLoad Callback called once the file is loaded
|
|
12
22
|
* @param {string} [accept=''] MIME type the file input should accept
|
|
13
23
|
*/
|
|
14
24
|
export declare function upload(onLoad: (dataUrl: string) => void, accept?: string): void;
|
|
25
|
+
/**
|
|
26
|
+
* Share a Blob object with the user's device
|
|
27
|
+
*
|
|
28
|
+
* @param {Blob} blob Blob object to share
|
|
29
|
+
* @param {ShareData} params Share parameters
|
|
30
|
+
* @param {string} params.filename Shared file name
|
|
31
|
+
* @returns {Promise<void>}
|
|
32
|
+
*/
|
|
33
|
+
export declare function share(blob: Blob, { filename, ...data }: {
|
|
34
|
+
filename: string;
|
|
35
|
+
} & ShareData): Promise<void>;
|
package/lib/files.js
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load a file
|
|
3
|
+
*
|
|
4
|
+
* @param {File} file File to load
|
|
5
|
+
* @returns {Promise<string>} Data URL of the file
|
|
6
|
+
*/
|
|
7
|
+
export async function load(file) {
|
|
8
|
+
return await new Promise((resolve, reject) => {
|
|
9
|
+
const fileReader = new FileReader();
|
|
10
|
+
fileReader.addEventListener('load', () => resolve(URL.createObjectURL(file)));
|
|
11
|
+
fileReader.addEventListener('error', () => reject(new Error(`Failed to load file: ${file.name}`)));
|
|
12
|
+
fileReader.readAsDataURL(file);
|
|
13
|
+
});
|
|
14
|
+
}
|
|
1
15
|
/**
|
|
2
16
|
* Download a Blob object into user files
|
|
3
17
|
*
|
|
4
|
-
* @param {Blob} blob
|
|
5
|
-
* @param {
|
|
18
|
+
* @param {Blob} blob Blob object to download
|
|
19
|
+
* @param {Object} params Download parameters
|
|
20
|
+
* @param {string} params.filename Downloaded file name
|
|
6
21
|
*/
|
|
7
|
-
export function download(blob, filename) {
|
|
22
|
+
export function download(blob, { filename }) {
|
|
8
23
|
const link = document.createElement('a');
|
|
9
24
|
link.setAttribute('href', URL.createObjectURL(blob));
|
|
10
25
|
link.setAttribute('download', filename);
|
|
@@ -15,7 +30,7 @@ export function download(blob, filename) {
|
|
|
15
30
|
/**
|
|
16
31
|
* Upload a file from user files
|
|
17
32
|
*
|
|
18
|
-
* @param {Function} onLoad
|
|
33
|
+
* @param {Function} onLoad Callback called once the file is loaded
|
|
19
34
|
* @param {string} [accept=''] MIME type the file input should accept
|
|
20
35
|
*/
|
|
21
36
|
export function upload(onLoad, accept = '') {
|
|
@@ -34,3 +49,27 @@ export function upload(onLoad, accept = '') {
|
|
|
34
49
|
input.click();
|
|
35
50
|
document.body.removeChild(input);
|
|
36
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Share a Blob object with the user's device
|
|
54
|
+
*
|
|
55
|
+
* @param {Blob} blob Blob object to share
|
|
56
|
+
* @param {ShareData} params Share parameters
|
|
57
|
+
* @param {string} params.filename Shared file name
|
|
58
|
+
* @returns {Promise<void>}
|
|
59
|
+
*/
|
|
60
|
+
export async function share(blob, { filename, ...data }) {
|
|
61
|
+
const file = new File([blob], filename, { type: blob.type });
|
|
62
|
+
try {
|
|
63
|
+
if (!navigator?.canShare({ files: [file] })) {
|
|
64
|
+
const error = new Error(`Failed to share file: ${filename}`);
|
|
65
|
+
error.name = 'NotAllowedError';
|
|
66
|
+
throw error;
|
|
67
|
+
}
|
|
68
|
+
await navigator.share({ files: [file], ...data });
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
if (error instanceof Error && error.name === 'NotAllowedError') {
|
|
72
|
+
download(blob, { filename });
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
package/lib/functions.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare function wait(delay?: number): Promise<void>;
|
|
|
14
14
|
* Create a debounced function that delays the execution of `callback` until a specified `delay` time has passed since the last call
|
|
15
15
|
*
|
|
16
16
|
* @param {Function} callback Function to debounce
|
|
17
|
-
* @param {number} delay
|
|
17
|
+
* @param {number} delay Delay (in milliseconds)
|
|
18
18
|
* @returns {Function} Debounced function
|
|
19
19
|
*/
|
|
20
20
|
export declare function debounce<T extends (...args: any[]) => void>(callback: T, delay: number): (...args: Parameters<T>) => void;
|
|
@@ -29,7 +29,7 @@ export declare function isDefined<T>(value: T): value is Exclude<T, undefined |
|
|
|
29
29
|
* Create a throttled function that limits the execution of `callback` to once every `limit` time
|
|
30
30
|
*
|
|
31
31
|
* @param {Function} callback Function to throttle
|
|
32
|
-
* @param {number} limit
|
|
32
|
+
* @param {number} limit Minimum interval between two calls (in milliseconds)
|
|
33
33
|
* @returns {Function} Throttled function
|
|
34
34
|
*/
|
|
35
35
|
export declare function throttle<T extends (...args: any[]) => void>(callback: T, limit: number): (...args: Parameters<T>) => void;
|
package/lib/functions.js
CHANGED
|
@@ -15,7 +15,7 @@ export function wait(delay = 0) {
|
|
|
15
15
|
* Create a debounced function that delays the execution of `callback` until a specified `delay` time has passed since the last call
|
|
16
16
|
*
|
|
17
17
|
* @param {Function} callback Function to debounce
|
|
18
|
-
* @param {number} delay
|
|
18
|
+
* @param {number} delay Delay (in milliseconds)
|
|
19
19
|
* @returns {Function} Debounced function
|
|
20
20
|
*/
|
|
21
21
|
export function debounce(callback, delay) {
|
|
@@ -41,7 +41,7 @@ export function isDefined(value) {
|
|
|
41
41
|
* Create a throttled function that limits the execution of `callback` to once every `limit` time
|
|
42
42
|
*
|
|
43
43
|
* @param {Function} callback Function to throttle
|
|
44
|
-
* @param {number} limit
|
|
44
|
+
* @param {number} limit Minimum interval between two calls (in milliseconds)
|
|
45
45
|
* @returns {Function} Throttled function
|
|
46
46
|
*/
|
|
47
47
|
export function throttle(callback, limit) {
|
package/lib/geometry.d.ts
CHANGED
|
@@ -13,21 +13,21 @@ export declare function toDegrees(radians: number): number;
|
|
|
13
13
|
*/
|
|
14
14
|
export declare function toRadians(degrees: number): number;
|
|
15
15
|
/**
|
|
16
|
-
* Calculate the angle from a point to another
|
|
16
|
+
* Calculate the angle (in radians) from a point to another
|
|
17
17
|
*
|
|
18
18
|
* @param {number} x1 X-axis coordinate of the start point
|
|
19
19
|
* @param {number} y1 Y-axis coordinate of the start point
|
|
20
20
|
* @param {number} x2 X-axis coordinate of the end point
|
|
21
21
|
* @param {number} y2 Y-axis coordinate of the end point
|
|
22
|
-
* @returns {number} Angle
|
|
22
|
+
* @returns {number} Angle (in radians)
|
|
23
23
|
*/
|
|
24
24
|
export declare function angle(x1: number, y1: number, x2: number, y2: number): number;
|
|
25
25
|
/**
|
|
26
|
-
* Find the closest angle between
|
|
26
|
+
* Find the closest angle (in radians) between two angles
|
|
27
27
|
*
|
|
28
28
|
* @param {number} startAngle Start angle (in radians)
|
|
29
|
-
* @param {number} endAngle
|
|
30
|
-
* @returns {number} Closest angle
|
|
29
|
+
* @param {number} endAngle End angle (in radians)
|
|
30
|
+
* @returns {number} Closest angle (in radians)
|
|
31
31
|
*/
|
|
32
32
|
export declare function closestAngle(startAngle: number, endAngle: number): number;
|
|
33
33
|
/**
|
|
@@ -62,7 +62,7 @@ export type FitOutput = {
|
|
|
62
62
|
/**
|
|
63
63
|
* Make a target fit a container (cover mode)
|
|
64
64
|
*
|
|
65
|
-
* @param {FitInput} target
|
|
65
|
+
* @param {FitInput} target Dimensions of the target
|
|
66
66
|
* @param {FitInput} container Dimensions of the container
|
|
67
67
|
* @returns {FitOutput}
|
|
68
68
|
*/
|
|
@@ -70,7 +70,7 @@ export declare function cover(target: FitInput, container: FitInput): FitOutput;
|
|
|
70
70
|
/**
|
|
71
71
|
* Make a target fit a container (contain mode)
|
|
72
72
|
*
|
|
73
|
-
* @param {FitInput} target
|
|
73
|
+
* @param {FitInput} target Dimensions of the target
|
|
74
74
|
* @param {FitInput} container Dimensions of the container
|
|
75
75
|
* @returns {FitOutput}
|
|
76
76
|
*/
|
package/lib/geometry.js
CHANGED
|
@@ -18,23 +18,23 @@ export function toRadians(degrees) {
|
|
|
18
18
|
return (degrees * PI) / 180;
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
21
|
-
* Calculate the angle from a point to another
|
|
21
|
+
* Calculate the angle (in radians) from a point to another
|
|
22
22
|
*
|
|
23
23
|
* @param {number} x1 X-axis coordinate of the start point
|
|
24
24
|
* @param {number} y1 Y-axis coordinate of the start point
|
|
25
25
|
* @param {number} x2 X-axis coordinate of the end point
|
|
26
26
|
* @param {number} y2 Y-axis coordinate of the end point
|
|
27
|
-
* @returns {number} Angle
|
|
27
|
+
* @returns {number} Angle (in radians)
|
|
28
28
|
*/
|
|
29
29
|
export function angle(x1, y1, x2, y2) {
|
|
30
30
|
return Math.atan2(y2 - y1, x2 - x1);
|
|
31
31
|
}
|
|
32
32
|
/**
|
|
33
|
-
* Find the closest angle between
|
|
33
|
+
* Find the closest angle (in radians) between two angles
|
|
34
34
|
*
|
|
35
35
|
* @param {number} startAngle Start angle (in radians)
|
|
36
|
-
* @param {number} endAngle
|
|
37
|
-
* @returns {number} Closest angle
|
|
36
|
+
* @param {number} endAngle End angle (in radians)
|
|
37
|
+
* @returns {number} Closest angle (in radians)
|
|
38
38
|
*/
|
|
39
39
|
export function closestAngle(startAngle, endAngle) {
|
|
40
40
|
const delta = endAngle - startAngle;
|
|
@@ -67,9 +67,9 @@ export function diagonal(width, height) {
|
|
|
67
67
|
/**
|
|
68
68
|
* Make a target fit a container
|
|
69
69
|
*
|
|
70
|
-
* @param {FitInput} target
|
|
70
|
+
* @param {FitInput} target Dimensions of the target
|
|
71
71
|
* @param {FitInput} container Dimensions of the container
|
|
72
|
-
* @param {string} mode
|
|
72
|
+
* @param {string} mode Can be 'contain' | 'cover'
|
|
73
73
|
* @returns {FitOutput}
|
|
74
74
|
*/
|
|
75
75
|
function fit(target, container, mode) {
|
|
@@ -93,7 +93,7 @@ function fit(target, container, mode) {
|
|
|
93
93
|
/**
|
|
94
94
|
* Make a target fit a container (cover mode)
|
|
95
95
|
*
|
|
96
|
-
* @param {FitInput} target
|
|
96
|
+
* @param {FitInput} target Dimensions of the target
|
|
97
97
|
* @param {FitInput} container Dimensions of the container
|
|
98
98
|
* @returns {FitOutput}
|
|
99
99
|
*/
|
|
@@ -103,7 +103,7 @@ export function cover(target, container) {
|
|
|
103
103
|
/**
|
|
104
104
|
* Make a target fit a container (contain mode)
|
|
105
105
|
*
|
|
106
|
-
* @param {FitInput} target
|
|
106
|
+
* @param {FitInput} target Dimensions of the target
|
|
107
107
|
* @param {FitInput} container Dimensions of the container
|
|
108
108
|
* @returns {FitOutput}
|
|
109
109
|
*/
|
package/lib/maths.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare function isPowerOf2(value: number): boolean;
|
|
|
22
22
|
/**
|
|
23
23
|
* Find closest power of 2 that fits a number
|
|
24
24
|
*
|
|
25
|
-
* @param {number} value
|
|
25
|
+
* @param {number} value Incoming value
|
|
26
26
|
* @param {string} [mode='ceil'] Can be 'floor' | 'ceil' | 'round'
|
|
27
27
|
* @returns {number} Computed power of 2
|
|
28
28
|
*/
|
|
@@ -37,7 +37,7 @@ export declare function sign(value: number): number;
|
|
|
37
37
|
/**
|
|
38
38
|
* Clamp a value between two bounds
|
|
39
39
|
*
|
|
40
|
-
* @param {number} value
|
|
40
|
+
* @param {number} value Value to clamp
|
|
41
41
|
* @param {number} [min=0] Minimum boundary
|
|
42
42
|
* @param {number} [max=1] Maximum boundary
|
|
43
43
|
* @returns {number} Clamped value
|
|
@@ -46,7 +46,7 @@ export declare function clamp(value: number, min?: number, max?: number): number
|
|
|
46
46
|
/**
|
|
47
47
|
* Round a number up to a nearest multiple
|
|
48
48
|
*
|
|
49
|
-
* @param {number} value
|
|
49
|
+
* @param {number} value Value to round
|
|
50
50
|
* @param {number} [multiple=1] Multiple to round to
|
|
51
51
|
* @returns {number} Closest multiple
|
|
52
52
|
*/
|
|
@@ -54,7 +54,7 @@ export declare function snap(value: number, multiple?: number): number;
|
|
|
54
54
|
/**
|
|
55
55
|
* Interpolate a value between two values using Linear interpolation (lerping)
|
|
56
56
|
*
|
|
57
|
-
* @param {number} t
|
|
57
|
+
* @param {number} t Normalized time value to interpolate
|
|
58
58
|
* @param {number} min Minimum value
|
|
59
59
|
* @param {number} max Maximum value
|
|
60
60
|
* @returns {number} Lerped value
|
|
@@ -64,28 +64,28 @@ export declare function lerp(t: number, min: number, max: number): number;
|
|
|
64
64
|
* Normalize a value between two bounds
|
|
65
65
|
*
|
|
66
66
|
* @param {number} value Value to normalize
|
|
67
|
-
* @param {number} min
|
|
68
|
-
* @param {number} max
|
|
67
|
+
* @param {number} min Minimum boundary
|
|
68
|
+
* @param {number} max Maximum boundary
|
|
69
69
|
* @returns {number} Normalized value
|
|
70
70
|
*/
|
|
71
71
|
export declare function normalize(value: number, min: number, max: number): number;
|
|
72
72
|
/**
|
|
73
73
|
* Re-map a number from one range to another
|
|
74
74
|
*
|
|
75
|
-
* @param {number} value
|
|
75
|
+
* @param {number} value Value to re-map
|
|
76
76
|
* @param {number} currentMin Lower bound of the value's current range
|
|
77
77
|
* @param {number} currentMax Upper bound of the value's current range
|
|
78
|
-
* @param {number} targetMin
|
|
79
|
-
* @param {number} targetMax
|
|
78
|
+
* @param {number} targetMin Lower bound of the value's target range
|
|
79
|
+
* @param {number} targetMax Upper bound of the value's target range
|
|
80
80
|
* @returns {number} Re-mapped value
|
|
81
81
|
*/
|
|
82
82
|
export declare function map(value: number, currentMin: number, currentMax: number, targetMin: number, targetMax: number): number;
|
|
83
83
|
/**
|
|
84
84
|
* Interpolate a value between two values using Triangular interpolation
|
|
85
85
|
*
|
|
86
|
-
* @param {number} t
|
|
87
|
-
* @param {number} min
|
|
88
|
-
* @param {number} max
|
|
86
|
+
* @param {number} t Normalized time value to interpolate
|
|
87
|
+
* @param {number} min Minimum value
|
|
88
|
+
* @param {number} max Maximum value
|
|
89
89
|
* @param {number} peak Peak value controling the interpolation triangle shape
|
|
90
90
|
* - peak <= min : linear (same as lerp)
|
|
91
91
|
* - peak <= max : linear (same as lerp)
|
|
@@ -96,9 +96,9 @@ export declare function triLerp(t: number, min: number, max: number, peak: numbe
|
|
|
96
96
|
/**
|
|
97
97
|
* Interpolate a value using Exponential interpolation
|
|
98
98
|
*
|
|
99
|
-
* @param {number} t
|
|
100
|
-
* @param {number} min
|
|
101
|
-
* @param {number} max
|
|
99
|
+
* @param {number} t Normalized time value to interpolate
|
|
100
|
+
* @param {number} min Minimum value
|
|
101
|
+
* @param {number} max Maximum value
|
|
102
102
|
* @param {number} power Exponent controling the interpolation curve shape
|
|
103
103
|
* - power > 1 : ease-in
|
|
104
104
|
* - power < 1 : ease-out
|
|
@@ -109,7 +109,7 @@ export declare function expLerp(t: number, min: number, max: number, power: numb
|
|
|
109
109
|
/**
|
|
110
110
|
* Interpolate a value using Quadratic Bézier interpolation
|
|
111
111
|
*
|
|
112
|
-
* @param {number} t
|
|
112
|
+
* @param {number} t Normalized time value to interpolate
|
|
113
113
|
* @param {number} p1 Start point
|
|
114
114
|
* @param {number} cp Control point
|
|
115
115
|
* @param {number} p2 End point
|
|
@@ -119,7 +119,7 @@ export declare function quadraticBezier(t: number, p1: number, cp: number, p2: n
|
|
|
119
119
|
/**
|
|
120
120
|
* Interpolate a value using Cubic Bézier interpolation
|
|
121
121
|
*
|
|
122
|
-
* @param {number} t
|
|
122
|
+
* @param {number} t Normalized time value to interpolate
|
|
123
123
|
* @param {number} p1 Start point
|
|
124
124
|
* @param {number} cp1 First control point
|
|
125
125
|
* @param {number} cp2 Second control point
|
|
@@ -130,7 +130,7 @@ export declare function cubicBezier(t: number, p1: number, cp1: number, cp2: num
|
|
|
130
130
|
/**
|
|
131
131
|
* Interpolate a value using Catmull-Rom interpolation
|
|
132
132
|
*
|
|
133
|
-
* @param {number} t
|
|
133
|
+
* @param {number} t Normalized time value to interpolate
|
|
134
134
|
* @param {number} p1 Start point
|
|
135
135
|
* @param {number} cp1 First control point
|
|
136
136
|
* @param {number} cp2 Second control point
|
|
@@ -141,7 +141,7 @@ export declare function catmullRom(t: number, p1: number, cp1: number, cp2: numb
|
|
|
141
141
|
/**
|
|
142
142
|
* Modulo absolute a value based on a length
|
|
143
143
|
*
|
|
144
|
-
* @param {number} value
|
|
144
|
+
* @param {number} value Value to modulate
|
|
145
145
|
* @param {number} length Total length
|
|
146
146
|
* @returns {number} Modulated value
|
|
147
147
|
*/
|
|
@@ -149,7 +149,7 @@ export declare function modAbs(value: number, length: number): number;
|
|
|
149
149
|
/**
|
|
150
150
|
* Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0
|
|
151
151
|
*
|
|
152
|
-
* @param {number} value
|
|
152
|
+
* @param {number} value Value to modulate
|
|
153
153
|
* @param {number} length Total length
|
|
154
154
|
* @returns {number} PingPonged value
|
|
155
155
|
*/
|
|
@@ -157,7 +157,7 @@ export declare function pingPong(value: number, length: number): number;
|
|
|
157
157
|
/**
|
|
158
158
|
* Smooth a value using cubic Hermite interpolation
|
|
159
159
|
*
|
|
160
|
-
* @param {number} value
|
|
160
|
+
* @param {number} value Value to smooth
|
|
161
161
|
* @param {number} [min=0] Minimum boundary
|
|
162
162
|
* @param {number} [max=1] Maximum boundary
|
|
163
163
|
* @returns {number} Normalized smoothed value
|
|
@@ -168,7 +168,7 @@ export declare function smoothstep(value: number, min?: number, max?: number): n
|
|
|
168
168
|
* - parabola(0) = parabola(1) = 0
|
|
169
169
|
* - parabola(0.5) = 1
|
|
170
170
|
*
|
|
171
|
-
* @param {number} x
|
|
171
|
+
* @param {number} x Normalized coordinate on X axis
|
|
172
172
|
* @param {number} [power=1] Parabola power
|
|
173
173
|
* @returns {number} Normalized re-mapped value
|
|
174
174
|
*/
|
|
@@ -190,10 +190,10 @@ export declare function average(array: number[]): number;
|
|
|
190
190
|
/**
|
|
191
191
|
* Smoothly interpolate a number toward another
|
|
192
192
|
*
|
|
193
|
-
* @param {number} value
|
|
194
|
-
* @param {number} target
|
|
193
|
+
* @param {number} value Value to interpolate
|
|
194
|
+
* @param {number} target Destination of the interpolation
|
|
195
195
|
* @param {number} damping A higher value will make the movement more sudden, and a lower value will make the movement more gradual
|
|
196
|
-
* @param {number} delta
|
|
196
|
+
* @param {number} delta Delta time (in seconds)
|
|
197
197
|
* @returns {number} Interpolated number
|
|
198
198
|
*/
|
|
199
199
|
export declare function damp(value: number, target: number, damping: number, delta: number): number;
|
package/lib/maths.js
CHANGED
|
@@ -28,7 +28,7 @@ export function isPowerOf2(value) {
|
|
|
28
28
|
/**
|
|
29
29
|
* Find closest power of 2 that fits a number
|
|
30
30
|
*
|
|
31
|
-
* @param {number} value
|
|
31
|
+
* @param {number} value Incoming value
|
|
32
32
|
* @param {string} [mode='ceil'] Can be 'floor' | 'ceil' | 'round'
|
|
33
33
|
* @returns {number} Computed power of 2
|
|
34
34
|
*/
|
|
@@ -51,7 +51,7 @@ export function sign(value) {
|
|
|
51
51
|
/**
|
|
52
52
|
* Clamp a value between two bounds
|
|
53
53
|
*
|
|
54
|
-
* @param {number} value
|
|
54
|
+
* @param {number} value Value to clamp
|
|
55
55
|
* @param {number} [min=0] Minimum boundary
|
|
56
56
|
* @param {number} [max=1] Maximum boundary
|
|
57
57
|
* @returns {number} Clamped value
|
|
@@ -62,7 +62,7 @@ export function clamp(value, min = 0, max = 1) {
|
|
|
62
62
|
/**
|
|
63
63
|
* Round a number up to a nearest multiple
|
|
64
64
|
*
|
|
65
|
-
* @param {number} value
|
|
65
|
+
* @param {number} value Value to round
|
|
66
66
|
* @param {number} [multiple=1] Multiple to round to
|
|
67
67
|
* @returns {number} Closest multiple
|
|
68
68
|
*/
|
|
@@ -74,7 +74,7 @@ export function snap(value, multiple = 1) {
|
|
|
74
74
|
/**
|
|
75
75
|
* Interpolate a value between two values using Linear interpolation (lerping)
|
|
76
76
|
*
|
|
77
|
-
* @param {number} t
|
|
77
|
+
* @param {number} t Normalized time value to interpolate
|
|
78
78
|
* @param {number} min Minimum value
|
|
79
79
|
* @param {number} max Maximum value
|
|
80
80
|
* @returns {number} Lerped value
|
|
@@ -86,8 +86,8 @@ export function lerp(t, min, max) {
|
|
|
86
86
|
* Normalize a value between two bounds
|
|
87
87
|
*
|
|
88
88
|
* @param {number} value Value to normalize
|
|
89
|
-
* @param {number} min
|
|
90
|
-
* @param {number} max
|
|
89
|
+
* @param {number} min Minimum boundary
|
|
90
|
+
* @param {number} max Maximum boundary
|
|
91
91
|
* @returns {number} Normalized value
|
|
92
92
|
*/
|
|
93
93
|
export function normalize(value, min, max) {
|
|
@@ -96,11 +96,11 @@ export function normalize(value, min, max) {
|
|
|
96
96
|
/**
|
|
97
97
|
* Re-map a number from one range to another
|
|
98
98
|
*
|
|
99
|
-
* @param {number} value
|
|
99
|
+
* @param {number} value Value to re-map
|
|
100
100
|
* @param {number} currentMin Lower bound of the value's current range
|
|
101
101
|
* @param {number} currentMax Upper bound of the value's current range
|
|
102
|
-
* @param {number} targetMin
|
|
103
|
-
* @param {number} targetMax
|
|
102
|
+
* @param {number} targetMin Lower bound of the value's target range
|
|
103
|
+
* @param {number} targetMax Upper bound of the value's target range
|
|
104
104
|
* @returns {number} Re-mapped value
|
|
105
105
|
*/
|
|
106
106
|
export function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
@@ -109,9 +109,9 @@ export function map(value, currentMin, currentMax, targetMin, targetMax) {
|
|
|
109
109
|
/**
|
|
110
110
|
* Interpolate a value between two values using Triangular interpolation
|
|
111
111
|
*
|
|
112
|
-
* @param {number} t
|
|
113
|
-
* @param {number} min
|
|
114
|
-
* @param {number} max
|
|
112
|
+
* @param {number} t Normalized time value to interpolate
|
|
113
|
+
* @param {number} min Minimum value
|
|
114
|
+
* @param {number} max Maximum value
|
|
115
115
|
* @param {number} peak Peak value controling the interpolation triangle shape
|
|
116
116
|
* - peak <= min : linear (same as lerp)
|
|
117
117
|
* - peak <= max : linear (same as lerp)
|
|
@@ -125,9 +125,9 @@ export function triLerp(t, min, max, peak) {
|
|
|
125
125
|
/**
|
|
126
126
|
* Interpolate a value using Exponential interpolation
|
|
127
127
|
*
|
|
128
|
-
* @param {number} t
|
|
129
|
-
* @param {number} min
|
|
130
|
-
* @param {number} max
|
|
128
|
+
* @param {number} t Normalized time value to interpolate
|
|
129
|
+
* @param {number} min Minimum value
|
|
130
|
+
* @param {number} max Maximum value
|
|
131
131
|
* @param {number} power Exponent controling the interpolation curve shape
|
|
132
132
|
* - power > 1 : ease-in
|
|
133
133
|
* - power < 1 : ease-out
|
|
@@ -141,7 +141,7 @@ export function expLerp(t, min, max, power) {
|
|
|
141
141
|
/**
|
|
142
142
|
* Interpolate a value using Quadratic Bézier interpolation
|
|
143
143
|
*
|
|
144
|
-
* @param {number} t
|
|
144
|
+
* @param {number} t Normalized time value to interpolate
|
|
145
145
|
* @param {number} p1 Start point
|
|
146
146
|
* @param {number} cp Control point
|
|
147
147
|
* @param {number} p2 End point
|
|
@@ -156,7 +156,7 @@ export function quadraticBezier(t, p1, cp, p2) {
|
|
|
156
156
|
/**
|
|
157
157
|
* Interpolate a value using Cubic Bézier interpolation
|
|
158
158
|
*
|
|
159
|
-
* @param {number} t
|
|
159
|
+
* @param {number} t Normalized time value to interpolate
|
|
160
160
|
* @param {number} p1 Start point
|
|
161
161
|
* @param {number} cp1 First control point
|
|
162
162
|
* @param {number} cp2 Second control point
|
|
@@ -174,7 +174,7 @@ export function cubicBezier(t, p1, cp1, cp2, p2) {
|
|
|
174
174
|
/**
|
|
175
175
|
* Interpolate a value using Catmull-Rom interpolation
|
|
176
176
|
*
|
|
177
|
-
* @param {number} t
|
|
177
|
+
* @param {number} t Normalized time value to interpolate
|
|
178
178
|
* @param {number} p1 Start point
|
|
179
179
|
* @param {number} cp1 First control point
|
|
180
180
|
* @param {number} cp2 Second control point
|
|
@@ -191,7 +191,7 @@ export function catmullRom(t, p1, cp1, cp2, p2) {
|
|
|
191
191
|
/**
|
|
192
192
|
* Modulo absolute a value based on a length
|
|
193
193
|
*
|
|
194
|
-
* @param {number} value
|
|
194
|
+
* @param {number} value Value to modulate
|
|
195
195
|
* @param {number} length Total length
|
|
196
196
|
* @returns {number} Modulated value
|
|
197
197
|
*/
|
|
@@ -204,7 +204,7 @@ export function modAbs(value, length) {
|
|
|
204
204
|
/**
|
|
205
205
|
* Move back and forth a value between 0 and length, so that it is never larger than length and never smaller than 0
|
|
206
206
|
*
|
|
207
|
-
* @param {number} value
|
|
207
|
+
* @param {number} value Value to modulate
|
|
208
208
|
* @param {number} length Total length
|
|
209
209
|
* @returns {number} PingPonged value
|
|
210
210
|
*/
|
|
@@ -215,7 +215,7 @@ export function pingPong(value, length) {
|
|
|
215
215
|
/**
|
|
216
216
|
* Smooth a value using cubic Hermite interpolation
|
|
217
217
|
*
|
|
218
|
-
* @param {number} value
|
|
218
|
+
* @param {number} value Value to smooth
|
|
219
219
|
* @param {number} [min=0] Minimum boundary
|
|
220
220
|
* @param {number} [max=1] Maximum boundary
|
|
221
221
|
* @returns {number} Normalized smoothed value
|
|
@@ -229,7 +229,7 @@ export function smoothstep(value, min = 0, max = 1) {
|
|
|
229
229
|
* - parabola(0) = parabola(1) = 0
|
|
230
230
|
* - parabola(0.5) = 1
|
|
231
231
|
*
|
|
232
|
-
* @param {number} x
|
|
232
|
+
* @param {number} x Normalized coordinate on X axis
|
|
233
233
|
* @param {number} [power=1] Parabola power
|
|
234
234
|
* @returns {number} Normalized re-mapped value
|
|
235
235
|
*/
|
|
@@ -257,10 +257,10 @@ export function average(array) {
|
|
|
257
257
|
/**
|
|
258
258
|
* Smoothly interpolate a number toward another
|
|
259
259
|
*
|
|
260
|
-
* @param {number} value
|
|
261
|
-
* @param {number} target
|
|
260
|
+
* @param {number} value Value to interpolate
|
|
261
|
+
* @param {number} target Destination of the interpolation
|
|
262
262
|
* @param {number} damping A higher value will make the movement more sudden, and a lower value will make the movement more gradual
|
|
263
|
-
* @param {number} delta
|
|
263
|
+
* @param {number} delta Delta time (in seconds)
|
|
264
264
|
* @returns {number} Interpolated number
|
|
265
265
|
*/
|
|
266
266
|
export function damp(value, target, damping, delta) {
|
package/lib/prng.d.ts
CHANGED
|
@@ -74,9 +74,9 @@ export declare function randomSign(prng: PRNGParameters, probability?: number):
|
|
|
74
74
|
/**
|
|
75
75
|
* Generate a pseudo-random floating-point number within a specified range
|
|
76
76
|
*
|
|
77
|
-
* @param {PRNGParameters} prng
|
|
78
|
-
* @param {number} [min=0]
|
|
79
|
-
* @param {number} [max=1]
|
|
77
|
+
* @param {PRNGParameters} prng PRNG parameters
|
|
78
|
+
* @param {number} [min=0] Minimum boundary
|
|
79
|
+
* @param {number} [max=1] Maximum boundary
|
|
80
80
|
* @param {number} [precision=2] Number of digits after the decimal point
|
|
81
81
|
* @returns {number} Generated float
|
|
82
82
|
*/
|
|
@@ -85,8 +85,8 @@ export declare function randomFloat(prng: PRNGParameters, min?: number, max?: nu
|
|
|
85
85
|
* Generate a pseudo-random integer number within a specified range
|
|
86
86
|
*
|
|
87
87
|
* @param {PRNGParameters} prng PRNG parameters
|
|
88
|
-
* @param {number} min
|
|
89
|
-
* @param {number} max
|
|
88
|
+
* @param {number} min Minimum boundary
|
|
89
|
+
* @param {number} max Maximum boundary
|
|
90
90
|
* @returns {number} Generated integer
|
|
91
91
|
*/
|
|
92
92
|
export declare function randomInt(prng: PRNGParameters, min: number, max: number): number;
|
|
@@ -101,7 +101,7 @@ export declare function randomHexColor(prng: PRNGParameters): string;
|
|
|
101
101
|
* Pick a pseudo-random item from a given array
|
|
102
102
|
*
|
|
103
103
|
* @param {PRNGParameters} prng PRNG parameters
|
|
104
|
-
* @param {unknown[]} array
|
|
104
|
+
* @param {unknown[]} array Array to pick the item from
|
|
105
105
|
* @returns {unknown|undefined} Random item picked
|
|
106
106
|
*/
|
|
107
107
|
export declare function randomItem<T = unknown>(prng: PRNGParameters, array: T[]): T | undefined;
|
|
@@ -109,7 +109,7 @@ export declare function randomItem<T = unknown>(prng: PRNGParameters, array: T[]
|
|
|
109
109
|
* Pick a pseudo-random property value from a given object
|
|
110
110
|
*
|
|
111
111
|
* @param {PRNGParameters} prng PRNG parameters
|
|
112
|
-
* @param {object} object
|
|
112
|
+
* @param {object} object Object to pick the property from
|
|
113
113
|
* @returns {unknown|undefined} Random item picked
|
|
114
114
|
*/
|
|
115
115
|
export declare function randomObjectProperty<T = unknown>(prng: PRNGParameters, object: Record<string, T>): T | undefined;
|
|
@@ -117,7 +117,7 @@ export declare function randomObjectProperty<T = unknown>(prng: PRNGParameters,
|
|
|
117
117
|
* Select a pseudo-random index from an array of weighted items
|
|
118
118
|
*
|
|
119
119
|
* @param {PRNGParameters} prng PRNG parameters
|
|
120
|
-
* @param {number[]} weights
|
|
120
|
+
* @param {number[]} weights Array of weights
|
|
121
121
|
* @returns {number} Random index based on weights
|
|
122
122
|
*/
|
|
123
123
|
export declare function randomIndex(prng: PRNGParameters, weights: number[]): number;
|
|
@@ -125,8 +125,8 @@ export declare function randomIndex(prng: PRNGParameters, weights: number[]): nu
|
|
|
125
125
|
* Generate a pseudo-random number fitting a Gaussian (normal) distribution
|
|
126
126
|
*
|
|
127
127
|
* @param {PRNGParameters} prng PRNG parameters
|
|
128
|
-
* @param {number} [mean=0]
|
|
129
|
-
* @param {number} [spread=1]
|
|
128
|
+
* @param {number} [mean=0] Central value
|
|
129
|
+
* @param {number} [spread=1] Standard deviation
|
|
130
130
|
* @returns {number} Generated number
|
|
131
131
|
*/
|
|
132
132
|
export declare function randomGaussian(prng: PRNGParameters, mean?: number, spread?: number): number;
|