toosoon-utils 4.3.0 → 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 +55 -58
- 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/colors/ColorScale.d.ts +1 -1
- package/lib/extras/frame-rate/FrameRate.d.ts +11 -8
- package/lib/extras/frame-rate/FrameRate.js +8 -5
- package/lib/extras/paths/Path.js +4 -7
- package/lib/extras/paths/PathContext.d.ts +10 -8
- package/lib/extras/paths/PathContext.js +19 -13
- 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 +13 -13
- package/lib/geometry.js +19 -19
- 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/extras/paths/Path.js
CHANGED
|
@@ -65,8 +65,7 @@ export default class Path extends Curve {
|
|
|
65
65
|
getPoints(divisions = 40) {
|
|
66
66
|
const points = [];
|
|
67
67
|
let lastPoint = null;
|
|
68
|
-
for (
|
|
69
|
-
const curve = this.curves[i];
|
|
68
|
+
for (const curve of this.curves) {
|
|
70
69
|
let resolution = divisions;
|
|
71
70
|
if (curve instanceof LineCurve || curve instanceof LineCurve3) {
|
|
72
71
|
resolution = 1;
|
|
@@ -80,9 +79,7 @@ export default class Path extends Curve {
|
|
|
80
79
|
else if (curve instanceof EllipseCurve) {
|
|
81
80
|
resolution *= 2;
|
|
82
81
|
}
|
|
83
|
-
const
|
|
84
|
-
for (let j = 0; j < pts.length; j++) {
|
|
85
|
-
const point = pts[j];
|
|
82
|
+
for (const point of curve.getPoints(resolution)) {
|
|
86
83
|
if (point?.equals(lastPoint))
|
|
87
84
|
continue;
|
|
88
85
|
points.push(point);
|
|
@@ -132,8 +129,8 @@ export default class Path extends Curve {
|
|
|
132
129
|
}
|
|
133
130
|
const lengths = [];
|
|
134
131
|
let sums = 0;
|
|
135
|
-
for (
|
|
136
|
-
sums +=
|
|
132
|
+
for (const curve of this.curves) {
|
|
133
|
+
sums += curve.getLength();
|
|
137
134
|
lengths.push(sums);
|
|
138
135
|
}
|
|
139
136
|
this._cacheArcLengths = lengths;
|
|
@@ -13,6 +13,8 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
|
|
|
13
13
|
protected _currentPosition: Vector2;
|
|
14
14
|
protected _currentTransform: DOMMatrix;
|
|
15
15
|
private _transformStack;
|
|
16
|
+
readonly autoClose: false;
|
|
17
|
+
constructor();
|
|
16
18
|
/**
|
|
17
19
|
* Create a path from a given list of points
|
|
18
20
|
*
|
|
@@ -111,7 +113,7 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
|
|
|
111
113
|
* @param {number} cy Y-axis coordinate of the center of the circle
|
|
112
114
|
* @param {number} rx X-radius of the ellipse
|
|
113
115
|
* @param {number} ry Y-radius of the ellipse
|
|
114
|
-
* @param {number} rotation
|
|
116
|
+
* @param {number} rotation Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis
|
|
115
117
|
* @param {number} startAngle Start angle of the arc (in radians)
|
|
116
118
|
* @param {number} endAngle End angle of the arc (in radians)
|
|
117
119
|
* @param {boolean} [counterclockwise] Flag indicating the direction of the arc
|
|
@@ -147,9 +149,9 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
|
|
|
147
149
|
* Draw a rectangular path from the start position specified by `x` and `y` to the end position using `width` and `height`
|
|
148
150
|
* Add an instance of {@link PolylineCurve} to this path
|
|
149
151
|
*
|
|
150
|
-
* @param {number} x
|
|
151
|
-
* @param {number} y
|
|
152
|
-
* @param {number} width
|
|
152
|
+
* @param {number} x X-axis coordinate of the rectangle starting point
|
|
153
|
+
* @param {number} y Y-axis coordinate of the rectangle starting point
|
|
154
|
+
* @param {number} width Rectangle width (Positive values are to the right and negative to the left)
|
|
153
155
|
* @param {number} height Rectangle height (Positive values are down, and negative are up)
|
|
154
156
|
* @return {this}
|
|
155
157
|
*/
|
|
@@ -158,9 +160,9 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
|
|
|
158
160
|
* Draw a rounded rectangular path from the start position specified by `x` and `y` to the end position using `width` and `height`
|
|
159
161
|
* Add an instance of {@link Path} to this path
|
|
160
162
|
*
|
|
161
|
-
* @param {number} x
|
|
162
|
-
* @param {number} y
|
|
163
|
-
* @param {number} width
|
|
163
|
+
* @param {number} x X-axis coordinate of the rectangle starting point
|
|
164
|
+
* @param {number} y Y-axis coordinate of the rectangle starting point
|
|
165
|
+
* @param {number} width Rectangle width (Positive values are to the right and negative to the left)
|
|
164
166
|
* @param {number} height Rectangle height (Positive values are down, and negative are up)
|
|
165
167
|
* @param {number|number[]} radius Radius of the circular arc to be used for the corners of the rectangle
|
|
166
168
|
* @return {this}
|
|
@@ -204,7 +206,6 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
|
|
|
204
206
|
getLineDash: CanvasRenderingContext2D['getLineDash'];
|
|
205
207
|
fillText: CanvasRenderingContext2D['fillText'];
|
|
206
208
|
strokeText: CanvasRenderingContext2D['strokeText'];
|
|
207
|
-
miterLimit: CanvasRenderingContext2D['miterLimit'];
|
|
208
209
|
fill: CanvasRenderingContext2D['fill'];
|
|
209
210
|
stroke: CanvasRenderingContext2D['stroke'];
|
|
210
211
|
clearRect: CanvasRenderingContext2D['clearRect'];
|
|
@@ -213,6 +214,7 @@ export default class PathContext extends Path<Vector2> implements CanvasRenderin
|
|
|
213
214
|
drawImage: CanvasRenderingContext2D['drawImage'];
|
|
214
215
|
clip: CanvasRenderingContext2D['clip'];
|
|
215
216
|
filter: CanvasRenderingContext2D['filter'];
|
|
217
|
+
miterLimit: CanvasRenderingContext2D['miterLimit'];
|
|
216
218
|
globalAlpha: CanvasRenderingContext2D['globalAlpha'];
|
|
217
219
|
globalCompositeOperation: CanvasRenderingContext2D['globalCompositeOperation'];
|
|
218
220
|
createLinearGradient: CanvasRenderingContext2D['createLinearGradient'];
|
|
@@ -15,6 +15,11 @@ export default class PathContext extends Path {
|
|
|
15
15
|
_currentPosition = new Vector2(NaN, NaN);
|
|
16
16
|
_currentTransform = new DOMMatrix();
|
|
17
17
|
_transformStack = [];
|
|
18
|
+
autoClose;
|
|
19
|
+
constructor() {
|
|
20
|
+
super({ autoClose: false });
|
|
21
|
+
this.autoClose = false;
|
|
22
|
+
}
|
|
18
23
|
/**
|
|
19
24
|
* Create a path from a given list of points
|
|
20
25
|
*
|
|
@@ -23,8 +28,8 @@ export default class PathContext extends Path {
|
|
|
23
28
|
*/
|
|
24
29
|
setFromPoints(points) {
|
|
25
30
|
this.moveTo(...points[0]);
|
|
26
|
-
for (
|
|
27
|
-
this.lineTo(...
|
|
31
|
+
for (const point of points) {
|
|
32
|
+
this.lineTo(...point);
|
|
28
33
|
}
|
|
29
34
|
return this;
|
|
30
35
|
}
|
|
@@ -187,7 +192,7 @@ export default class PathContext extends Path {
|
|
|
187
192
|
* @param {number} cy Y-axis coordinate of the center of the circle
|
|
188
193
|
* @param {number} rx X-radius of the ellipse
|
|
189
194
|
* @param {number} ry Y-radius of the ellipse
|
|
190
|
-
* @param {number} rotation
|
|
195
|
+
* @param {number} rotation Rotation angle of the ellipse (in radians), counterclockwise from the positive X-axis
|
|
191
196
|
* @param {number} startAngle Start angle of the arc (in radians)
|
|
192
197
|
* @param {number} endAngle End angle of the arc (in radians)
|
|
193
198
|
* @param {boolean} [counterclockwise] Flag indicating the direction of the arc
|
|
@@ -280,8 +285,7 @@ export default class PathContext extends Path {
|
|
|
280
285
|
const c = t1.clone().add(n1.clone().multiplyScalar(normalLength));
|
|
281
286
|
const startAngle = Math.atan2(t1.y - c.y, t1.x - c.x);
|
|
282
287
|
const endAngle = Math.atan2(t2.y - c.y, t2.x - c.x);
|
|
283
|
-
const
|
|
284
|
-
const counterclockwise = deltaAngle < 0;
|
|
288
|
+
const counterclockwise = (p0.y - p1.y) * (p2.x - p0.x) <= (p0.x - p1.x) * (p2.y - p0.y);
|
|
285
289
|
t1.applyMatrix(this._currentTransform);
|
|
286
290
|
t2.applyMatrix(this._currentTransform);
|
|
287
291
|
c.applyMatrix(this._currentTransform);
|
|
@@ -299,9 +303,9 @@ export default class PathContext extends Path {
|
|
|
299
303
|
* Draw a rectangular path from the start position specified by `x` and `y` to the end position using `width` and `height`
|
|
300
304
|
* Add an instance of {@link PolylineCurve} to this path
|
|
301
305
|
*
|
|
302
|
-
* @param {number} x
|
|
303
|
-
* @param {number} y
|
|
304
|
-
* @param {number} width
|
|
306
|
+
* @param {number} x X-axis coordinate of the rectangle starting point
|
|
307
|
+
* @param {number} y Y-axis coordinate of the rectangle starting point
|
|
308
|
+
* @param {number} width Rectangle width (Positive values are to the right and negative to the left)
|
|
305
309
|
* @param {number} height Rectangle height (Positive values are down, and negative are up)
|
|
306
310
|
* @return {this}
|
|
307
311
|
*/
|
|
@@ -320,9 +324,9 @@ export default class PathContext extends Path {
|
|
|
320
324
|
* Draw a rounded rectangular path from the start position specified by `x` and `y` to the end position using `width` and `height`
|
|
321
325
|
* Add an instance of {@link Path} to this path
|
|
322
326
|
*
|
|
323
|
-
* @param {number} x
|
|
324
|
-
* @param {number} y
|
|
325
|
-
* @param {number} width
|
|
327
|
+
* @param {number} x X-axis coordinate of the rectangle starting point
|
|
328
|
+
* @param {number} y Y-axis coordinate of the rectangle starting point
|
|
329
|
+
* @param {number} width Rectangle width (Positive values are to the right and negative to the left)
|
|
326
330
|
* @param {number} height Rectangle height (Positive values are down, and negative are up)
|
|
327
331
|
* @param {number|number[]} radius Radius of the circular arc to be used for the corners of the rectangle
|
|
328
332
|
* @return {this}
|
|
@@ -338,7 +342,7 @@ export default class PathContext extends Path {
|
|
|
338
342
|
topRightRadius = Math.min(topRightRadius, maxRadius);
|
|
339
343
|
bottomRightRadius = Math.min(bottomRightRadius, maxRadius);
|
|
340
344
|
bottomLeftRadius = Math.min(bottomLeftRadius, maxRadius);
|
|
341
|
-
const curve = new PathContext(
|
|
345
|
+
const curve = new PathContext();
|
|
342
346
|
curve.setTransform(this.getTransform());
|
|
343
347
|
// Top-Right corner
|
|
344
348
|
if (topRightRadius > 0) {
|
|
@@ -372,7 +376,9 @@ export default class PathContext extends Path {
|
|
|
372
376
|
else {
|
|
373
377
|
curve.lineTo(x, y);
|
|
374
378
|
}
|
|
379
|
+
curve.closePath();
|
|
375
380
|
this.add(curve);
|
|
381
|
+
this.moveTo(x, y);
|
|
376
382
|
return this;
|
|
377
383
|
}
|
|
378
384
|
setTransform(a, b, c, d, e, f) {
|
|
@@ -503,7 +509,6 @@ export default class PathContext extends Path {
|
|
|
503
509
|
getLineDash;
|
|
504
510
|
fillText;
|
|
505
511
|
strokeText;
|
|
506
|
-
miterLimit;
|
|
507
512
|
fill;
|
|
508
513
|
stroke;
|
|
509
514
|
clearRect;
|
|
@@ -512,6 +517,7 @@ export default class PathContext extends Path {
|
|
|
512
517
|
drawImage;
|
|
513
518
|
clip;
|
|
514
519
|
filter;
|
|
520
|
+
miterLimit;
|
|
515
521
|
globalAlpha;
|
|
516
522
|
globalCompositeOperation;
|
|
517
523
|
createLinearGradient;
|
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,30 +13,30 @@ 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
|
-
* @param {number}
|
|
29
|
-
* @param {number}
|
|
30
|
-
* @returns {number} Closest angle
|
|
28
|
+
* @param {number} startAngle Start angle (in radians)
|
|
29
|
+
* @param {number} endAngle End angle (in radians)
|
|
30
|
+
* @returns {number} Closest angle (in radians)
|
|
31
31
|
*/
|
|
32
|
-
export declare function closestAngle(
|
|
32
|
+
export declare function closestAngle(startAngle: number, endAngle: number): number;
|
|
33
33
|
/**
|
|
34
34
|
* Calculate the distance between two points
|
|
35
35
|
*
|
|
36
|
-
* @param {number} x1 X-axis coordinate of the
|
|
37
|
-
* @param {number} y1 Y-axis coordinate of the
|
|
38
|
-
* @param {number} x2 X-axis coordinate of the
|
|
39
|
-
* @param {number} y2 Y-axis coordinate of the
|
|
36
|
+
* @param {number} x1 X-axis coordinate of the start point
|
|
37
|
+
* @param {number} y1 Y-axis coordinate of the start point
|
|
38
|
+
* @param {number} x2 X-axis coordinate of the end point
|
|
39
|
+
* @param {number} y2 Y-axis coordinate of the end point
|
|
40
40
|
* @returns {number} Computed distance
|
|
41
41
|
*/
|
|
42
42
|
export declare function distance(x1: number, y1: number, x2: number, y2: number): number;
|
|
@@ -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,35 +18,35 @@ 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
|
-
* @param {number}
|
|
36
|
-
* @param {number}
|
|
37
|
-
* @returns {number} Closest angle
|
|
35
|
+
* @param {number} startAngle Start angle (in radians)
|
|
36
|
+
* @param {number} endAngle End angle (in radians)
|
|
37
|
+
* @returns {number} Closest angle (in radians)
|
|
38
38
|
*/
|
|
39
|
-
export function closestAngle(
|
|
40
|
-
const delta =
|
|
41
|
-
return delta > PI ?
|
|
39
|
+
export function closestAngle(startAngle, endAngle) {
|
|
40
|
+
const delta = endAngle - startAngle;
|
|
41
|
+
return delta > PI ? endAngle - 2 * PI : endAngle < -PI ? delta + 2 * PI : endAngle;
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* Calculate the distance between two points
|
|
45
45
|
*
|
|
46
|
-
* @param {number} x1 X-axis coordinate of the
|
|
47
|
-
* @param {number} y1 Y-axis coordinate of the
|
|
48
|
-
* @param {number} x2 X-axis coordinate of the
|
|
49
|
-
* @param {number} y2 Y-axis coordinate of the
|
|
46
|
+
* @param {number} x1 X-axis coordinate of the start point
|
|
47
|
+
* @param {number} y1 Y-axis coordinate of the start point
|
|
48
|
+
* @param {number} x2 X-axis coordinate of the end point
|
|
49
|
+
* @param {number} y2 Y-axis coordinate of the end point
|
|
50
50
|
* @returns {number} Computed distance
|
|
51
51
|
*/
|
|
52
52
|
export function distance(x1, y1, x2, y2) {
|
|
@@ -67,10 +67,10 @@ export function diagonal(width, height) {
|
|
|
67
67
|
/**
|
|
68
68
|
* Make a target fit a container
|
|
69
69
|
*
|
|
70
|
-
* @param {
|
|
71
|
-
* @param {
|
|
72
|
-
* @param {string} mode
|
|
73
|
-
* @returns {
|
|
70
|
+
* @param {FitInput} target Dimensions of the target
|
|
71
|
+
* @param {FitInput} container Dimensions of the container
|
|
72
|
+
* @param {string} mode Can be 'contain' | 'cover'
|
|
73
|
+
* @returns {FitOutput}
|
|
74
74
|
*/
|
|
75
75
|
function fit(target, container, mode) {
|
|
76
76
|
const ratioWidth = container.width / target.width;
|
|
@@ -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;
|