litecanvas 0.83.0 → 0.83.2
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 +26 -20
- package/dist/dist.dev.js +113 -161
- package/dist/dist.js +72 -54
- package/dist/dist.min.js +1 -1
- package/package.json +6 -3
- package/src/index.js +105 -170
- package/types/index.d.ts +12 -43
- package/types/types.d.ts +7 -22
package/src/index.js
CHANGED
|
@@ -20,9 +20,7 @@ export default function litecanvas(settings = {}) {
|
|
|
20
20
|
/** @type {(elem:HTMLElement, evt:string, callback:(event:Event)=>void)=>void} */
|
|
21
21
|
on = (elem, evt, callback) => {
|
|
22
22
|
elem.addEventListener(evt, callback, false)
|
|
23
|
-
_browserEventListeners.push(() =>
|
|
24
|
-
elem.removeEventListener(evt, callback, false)
|
|
25
|
-
)
|
|
23
|
+
_browserEventListeners.push(() => elem.removeEventListener(evt, callback, false))
|
|
26
24
|
},
|
|
27
25
|
isNumber = Number.isFinite,
|
|
28
26
|
/** @type {LitecanvasOptions} */
|
|
@@ -135,7 +133,7 @@ export default function litecanvas(settings = {}) {
|
|
|
135
133
|
*
|
|
136
134
|
* @type {number}
|
|
137
135
|
*/
|
|
138
|
-
HALF_PI:
|
|
136
|
+
HALF_PI: TWO_PI / 4,
|
|
139
137
|
|
|
140
138
|
/**
|
|
141
139
|
* Calculates a linear (interpolation) value over t%.
|
|
@@ -212,10 +210,7 @@ export default function litecanvas(settings = {}) {
|
|
|
212
210
|
DEV: assert(isNumber(value), 'clamp: 1st param must be a number')
|
|
213
211
|
DEV: assert(isNumber(min), 'clamp: 2nd param must be a number')
|
|
214
212
|
DEV: assert(isNumber(max), 'clamp: 3rd param must be a number')
|
|
215
|
-
DEV: assert(
|
|
216
|
-
max > min,
|
|
217
|
-
'randi: the 2nd param must be less than the 3rd param'
|
|
218
|
-
)
|
|
213
|
+
DEV: assert(max > min, 'clamp: the 2nd param must be less than the 3rd param')
|
|
219
214
|
|
|
220
215
|
if (value < min) return min
|
|
221
216
|
if (value > max) return max
|
|
@@ -234,14 +229,7 @@ export default function litecanvas(settings = {}) {
|
|
|
234
229
|
DEV: assert(isNumber(value), 'wrap: 1st param must be a number')
|
|
235
230
|
DEV: assert(isNumber(min), 'wrap: 2nd param must be a number')
|
|
236
231
|
DEV: assert(isNumber(max), 'wrap: 3rd param must be a number')
|
|
237
|
-
DEV: assert(
|
|
238
|
-
max > min,
|
|
239
|
-
'randi: the 2nd param must be less than the 3rd param'
|
|
240
|
-
)
|
|
241
|
-
DEV: assert(
|
|
242
|
-
max !== min,
|
|
243
|
-
'randi: the 2nd param must be not equal to the 3rd param'
|
|
244
|
-
)
|
|
232
|
+
DEV: assert(max > min, 'wrap: the 2nd param must be less than the 3rd param')
|
|
245
233
|
|
|
246
234
|
return value - (max - min) * math.floor((value - min) / (max - min))
|
|
247
235
|
},
|
|
@@ -263,6 +251,7 @@ export default function litecanvas(settings = {}) {
|
|
|
263
251
|
DEV: assert(isNumber(stop1), 'map: 3rd param must be a number')
|
|
264
252
|
DEV: assert(isNumber(start2), 'map: 4th param must be a number')
|
|
265
253
|
DEV: assert(isNumber(stop2), 'map: 5th param must be a number')
|
|
254
|
+
DEV: assert(max !== min, 'map: the 3rd param must be different than the 2nd param')
|
|
266
255
|
|
|
267
256
|
// prettier-ignore
|
|
268
257
|
const result = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
|
|
@@ -292,11 +281,19 @@ export default function litecanvas(settings = {}) {
|
|
|
292
281
|
*
|
|
293
282
|
* @param {number} from - the lower bound
|
|
294
283
|
* @param {number} to - the higher bound
|
|
295
|
-
* @param {number} t - the
|
|
296
|
-
* @param {(n: number) => number} fn - the periodic function (which default to `Math.sin`)
|
|
284
|
+
* @param {number} t - value passed to the periodic function
|
|
285
|
+
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
|
|
297
286
|
*/
|
|
298
|
-
wave: (from, to, t, fn = Math.sin) =>
|
|
299
|
-
|
|
287
|
+
wave: (from, to, t, fn = Math.sin) => {
|
|
288
|
+
DEV: assert(isNumber(from), 'wave: 1st param must be a number')
|
|
289
|
+
DEV: assert(isNumber(to), 'wave: 2nd param must be a number')
|
|
290
|
+
DEV: assert(isNumber(t), 'wave: 3rd param must be a number')
|
|
291
|
+
DEV: assert(
|
|
292
|
+
'function' === typeof fn,
|
|
293
|
+
'wave: 4rd param must be a function (n: number) => number'
|
|
294
|
+
)
|
|
295
|
+
return from + ((fn(t) + 1) / 2) * (to - from)
|
|
296
|
+
},
|
|
300
297
|
|
|
301
298
|
/** RNG API */
|
|
302
299
|
/**
|
|
@@ -310,10 +307,7 @@ export default function litecanvas(settings = {}) {
|
|
|
310
307
|
rand: (min = 0.0, max = 1.0) => {
|
|
311
308
|
DEV: assert(isNumber(min), 'rand: 1st param must be a number')
|
|
312
309
|
DEV: assert(isNumber(max), 'rand: 2nd param must be a number')
|
|
313
|
-
DEV: assert(
|
|
314
|
-
max > min,
|
|
315
|
-
'rand: the 1st param must be less than the 2nd param'
|
|
316
|
-
)
|
|
310
|
+
DEV: assert(max > min, 'rand: the 1st param must be less than the 2nd param')
|
|
317
311
|
|
|
318
312
|
const a = 1664525
|
|
319
313
|
const c = 1013904223
|
|
@@ -334,10 +328,7 @@ export default function litecanvas(settings = {}) {
|
|
|
334
328
|
randi: (min = 0, max = 1) => {
|
|
335
329
|
DEV: assert(isNumber(min), 'randi: 1st param must be a number')
|
|
336
330
|
DEV: assert(isNumber(max), 'randi: 2nd param must be a number')
|
|
337
|
-
DEV: assert(
|
|
338
|
-
max > min,
|
|
339
|
-
'randi: the 1st param must be less than the 2nd param'
|
|
340
|
-
)
|
|
331
|
+
DEV: assert(max > min, 'randi: the 1st param must be less than the 2nd param')
|
|
341
332
|
|
|
342
333
|
return math.floor(instance.rand(min, max + 1))
|
|
343
334
|
},
|
|
@@ -373,13 +364,7 @@ export default function litecanvas(settings = {}) {
|
|
|
373
364
|
if (null == color) {
|
|
374
365
|
_ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height)
|
|
375
366
|
} else {
|
|
376
|
-
instance.rectfill(
|
|
377
|
-
0,
|
|
378
|
-
0,
|
|
379
|
-
_ctx.canvas.width,
|
|
380
|
-
_ctx.canvas.height,
|
|
381
|
-
color
|
|
382
|
-
)
|
|
367
|
+
instance.rectfill(0, 0, _ctx.canvas.width, _ctx.canvas.height, color)
|
|
383
368
|
}
|
|
384
369
|
},
|
|
385
370
|
|
|
@@ -392,14 +377,13 @@ export default function litecanvas(settings = {}) {
|
|
|
392
377
|
* @param {number} height
|
|
393
378
|
* @param {number} [color=0] the color index
|
|
394
379
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
380
|
+
*
|
|
381
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
395
382
|
*/
|
|
396
383
|
rect(x, y, width, height, color, radii) {
|
|
397
384
|
DEV: assert(isNumber(x), 'rect: 1st param must be a number')
|
|
398
385
|
DEV: assert(isNumber(y), 'rect: 2nd param must be a number')
|
|
399
|
-
DEV: assert(
|
|
400
|
-
isNumber(width) && width > 0,
|
|
401
|
-
'rect: 3rd param must be a positive number'
|
|
402
|
-
)
|
|
386
|
+
DEV: assert(isNumber(width) && width > 0, 'rect: 3rd param must be a positive number')
|
|
403
387
|
DEV: assert(
|
|
404
388
|
isNumber(height) && height >= 0,
|
|
405
389
|
'rect: 4th param must be a positive number or zero'
|
|
@@ -409,9 +393,7 @@ export default function litecanvas(settings = {}) {
|
|
|
409
393
|
'rect: 5th param must be a positive number or zero'
|
|
410
394
|
)
|
|
411
395
|
DEV: assert(
|
|
412
|
-
null == radii ||
|
|
413
|
-
isNumber(radii) ||
|
|
414
|
-
(Array.isArray(radii) && radii.length >= 1),
|
|
396
|
+
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
415
397
|
'rect: 6th param must be a number or array of numbers'
|
|
416
398
|
)
|
|
417
399
|
|
|
@@ -452,20 +434,12 @@ export default function litecanvas(settings = {}) {
|
|
|
452
434
|
'rectfill: 5th param must be a positive number or zero'
|
|
453
435
|
)
|
|
454
436
|
DEV: assert(
|
|
455
|
-
null == radii ||
|
|
456
|
-
isNumber(radii) ||
|
|
457
|
-
(Array.isArray(radii) && radii.length >= 1),
|
|
437
|
+
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
458
438
|
'rectfill: 6th param must be a number or array of at least 2 numbers'
|
|
459
439
|
)
|
|
460
440
|
|
|
461
441
|
_ctx.beginPath()
|
|
462
|
-
_ctx[radii ? 'roundRect' : 'rect'](
|
|
463
|
-
~~x,
|
|
464
|
-
~~y,
|
|
465
|
-
~~width,
|
|
466
|
-
~~height,
|
|
467
|
-
radii
|
|
468
|
-
)
|
|
442
|
+
_ctx[radii ? 'roundRect' : 'rect'](~~x, ~~y, ~~width, ~~height, radii)
|
|
469
443
|
instance.fill(color)
|
|
470
444
|
},
|
|
471
445
|
|
|
@@ -531,14 +505,8 @@ export default function litecanvas(settings = {}) {
|
|
|
531
505
|
line(x1, y1, x2, y2, color) {
|
|
532
506
|
DEV: assert(isNumber(x1), 'line: 1st param must be a number')
|
|
533
507
|
DEV: assert(isNumber(y1), 'line: 2nd param must be a number')
|
|
534
|
-
DEV: assert(
|
|
535
|
-
|
|
536
|
-
'line: 3rd param must be a positive number or zero'
|
|
537
|
-
)
|
|
538
|
-
DEV: assert(
|
|
539
|
-
isNumber(y2),
|
|
540
|
-
'line: 4th param must be a positive number or zero'
|
|
541
|
-
)
|
|
508
|
+
DEV: assert(isNumber(x2), 'line: 3rd param must be a positive number or zero')
|
|
509
|
+
DEV: assert(isNumber(y2), 'line: 4th param must be a positive number or zero')
|
|
542
510
|
DEV: assert(
|
|
543
511
|
null == color || (isNumber(color) && color >= 0),
|
|
544
512
|
'line: 5th param must be a positive number or zero'
|
|
@@ -584,10 +552,7 @@ export default function litecanvas(settings = {}) {
|
|
|
584
552
|
Array.isArray(segments) && segments.length > 0,
|
|
585
553
|
'linedash: 1st param must be an array of numbers'
|
|
586
554
|
)
|
|
587
|
-
DEV: assert(
|
|
588
|
-
isNumber(offset),
|
|
589
|
-
'linedash: 2nd param must be a number'
|
|
590
|
-
)
|
|
555
|
+
DEV: assert(isNumber(offset), 'linedash: 2nd param must be a number')
|
|
591
556
|
|
|
592
557
|
_ctx.setLineDash(segments)
|
|
593
558
|
_ctx.lineDashOffset = offset
|
|
@@ -610,10 +575,7 @@ export default function litecanvas(settings = {}) {
|
|
|
610
575
|
null == color || (isNumber(color) && color >= 0),
|
|
611
576
|
'text: 4th param must be a positive number or zero'
|
|
612
577
|
)
|
|
613
|
-
DEV: assert(
|
|
614
|
-
'string' === typeof fontStyle,
|
|
615
|
-
'text: 5th param must be a string'
|
|
616
|
-
)
|
|
578
|
+
DEV: assert('string' === typeof fontStyle, 'text: 5th param must be a string')
|
|
617
579
|
|
|
618
580
|
_ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`
|
|
619
581
|
_ctx.fillStyle = _colors[~~color % _colors.length]
|
|
@@ -626,10 +588,7 @@ export default function litecanvas(settings = {}) {
|
|
|
626
588
|
* @param {string} family
|
|
627
589
|
*/
|
|
628
590
|
textfont(family) {
|
|
629
|
-
DEV: assert(
|
|
630
|
-
'string' === typeof family,
|
|
631
|
-
'textfont: 1st param must be a string'
|
|
632
|
-
)
|
|
591
|
+
DEV: assert('string' === typeof family, 'textfont: 1st param must be a string')
|
|
633
592
|
|
|
634
593
|
_fontFamily = family
|
|
635
594
|
},
|
|
@@ -655,20 +614,14 @@ export default function litecanvas(settings = {}) {
|
|
|
655
614
|
*/
|
|
656
615
|
textalign(align, baseline) {
|
|
657
616
|
DEV: assert(
|
|
658
|
-
null == align ||
|
|
659
|
-
['left', 'right', 'center', 'start', 'end'].includes(align),
|
|
617
|
+
null == align || ['left', 'right', 'center', 'start', 'end'].includes(align),
|
|
660
618
|
'textalign: 1st param must be null or one of the following strings: center, left, right, start or end.'
|
|
661
619
|
)
|
|
662
620
|
DEV: assert(
|
|
663
621
|
null == baseline ||
|
|
664
|
-
[
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
'middle',
|
|
668
|
-
'hanging',
|
|
669
|
-
'alphabetic',
|
|
670
|
-
'ideographic',
|
|
671
|
-
].includes(baseline),
|
|
622
|
+
['top', 'bottom', 'middle', 'hanging', 'alphabetic', 'ideographic'].includes(
|
|
623
|
+
baseline
|
|
624
|
+
),
|
|
672
625
|
'textalign: 2nd param must be null or one of the following strings: middle, top, bottom, hanging, alphabetic or ideographic.'
|
|
673
626
|
)
|
|
674
627
|
|
|
@@ -806,10 +759,7 @@ export default function litecanvas(settings = {}) {
|
|
|
806
759
|
*/
|
|
807
760
|
scale: (x, y) => {
|
|
808
761
|
DEV: assert(isNumber(x), 'scale: 1st param must be a number')
|
|
809
|
-
DEV: assert(
|
|
810
|
-
null == y || isNumber(y),
|
|
811
|
-
'scale: 2nd param must be a number'
|
|
812
|
-
)
|
|
762
|
+
DEV: assert(null == y || isNumber(y), 'scale: 2nd param must be a number')
|
|
813
763
|
|
|
814
764
|
return _ctx.scale(x, y || x)
|
|
815
765
|
},
|
|
@@ -906,14 +856,13 @@ export default function litecanvas(settings = {}) {
|
|
|
906
856
|
/**
|
|
907
857
|
* Turn given path into a clipping region.
|
|
908
858
|
*
|
|
859
|
+
* Note: always call `push()` before and `pop()` after.
|
|
860
|
+
*
|
|
909
861
|
* @param {Path2D} path
|
|
910
862
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
|
|
911
863
|
*/
|
|
912
864
|
clip(path) {
|
|
913
|
-
DEV: assert(
|
|
914
|
-
path instanceof Path2D,
|
|
915
|
-
'clip: 1st param must be a Path2D instance'
|
|
916
|
-
)
|
|
865
|
+
DEV: assert(path instanceof Path2D, 'clip: 1st param must be a Path2D instance')
|
|
917
866
|
|
|
918
867
|
_ctx.clip(path)
|
|
919
868
|
},
|
|
@@ -936,15 +885,11 @@ export default function litecanvas(settings = {}) {
|
|
|
936
885
|
'sfx: 1st param must be an array'
|
|
937
886
|
)
|
|
938
887
|
DEV: assert(isNumber(pitchSlide), 'sfx: 2nd param must be a number')
|
|
939
|
-
DEV: assert(
|
|
940
|
-
isNumber(volumeFactor),
|
|
941
|
-
'sfx: 3rd param must be a number'
|
|
942
|
-
)
|
|
888
|
+
DEV: assert(isNumber(volumeFactor), 'sfx: 3rd param must be a number')
|
|
943
889
|
|
|
944
890
|
if (
|
|
945
891
|
root.zzfxV <= 0 ||
|
|
946
|
-
(navigator.userActivation &&
|
|
947
|
-
!navigator.userActivation.hasBeenActive)
|
|
892
|
+
(navigator.userActivation && !navigator.userActivation.hasBeenActive)
|
|
948
893
|
) {
|
|
949
894
|
return false
|
|
950
895
|
}
|
|
@@ -982,18 +927,16 @@ export default function litecanvas(settings = {}) {
|
|
|
982
927
|
* @param {pluginCallback} callback
|
|
983
928
|
*/
|
|
984
929
|
use(callback, config = {}) {
|
|
985
|
-
DEV: assert(
|
|
986
|
-
|
|
987
|
-
'use: 1st param must be a function'
|
|
988
|
-
)
|
|
989
|
-
DEV: assert(
|
|
990
|
-
'object' === typeof config,
|
|
991
|
-
'use: 2nd param must be an object'
|
|
992
|
-
)
|
|
930
|
+
DEV: assert('function' === typeof callback, 'use: 1st param must be a function')
|
|
931
|
+
DEV: assert('object' === typeof config, 'use: 2nd param must be an object')
|
|
993
932
|
|
|
994
|
-
_initialized
|
|
995
|
-
|
|
996
|
-
|
|
933
|
+
if (_initialized) {
|
|
934
|
+
// load the plugin now
|
|
935
|
+
loadPlugin(callback, config)
|
|
936
|
+
} else {
|
|
937
|
+
// schedule to load the plugin right before the "init" event
|
|
938
|
+
_plugins.push([callback, config])
|
|
939
|
+
}
|
|
997
940
|
},
|
|
998
941
|
|
|
999
942
|
/**
|
|
@@ -1004,14 +947,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1004
947
|
* @returns {Function} a function to remove the listener
|
|
1005
948
|
*/
|
|
1006
949
|
listen(eventName, callback) {
|
|
1007
|
-
DEV: assert(
|
|
1008
|
-
|
|
1009
|
-
'listen: 1st param must be a string'
|
|
1010
|
-
)
|
|
1011
|
-
DEV: assert(
|
|
1012
|
-
'function' === typeof callback,
|
|
1013
|
-
'listen: 2nd param must be a function'
|
|
1014
|
-
)
|
|
950
|
+
DEV: assert('string' === typeof eventName, 'listen: 1st param must be a string')
|
|
951
|
+
DEV: assert('function' === typeof callback, 'listen: 2nd param must be a function')
|
|
1015
952
|
|
|
1016
953
|
eventName = eventName.toLowerCase()
|
|
1017
954
|
|
|
@@ -1032,10 +969,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1032
969
|
* @param {*} [arg4] any data to be passed over the listeners
|
|
1033
970
|
*/
|
|
1034
971
|
emit(eventName, arg1, arg2, arg3, arg4) {
|
|
1035
|
-
DEV: assert(
|
|
1036
|
-
'string' === typeof eventName,
|
|
1037
|
-
'emit: 1st param must be a string'
|
|
1038
|
-
)
|
|
972
|
+
DEV: assert('string' === typeof eventName, 'emit: 1st param must be a string')
|
|
1039
973
|
if (_initialized) {
|
|
1040
974
|
eventName = eventName.toLowerCase()
|
|
1041
975
|
|
|
@@ -1065,14 +999,9 @@ export default function litecanvas(settings = {}) {
|
|
|
1065
999
|
* @param {*} value
|
|
1066
1000
|
*/
|
|
1067
1001
|
def(key, value) {
|
|
1068
|
-
DEV: assert(
|
|
1069
|
-
'string' === typeof key,
|
|
1070
|
-
'def: 1st param must be a string'
|
|
1071
|
-
)
|
|
1002
|
+
DEV: assert('string' === typeof key, 'def: 1st param must be a string')
|
|
1072
1003
|
DEV: if (null == value) {
|
|
1073
|
-
console.warn(
|
|
1074
|
-
`def: key "${key}" was defined as ${value} but now is null`
|
|
1075
|
-
)
|
|
1004
|
+
console.warn(`def: key "${key}" was defined as ${value} but now is null`)
|
|
1076
1005
|
}
|
|
1077
1006
|
|
|
1078
1007
|
instance[key] = value
|
|
@@ -1118,10 +1047,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1118
1047
|
* @returns {any}
|
|
1119
1048
|
*/
|
|
1120
1049
|
stat(n) {
|
|
1121
|
-
DEV: assert(
|
|
1122
|
-
isNumber(n) && n >= 0,
|
|
1123
|
-
'stat: 1st param must be a positive number'
|
|
1124
|
-
)
|
|
1050
|
+
DEV: assert(isNumber(n) && n >= 0, 'stat: 1st param must be a positive number')
|
|
1125
1051
|
|
|
1126
1052
|
const list = [
|
|
1127
1053
|
// 0
|
|
@@ -1213,30 +1139,50 @@ export default function litecanvas(settings = {}) {
|
|
|
1213
1139
|
|
|
1214
1140
|
// default mouse/touch handlers
|
|
1215
1141
|
if (settings.tapEvents) {
|
|
1216
|
-
const _getXY =
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1142
|
+
const _getXY =
|
|
1143
|
+
/**
|
|
1144
|
+
* @param {number} pageX
|
|
1145
|
+
* @param {number} pageY
|
|
1146
|
+
*/
|
|
1147
|
+
(pageX, pageY) => [
|
|
1148
|
+
(pageX - _canvas.offsetLeft) / _scale,
|
|
1149
|
+
(pageY - _canvas.offsetTop) / _scale,
|
|
1150
|
+
],
|
|
1220
1151
|
_taps = new Map(),
|
|
1221
|
-
_registerTap =
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1152
|
+
_registerTap =
|
|
1153
|
+
/**
|
|
1154
|
+
* @param {number} id
|
|
1155
|
+
* @param {number} [x]
|
|
1156
|
+
* @param {number} [y]
|
|
1157
|
+
*/
|
|
1158
|
+
(id, x, y) => {
|
|
1159
|
+
const tap = {
|
|
1160
|
+
x,
|
|
1161
|
+
y,
|
|
1162
|
+
startX: x,
|
|
1163
|
+
startY: y,
|
|
1164
|
+
// timestamp
|
|
1165
|
+
ts: performance.now(),
|
|
1166
|
+
}
|
|
1167
|
+
_taps.set(id, tap)
|
|
1168
|
+
return tap
|
|
1169
|
+
},
|
|
1170
|
+
_updateTap =
|
|
1171
|
+
/**
|
|
1172
|
+
* @param {number} id
|
|
1173
|
+
* @param {number} x
|
|
1174
|
+
* @param {number} y
|
|
1175
|
+
*/
|
|
1176
|
+
(id, x, y) => {
|
|
1177
|
+
const tap = _taps.get(id) || _registerTap(id)
|
|
1178
|
+
tap.x = x
|
|
1179
|
+
tap.y = y
|
|
1180
|
+
},
|
|
1181
|
+
_checkTapped =
|
|
1182
|
+
/**
|
|
1183
|
+
* @param {{ts: number}} tap
|
|
1184
|
+
*/
|
|
1185
|
+
(tap) => tap && performance.now() - tap.ts <= 300,
|
|
1240
1186
|
preventDefault = (ev) => ev.preventDefault()
|
|
1241
1187
|
|
|
1242
1188
|
let _pressingMouse = false
|
|
@@ -1384,9 +1330,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1384
1330
|
*/
|
|
1385
1331
|
const keyCheck = (keySet, key = '') => {
|
|
1386
1332
|
key = key.toLowerCase()
|
|
1387
|
-
return !key
|
|
1388
|
-
? keySet.size > 0
|
|
1389
|
-
: keySet.has('space' === key ? ' ' : key)
|
|
1333
|
+
return !key ? keySet.size > 0 : keySet.has('space' === key ? ' ' : key)
|
|
1390
1334
|
}
|
|
1391
1335
|
|
|
1392
1336
|
// @ts-ignore
|
|
@@ -1505,10 +1449,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1505
1449
|
|
|
1506
1450
|
_canvas = _canvas || document.createElement('canvas')
|
|
1507
1451
|
|
|
1508
|
-
DEV: assert(
|
|
1509
|
-
_canvas && _canvas.tagName === 'CANVAS',
|
|
1510
|
-
'Invalid canvas element'
|
|
1511
|
-
)
|
|
1452
|
+
DEV: assert(_canvas && _canvas.tagName === 'CANVAS', 'Invalid canvas element')
|
|
1512
1453
|
|
|
1513
1454
|
instance.def('CANVAS', _canvas)
|
|
1514
1455
|
_ctx = _canvas.getContext('2d')
|
|
@@ -1527,18 +1468,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1527
1468
|
|
|
1528
1469
|
function resizeCanvas() {
|
|
1529
1470
|
DEV: assert(
|
|
1530
|
-
null == settings.width ||
|
|
1531
|
-
(isNumber(settings.width) && settings.width > 0),
|
|
1471
|
+
null == settings.width || (isNumber(settings.width) && settings.width > 0),
|
|
1532
1472
|
'Litecanvas\' option "width" should be a positive number when defined'
|
|
1533
1473
|
)
|
|
1534
1474
|
DEV: assert(
|
|
1535
|
-
null == settings.height ||
|
|
1536
|
-
(isNumber(settings.height) && settings.height > 0),
|
|
1475
|
+
null == settings.height || (isNumber(settings.height) && settings.height > 0),
|
|
1537
1476
|
'Litecanvas\' option "height" should be a positive number when defined'
|
|
1538
1477
|
)
|
|
1539
1478
|
DEV: assert(
|
|
1540
|
-
null == settings.height ||
|
|
1541
|
-
(settings.width > 0 && settings.height > 0),
|
|
1479
|
+
null == settings.height || (settings.width > 0 && settings.height > 0),
|
|
1542
1480
|
'Litecanvas\' option "width" is required when the option "height" is defined'
|
|
1543
1481
|
)
|
|
1544
1482
|
|
|
@@ -1557,10 +1495,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1557
1495
|
_canvas.style.margin = 'auto'
|
|
1558
1496
|
}
|
|
1559
1497
|
|
|
1560
|
-
_scale = math.min(
|
|
1561
|
-
root.innerWidth / instance.W,
|
|
1562
|
-
root.innerHeight / instance.H
|
|
1563
|
-
)
|
|
1498
|
+
_scale = math.min(root.innerWidth / instance.W, root.innerHeight / instance.H)
|
|
1564
1499
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1
|
|
1565
1500
|
|
|
1566
1501
|
_canvas.style.width = instance.W * _scale + 'px'
|
package/types/index.d.ts
CHANGED
|
@@ -3,9 +3,7 @@ import './types.d.ts'
|
|
|
3
3
|
/**
|
|
4
4
|
* The litecanvas constructor
|
|
5
5
|
*/
|
|
6
|
-
export default function litecanvas(
|
|
7
|
-
settings?: LitecanvasOptions
|
|
8
|
-
): LitecanvasInstance
|
|
6
|
+
export default function litecanvas(settings?: LitecanvasOptions): LitecanvasInstance
|
|
9
7
|
|
|
10
8
|
declare global {
|
|
11
9
|
function litecanvas(settings?: LitecanvasOptions): LitecanvasInstance
|
|
@@ -131,15 +129,10 @@ declare global {
|
|
|
131
129
|
*
|
|
132
130
|
* @param from - the lower bound
|
|
133
131
|
* @param to - the higher bound
|
|
134
|
-
* @param t - the
|
|
135
|
-
* @param fn - the periodic function (which default to `Math.sin`)
|
|
136
|
-
*/
|
|
137
|
-
function wave(
|
|
138
|
-
from: number,
|
|
139
|
-
to: number,
|
|
140
|
-
t: number,
|
|
141
|
-
fn?: (n: number) => number
|
|
142
|
-
): number
|
|
132
|
+
* @param t - the value passed to the periodic function
|
|
133
|
+
* @param fn= - the periodic function (which default to `Math.sin`)
|
|
134
|
+
*/
|
|
135
|
+
function wave(from: number, to: number, t: number, fn?: (n: number) => number): number
|
|
143
136
|
/**
|
|
144
137
|
* Returns the sine of a number in radians
|
|
145
138
|
*/
|
|
@@ -249,6 +242,8 @@ declare global {
|
|
|
249
242
|
* @param height
|
|
250
243
|
* @param [color=0] the color index
|
|
251
244
|
* @param [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
245
|
+
*
|
|
246
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
252
247
|
*/
|
|
253
248
|
function rect(
|
|
254
249
|
x: number,
|
|
@@ -293,12 +288,7 @@ declare global {
|
|
|
293
288
|
* @param radius
|
|
294
289
|
* @param [color=0] the color index
|
|
295
290
|
*/
|
|
296
|
-
function circfill(
|
|
297
|
-
x: number,
|
|
298
|
-
y: number,
|
|
299
|
-
radius: number,
|
|
300
|
-
color?: number
|
|
301
|
-
): void
|
|
291
|
+
function circfill(x: number, y: number, radius: number, color?: number): void
|
|
302
292
|
/**
|
|
303
293
|
* Draw a line
|
|
304
294
|
*
|
|
@@ -308,13 +298,7 @@ declare global {
|
|
|
308
298
|
* @param y2
|
|
309
299
|
* @param [color=0] the color index
|
|
310
300
|
*/
|
|
311
|
-
function line(
|
|
312
|
-
x1: number,
|
|
313
|
-
y1: number,
|
|
314
|
-
x2: number,
|
|
315
|
-
y2: number,
|
|
316
|
-
color?: number
|
|
317
|
-
): void
|
|
301
|
+
function line(x1: number, y1: number, x2: number, y2: number, color?: number): void
|
|
318
302
|
/**
|
|
319
303
|
* Sets the thickness of lines
|
|
320
304
|
*
|
|
@@ -342,13 +326,7 @@ declare global {
|
|
|
342
326
|
* @param [color=3] the color index
|
|
343
327
|
* @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
|
|
344
328
|
*/
|
|
345
|
-
function text(
|
|
346
|
-
x: number,
|
|
347
|
-
y: number,
|
|
348
|
-
message: string,
|
|
349
|
-
color?: number,
|
|
350
|
-
fontStyle?: string
|
|
351
|
-
): void
|
|
329
|
+
function text(x: number, y: number, message: string, color?: number, fontStyle?: string): void
|
|
352
330
|
/**
|
|
353
331
|
* Set the font family
|
|
354
332
|
*
|
|
@@ -369,10 +347,7 @@ declare global {
|
|
|
369
347
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textBaseline
|
|
370
348
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
|
|
371
349
|
*/
|
|
372
|
-
function textalign(
|
|
373
|
-
align: CanvasTextAlign,
|
|
374
|
-
baseline: CanvasTextBaseline
|
|
375
|
-
): void
|
|
350
|
+
function textalign(align: CanvasTextAlign, baseline: CanvasTextBaseline): void
|
|
376
351
|
|
|
377
352
|
/** IMAGE GRAPHICS API */
|
|
378
353
|
/**
|
|
@@ -552,13 +527,7 @@ declare global {
|
|
|
552
527
|
* @param [arg3] any data to be passed over the listeners
|
|
553
528
|
* @param [arg4] any data to be passed over the listeners
|
|
554
529
|
*/
|
|
555
|
-
function emit(
|
|
556
|
-
event: string,
|
|
557
|
-
arg1?: any,
|
|
558
|
-
arg2?: any,
|
|
559
|
-
arg3?: any,
|
|
560
|
-
arg4?: any
|
|
561
|
-
): void
|
|
530
|
+
function emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void
|
|
562
531
|
/**
|
|
563
532
|
* Set or reset the color palette
|
|
564
533
|
*
|
package/types/types.d.ts
CHANGED
|
@@ -120,15 +120,10 @@ type LitecanvasInstance = {
|
|
|
120
120
|
*
|
|
121
121
|
* @param from - the lower bound
|
|
122
122
|
* @param to - the higher bound
|
|
123
|
-
* @param t - the
|
|
123
|
+
* @param t - the value passed to the periodic function
|
|
124
124
|
* @param fn - the periodic function (which default to `Math.sin`)
|
|
125
125
|
*/
|
|
126
|
-
wave(
|
|
127
|
-
from: number,
|
|
128
|
-
to: number,
|
|
129
|
-
t: number,
|
|
130
|
-
fn?: (n: number) => number
|
|
131
|
-
): number
|
|
126
|
+
wave(from: number, to: number, t: number, fn?: (n: number) => number): number
|
|
132
127
|
/**
|
|
133
128
|
* Returns the sine of a number in radians
|
|
134
129
|
*/
|
|
@@ -238,6 +233,8 @@ type LitecanvasInstance = {
|
|
|
238
233
|
* @param height
|
|
239
234
|
* @param [color=0] the color index
|
|
240
235
|
* @param [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
236
|
+
*
|
|
237
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
241
238
|
*/
|
|
242
239
|
rect(
|
|
243
240
|
x: number,
|
|
@@ -320,13 +317,7 @@ type LitecanvasInstance = {
|
|
|
320
317
|
* @param [color=3] the color index
|
|
321
318
|
* @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
|
|
322
319
|
*/
|
|
323
|
-
text(
|
|
324
|
-
x: number,
|
|
325
|
-
y: number,
|
|
326
|
-
message: string,
|
|
327
|
-
color?: number,
|
|
328
|
-
fontStyle?: string
|
|
329
|
-
): void
|
|
320
|
+
text(x: number, y: number, message: string, color?: number, fontStyle?: string): void
|
|
330
321
|
/**
|
|
331
322
|
* Set the font family
|
|
332
323
|
*
|
|
@@ -472,11 +463,7 @@ type LitecanvasInstance = {
|
|
|
472
463
|
*
|
|
473
464
|
* @see https://github.com/KilledByAPixel/ZzFX
|
|
474
465
|
*/
|
|
475
|
-
sfx(
|
|
476
|
-
zzfxParams?: number[],
|
|
477
|
-
pitchSlide?: number,
|
|
478
|
-
volumeFactor?: number
|
|
479
|
-
): number[] | boolean
|
|
466
|
+
sfx(zzfxParams?: number[], pitchSlide?: number, volumeFactor?: number): number[] | boolean
|
|
480
467
|
/**
|
|
481
468
|
* Set the ZzFX's global volume factor.
|
|
482
469
|
* Note: use 0 to mute all sound effects.
|
|
@@ -660,8 +647,6 @@ type LitecanvasGameLoop = {
|
|
|
660
647
|
tapping?: (tapX: number, tapY: number, tapId: number) => void
|
|
661
648
|
}
|
|
662
649
|
|
|
663
|
-
type drawCallback = (
|
|
664
|
-
context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
|
|
665
|
-
) => void
|
|
650
|
+
type drawCallback = (context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => void
|
|
666
651
|
|
|
667
652
|
type pluginCallback = (instance: LitecanvasInstance, config?: any) => any
|