toosoon-utils 3.0.4 → 4.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/README.md +196 -79
- package/lib/classes/_pool.js +4 -7
- package/lib/classes/color-scale.d.ts +1 -1
- package/lib/classes/color-scale.js +37 -40
- package/lib/classes/frame-rate.d.ts +4 -4
- package/lib/classes/frame-rate.js +11 -14
- package/lib/colors.d.ts +2 -2
- package/lib/colors.js +36 -58
- package/lib/constants.js +6 -9
- package/lib/dom.d.ts +3 -3
- package/lib/dom.js +6 -12
- package/lib/files.js +2 -7
- package/lib/functions.js +14 -18
- package/lib/geometry.d.ts +1 -1
- package/lib/geometry.js +13 -25
- package/lib/maths.js +19 -41
- package/lib/prng.js +16 -35
- package/lib/query.d.ts +21 -0
- package/lib/query.js +33 -0
- package/lib/random.d.ts +1 -1
- package/lib/random.js +15 -31
- package/lib/strings.d.ts +59 -3
- package/lib/strings.js +117 -10
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types.js +1 -2
- package/package.json +4 -1
- package/tsconfig.json +2 -0
package/lib/colors.js
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var constants_1 = require("./constants");
|
|
5
|
-
var geometry_1 = require("./geometry");
|
|
6
|
-
var maths_1 = require("./maths");
|
|
1
|
+
import { W3CX11 } from './constants';
|
|
2
|
+
import { toDegrees, toRadians } from './geometry';
|
|
3
|
+
import { clamp } from './maths';
|
|
7
4
|
/**
|
|
8
5
|
* Normalize a color representation into RGB
|
|
9
6
|
*
|
|
10
7
|
* @param {ColorRepresentation} color Color representation
|
|
11
8
|
* @returns {[number,number,number]} Normalized RGB color
|
|
12
9
|
*/
|
|
13
|
-
function normalizeColor(color) {
|
|
10
|
+
export function normalizeColor(color) {
|
|
14
11
|
var _a;
|
|
15
12
|
if (typeof color === 'string') {
|
|
16
|
-
return hexToRgb((_a =
|
|
13
|
+
return hexToRgb((_a = W3CX11[color]) !== null && _a !== void 0 ? _a : color);
|
|
17
14
|
}
|
|
18
15
|
else if (typeof color === 'number') {
|
|
19
16
|
return hexToRgb(color);
|
|
@@ -22,7 +19,6 @@ function normalizeColor(color) {
|
|
|
22
19
|
return color;
|
|
23
20
|
}
|
|
24
21
|
}
|
|
25
|
-
exports.normalizeColor = normalizeColor;
|
|
26
22
|
// ******************************************
|
|
27
23
|
// RGB & Hexadecimal color spaces
|
|
28
24
|
// ******************************************
|
|
@@ -32,7 +28,7 @@ exports.normalizeColor = normalizeColor;
|
|
|
32
28
|
* @param {string} hex Hexadecimal string
|
|
33
29
|
* @returns {string} Normalized hexadecimal string
|
|
34
30
|
*/
|
|
35
|
-
function normalizeHexString(hex) {
|
|
31
|
+
export function normalizeHexString(hex) {
|
|
36
32
|
var match;
|
|
37
33
|
var result = '000000';
|
|
38
34
|
hex = hex.toLocaleLowerCase();
|
|
@@ -50,7 +46,6 @@ function normalizeHexString(hex) {
|
|
|
50
46
|
}
|
|
51
47
|
return "#".concat(result);
|
|
52
48
|
}
|
|
53
|
-
exports.normalizeHexString = normalizeHexString;
|
|
54
49
|
/**
|
|
55
50
|
* Convert RGB to hexadecimal
|
|
56
51
|
* Note: rgb values are contained in the interval [0, 1]
|
|
@@ -58,11 +53,10 @@ exports.normalizeHexString = normalizeHexString;
|
|
|
58
53
|
* @param {[number, number, number]} rgb RGB color
|
|
59
54
|
* @returns {number} Hexadecimal color
|
|
60
55
|
*/
|
|
61
|
-
function rgbToHex(_a) {
|
|
56
|
+
export function rgbToHex(_a) {
|
|
62
57
|
var r = _a[0], g = _a[1], b = _a[2];
|
|
63
58
|
return ((r * 255) << 16) ^ ((g * 255) << 8) ^ ((b * 255) << 0);
|
|
64
59
|
}
|
|
65
|
-
exports.rgbToHex = rgbToHex;
|
|
66
60
|
/**
|
|
67
61
|
* Convert RGB to hexadecimal string
|
|
68
62
|
* Note: rgb values are contained in the interval [0, 1]
|
|
@@ -70,23 +64,22 @@ exports.rgbToHex = rgbToHex;
|
|
|
70
64
|
* @param {[number, number, number]} rgb RGB color
|
|
71
65
|
* @returns {string} Hexadecimal string
|
|
72
66
|
*/
|
|
73
|
-
function rgbToHexString(_a) {
|
|
67
|
+
export function rgbToHexString(_a) {
|
|
74
68
|
var r = _a[0], g = _a[1], b = _a[2];
|
|
75
|
-
r =
|
|
76
|
-
g =
|
|
77
|
-
b =
|
|
69
|
+
r = clamp(Math.round(r * 255), 0, 255);
|
|
70
|
+
g = clamp(Math.round(g * 255), 0, 255);
|
|
71
|
+
b = clamp(Math.round(b * 255), 0, 255);
|
|
78
72
|
var result = (b | (g << 8) | (r << 16) | (1 << 24)).toString(16).slice(1);
|
|
79
73
|
return "#".concat(result);
|
|
80
74
|
}
|
|
81
|
-
exports.rgbToHexString = rgbToHexString;
|
|
82
75
|
/**
|
|
83
76
|
* Convert hexadecimal to RGB
|
|
84
77
|
* Note: rgb values are contained in the interval [0, 1]
|
|
85
78
|
*
|
|
86
|
-
* @param {
|
|
79
|
+
* @param {number|string} hex Hexadecimal color
|
|
87
80
|
* @returns {[number, number, number]} RGB color
|
|
88
81
|
*/
|
|
89
|
-
function hexToRgb(hex) {
|
|
82
|
+
export function hexToRgb(hex) {
|
|
90
83
|
if (typeof hex === 'number') {
|
|
91
84
|
hex = Math.floor(hex);
|
|
92
85
|
}
|
|
@@ -99,7 +92,6 @@ function hexToRgb(hex) {
|
|
|
99
92
|
var b = (hex & 255) / 255;
|
|
100
93
|
return [r, g, b];
|
|
101
94
|
}
|
|
102
|
-
exports.hexToRgb = hexToRgb;
|
|
103
95
|
/**
|
|
104
96
|
* Lighten a color
|
|
105
97
|
*
|
|
@@ -107,7 +99,7 @@ exports.hexToRgb = hexToRgb;
|
|
|
107
99
|
* @param {number} [amount=0] Amount of the color offset
|
|
108
100
|
* @returns {string} Computed hexadecimal
|
|
109
101
|
*/
|
|
110
|
-
function lighten(hex, amount) {
|
|
102
|
+
export function lighten(hex, amount) {
|
|
111
103
|
if (amount === void 0) { amount = 0; }
|
|
112
104
|
var prefix = '';
|
|
113
105
|
if (hex[0] === '#') {
|
|
@@ -115,16 +107,15 @@ function lighten(hex, amount) {
|
|
|
115
107
|
prefix = '#';
|
|
116
108
|
}
|
|
117
109
|
var value = parseInt(hex, 16);
|
|
118
|
-
var r =
|
|
119
|
-
var b =
|
|
120
|
-
var g =
|
|
110
|
+
var r = clamp((value >> 16) + amount, 0, 255);
|
|
111
|
+
var b = clamp(((value >> 8) & 0x00ff) + amount, 0, 255);
|
|
112
|
+
var g = clamp((value & 0x0000ff) + amount, 0, 255);
|
|
121
113
|
var result = g | (b << 8) | (r << 16);
|
|
122
114
|
if (r === 0 && g === 0 && b === 0 && amount !== 0) {
|
|
123
115
|
result = '000000';
|
|
124
116
|
}
|
|
125
117
|
return prefix + result.toString(16);
|
|
126
118
|
}
|
|
127
|
-
exports.lighten = lighten;
|
|
128
119
|
/**
|
|
129
120
|
* Darken a color
|
|
130
121
|
*
|
|
@@ -132,11 +123,10 @@ exports.lighten = lighten;
|
|
|
132
123
|
* @param {number} [amount=0] Amount of the color offset
|
|
133
124
|
* @returns {string} Computed hexadecimal
|
|
134
125
|
*/
|
|
135
|
-
function darken(hex, amount) {
|
|
126
|
+
export function darken(hex, amount) {
|
|
136
127
|
if (amount === void 0) { amount = 0; }
|
|
137
128
|
return lighten(hex, -amount);
|
|
138
129
|
}
|
|
139
|
-
exports.darken = darken;
|
|
140
130
|
// ***************************************************
|
|
141
131
|
// RGB & Hue-Saturation-Lightness (HSL) color spaces
|
|
142
132
|
// ***************************************************
|
|
@@ -147,12 +137,11 @@ exports.darken = darken;
|
|
|
147
137
|
* @param {string} hsl HSL string (format: 'hsl(360, 100%, 100%)')
|
|
148
138
|
* @returns {[number, number, number]} Normalized HSL color
|
|
149
139
|
*/
|
|
150
|
-
function normalizeHslString(hsl) {
|
|
140
|
+
export function normalizeHslString(hsl) {
|
|
151
141
|
var _a, _b;
|
|
152
142
|
var _c = (_b = (_a = hsl.match(/\d+/g)) === null || _a === void 0 ? void 0 : _a.map(Number)) !== null && _b !== void 0 ? _b : [0, 0, 0], h = _c[0], s = _c[1], l = _c[2];
|
|
153
143
|
return [h, s / 100, l / 100];
|
|
154
144
|
}
|
|
155
|
-
exports.normalizeHslString = normalizeHslString;
|
|
156
145
|
/**
|
|
157
146
|
* Convert RGB to HSL
|
|
158
147
|
* Notes:
|
|
@@ -162,7 +151,7 @@ exports.normalizeHslString = normalizeHslString;
|
|
|
162
151
|
* @param {[number, number, number]} rgb RGB color
|
|
163
152
|
* @returns {[number, number, number]} HSL color
|
|
164
153
|
*/
|
|
165
|
-
function rgbToHsl(_a) {
|
|
154
|
+
export function rgbToHsl(_a) {
|
|
166
155
|
var r = _a[0], g = _a[1], b = _a[2];
|
|
167
156
|
var l = Math.max(r, g, b);
|
|
168
157
|
var s = l - Math.min(r, g, b);
|
|
@@ -173,7 +162,6 @@ function rgbToHsl(_a) {
|
|
|
173
162
|
(2 * l - s) / 2
|
|
174
163
|
];
|
|
175
164
|
}
|
|
176
|
-
exports.rgbToHsl = rgbToHsl;
|
|
177
165
|
/**
|
|
178
166
|
* Convert HSL to RGB
|
|
179
167
|
* Notes:
|
|
@@ -183,14 +171,13 @@ exports.rgbToHsl = rgbToHsl;
|
|
|
183
171
|
* @param {[number, number, number]} hsl HSL color
|
|
184
172
|
* @returns {[number, number, number]} RGB color
|
|
185
173
|
*/
|
|
186
|
-
function hslToRgb(_a) {
|
|
174
|
+
export function hslToRgb(_a) {
|
|
187
175
|
var h = _a[0], s = _a[1], l = _a[2];
|
|
188
176
|
var a = s * Math.min(l, 1 - l);
|
|
189
177
|
var k = function (v) { return (v + h / 30) % 12; };
|
|
190
178
|
var f = function (v) { return l - a * Math.max(-1, Math.min(k(v) - 3, Math.min(9 - k(v), 1))); };
|
|
191
179
|
return [f(0), f(8), f(4)];
|
|
192
180
|
}
|
|
193
|
-
exports.hslToRgb = hslToRgb;
|
|
194
181
|
// ***************************************************
|
|
195
182
|
// RGB & Hue-Saturation-Brightness (HSB) color spaces
|
|
196
183
|
// ***************************************************
|
|
@@ -203,7 +190,7 @@ exports.hslToRgb = hslToRgb;
|
|
|
203
190
|
* @param {[number, number, number]} rgb RGB color
|
|
204
191
|
* @returns {[number, number, number]} HSB color
|
|
205
192
|
*/
|
|
206
|
-
function rgbToHsb(_a) {
|
|
193
|
+
export function rgbToHsb(_a) {
|
|
207
194
|
var r = _a[0], g = _a[1], b = _a[2];
|
|
208
195
|
var max = Math.max(r, g, b);
|
|
209
196
|
var min = Math.min(r, g, b);
|
|
@@ -211,7 +198,6 @@ function rgbToHsb(_a) {
|
|
|
211
198
|
var h = delta === 0 ? 0 : delta && max === r ? (g - b) / delta : max === g ? 2 + (b - r) / delta : 4 + (r - g) / delta;
|
|
212
199
|
return [60 * (h < 0 ? h + 6 : h), max && delta / max, max];
|
|
213
200
|
}
|
|
214
|
-
exports.rgbToHsb = rgbToHsb;
|
|
215
201
|
/**
|
|
216
202
|
* Convert HSB to RGB
|
|
217
203
|
* Notes:
|
|
@@ -221,13 +207,12 @@ exports.rgbToHsb = rgbToHsb;
|
|
|
221
207
|
* @param {[number, number, number]} hsb HSB color
|
|
222
208
|
* @returns {[number, number, number]} RGB color
|
|
223
209
|
*/
|
|
224
|
-
function hsbToRgb(_a) {
|
|
210
|
+
export function hsbToRgb(_a) {
|
|
225
211
|
var h = _a[0], s = _a[1], b = _a[2];
|
|
226
212
|
var k = function (v) { return (v + h / 60) % 6; };
|
|
227
213
|
var f = function (v) { return b * (1 - s * Math.max(0, Math.min(k(v), 4 - k(v), 1))); };
|
|
228
214
|
return [f(5), f(3), f(1)];
|
|
229
215
|
}
|
|
230
|
-
exports.hsbToRgb = hsbToRgb;
|
|
231
216
|
// *********************************************
|
|
232
217
|
// LAB & Hue-Chroma-Luminance (HCL) color spaces
|
|
233
218
|
// *********************************************
|
|
@@ -238,13 +223,12 @@ exports.hsbToRgb = hsbToRgb;
|
|
|
238
223
|
* @param {[number, number, number]} lab LAB color
|
|
239
224
|
* @returns {[number, number, number]} HCL color
|
|
240
225
|
*/
|
|
241
|
-
function labToHcl(_a) {
|
|
226
|
+
export function labToHcl(_a) {
|
|
242
227
|
var l = _a[0], a = _a[1], b = _a[2];
|
|
243
228
|
var c = Math.sqrt(a * a + b * b);
|
|
244
229
|
var h = abToHue(a, b);
|
|
245
230
|
return [h, c, l];
|
|
246
231
|
}
|
|
247
|
-
exports.labToHcl = labToHcl;
|
|
248
232
|
/**
|
|
249
233
|
* Convert HCL to LAB
|
|
250
234
|
* -> http://www.brucelindbloom.com/index.html?Eqn_LCH_to_Lab.html
|
|
@@ -252,13 +236,12 @@ exports.labToHcl = labToHcl;
|
|
|
252
236
|
* @param {[number, number, number]} hcl HCL color
|
|
253
237
|
* @returns {[number, number, number]} LAB color space
|
|
254
238
|
*/
|
|
255
|
-
function hclToLab(_a) {
|
|
239
|
+
export function hclToLab(_a) {
|
|
256
240
|
var h = _a[0], c = _a[1], l = _a[2];
|
|
257
|
-
var a = c * Math.cos(
|
|
258
|
-
var b = c * Math.sin(
|
|
241
|
+
var a = c * Math.cos(toRadians(h));
|
|
242
|
+
var b = c * Math.sin(toRadians(h));
|
|
259
243
|
return [l, a, b];
|
|
260
244
|
}
|
|
261
|
-
exports.hclToLab = hclToLab;
|
|
262
245
|
/**
|
|
263
246
|
* Convert A and B of LAB to Hue of LCH
|
|
264
247
|
* -> https://stackoverflow.com/questions/53733379/conversion-of-cielab-to-cielchab-not-yielding-correct-result
|
|
@@ -290,7 +273,7 @@ function abToHue(a, b) {
|
|
|
290
273
|
else if (a > 0 && b < 0) {
|
|
291
274
|
xBias = 360;
|
|
292
275
|
}
|
|
293
|
-
return
|
|
276
|
+
return toDegrees(Math.atan(b / a)) + xBias;
|
|
294
277
|
}
|
|
295
278
|
// ******************************************
|
|
296
279
|
// LAB & RGB color spaces
|
|
@@ -305,7 +288,7 @@ var f4 = function (v) { return (v > 0.008856 ? Math.pow(v, 1 / 3) : 7.787 * v +
|
|
|
305
288
|
* @param {[number, number, number]} lab LAB color
|
|
306
289
|
* @returns {[number, number, number]} RGB color
|
|
307
290
|
*/
|
|
308
|
-
function labToRgb(_a) {
|
|
291
|
+
export function labToRgb(_a) {
|
|
309
292
|
var l = _a[0], a = _a[1], b = _a[2];
|
|
310
293
|
var y = (l + 16) / 116;
|
|
311
294
|
var x = a / 500 + y;
|
|
@@ -314,19 +297,18 @@ function labToRgb(_a) {
|
|
|
314
297
|
y = 1.0 * f1(y);
|
|
315
298
|
z = 1.08883 * f1(z);
|
|
316
299
|
return [
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
300
|
+
clamp(f2(x * 3.2406 + y * -1.5372 + z * -0.4986)),
|
|
301
|
+
clamp(f2(x * -0.9689 + y * 1.8758 + z * 0.0415)),
|
|
302
|
+
clamp(f2(x * 0.0557 + y * -0.204 + z * 1.057))
|
|
320
303
|
];
|
|
321
304
|
}
|
|
322
|
-
exports.labToRgb = labToRgb;
|
|
323
305
|
/**
|
|
324
306
|
* Converts RGB to LAB
|
|
325
307
|
*
|
|
326
308
|
* @param {[number, number, number]} rgb RGB color
|
|
327
309
|
* @returns {[number, number, number]} LAB color
|
|
328
310
|
*/
|
|
329
|
-
function rgbToLab(_a) {
|
|
311
|
+
export function rgbToLab(_a) {
|
|
330
312
|
var r = _a[0], g = _a[1], b = _a[2];
|
|
331
313
|
r = f3(r);
|
|
332
314
|
g = f3(g);
|
|
@@ -336,7 +318,6 @@ function rgbToLab(_a) {
|
|
|
336
318
|
var z = f4((r * 0.0193 + g * 0.1192 + b * 0.9505) / 1.08883);
|
|
337
319
|
return [116 * y - 16, 500 * (x - y), 200 * (y - z)];
|
|
338
320
|
}
|
|
339
|
-
exports.rgbToLab = rgbToLab;
|
|
340
321
|
/**
|
|
341
322
|
* Get the delta from two LAB colors
|
|
342
323
|
*
|
|
@@ -344,7 +325,7 @@ exports.rgbToLab = rgbToLab;
|
|
|
344
325
|
* @param {[number, number, number]} labB Second LAB color
|
|
345
326
|
* @returns {number} Delta
|
|
346
327
|
*/
|
|
347
|
-
function deltaE(labA, labB) {
|
|
328
|
+
export function deltaE(labA, labB) {
|
|
348
329
|
var deltaL = labA[0] - labB[0];
|
|
349
330
|
var deltaA = labA[1] - labB[1];
|
|
350
331
|
var deltaB = labA[2] - labB[2];
|
|
@@ -361,7 +342,6 @@ function deltaE(labA, labB) {
|
|
|
361
342
|
var i = deltaLKlsl * deltaLKlsl + deltaCkcsc * deltaCkcsc + deltaHkhsh * deltaHkhsh;
|
|
362
343
|
return i < 0 ? 0 : Math.sqrt(i);
|
|
363
344
|
}
|
|
364
|
-
exports.deltaE = deltaE;
|
|
365
345
|
// *********************************************
|
|
366
346
|
// RGB & Hue-Chroma-Luminance (HCL) color spaces
|
|
367
347
|
// *********************************************
|
|
@@ -371,19 +351,17 @@ exports.deltaE = deltaE;
|
|
|
371
351
|
* @param {[number, number, number]} rgb RGB color
|
|
372
352
|
* @returns {[number, number, number]} HCL color
|
|
373
353
|
*/
|
|
374
|
-
function rgbToHcl(_a) {
|
|
354
|
+
export function rgbToHcl(_a) {
|
|
375
355
|
var r = _a[0], g = _a[1], b = _a[2];
|
|
376
356
|
return labToHcl(rgbToLab([r, g, b]));
|
|
377
357
|
}
|
|
378
|
-
exports.rgbToHcl = rgbToHcl;
|
|
379
358
|
/**
|
|
380
359
|
* Converts HCL to RGB
|
|
381
360
|
*
|
|
382
361
|
* @param {[number, number, number]} hcl RGB color
|
|
383
362
|
* @returns {[number, number, number]} RGB color
|
|
384
363
|
*/
|
|
385
|
-
function hclToRgb(_a) {
|
|
364
|
+
export function hclToRgb(_a) {
|
|
386
365
|
var h = _a[0], c = _a[1], l = _a[2];
|
|
387
366
|
return labToRgb(hclToLab([h, c, l]));
|
|
388
367
|
}
|
|
389
|
-
exports.hclToRgb = hclToRgb;
|
package/lib/constants.js
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
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
1
|
// *********************
|
|
5
2
|
// Maths
|
|
6
3
|
// *********************
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
export var EPSILON = 1e-10;
|
|
5
|
+
export var PI = Math.PI;
|
|
6
|
+
export var TWO_PI = Math.PI * 2;
|
|
7
|
+
export var HALF_PI = Math.PI / 2;
|
|
8
|
+
export var QUARTER_PI = Math.PI / 4;
|
|
12
9
|
// *********************
|
|
13
10
|
// Colors
|
|
14
11
|
// *********************
|
|
15
12
|
// X11 colors
|
|
16
13
|
// -> https://www.w3.org/TR/css-color-3/#svg-color
|
|
17
|
-
|
|
14
|
+
export var W3CX11 = {
|
|
18
15
|
aliceblue: 0xf0f8ff,
|
|
19
16
|
antiquewhite: 0xfaebd7,
|
|
20
17
|
aqua: 0x00ffff,
|
package/lib/dom.d.ts
CHANGED
|
@@ -2,15 +2,15 @@
|
|
|
2
2
|
* Find the closest parent that matches a selector
|
|
3
3
|
*
|
|
4
4
|
* @param {Element|null} element Target element
|
|
5
|
-
* @param {
|
|
5
|
+
* @param {Element|string} selector Selector or parent to match
|
|
6
6
|
* @returns {Element|null}
|
|
7
7
|
*/
|
|
8
8
|
export declare function closest(element: Element | null, selector: Element | string): Element | null;
|
|
9
9
|
/**
|
|
10
10
|
* Create a canvas and 2d context
|
|
11
11
|
*
|
|
12
|
-
* @param {
|
|
13
|
-
* @param {
|
|
12
|
+
* @param {number} width Width of the canvas
|
|
13
|
+
* @param {number} height Height of the canvas
|
|
14
14
|
* @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D }}
|
|
15
15
|
*/
|
|
16
16
|
export declare function createCanvas(width: number, height: number): {
|
package/lib/dom.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.injectStyles = exports.createCanvas = exports.closest = void 0;
|
|
4
1
|
var DOCUMENT_NODE_TYPE = 9;
|
|
5
2
|
/**
|
|
6
3
|
* Find the closest parent that matches a selector
|
|
7
4
|
*
|
|
8
5
|
* @param {Element|null} element Target element
|
|
9
|
-
* @param {
|
|
6
|
+
* @param {Element|string} selector Selector or parent to match
|
|
10
7
|
* @returns {Element|null}
|
|
11
8
|
*/
|
|
12
|
-
function closest(element, selector) {
|
|
9
|
+
export function closest(element, selector) {
|
|
13
10
|
var current = element;
|
|
14
11
|
while (current && current.nodeType !== DOCUMENT_NODE_TYPE) {
|
|
15
12
|
if ((typeof selector === 'string' && current.matches(selector)) || current === selector) {
|
|
@@ -19,15 +16,14 @@ function closest(element, selector) {
|
|
|
19
16
|
}
|
|
20
17
|
return current;
|
|
21
18
|
}
|
|
22
|
-
exports.closest = closest;
|
|
23
19
|
/**
|
|
24
20
|
* Create a canvas and 2d context
|
|
25
21
|
*
|
|
26
|
-
* @param {
|
|
27
|
-
* @param {
|
|
22
|
+
* @param {number} width Width of the canvas
|
|
23
|
+
* @param {number} height Height of the canvas
|
|
28
24
|
* @returns {{ canvas: HTMLCanvasElement, ctx: CanvasRenderingContext2D }}
|
|
29
25
|
*/
|
|
30
|
-
function createCanvas(width, height) {
|
|
26
|
+
export function createCanvas(width, height) {
|
|
31
27
|
var _a;
|
|
32
28
|
var canvas = document.createElement('canvas');
|
|
33
29
|
canvas.width = width;
|
|
@@ -35,13 +31,12 @@ function createCanvas(width, height) {
|
|
|
35
31
|
var ctx = (_a = canvas.getContext('2d')) !== null && _a !== void 0 ? _a : new CanvasRenderingContext2D();
|
|
36
32
|
return { canvas: canvas, ctx: ctx };
|
|
37
33
|
}
|
|
38
|
-
exports.createCanvas = createCanvas;
|
|
39
34
|
/**
|
|
40
35
|
* Inject CSS styles in `document.head`
|
|
41
36
|
*
|
|
42
37
|
* @param {string} styles CSS styles to inject
|
|
43
38
|
*/
|
|
44
|
-
function injectStyles(styles) {
|
|
39
|
+
export function injectStyles(styles) {
|
|
45
40
|
var $style = document.createElement('style');
|
|
46
41
|
$style.innerHTML = styles;
|
|
47
42
|
var $before = document.querySelector('head link[rel=stylesheet], head style');
|
|
@@ -50,4 +45,3 @@ function injectStyles(styles) {
|
|
|
50
45
|
else
|
|
51
46
|
document.head.appendChild($style);
|
|
52
47
|
}
|
|
53
|
-
exports.injectStyles = injectStyles;
|
package/lib/files.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.upload = exports.download = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* Download a Blob object into user files
|
|
6
3
|
*
|
|
7
4
|
* @param {Blob} blob Blob object to download
|
|
8
5
|
* @param {string} filename Downloaded file name
|
|
9
6
|
*/
|
|
10
|
-
function download(blob, filename) {
|
|
7
|
+
export function download(blob, filename) {
|
|
11
8
|
var link = document.createElement('a');
|
|
12
9
|
link.setAttribute('href', URL.createObjectURL(blob));
|
|
13
10
|
link.setAttribute('download', filename);
|
|
@@ -15,14 +12,13 @@ function download(blob, filename) {
|
|
|
15
12
|
link.click();
|
|
16
13
|
document.body.removeChild(link);
|
|
17
14
|
}
|
|
18
|
-
exports.download = download;
|
|
19
15
|
/**
|
|
20
16
|
* Upload a file from user files
|
|
21
17
|
*
|
|
22
18
|
* @param {Function} onLoad Callback called once the file is loaded
|
|
23
19
|
* @param {string} [accept=''] MIME type the file input should accept
|
|
24
20
|
*/
|
|
25
|
-
function upload(onLoad, accept) {
|
|
21
|
+
export function upload(onLoad, accept) {
|
|
26
22
|
if (accept === void 0) { accept = ''; }
|
|
27
23
|
var input = document.createElement('input');
|
|
28
24
|
input.setAttribute('type', 'file');
|
|
@@ -40,4 +36,3 @@ function upload(onLoad, accept) {
|
|
|
40
36
|
input.click();
|
|
41
37
|
document.body.removeChild(input);
|
|
42
38
|
}
|
|
43
|
-
exports.upload = upload;
|
package/lib/functions.js
CHANGED
|
@@ -1,22 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.now = exports.defer = exports.throttle = exports.debounce = exports.wait = exports.noop = void 0;
|
|
4
1
|
/**
|
|
5
2
|
* No-op function
|
|
6
3
|
*/
|
|
7
|
-
var noop = function () { };
|
|
8
|
-
exports.noop = noop;
|
|
4
|
+
export var noop = function () { };
|
|
9
5
|
/**
|
|
10
6
|
* Promise wrapped setTimeout
|
|
11
7
|
*
|
|
12
8
|
* @param {number} [delay=0] Time to wait (in milliseconds)
|
|
13
9
|
* @returns {Promise}
|
|
14
10
|
*/
|
|
15
|
-
function wait(delay) {
|
|
11
|
+
export function wait(delay) {
|
|
16
12
|
if (delay === void 0) { delay = 0; }
|
|
17
13
|
return new Promise(function (resolve) { return setTimeout(resolve, delay); });
|
|
18
14
|
}
|
|
19
|
-
exports.wait = wait;
|
|
20
15
|
/**
|
|
21
16
|
* Create a debounced function that delays the execution of `callback` until a specified `delay` time has passed since the last call
|
|
22
17
|
*
|
|
@@ -24,7 +19,7 @@ exports.wait = wait;
|
|
|
24
19
|
* @param {number} delay Delay (in milliseconds)
|
|
25
20
|
* @returns {Function} Debounced function
|
|
26
21
|
*/
|
|
27
|
-
function debounce(callback, delay) {
|
|
22
|
+
export function debounce(callback, delay) {
|
|
28
23
|
var timeout = null;
|
|
29
24
|
return function () {
|
|
30
25
|
var args = [];
|
|
@@ -36,7 +31,6 @@ function debounce(callback, delay) {
|
|
|
36
31
|
timeout = setTimeout(function () { return callback.apply(void 0, args); }, delay);
|
|
37
32
|
};
|
|
38
33
|
}
|
|
39
|
-
exports.debounce = debounce;
|
|
40
34
|
/**
|
|
41
35
|
* Create a throttled function that limits the execution of `callback` to once every `limit` time
|
|
42
36
|
*
|
|
@@ -44,27 +38,26 @@ exports.debounce = debounce;
|
|
|
44
38
|
* @param {number} limit Minimum interval between two calls (in milliseconds)
|
|
45
39
|
* @returns {Function} Throttled function
|
|
46
40
|
*/
|
|
47
|
-
function throttle(callback, limit) {
|
|
41
|
+
export function throttle(callback, limit) {
|
|
48
42
|
var lastTime = 0;
|
|
49
43
|
return function () {
|
|
50
44
|
var args = [];
|
|
51
45
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
52
46
|
args[_i] = arguments[_i];
|
|
53
47
|
}
|
|
54
|
-
var time =
|
|
48
|
+
var time = now();
|
|
55
49
|
if (time - lastTime >= limit) {
|
|
56
50
|
lastTime = time;
|
|
57
51
|
callback.apply(void 0, args);
|
|
58
52
|
}
|
|
59
53
|
};
|
|
60
54
|
}
|
|
61
|
-
exports.throttle = throttle;
|
|
62
55
|
/**
|
|
63
56
|
* Deferred promise implementation
|
|
64
57
|
*
|
|
65
58
|
* @returns {Deferred}
|
|
66
59
|
*/
|
|
67
|
-
function defer() {
|
|
60
|
+
export function defer() {
|
|
68
61
|
var resolve;
|
|
69
62
|
var reject;
|
|
70
63
|
var promise = new Promise(function (_resolve, _reject) {
|
|
@@ -73,10 +66,13 @@ function defer() {
|
|
|
73
66
|
});
|
|
74
67
|
return { promise: promise, resolve: resolve, reject: reject };
|
|
75
68
|
}
|
|
76
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Polyfill for `now()` functions
|
|
71
|
+
*/
|
|
72
|
+
export var now;
|
|
77
73
|
// In node.js, use `process.hrtime`
|
|
78
74
|
if (typeof process !== 'undefined' && process.hrtime) {
|
|
79
|
-
|
|
75
|
+
now = function () {
|
|
80
76
|
// Convert [seconds, nanoseconds] to milliseconds
|
|
81
77
|
var time = process.hrtime();
|
|
82
78
|
return time[0] * 1000 + time[1] / 1000000;
|
|
@@ -85,13 +81,13 @@ if (typeof process !== 'undefined' && process.hrtime) {
|
|
|
85
81
|
// In a browser use `performance` or `Date`
|
|
86
82
|
else if (typeof performance !== 'undefined') {
|
|
87
83
|
// This must be bound, because directly assigning this function leads to an invocation exception in Chrome
|
|
88
|
-
|
|
84
|
+
now = performance.now.bind(performance);
|
|
89
85
|
}
|
|
90
86
|
else if (typeof Date.now !== 'undefined') {
|
|
91
|
-
|
|
87
|
+
now = Date.now;
|
|
92
88
|
}
|
|
93
89
|
else {
|
|
94
|
-
|
|
90
|
+
now = function () {
|
|
95
91
|
return new Date().getTime();
|
|
96
92
|
};
|
|
97
93
|
}
|
package/lib/geometry.d.ts
CHANGED