toosoon-utils 1.0.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/.prettierrc +7 -0
- package/LICENSE +21 -0
- package/lib/colors.d.ts +147 -0
- package/lib/colors.js +372 -0
- package/lib/constants.d.ts +162 -0
- package/lib/constants.js +173 -0
- package/lib/dom.d.ts +25 -0
- package/lib/dom.js +53 -0
- package/lib/files.d.ts +14 -0
- package/lib/files.js +43 -0
- package/lib/functions.d.ts +18 -0
- package/lib/functions.js +33 -0
- package/lib/geometry.d.ts +89 -0
- package/lib/geometry.js +140 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +27 -0
- package/lib/maths.d.ts +159 -0
- package/lib/maths.js +238 -0
- package/lib/now.d.ts +5 -0
- package/lib/now.js +28 -0
- package/lib/random.d.ts +91 -0
- package/lib/random.js +180 -0
- package/lib/strings.d.ts +14 -0
- package/lib/strings.js +23 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/lib/types.d.ts +15 -0
- package/lib/types.js +2 -0
- package/package.json +37 -0
- package/src/colors.ts +376 -0
- package/src/constants.ts +172 -0
- package/src/dom.ts +50 -0
- package/src/files.ts +42 -0
- package/src/functions.ts +31 -0
- package/src/geometry.ts +160 -0
- package/src/index.ts +12 -0
- package/src/maths.ts +230 -0
- package/src/now.ts +26 -0
- package/src/random.ts +162 -0
- package/src/strings.ts +19 -0
- package/src/types.ts +24 -0
- package/tsconfig.json +23 -0
package/lib/constants.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.W3CX11 = exports.QUARTER_PI = exports.HALF_PI = exports.TWO_PI = exports.PI = exports.EPSILON = void 0;
|
|
4
|
+
// *********************
|
|
5
|
+
// Maths
|
|
6
|
+
// *********************
|
|
7
|
+
exports.EPSILON = 1e-10;
|
|
8
|
+
exports.PI = Math.PI;
|
|
9
|
+
exports.TWO_PI = Math.PI * 2;
|
|
10
|
+
exports.HALF_PI = Math.PI / 2;
|
|
11
|
+
exports.QUARTER_PI = Math.PI / 4;
|
|
12
|
+
// *********************
|
|
13
|
+
// Color
|
|
14
|
+
// *********************
|
|
15
|
+
// X11 colors
|
|
16
|
+
// -> https://www.w3.org/TR/css-color-3/#svg-color
|
|
17
|
+
exports.W3CX11 = {
|
|
18
|
+
aliceblue: '#f0f8ff',
|
|
19
|
+
antiquewhite: '#faebd7',
|
|
20
|
+
aqua: '#00ffff',
|
|
21
|
+
aquamarine: '#7fffd4',
|
|
22
|
+
azure: '#f0ffff',
|
|
23
|
+
beige: '#f5f5dc',
|
|
24
|
+
bisque: '#ffe4c4',
|
|
25
|
+
black: '#000000',
|
|
26
|
+
blanchedalmond: '#ffebcd',
|
|
27
|
+
blue: '#0000ff',
|
|
28
|
+
blueviolet: '#8a2be2',
|
|
29
|
+
brown: '#a52a2a',
|
|
30
|
+
burlywood: '#deb887',
|
|
31
|
+
cadetblue: '#5f9ea0',
|
|
32
|
+
chartreuse: '#7fff00',
|
|
33
|
+
chocolate: '#d2691e',
|
|
34
|
+
coral: '#ff7f50',
|
|
35
|
+
cornflower: '#6495ed',
|
|
36
|
+
cornflowerblue: '#6495ed',
|
|
37
|
+
cornsilk: '#fff8dc',
|
|
38
|
+
crimson: '#dc143c',
|
|
39
|
+
cyan: '#00ffff',
|
|
40
|
+
darkblue: '#00008b',
|
|
41
|
+
darkcyan: '#008b8b',
|
|
42
|
+
darkgoldenrod: '#b8860b',
|
|
43
|
+
darkgray: '#a9a9a9',
|
|
44
|
+
darkgreen: '#006400',
|
|
45
|
+
darkgrey: '#a9a9a9',
|
|
46
|
+
darkkhaki: '#bdb76b',
|
|
47
|
+
darkmagenta: '#8b008b',
|
|
48
|
+
darkolivegreen: '#556b2f',
|
|
49
|
+
darkorange: '#ff8c00',
|
|
50
|
+
darkorchid: '#9932cc',
|
|
51
|
+
darkred: '#8b0000',
|
|
52
|
+
darksalmon: '#e9967a',
|
|
53
|
+
darkseagreen: '#8fbc8f',
|
|
54
|
+
darkslateblue: '#483d8b',
|
|
55
|
+
darkslategray: '#2f4f4f',
|
|
56
|
+
darkslategrey: '#2f4f4f',
|
|
57
|
+
darkturquoise: '#00ced1',
|
|
58
|
+
darkviolet: '#9400d3',
|
|
59
|
+
deeppink: '#ff1493',
|
|
60
|
+
deepskyblue: '#00bfff',
|
|
61
|
+
dimgray: '#696969',
|
|
62
|
+
dimgrey: '#696969',
|
|
63
|
+
dodgerblue: '#1e90ff',
|
|
64
|
+
firebrick: '#b22222',
|
|
65
|
+
floralwhite: '#fffaf0',
|
|
66
|
+
forestgreen: '#228b22',
|
|
67
|
+
fuchsia: '#ff00ff',
|
|
68
|
+
gainsboro: '#dcdcdc',
|
|
69
|
+
ghostwhite: '#f8f8ff',
|
|
70
|
+
gold: '#ffd700',
|
|
71
|
+
goldenrod: '#daa520',
|
|
72
|
+
gray: '#808080',
|
|
73
|
+
green: '#008000',
|
|
74
|
+
greenyellow: '#adff2f',
|
|
75
|
+
grey: '#808080',
|
|
76
|
+
honeydew: '#f0fff0',
|
|
77
|
+
hotpink: '#ff69b4',
|
|
78
|
+
indianred: '#cd5c5c',
|
|
79
|
+
indigo: '#4b0082',
|
|
80
|
+
ivory: '#fffff0',
|
|
81
|
+
khaki: '#f0e68c',
|
|
82
|
+
laserlemon: '#ffff54',
|
|
83
|
+
lavender: '#e6e6fa',
|
|
84
|
+
lavenderblush: '#fff0f5',
|
|
85
|
+
lawngreen: '#7cfc00',
|
|
86
|
+
lemonchiffon: '#fffacd',
|
|
87
|
+
lightblue: '#add8e6',
|
|
88
|
+
lightcoral: '#f08080',
|
|
89
|
+
lightcyan: '#e0ffff',
|
|
90
|
+
lightgoldenrod: '#fafad2',
|
|
91
|
+
lightgoldenrodyellow: '#fafad2',
|
|
92
|
+
lightgray: '#d3d3d3',
|
|
93
|
+
lightgreen: '#90ee90',
|
|
94
|
+
lightgrey: '#d3d3d3',
|
|
95
|
+
lightpink: '#ffb6c1',
|
|
96
|
+
lightsalmon: '#ffa07a',
|
|
97
|
+
lightseagreen: '#20b2aa',
|
|
98
|
+
lightskyblue: '#87cefa',
|
|
99
|
+
lightslategray: '#778899',
|
|
100
|
+
lightslategrey: '#778899',
|
|
101
|
+
lightsteelblue: '#b0c4de',
|
|
102
|
+
lightyellow: '#ffffe0',
|
|
103
|
+
lime: '#00ff00',
|
|
104
|
+
limegreen: '#32cd32',
|
|
105
|
+
linen: '#faf0e6',
|
|
106
|
+
magenta: '#ff00ff',
|
|
107
|
+
maroon: '#800000',
|
|
108
|
+
maroon2: '#7f0000',
|
|
109
|
+
maroon3: '#b03060',
|
|
110
|
+
mediumaquamarine: '#66cdaa',
|
|
111
|
+
mediumblue: '#0000cd',
|
|
112
|
+
mediumorchid: '#ba55d3',
|
|
113
|
+
mediumpurple: '#9370db',
|
|
114
|
+
mediumseagreen: '#3cb371',
|
|
115
|
+
mediumslateblue: '#7b68ee',
|
|
116
|
+
mediumspringgreen: '#00fa9a',
|
|
117
|
+
mediumturquoise: '#48d1cc',
|
|
118
|
+
mediumvioletred: '#c71585',
|
|
119
|
+
midnightblue: '#191970',
|
|
120
|
+
mintcream: '#f5fffa',
|
|
121
|
+
mistyrose: '#ffe4e1',
|
|
122
|
+
moccasin: '#ffe4b5',
|
|
123
|
+
navajowhite: '#ffdead',
|
|
124
|
+
navy: '#000080',
|
|
125
|
+
oldlace: '#fdf5e6',
|
|
126
|
+
olive: '#808000',
|
|
127
|
+
olivedrab: '#6b8e23',
|
|
128
|
+
orange: '#ffa500',
|
|
129
|
+
orangered: '#ff4500',
|
|
130
|
+
orchid: '#da70d6',
|
|
131
|
+
palegoldenrod: '#eee8aa',
|
|
132
|
+
palegreen: '#98fb98',
|
|
133
|
+
paleturquoise: '#afeeee',
|
|
134
|
+
palevioletred: '#db7093',
|
|
135
|
+
papayawhip: '#ffefd5',
|
|
136
|
+
peachpuff: '#ffdab9',
|
|
137
|
+
peru: '#cd853f',
|
|
138
|
+
pink: '#ffc0cb',
|
|
139
|
+
plum: '#dda0dd',
|
|
140
|
+
powderblue: '#b0e0e6',
|
|
141
|
+
purple: '#800080',
|
|
142
|
+
purple2: '#7f007f',
|
|
143
|
+
purple3: '#a020f0',
|
|
144
|
+
rebeccapurple: '#663399',
|
|
145
|
+
red: '#ff0000',
|
|
146
|
+
rosybrown: '#bc8f8f',
|
|
147
|
+
royalblue: '#4169e1',
|
|
148
|
+
saddlebrown: '#8b4513',
|
|
149
|
+
salmon: '#fa8072',
|
|
150
|
+
sandybrown: '#f4a460',
|
|
151
|
+
seagreen: '#2e8b57',
|
|
152
|
+
seashell: '#fff5ee',
|
|
153
|
+
sienna: '#a0522d',
|
|
154
|
+
silver: '#c0c0c0',
|
|
155
|
+
skyblue: '#87ceeb',
|
|
156
|
+
slateblue: '#6a5acd',
|
|
157
|
+
slategray: '#708090',
|
|
158
|
+
slategrey: '#708090',
|
|
159
|
+
snow: '#fffafa',
|
|
160
|
+
springgreen: '#00ff7f',
|
|
161
|
+
steelblue: '#4682b4',
|
|
162
|
+
tan: '#d2b48c',
|
|
163
|
+
teal: '#008080',
|
|
164
|
+
thistle: '#d8bfd8',
|
|
165
|
+
tomato: '#ff6347',
|
|
166
|
+
turquoise: '#40e0d0',
|
|
167
|
+
violet: '#ee82ee',
|
|
168
|
+
wheat: '#f5deb3',
|
|
169
|
+
white: '#ffffff',
|
|
170
|
+
whitesmoke: '#f5f5f5',
|
|
171
|
+
yellow: '#ffff00',
|
|
172
|
+
yellowgreen: '#9acd32'
|
|
173
|
+
};
|
package/lib/dom.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Find the closest parent that matches a selector
|
|
3
|
+
*
|
|
4
|
+
* @param {Element} element Target element
|
|
5
|
+
* @param {(Element|string)} selector Selector or parent to match
|
|
6
|
+
* @returns {Element|null}
|
|
7
|
+
*/
|
|
8
|
+
export declare function closest(element: Element, selector: Element | string): Element | null;
|
|
9
|
+
/**
|
|
10
|
+
* Create a canvas and 2d context
|
|
11
|
+
*
|
|
12
|
+
* @param {Number} width Width of the canvas
|
|
13
|
+
* @param {Number} height Height of the canvas
|
|
14
|
+
* @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D }}
|
|
15
|
+
*/
|
|
16
|
+
export declare function createCanvas(width: number, height: number): {
|
|
17
|
+
canvas: HTMLCanvasElement;
|
|
18
|
+
ctx: CanvasRenderingContext2D;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Inject CSS style in `document.head`
|
|
22
|
+
*
|
|
23
|
+
* @param {string} cssContent CSS style to inject
|
|
24
|
+
*/
|
|
25
|
+
export declare function injectStyles(cssContent: string): void;
|
package/lib/dom.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.injectStyles = exports.createCanvas = exports.closest = void 0;
|
|
4
|
+
var DOCUMENT_NODE_TYPE = 9;
|
|
5
|
+
/**
|
|
6
|
+
* Find the closest parent that matches a selector
|
|
7
|
+
*
|
|
8
|
+
* @param {Element} element Target element
|
|
9
|
+
* @param {(Element|string)} selector Selector or parent to match
|
|
10
|
+
* @returns {Element|null}
|
|
11
|
+
*/
|
|
12
|
+
function closest(element, selector) {
|
|
13
|
+
var current = element;
|
|
14
|
+
while (current && current.nodeType !== DOCUMENT_NODE_TYPE) {
|
|
15
|
+
if ((typeof selector === 'string' && current.matches(selector)) || current === selector) {
|
|
16
|
+
return current;
|
|
17
|
+
}
|
|
18
|
+
current = element.parentNode;
|
|
19
|
+
}
|
|
20
|
+
return current;
|
|
21
|
+
}
|
|
22
|
+
exports.closest = closest;
|
|
23
|
+
/**
|
|
24
|
+
* Create a canvas and 2d context
|
|
25
|
+
*
|
|
26
|
+
* @param {Number} width Width of the canvas
|
|
27
|
+
* @param {Number} height Height of the canvas
|
|
28
|
+
* @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D }}
|
|
29
|
+
*/
|
|
30
|
+
function createCanvas(width, height) {
|
|
31
|
+
var _a;
|
|
32
|
+
var canvas = document.createElement('canvas');
|
|
33
|
+
canvas.width = width;
|
|
34
|
+
canvas.height = height;
|
|
35
|
+
var ctx = (_a = canvas.getContext('2d')) !== null && _a !== void 0 ? _a : new CanvasRenderingContext2D();
|
|
36
|
+
return { canvas: canvas, ctx: ctx };
|
|
37
|
+
}
|
|
38
|
+
exports.createCanvas = createCanvas;
|
|
39
|
+
/**
|
|
40
|
+
* Inject CSS style in `document.head`
|
|
41
|
+
*
|
|
42
|
+
* @param {string} cssContent CSS style to inject
|
|
43
|
+
*/
|
|
44
|
+
function injectStyles(cssContent) {
|
|
45
|
+
var $style = document.createElement('style');
|
|
46
|
+
$style.innerHTML = cssContent;
|
|
47
|
+
var $before = document.querySelector('head link[rel=stylesheet], head style');
|
|
48
|
+
if ($before)
|
|
49
|
+
document.head.insertBefore($style, $before);
|
|
50
|
+
else
|
|
51
|
+
document.head.appendChild($style);
|
|
52
|
+
}
|
|
53
|
+
exports.injectStyles = injectStyles;
|
package/lib/files.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Download a Blob object into user files
|
|
3
|
+
*
|
|
4
|
+
* @param {Blob} blob Blob object to download
|
|
5
|
+
* @param {string} filename Downloaded file name
|
|
6
|
+
*/
|
|
7
|
+
export declare function download(blob: Blob, filename: string): void;
|
|
8
|
+
/**
|
|
9
|
+
* Upload a file from user files
|
|
10
|
+
*
|
|
11
|
+
* @param {Function} onLoad Callback called once the file is loaded
|
|
12
|
+
* @param {string} [accept=''] Types the file input should accept
|
|
13
|
+
*/
|
|
14
|
+
export declare function upload(onLoad: (dataUrl: string) => void, accept?: string): void;
|
package/lib/files.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.upload = exports.download = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Download a Blob object into user files
|
|
6
|
+
*
|
|
7
|
+
* @param {Blob} blob Blob object to download
|
|
8
|
+
* @param {string} filename Downloaded file name
|
|
9
|
+
*/
|
|
10
|
+
function download(blob, filename) {
|
|
11
|
+
var link = document.createElement('a');
|
|
12
|
+
link.setAttribute('href', URL.createObjectURL(blob));
|
|
13
|
+
link.setAttribute('download', filename);
|
|
14
|
+
document.body.appendChild(link);
|
|
15
|
+
link.click();
|
|
16
|
+
document.body.removeChild(link);
|
|
17
|
+
}
|
|
18
|
+
exports.download = download;
|
|
19
|
+
/**
|
|
20
|
+
* Upload a file from user files
|
|
21
|
+
*
|
|
22
|
+
* @param {Function} onLoad Callback called once the file is loaded
|
|
23
|
+
* @param {string} [accept=''] Types the file input should accept
|
|
24
|
+
*/
|
|
25
|
+
function upload(onLoad, accept) {
|
|
26
|
+
if (accept === void 0) { accept = ''; }
|
|
27
|
+
var input = document.createElement('input');
|
|
28
|
+
input.setAttribute('type', 'file');
|
|
29
|
+
input.setAttribute('accept', accept);
|
|
30
|
+
input.addEventListener('change', function (event) {
|
|
31
|
+
var _a, _b;
|
|
32
|
+
var file = (_b = (_a = event.target) === null || _a === void 0 ? void 0 : _a.files) === null || _b === void 0 ? void 0 : _b[0];
|
|
33
|
+
if (file) {
|
|
34
|
+
var fileReader = new FileReader();
|
|
35
|
+
fileReader.addEventListener('load', function () { return onLoad(URL.createObjectURL(file)); });
|
|
36
|
+
fileReader.readAsDataURL(file);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
document.body.appendChild(input);
|
|
40
|
+
input.click();
|
|
41
|
+
document.body.removeChild(input);
|
|
42
|
+
}
|
|
43
|
+
exports.upload = upload;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Deferred } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* No-op function
|
|
4
|
+
*/
|
|
5
|
+
export declare const noop: () => void;
|
|
6
|
+
/**
|
|
7
|
+
* Promise wrapped setTimeout
|
|
8
|
+
*
|
|
9
|
+
* @param {number} timeout Time in ms
|
|
10
|
+
* @returns {Promise}
|
|
11
|
+
*/
|
|
12
|
+
export declare function wait(timeout: number): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Deferred promise implementation
|
|
15
|
+
*
|
|
16
|
+
* @returns {Deferred}
|
|
17
|
+
*/
|
|
18
|
+
export declare function defer<T = void>(): Deferred<T>;
|
package/lib/functions.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defer = exports.wait = exports.noop = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* No-op function
|
|
6
|
+
*/
|
|
7
|
+
var noop = function () { };
|
|
8
|
+
exports.noop = noop;
|
|
9
|
+
/**
|
|
10
|
+
* Promise wrapped setTimeout
|
|
11
|
+
*
|
|
12
|
+
* @param {number} timeout Time in ms
|
|
13
|
+
* @returns {Promise}
|
|
14
|
+
*/
|
|
15
|
+
function wait(timeout) {
|
|
16
|
+
return new Promise(function (resolve) { return setTimeout(resolve, timeout); });
|
|
17
|
+
}
|
|
18
|
+
exports.wait = wait;
|
|
19
|
+
/**
|
|
20
|
+
* Deferred promise implementation
|
|
21
|
+
*
|
|
22
|
+
* @returns {Deferred}
|
|
23
|
+
*/
|
|
24
|
+
function defer() {
|
|
25
|
+
var resolve;
|
|
26
|
+
var reject;
|
|
27
|
+
var promise = new Promise(function (_resolve, _reject) {
|
|
28
|
+
resolve = _resolve;
|
|
29
|
+
reject = _reject;
|
|
30
|
+
});
|
|
31
|
+
return { promise: promise, resolve: resolve, reject: reject };
|
|
32
|
+
}
|
|
33
|
+
exports.defer = defer;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Vector3 } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Convert a radians value into degrees
|
|
4
|
+
*
|
|
5
|
+
* @param {number} radians Angle in radians
|
|
6
|
+
* @returns {number} Angle in degrees
|
|
7
|
+
*/
|
|
8
|
+
export declare function toDegrees(radians: number): number;
|
|
9
|
+
/**
|
|
10
|
+
* Convert a degrees value into radians
|
|
11
|
+
*
|
|
12
|
+
* @param {number} degrees Angle in degrees
|
|
13
|
+
* @returns {number} Angle in radians
|
|
14
|
+
*/
|
|
15
|
+
export declare function toRadians(degrees: number): number;
|
|
16
|
+
/**
|
|
17
|
+
* Calculate the angle from a point to another
|
|
18
|
+
*
|
|
19
|
+
* @param {number} x1 X value of the first point
|
|
20
|
+
* @param {number} y1 Y value of the first point
|
|
21
|
+
* @param {number} x2 X value of the second point
|
|
22
|
+
* @param {number} y2 Y value of the second point
|
|
23
|
+
* @returns {number} Angle
|
|
24
|
+
*/
|
|
25
|
+
export declare function angle(x1: number, y1: number, x2: number, y2: number): number;
|
|
26
|
+
/**
|
|
27
|
+
* Find the closest angle betwween to angles
|
|
28
|
+
*
|
|
29
|
+
* @param {number} source Source angle in radians
|
|
30
|
+
* @param {number} target Target angle in radians
|
|
31
|
+
* @returns {number} Closest angle
|
|
32
|
+
*/
|
|
33
|
+
export declare function closestAngle(source: number, target: number): number;
|
|
34
|
+
/**
|
|
35
|
+
* Calculate the distance between two points
|
|
36
|
+
*
|
|
37
|
+
* @param {number} x1 X coord of the first point
|
|
38
|
+
* @param {number} y1 Y coord of the first point
|
|
39
|
+
* @param {number} x2 X coord of the second point
|
|
40
|
+
* @param {number} y2 Y coord of the second point
|
|
41
|
+
* @returns {number} Computed distance
|
|
42
|
+
*/
|
|
43
|
+
export declare function distance(x1: number, y1: number, x2: number, y2: number): number;
|
|
44
|
+
/**
|
|
45
|
+
* Calculate the length of the diagonal of a rectangle
|
|
46
|
+
*
|
|
47
|
+
* @param {number} width Width of the rectangle
|
|
48
|
+
* @param {number} height Height of the rectangle
|
|
49
|
+
* @returns {number} Diagonal length
|
|
50
|
+
*/
|
|
51
|
+
export declare function diagonal(width: number, height: number): number;
|
|
52
|
+
/**
|
|
53
|
+
* Convert radians to a 3D point on the surface of a unit sphere
|
|
54
|
+
*
|
|
55
|
+
* @param {number} radius Radius of the sphere
|
|
56
|
+
* @param {number} phi Polar angle from the y (up) axis : 0 to PI
|
|
57
|
+
* @param {number} theta Equator angle around the y (up) axis : 0 to 2 PI
|
|
58
|
+
* @param {Vector3} target Target vector
|
|
59
|
+
* @returns {Vector3}
|
|
60
|
+
*/
|
|
61
|
+
export declare function radToSphere(radius: number, phi: number, theta: number, target?: Vector3): Vector3;
|
|
62
|
+
interface FitInput {
|
|
63
|
+
width: number;
|
|
64
|
+
height: number;
|
|
65
|
+
}
|
|
66
|
+
interface FitOutput {
|
|
67
|
+
left: number;
|
|
68
|
+
top: number;
|
|
69
|
+
width: number;
|
|
70
|
+
height: number;
|
|
71
|
+
scale: number;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Make a target fit a container (cover mode)
|
|
75
|
+
*
|
|
76
|
+
* @param {FitInput} target Dimension of the target
|
|
77
|
+
* @param {FitInput} container Dimension of the container
|
|
78
|
+
* @returns {FitOutput}
|
|
79
|
+
*/
|
|
80
|
+
export declare function cover(target: FitInput, container: FitInput): FitOutput;
|
|
81
|
+
/**
|
|
82
|
+
* Make a target fit a container (contain mode)
|
|
83
|
+
*
|
|
84
|
+
* @param {FitInput} target Dimension of the target
|
|
85
|
+
* @param {FitInput} container Dimension of the container
|
|
86
|
+
* @returns {FitOutput}
|
|
87
|
+
*/
|
|
88
|
+
export declare function contain(target: FitInput, container: FitInput): FitOutput;
|
|
89
|
+
export {};
|
package/lib/geometry.js
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
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");
|
|
5
|
+
/**
|
|
6
|
+
* Convert a radians value into degrees
|
|
7
|
+
*
|
|
8
|
+
* @param {number} radians Angle in radians
|
|
9
|
+
* @returns {number} Angle in degrees
|
|
10
|
+
*/
|
|
11
|
+
function toDegrees(radians) {
|
|
12
|
+
return (radians * 180) / constants_1.PI;
|
|
13
|
+
}
|
|
14
|
+
exports.toDegrees = toDegrees;
|
|
15
|
+
/**
|
|
16
|
+
* Convert a degrees value into radians
|
|
17
|
+
*
|
|
18
|
+
* @param {number} degrees Angle in degrees
|
|
19
|
+
* @returns {number} Angle in radians
|
|
20
|
+
*/
|
|
21
|
+
function toRadians(degrees) {
|
|
22
|
+
return (degrees * constants_1.PI) / 180;
|
|
23
|
+
}
|
|
24
|
+
exports.toRadians = toRadians;
|
|
25
|
+
/**
|
|
26
|
+
* Calculate the angle from a point to another
|
|
27
|
+
*
|
|
28
|
+
* @param {number} x1 X value of the first point
|
|
29
|
+
* @param {number} y1 Y value of the first point
|
|
30
|
+
* @param {number} x2 X value of the second point
|
|
31
|
+
* @param {number} y2 Y value of the second point
|
|
32
|
+
* @returns {number} Angle
|
|
33
|
+
*/
|
|
34
|
+
function angle(x1, y1, x2, y2) {
|
|
35
|
+
return Math.atan2(y2 - y1, x2 - x1);
|
|
36
|
+
}
|
|
37
|
+
exports.angle = angle;
|
|
38
|
+
/**
|
|
39
|
+
* Find the closest angle betwween to angles
|
|
40
|
+
*
|
|
41
|
+
* @param {number} source Source angle in radians
|
|
42
|
+
* @param {number} target Target angle in radians
|
|
43
|
+
* @returns {number} Closest angle
|
|
44
|
+
*/
|
|
45
|
+
function closestAngle(source, target) {
|
|
46
|
+
var delta = target - source;
|
|
47
|
+
return delta > constants_1.PI ? target - 2 * constants_1.PI : target < -constants_1.PI ? delta + 2 * constants_1.PI : target;
|
|
48
|
+
}
|
|
49
|
+
exports.closestAngle = closestAngle;
|
|
50
|
+
/**
|
|
51
|
+
* Calculate the distance between two points
|
|
52
|
+
*
|
|
53
|
+
* @param {number} x1 X coord of the first point
|
|
54
|
+
* @param {number} y1 Y coord of the first point
|
|
55
|
+
* @param {number} x2 X coord of the second point
|
|
56
|
+
* @param {number} y2 Y coord of the second point
|
|
57
|
+
* @returns {number} Computed distance
|
|
58
|
+
*/
|
|
59
|
+
function distance(x1, y1, x2, y2) {
|
|
60
|
+
var dx = x1 - x2;
|
|
61
|
+
var dy = y1 - y2;
|
|
62
|
+
return Math.sqrt(dx * dx + dy * dy);
|
|
63
|
+
}
|
|
64
|
+
exports.distance = distance;
|
|
65
|
+
/**
|
|
66
|
+
* Calculate the length of the diagonal of a rectangle
|
|
67
|
+
*
|
|
68
|
+
* @param {number} width Width of the rectangle
|
|
69
|
+
* @param {number} height Height of the rectangle
|
|
70
|
+
* @returns {number} Diagonal length
|
|
71
|
+
*/
|
|
72
|
+
function diagonal(width, height) {
|
|
73
|
+
return Math.sqrt(width * width + height * height);
|
|
74
|
+
}
|
|
75
|
+
exports.diagonal = diagonal;
|
|
76
|
+
/**
|
|
77
|
+
* Convert radians to a 3D point on the surface of a unit sphere
|
|
78
|
+
*
|
|
79
|
+
* @param {number} radius Radius of the sphere
|
|
80
|
+
* @param {number} phi Polar angle from the y (up) axis : 0 to PI
|
|
81
|
+
* @param {number} theta Equator angle around the y (up) axis : 0 to 2 PI
|
|
82
|
+
* @param {Vector3} target Target vector
|
|
83
|
+
* @returns {Vector3}
|
|
84
|
+
*/
|
|
85
|
+
function radToSphere(radius, phi, theta, target) {
|
|
86
|
+
if (target === void 0) { target = { x: 0, y: 0, z: 0 }; }
|
|
87
|
+
target.x = radius * Math.sin(phi) * Math.sin(theta);
|
|
88
|
+
target.y = radius * Math.cos(phi);
|
|
89
|
+
target.z = radius * Math.sin(phi) * Math.cos(theta);
|
|
90
|
+
return target;
|
|
91
|
+
}
|
|
92
|
+
exports.radToSphere = radToSphere;
|
|
93
|
+
/**
|
|
94
|
+
* Make a target fit a container
|
|
95
|
+
*
|
|
96
|
+
* @param {FitInput} target Dimension of the target
|
|
97
|
+
* @param {FitInput} container Dimension of the container
|
|
98
|
+
* @param {string} mode Can be 'contain' | 'cover'
|
|
99
|
+
* @returns {FitOutput}
|
|
100
|
+
*/
|
|
101
|
+
function fit(target, container, mode) {
|
|
102
|
+
var ratioWidth = container.width / target.width;
|
|
103
|
+
var ratioHeight = container.height / target.height;
|
|
104
|
+
var scale;
|
|
105
|
+
if (mode === 'contain') {
|
|
106
|
+
scale = ratioWidth < ratioHeight ? ratioWidth : ratioHeight;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
scale = ratioWidth > ratioHeight ? ratioWidth : ratioHeight;
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
left: (container.width - target.width * scale) >> 1,
|
|
113
|
+
top: (container.height - target.height * scale) >> 1,
|
|
114
|
+
width: target.width * scale,
|
|
115
|
+
height: target.height * scale,
|
|
116
|
+
scale: scale
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Make a target fit a container (cover mode)
|
|
121
|
+
*
|
|
122
|
+
* @param {FitInput} target Dimension of the target
|
|
123
|
+
* @param {FitInput} container Dimension of the container
|
|
124
|
+
* @returns {FitOutput}
|
|
125
|
+
*/
|
|
126
|
+
function cover(target, container) {
|
|
127
|
+
return fit(target, container, 'cover');
|
|
128
|
+
}
|
|
129
|
+
exports.cover = cover;
|
|
130
|
+
/**
|
|
131
|
+
* Make a target fit a container (contain mode)
|
|
132
|
+
*
|
|
133
|
+
* @param {FitInput} target Dimension of the target
|
|
134
|
+
* @param {FitInput} container Dimension of the container
|
|
135
|
+
* @returns {FitOutput}
|
|
136
|
+
*/
|
|
137
|
+
function contain(target, container) {
|
|
138
|
+
return fit(target, container, 'contain');
|
|
139
|
+
}
|
|
140
|
+
exports.contain = contain;
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './colors';
|
|
2
|
+
export * from './dom';
|
|
3
|
+
export * from './files';
|
|
4
|
+
export * from './functions';
|
|
5
|
+
export * from './geometry';
|
|
6
|
+
export * from './maths';
|
|
7
|
+
export * from './now';
|
|
8
|
+
export * from './random';
|
|
9
|
+
export * from './strings';
|
|
10
|
+
export * from './constants';
|
|
11
|
+
export * from './types';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./colors"), exports);
|
|
18
|
+
__exportStar(require("./dom"), exports);
|
|
19
|
+
__exportStar(require("./files"), exports);
|
|
20
|
+
__exportStar(require("./functions"), exports);
|
|
21
|
+
__exportStar(require("./geometry"), exports);
|
|
22
|
+
__exportStar(require("./maths"), exports);
|
|
23
|
+
__exportStar(require("./now"), exports);
|
|
24
|
+
__exportStar(require("./random"), exports);
|
|
25
|
+
__exportStar(require("./strings"), exports);
|
|
26
|
+
__exportStar(require("./constants"), exports);
|
|
27
|
+
__exportStar(require("./types"), exports);
|