litecanvas 0.86.0 → 0.87.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/dist/dist.dev.js +188 -123
- package/dist/dist.js +1 -1
- package/dist/dist.min.js +1 -1
- package/package.json +8 -5
- package/src/index.js +187 -123
package/src/index.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { setupZzFX } from './zzfx.js'
|
|
3
3
|
import { defaultPalette } from './palette.js'
|
|
4
4
|
import { assert } from './dev.js'
|
|
5
|
+
import { version } from '../version.js'
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* The litecanvas constructor
|
|
@@ -131,9 +132,9 @@ export default function litecanvas(settings = {}) {
|
|
|
131
132
|
* @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
|
|
132
133
|
*/
|
|
133
134
|
lerp: (start, end, t) => {
|
|
134
|
-
DEV: assert(isNumber(start), 'lerp
|
|
135
|
-
DEV: assert(isNumber(end), 'lerp
|
|
136
|
-
DEV: assert(isNumber(t), 'lerp
|
|
135
|
+
DEV: assert(isNumber(start), '[litecanvas] lerp() 1st param must be a number')
|
|
136
|
+
DEV: assert(isNumber(end), '[litecanvas] lerp() 2nd param must be a number')
|
|
137
|
+
DEV: assert(isNumber(t), '[litecanvas] lerp() 3rd param must be a number')
|
|
137
138
|
|
|
138
139
|
return t * (end - start) + start
|
|
139
140
|
},
|
|
@@ -172,10 +173,10 @@ export default function litecanvas(settings = {}) {
|
|
|
172
173
|
* @returns {number} rounded number.
|
|
173
174
|
*/
|
|
174
175
|
round: (n, precision = 0) => {
|
|
175
|
-
DEV: assert(isNumber(n), 'round
|
|
176
|
+
DEV: assert(isNumber(n), '[litecanvas] round() 1st param must be a number')
|
|
176
177
|
DEV: assert(
|
|
177
178
|
null == precision || (isNumber(precision) && precision >= 0),
|
|
178
|
-
'round
|
|
179
|
+
'[litecanvas] round() 2nd param must be a positive number or zero'
|
|
179
180
|
)
|
|
180
181
|
if (!precision) {
|
|
181
182
|
return math.round(n)
|
|
@@ -193,10 +194,13 @@ export default function litecanvas(settings = {}) {
|
|
|
193
194
|
* @returns {number}
|
|
194
195
|
*/
|
|
195
196
|
clamp: (value, min, max) => {
|
|
196
|
-
DEV: assert(isNumber(value), 'clamp
|
|
197
|
-
DEV: assert(isNumber(min), 'clamp
|
|
198
|
-
DEV: assert(isNumber(max), 'clamp
|
|
199
|
-
DEV: assert(
|
|
197
|
+
DEV: assert(isNumber(value), '[litecanvas] clamp() 1st param must be a number')
|
|
198
|
+
DEV: assert(isNumber(min), '[litecanvas] clamp() 2nd param must be a number')
|
|
199
|
+
DEV: assert(isNumber(max), '[litecanvas] clamp() 3rd param must be a number')
|
|
200
|
+
DEV: assert(
|
|
201
|
+
max > min,
|
|
202
|
+
'[litecanvas] clamp() the 2nd param must be less than the 3rd param'
|
|
203
|
+
)
|
|
200
204
|
|
|
201
205
|
if (value < min) return min
|
|
202
206
|
if (value > max) return max
|
|
@@ -212,10 +216,13 @@ export default function litecanvas(settings = {}) {
|
|
|
212
216
|
* @returns {number}
|
|
213
217
|
*/
|
|
214
218
|
wrap: (value, min, max) => {
|
|
215
|
-
DEV: assert(isNumber(value), 'wrap
|
|
216
|
-
DEV: assert(isNumber(min), 'wrap
|
|
217
|
-
DEV: assert(isNumber(max), 'wrap
|
|
218
|
-
DEV: assert(
|
|
219
|
+
DEV: assert(isNumber(value), '[litecanvas] wrap() 1st param must be a number')
|
|
220
|
+
DEV: assert(isNumber(min), '[litecanvas] wrap() 2nd param must be a number')
|
|
221
|
+
DEV: assert(isNumber(max), '[litecanvas] wrap() 3rd param must be a number')
|
|
222
|
+
DEV: assert(
|
|
223
|
+
max > min,
|
|
224
|
+
'[litecanvas] wrap() the 2nd param must be less than the 3rd param'
|
|
225
|
+
)
|
|
219
226
|
|
|
220
227
|
return value - (max - min) * math.floor((value - min) / (max - min))
|
|
221
228
|
},
|
|
@@ -232,12 +239,15 @@ export default function litecanvas(settings = {}) {
|
|
|
232
239
|
* @returns {number} the remapped number
|
|
233
240
|
*/
|
|
234
241
|
map(value, start1, stop1, start2, stop2, withinBounds) {
|
|
235
|
-
DEV: assert(isNumber(value), 'map
|
|
236
|
-
DEV: assert(isNumber(start1), 'map
|
|
237
|
-
DEV: assert(isNumber(stop1), 'map
|
|
238
|
-
DEV: assert(isNumber(start2), 'map
|
|
239
|
-
DEV: assert(isNumber(stop2), 'map
|
|
240
|
-
DEV: assert(
|
|
242
|
+
DEV: assert(isNumber(value), '[litecanvas] map() 1st param must be a number')
|
|
243
|
+
DEV: assert(isNumber(start1), '[litecanvas] map() 2nd param must be a number')
|
|
244
|
+
DEV: assert(isNumber(stop1), '[litecanvas] map() 3rd param must be a number')
|
|
245
|
+
DEV: assert(isNumber(start2), '[litecanvas] map() 4th param must be a number')
|
|
246
|
+
DEV: assert(isNumber(stop2), '[litecanvas] map() 5th param must be a number')
|
|
247
|
+
DEV: assert(
|
|
248
|
+
stop1 !== start1,
|
|
249
|
+
'[litecanvas] map() the 2nd param must be different than the 3rd param'
|
|
250
|
+
)
|
|
241
251
|
|
|
242
252
|
// prettier-ignore
|
|
243
253
|
const result = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
|
|
@@ -255,10 +265,13 @@ export default function litecanvas(settings = {}) {
|
|
|
255
265
|
* @returns {number} the normalized number.
|
|
256
266
|
*/
|
|
257
267
|
norm: (value, start, stop) => {
|
|
258
|
-
DEV: assert(isNumber(value), 'norm
|
|
259
|
-
DEV: assert(isNumber(start), 'norm
|
|
260
|
-
DEV: assert(isNumber(stop), 'norm
|
|
261
|
-
DEV: assert(
|
|
268
|
+
DEV: assert(isNumber(value), '[litecanvas] norm() 1st param must be a number')
|
|
269
|
+
DEV: assert(isNumber(start), '[litecanvas] norm() 2nd param must be a number')
|
|
270
|
+
DEV: assert(isNumber(stop), '[litecanvas] norm() 3rd param must be a number')
|
|
271
|
+
DEV: assert(
|
|
272
|
+
start !== stop,
|
|
273
|
+
'[litecanvas] norm() the 2nd param must be different than the 3rd param'
|
|
274
|
+
)
|
|
262
275
|
|
|
263
276
|
return instance.map(value, start, stop, 0, 1)
|
|
264
277
|
},
|
|
@@ -272,12 +285,12 @@ export default function litecanvas(settings = {}) {
|
|
|
272
285
|
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
|
|
273
286
|
*/
|
|
274
287
|
wave: (from, to, t, fn = Math.sin) => {
|
|
275
|
-
DEV: assert(isNumber(from), 'wave
|
|
276
|
-
DEV: assert(isNumber(to), 'wave
|
|
277
|
-
DEV: assert(isNumber(t), 'wave
|
|
288
|
+
DEV: assert(isNumber(from), '[litecanvas] wave() 1st param must be a number')
|
|
289
|
+
DEV: assert(isNumber(to), '[litecanvas] wave() 2nd param must be a number')
|
|
290
|
+
DEV: assert(isNumber(t), '[litecanvas] wave() 3rd param must be a number')
|
|
278
291
|
DEV: assert(
|
|
279
292
|
'function' === typeof fn,
|
|
280
|
-
'wave
|
|
293
|
+
'[litecanvas] wave() 4rd param must be a function (n: number) => number'
|
|
281
294
|
)
|
|
282
295
|
return from + ((fn(t) + 1) / 2) * (to - from)
|
|
283
296
|
},
|
|
@@ -292,9 +305,12 @@ export default function litecanvas(settings = {}) {
|
|
|
292
305
|
* @returns {number} the random number
|
|
293
306
|
*/
|
|
294
307
|
rand: (min = 0.0, max = 1.0) => {
|
|
295
|
-
DEV: assert(isNumber(min), 'rand
|
|
296
|
-
DEV: assert(isNumber(max), 'rand
|
|
297
|
-
DEV: assert(
|
|
308
|
+
DEV: assert(isNumber(min), '[litecanvas] rand() 1st param must be a number')
|
|
309
|
+
DEV: assert(isNumber(max), '[litecanvas] rand() 2nd param must be a number')
|
|
310
|
+
DEV: assert(
|
|
311
|
+
max > min,
|
|
312
|
+
'[litecanvas] rand() the 1st param must be less than the 2nd param'
|
|
313
|
+
)
|
|
298
314
|
|
|
299
315
|
const a = 1664525
|
|
300
316
|
const c = 1013904223
|
|
@@ -313,9 +329,12 @@ export default function litecanvas(settings = {}) {
|
|
|
313
329
|
* @returns {number} the random number
|
|
314
330
|
*/
|
|
315
331
|
randi: (min = 0, max = 1) => {
|
|
316
|
-
DEV: assert(isNumber(min), 'randi
|
|
317
|
-
DEV: assert(isNumber(max), 'randi
|
|
318
|
-
DEV: assert(
|
|
332
|
+
DEV: assert(isNumber(min), '[litecanvas] randi() 1st param must be a number')
|
|
333
|
+
DEV: assert(isNumber(max), '[litecanvas] randi() 2nd param must be a number')
|
|
334
|
+
DEV: assert(
|
|
335
|
+
max > min,
|
|
336
|
+
'[litecanvas] randi() the 1st param must be less than the 2nd param'
|
|
337
|
+
)
|
|
319
338
|
|
|
320
339
|
return math.floor(instance.rand(min, max + 1))
|
|
321
340
|
},
|
|
@@ -330,7 +349,7 @@ export default function litecanvas(settings = {}) {
|
|
|
330
349
|
rseed(value) {
|
|
331
350
|
DEV: assert(
|
|
332
351
|
null == value || (isNumber(value) && value >= 0),
|
|
333
|
-
'rseed
|
|
352
|
+
'[litecanvas] rseed() 1st param must be a positive number or zero'
|
|
334
353
|
)
|
|
335
354
|
|
|
336
355
|
_rngSeed = ~~value
|
|
@@ -345,7 +364,7 @@ export default function litecanvas(settings = {}) {
|
|
|
345
364
|
cls(color) {
|
|
346
365
|
DEV: assert(
|
|
347
366
|
null == color || (isNumber(color) && color >= 0),
|
|
348
|
-
'cls
|
|
367
|
+
'[litecanvas] cls() 1st param must be a positive number or zero or undefined'
|
|
349
368
|
)
|
|
350
369
|
|
|
351
370
|
if (null == color) {
|
|
@@ -368,20 +387,23 @@ export default function litecanvas(settings = {}) {
|
|
|
368
387
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
369
388
|
*/
|
|
370
389
|
rect(x, y, width, height, color, radii) {
|
|
371
|
-
DEV: assert(isNumber(x), 'rect
|
|
372
|
-
DEV: assert(isNumber(y), 'rect
|
|
373
|
-
DEV: assert(
|
|
390
|
+
DEV: assert(isNumber(x), '[litecanvas] rect() 1st param must be a number')
|
|
391
|
+
DEV: assert(isNumber(y), '[litecanvas] rect() 2nd param must be a number')
|
|
392
|
+
DEV: assert(
|
|
393
|
+
isNumber(width) && width > 0,
|
|
394
|
+
'[litecanvas] rect() 3rd param must be a positive number'
|
|
395
|
+
)
|
|
374
396
|
DEV: assert(
|
|
375
397
|
isNumber(height) && height >= 0,
|
|
376
|
-
'rect
|
|
398
|
+
'[litecanvas] rect() 4th param must be a positive number or zero'
|
|
377
399
|
)
|
|
378
400
|
DEV: assert(
|
|
379
401
|
null == color || (isNumber(color) && color >= 0),
|
|
380
|
-
'rect
|
|
402
|
+
'[litecanvas] rect() 5th param must be a positive number or zero'
|
|
381
403
|
)
|
|
382
404
|
DEV: assert(
|
|
383
405
|
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
384
|
-
'rect
|
|
406
|
+
'[litecanvas] rect() 6th param must be a number or array of numbers'
|
|
385
407
|
)
|
|
386
408
|
|
|
387
409
|
_ctx.beginPath()
|
|
@@ -406,23 +428,23 @@ export default function litecanvas(settings = {}) {
|
|
|
406
428
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
407
429
|
*/
|
|
408
430
|
rectfill(x, y, width, height, color, radii) {
|
|
409
|
-
DEV: assert(isNumber(x), 'rectfill
|
|
410
|
-
DEV: assert(isNumber(y), 'rectfill
|
|
431
|
+
DEV: assert(isNumber(x), '[litecanvas] rectfill() 1st param must be a number')
|
|
432
|
+
DEV: assert(isNumber(y), '[litecanvas] rectfill() 2nd param must be a number')
|
|
411
433
|
DEV: assert(
|
|
412
434
|
isNumber(width) && width >= 0,
|
|
413
|
-
'rectfill
|
|
435
|
+
'[litecanvas] rectfill() 3rd param must be a positive number or zero'
|
|
414
436
|
)
|
|
415
437
|
DEV: assert(
|
|
416
438
|
isNumber(height) && height >= 0,
|
|
417
|
-
'rectfill
|
|
439
|
+
'[litecanvas] rectfill() 4th param must be a positive number or zero'
|
|
418
440
|
)
|
|
419
441
|
DEV: assert(
|
|
420
442
|
null == color || (isNumber(color) && color >= 0),
|
|
421
|
-
'rectfill
|
|
443
|
+
'[litecanvas] rectfill() 5th param must be a positive number or zero'
|
|
422
444
|
)
|
|
423
445
|
DEV: assert(
|
|
424
446
|
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
425
|
-
'rectfill
|
|
447
|
+
'[litecanvas] rectfill() 6th param must be a number or array of at least 2 numbers'
|
|
426
448
|
)
|
|
427
449
|
|
|
428
450
|
_ctx.beginPath()
|
|
@@ -439,15 +461,15 @@ export default function litecanvas(settings = {}) {
|
|
|
439
461
|
* @param {number} [color=0] the color index
|
|
440
462
|
*/
|
|
441
463
|
circ(x, y, radius, color) {
|
|
442
|
-
DEV: assert(isNumber(x), 'circ
|
|
443
|
-
DEV: assert(isNumber(y), 'circ
|
|
464
|
+
DEV: assert(isNumber(x), '[litecanvas] circ() 1st param must be a number')
|
|
465
|
+
DEV: assert(isNumber(y), '[litecanvas] circ() 2nd param must be a number')
|
|
444
466
|
DEV: assert(
|
|
445
467
|
isNumber(radius) && radius >= 0,
|
|
446
|
-
'circ
|
|
468
|
+
'[litecanvas] circ() 3rd param must be a positive number or zero'
|
|
447
469
|
)
|
|
448
470
|
DEV: assert(
|
|
449
471
|
null == color || (isNumber(color) && color >= 0),
|
|
450
|
-
'circ
|
|
472
|
+
'[litecanvas] circ() 4th param must be a positive number or zero'
|
|
451
473
|
)
|
|
452
474
|
|
|
453
475
|
_ctx.beginPath()
|
|
@@ -464,15 +486,15 @@ export default function litecanvas(settings = {}) {
|
|
|
464
486
|
* @param {number} [color=0] the color index
|
|
465
487
|
*/
|
|
466
488
|
circfill(x, y, radius, color) {
|
|
467
|
-
DEV: assert(isNumber(x), 'circfill
|
|
468
|
-
DEV: assert(isNumber(y), 'circfill
|
|
489
|
+
DEV: assert(isNumber(x), '[litecanvas] circfill() 1st param must be a number')
|
|
490
|
+
DEV: assert(isNumber(y), '[litecanvas] circfill() 2nd param must be a number')
|
|
469
491
|
DEV: assert(
|
|
470
492
|
isNumber(radius) && radius >= 0,
|
|
471
|
-
'circfill
|
|
493
|
+
'[litecanvas] circfill() 3rd param must be a positive number or zero'
|
|
472
494
|
)
|
|
473
495
|
DEV: assert(
|
|
474
496
|
null == color || (isNumber(color) && color >= 0),
|
|
475
|
-
'circfill
|
|
497
|
+
'[litecanvas] circfill() 4th param must be a positive number or zero'
|
|
476
498
|
)
|
|
477
499
|
|
|
478
500
|
_ctx.beginPath()
|
|
@@ -490,19 +512,19 @@ export default function litecanvas(settings = {}) {
|
|
|
490
512
|
* @param {number} [color=0] the color index
|
|
491
513
|
*/
|
|
492
514
|
oval(x, y, radiusX, radiusY, color) {
|
|
493
|
-
DEV: assert(isNumber(x), 'oval
|
|
494
|
-
DEV: assert(isNumber(y), 'oval
|
|
515
|
+
DEV: assert(isNumber(x), '[litecanvas] oval() 1st param must be a number')
|
|
516
|
+
DEV: assert(isNumber(y), '[litecanvas] oval() 2nd param must be a number')
|
|
495
517
|
DEV: assert(
|
|
496
518
|
isNumber(radiusX) && radiusX >= 0,
|
|
497
|
-
'oval
|
|
519
|
+
'[litecanvas] oval() 3rd param must be a positive number or zero'
|
|
498
520
|
)
|
|
499
521
|
DEV: assert(
|
|
500
522
|
isNumber(radiusY) && radiusY >= 0,
|
|
501
|
-
'oval
|
|
523
|
+
'[litecanvas] oval() 4th param must be a positive number or zero'
|
|
502
524
|
)
|
|
503
525
|
DEV: assert(
|
|
504
526
|
null == color || (isNumber(color) && color >= 0),
|
|
505
|
-
'oval
|
|
527
|
+
'[litecanvas] oval() 5th param must be a positive number or zero'
|
|
506
528
|
)
|
|
507
529
|
|
|
508
530
|
_ctx.beginPath()
|
|
@@ -520,19 +542,19 @@ export default function litecanvas(settings = {}) {
|
|
|
520
542
|
* @param {number} [color=0] the color index
|
|
521
543
|
*/
|
|
522
544
|
ovalfill(x, y, radiusX, radiusY, color) {
|
|
523
|
-
DEV: assert(isNumber(x), 'ovalfill
|
|
524
|
-
DEV: assert(isNumber(y), 'ovalfill
|
|
545
|
+
DEV: assert(isNumber(x), '[litecanvas] ovalfill() 1st param must be a number')
|
|
546
|
+
DEV: assert(isNumber(y), '[litecanvas] ovalfill() 2nd param must be a number')
|
|
525
547
|
DEV: assert(
|
|
526
548
|
isNumber(radiusX) && radiusX >= 0,
|
|
527
|
-
'ovalfill
|
|
549
|
+
'[litecanvas] ovalfill() 3rd param must be a positive number or zero'
|
|
528
550
|
)
|
|
529
551
|
DEV: assert(
|
|
530
552
|
isNumber(radiusY) && radiusY >= 0,
|
|
531
|
-
'ovalfill
|
|
553
|
+
'[litecanvas] ovalfill() 4th param must be a positive number or zero'
|
|
532
554
|
)
|
|
533
555
|
DEV: assert(
|
|
534
556
|
null == color || (isNumber(color) && color >= 0),
|
|
535
|
-
'ovalfill
|
|
557
|
+
'[litecanvas] ovalfill() 5th param must be a positive number or zero'
|
|
536
558
|
)
|
|
537
559
|
|
|
538
560
|
_ctx.beginPath()
|
|
@@ -550,13 +572,19 @@ export default function litecanvas(settings = {}) {
|
|
|
550
572
|
* @param {number} [color=0] the color index
|
|
551
573
|
*/
|
|
552
574
|
line(x1, y1, x2, y2, color) {
|
|
553
|
-
DEV: assert(isNumber(x1), 'line
|
|
554
|
-
DEV: assert(isNumber(y1), 'line
|
|
555
|
-
DEV: assert(
|
|
556
|
-
|
|
575
|
+
DEV: assert(isNumber(x1), '[litecanvas] line() 1st param must be a number')
|
|
576
|
+
DEV: assert(isNumber(y1), '[litecanvas] line() 2nd param must be a number')
|
|
577
|
+
DEV: assert(
|
|
578
|
+
isNumber(x2),
|
|
579
|
+
'[litecanvas] line() 3rd param must be a positive number or zero'
|
|
580
|
+
)
|
|
581
|
+
DEV: assert(
|
|
582
|
+
isNumber(y2),
|
|
583
|
+
'[litecanvas] line() 4th param must be a positive number or zero'
|
|
584
|
+
)
|
|
557
585
|
DEV: assert(
|
|
558
586
|
null == color || (isNumber(color) && color >= 0),
|
|
559
|
-
'line
|
|
587
|
+
'[litecanvas] line() 5th param must be a positive number or zero'
|
|
560
588
|
)
|
|
561
589
|
|
|
562
590
|
_ctx.beginPath()
|
|
@@ -579,7 +607,7 @@ export default function litecanvas(settings = {}) {
|
|
|
579
607
|
linewidth(value) {
|
|
580
608
|
DEV: assert(
|
|
581
609
|
isNumber(value) && ~~value > 0,
|
|
582
|
-
'linewidth
|
|
610
|
+
'[litecanvas] linewidth() 1st param must be a positive number'
|
|
583
611
|
)
|
|
584
612
|
|
|
585
613
|
_ctx.lineWidth = ~~value
|
|
@@ -597,9 +625,9 @@ export default function litecanvas(settings = {}) {
|
|
|
597
625
|
linedash(segments, offset = 0) {
|
|
598
626
|
DEV: assert(
|
|
599
627
|
Array.isArray(segments) && segments.length > 0,
|
|
600
|
-
'linedash
|
|
628
|
+
'[litecanvas] linedash() 1st param must be an array of numbers'
|
|
601
629
|
)
|
|
602
|
-
DEV: assert(isNumber(offset), 'linedash
|
|
630
|
+
DEV: assert(isNumber(offset), '[litecanvas] linedash() 2nd param must be a number')
|
|
603
631
|
|
|
604
632
|
_ctx.setLineDash(segments)
|
|
605
633
|
_ctx.lineDashOffset = offset
|
|
@@ -616,13 +644,16 @@ export default function litecanvas(settings = {}) {
|
|
|
616
644
|
* @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
|
|
617
645
|
*/
|
|
618
646
|
text(x, y, message, color = 3, fontStyle = 'normal') {
|
|
619
|
-
DEV: assert(isNumber(x), 'text
|
|
620
|
-
DEV: assert(isNumber(y), 'text
|
|
647
|
+
DEV: assert(isNumber(x), '[litecanvas] text() 1st param must be a number')
|
|
648
|
+
DEV: assert(isNumber(y), '[litecanvas] text() 2nd param must be a number')
|
|
621
649
|
DEV: assert(
|
|
622
650
|
null == color || (isNumber(color) && color >= 0),
|
|
623
|
-
'text
|
|
651
|
+
'[litecanvas] text() 4th param must be a positive number or zero'
|
|
652
|
+
)
|
|
653
|
+
DEV: assert(
|
|
654
|
+
'string' === typeof fontStyle,
|
|
655
|
+
'[litecanvas] text() 5th param must be a string'
|
|
624
656
|
)
|
|
625
|
-
DEV: assert('string' === typeof fontStyle, 'text: 5th param must be a string')
|
|
626
657
|
|
|
627
658
|
_ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`
|
|
628
659
|
_ctx.fillStyle = _colors[~~color % _colors.length]
|
|
@@ -635,7 +666,10 @@ export default function litecanvas(settings = {}) {
|
|
|
635
666
|
* @param {string} family
|
|
636
667
|
*/
|
|
637
668
|
textfont(family) {
|
|
638
|
-
DEV: assert(
|
|
669
|
+
DEV: assert(
|
|
670
|
+
'string' === typeof family,
|
|
671
|
+
'[litecanvas] textfont() 1st param must be a string'
|
|
672
|
+
)
|
|
639
673
|
|
|
640
674
|
_fontFamily = family
|
|
641
675
|
},
|
|
@@ -646,7 +680,7 @@ export default function litecanvas(settings = {}) {
|
|
|
646
680
|
* @param {number} size
|
|
647
681
|
*/
|
|
648
682
|
textsize(size) {
|
|
649
|
-
DEV: assert(isNumber(size), 'textsize
|
|
683
|
+
DEV: assert(isNumber(size), '[litecanvas] textsize() 1st param must be a number')
|
|
650
684
|
|
|
651
685
|
_fontSize = size
|
|
652
686
|
},
|
|
@@ -662,14 +696,14 @@ export default function litecanvas(settings = {}) {
|
|
|
662
696
|
textalign(align, baseline) {
|
|
663
697
|
DEV: assert(
|
|
664
698
|
null == align || ['left', 'right', 'center', 'start', 'end'].includes(align),
|
|
665
|
-
'textalign
|
|
699
|
+
'[litecanvas] textalign() 1st param must be null or one of the following strings: center, left, right, start or end.'
|
|
666
700
|
)
|
|
667
701
|
DEV: assert(
|
|
668
702
|
null == baseline ||
|
|
669
703
|
['top', 'bottom', 'middle', 'hanging', 'alphabetic', 'ideographic'].includes(
|
|
670
704
|
baseline
|
|
671
705
|
),
|
|
672
|
-
'textalign
|
|
706
|
+
'[litecanvas] textalign() 2nd param must be null or one of the following strings: middle, top, bottom, hanging, alphabetic or ideographic.'
|
|
673
707
|
)
|
|
674
708
|
|
|
675
709
|
if (align) _ctx.textAlign = align
|
|
@@ -685,8 +719,8 @@ export default function litecanvas(settings = {}) {
|
|
|
685
719
|
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
686
720
|
*/
|
|
687
721
|
image(x, y, source) {
|
|
688
|
-
DEV: assert(isNumber(x), 'image
|
|
689
|
-
DEV: assert(isNumber(y), 'image
|
|
722
|
+
DEV: assert(isNumber(x), '[litecanvas] image() 1st param must be a number')
|
|
723
|
+
DEV: assert(isNumber(y), '[litecanvas] image() 2nd param must be a number')
|
|
690
724
|
|
|
691
725
|
_ctx.drawImage(source, ~~x, ~~y)
|
|
692
726
|
},
|
|
@@ -704,22 +738,25 @@ export default function litecanvas(settings = {}) {
|
|
|
704
738
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
705
739
|
*/
|
|
706
740
|
paint(width, height, drawing, options = {}) {
|
|
707
|
-
DEV: assert(
|
|
741
|
+
DEV: assert(
|
|
742
|
+
isNumber(width) && width >= 1,
|
|
743
|
+
'[litecanvas] paint() 1st param must be a positive number'
|
|
744
|
+
)
|
|
708
745
|
DEV: assert(
|
|
709
746
|
isNumber(height) && height >= 1,
|
|
710
|
-
'paint
|
|
747
|
+
'[litecanvas] paint() 2nd param must be a positive number'
|
|
711
748
|
)
|
|
712
749
|
DEV: assert(
|
|
713
750
|
'function' === typeof drawing || Array.isArray(drawing),
|
|
714
|
-
'paint
|
|
751
|
+
'[litecanvas] paint() 3rd param must be a function or array'
|
|
715
752
|
)
|
|
716
753
|
DEV: assert(
|
|
717
754
|
(options && null == options.scale) || isNumber(options.scale),
|
|
718
|
-
'paint
|
|
755
|
+
'[litecanvas] paint() 4th param (options.scale) must be a number'
|
|
719
756
|
)
|
|
720
757
|
DEV: assert(
|
|
721
758
|
(options && null == options.canvas) || options.canvas instanceof OffscreenCanvas,
|
|
722
|
-
'paint
|
|
759
|
+
'[litecanvas] paint() 4th param (options.canvas) must be an OffscreenCanvas'
|
|
723
760
|
)
|
|
724
761
|
|
|
725
762
|
const /** @type {OffscreenCanvas} */
|
|
@@ -796,8 +833,8 @@ export default function litecanvas(settings = {}) {
|
|
|
796
833
|
* @param {number} y
|
|
797
834
|
*/
|
|
798
835
|
translate: (x, y) => {
|
|
799
|
-
DEV: assert(isNumber(x), 'translate
|
|
800
|
-
DEV: assert(isNumber(y), 'translate
|
|
836
|
+
DEV: assert(isNumber(x), '[litecanvas] translate() 1st param must be a number')
|
|
837
|
+
DEV: assert(isNumber(y), '[litecanvas] translate() 2nd param must be a number')
|
|
801
838
|
|
|
802
839
|
return _ctx.translate(~~x, ~~y)
|
|
803
840
|
},
|
|
@@ -809,8 +846,8 @@ export default function litecanvas(settings = {}) {
|
|
|
809
846
|
* @param {number} [y]
|
|
810
847
|
*/
|
|
811
848
|
scale: (x, y) => {
|
|
812
|
-
DEV: assert(isNumber(x), 'scale
|
|
813
|
-
DEV: assert(null == y || isNumber(y), 'scale
|
|
849
|
+
DEV: assert(isNumber(x), '[litecanvas] scale() 1st param must be a number')
|
|
850
|
+
DEV: assert(null == y || isNumber(y), '[litecanvas] scale() 2nd param must be a number')
|
|
814
851
|
|
|
815
852
|
return _ctx.scale(x, y || x)
|
|
816
853
|
},
|
|
@@ -821,7 +858,7 @@ export default function litecanvas(settings = {}) {
|
|
|
821
858
|
* @param {number} radians
|
|
822
859
|
*/
|
|
823
860
|
rotate: (radians) => {
|
|
824
|
-
DEV: assert(isNumber(radians), 'rotate
|
|
861
|
+
DEV: assert(isNumber(radians), '[litecanvas] rotate() 1st param must be a number')
|
|
825
862
|
|
|
826
863
|
return _ctx.rotate(radians)
|
|
827
864
|
},
|
|
@@ -833,7 +870,7 @@ export default function litecanvas(settings = {}) {
|
|
|
833
870
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
834
871
|
*/
|
|
835
872
|
alpha(value) {
|
|
836
|
-
DEV: assert(isNumber(value), 'alpha
|
|
873
|
+
DEV: assert(isNumber(value), '[litecanvas] alpha() 1st param must be a number')
|
|
837
874
|
|
|
838
875
|
_ctx.globalAlpha = instance.clamp(value, 0, 1)
|
|
839
876
|
},
|
|
@@ -850,7 +887,7 @@ export default function litecanvas(settings = {}) {
|
|
|
850
887
|
path: (arg) => {
|
|
851
888
|
DEV: assert(
|
|
852
889
|
null == arg || 'string' === typeof arg || arg instanceof Path2D,
|
|
853
|
-
'path
|
|
890
|
+
'[litecanvas] path() 1st param must be a string or a Path2D instance'
|
|
854
891
|
)
|
|
855
892
|
|
|
856
893
|
return new Path2D(arg)
|
|
@@ -865,11 +902,11 @@ export default function litecanvas(settings = {}) {
|
|
|
865
902
|
fill(color, path) {
|
|
866
903
|
DEV: assert(
|
|
867
904
|
null == color || (isNumber(color) && color >= 0),
|
|
868
|
-
'fill
|
|
905
|
+
'[litecanvas] fill() 1st param must be a positive number or zero'
|
|
869
906
|
)
|
|
870
907
|
DEV: assert(
|
|
871
908
|
null == path || path instanceof Path2D,
|
|
872
|
-
'fill
|
|
909
|
+
'[litecanvas] fill() 2nd param must be a Path2D instance'
|
|
873
910
|
)
|
|
874
911
|
|
|
875
912
|
_ctx.fillStyle = _colors[~~color % _colors.length]
|
|
@@ -889,11 +926,11 @@ export default function litecanvas(settings = {}) {
|
|
|
889
926
|
stroke(color, path) {
|
|
890
927
|
DEV: assert(
|
|
891
928
|
null == color || (isNumber(color) && color >= 0),
|
|
892
|
-
'stroke
|
|
929
|
+
'[litecanvas] stroke() 1st param must be a positive number or zero'
|
|
893
930
|
)
|
|
894
931
|
DEV: assert(
|
|
895
932
|
null == path || path instanceof Path2D,
|
|
896
|
-
'stroke
|
|
933
|
+
'[litecanvas] stroke() 2nd param must be a Path2D instance'
|
|
897
934
|
)
|
|
898
935
|
|
|
899
936
|
_ctx.strokeStyle = _colors[~~color % _colors.length]
|
|
@@ -913,7 +950,10 @@ export default function litecanvas(settings = {}) {
|
|
|
913
950
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
|
|
914
951
|
*/
|
|
915
952
|
clip(path) {
|
|
916
|
-
DEV: assert(
|
|
953
|
+
DEV: assert(
|
|
954
|
+
path instanceof Path2D,
|
|
955
|
+
'[litecanvas] clip() 1st param must be a Path2D instance'
|
|
956
|
+
)
|
|
917
957
|
|
|
918
958
|
_ctx.clip(path)
|
|
919
959
|
},
|
|
@@ -933,10 +973,10 @@ export default function litecanvas(settings = {}) {
|
|
|
933
973
|
sfx(zzfxParams, pitchSlide = 0, volumeFactor = 1) {
|
|
934
974
|
DEV: assert(
|
|
935
975
|
null == zzfxParams || Array.isArray(zzfxParams),
|
|
936
|
-
'sfx
|
|
976
|
+
'[litecanvas] sfx() 1st param must be an array'
|
|
937
977
|
)
|
|
938
|
-
DEV: assert(isNumber(pitchSlide), 'sfx
|
|
939
|
-
DEV: assert(isNumber(volumeFactor), 'sfx
|
|
978
|
+
DEV: assert(isNumber(pitchSlide), '[litecanvas] sfx() 2nd param must be a number')
|
|
979
|
+
DEV: assert(isNumber(volumeFactor), '[litecanvas] sfx() 3rd param must be a number')
|
|
940
980
|
|
|
941
981
|
if (
|
|
942
982
|
// @ts-ignore
|
|
@@ -967,7 +1007,7 @@ export default function litecanvas(settings = {}) {
|
|
|
967
1007
|
* @param {number} value
|
|
968
1008
|
*/
|
|
969
1009
|
volume(value) {
|
|
970
|
-
DEV: assert(isNumber(value), 'volume
|
|
1010
|
+
DEV: assert(isNumber(value), '[litecanvas] volume() 1st param must be a number')
|
|
971
1011
|
|
|
972
1012
|
// @ts-ignore
|
|
973
1013
|
root.zzfxV = value
|
|
@@ -986,8 +1026,14 @@ export default function litecanvas(settings = {}) {
|
|
|
986
1026
|
* @param {pluginCallback} callback
|
|
987
1027
|
*/
|
|
988
1028
|
use(callback, config = {}) {
|
|
989
|
-
DEV: assert(
|
|
990
|
-
|
|
1029
|
+
DEV: assert(
|
|
1030
|
+
'function' === typeof callback,
|
|
1031
|
+
'[litecanvas] use() 1st param must be a function'
|
|
1032
|
+
)
|
|
1033
|
+
DEV: assert(
|
|
1034
|
+
'object' === typeof config,
|
|
1035
|
+
'[litecanvas] use() 2nd param must be an object'
|
|
1036
|
+
)
|
|
991
1037
|
|
|
992
1038
|
if (_initialized) {
|
|
993
1039
|
// load the plugin now
|
|
@@ -1006,8 +1052,14 @@ export default function litecanvas(settings = {}) {
|
|
|
1006
1052
|
* @returns {Function} a function to remove the listener
|
|
1007
1053
|
*/
|
|
1008
1054
|
listen(eventName, callback) {
|
|
1009
|
-
DEV: assert(
|
|
1010
|
-
|
|
1055
|
+
DEV: assert(
|
|
1056
|
+
'string' === typeof eventName,
|
|
1057
|
+
'[litecanvas] listen() 1st param must be a string'
|
|
1058
|
+
)
|
|
1059
|
+
DEV: assert(
|
|
1060
|
+
'function' === typeof callback,
|
|
1061
|
+
'[litecanvas] listen() 2nd param must be a function'
|
|
1062
|
+
)
|
|
1011
1063
|
|
|
1012
1064
|
eventName = eventName.toLowerCase()
|
|
1013
1065
|
|
|
@@ -1028,7 +1080,10 @@ export default function litecanvas(settings = {}) {
|
|
|
1028
1080
|
* @param {*} [arg4] any data to be passed over the listeners
|
|
1029
1081
|
*/
|
|
1030
1082
|
emit(eventName, arg1, arg2, arg3, arg4) {
|
|
1031
|
-
DEV: assert(
|
|
1083
|
+
DEV: assert(
|
|
1084
|
+
'string' === typeof eventName,
|
|
1085
|
+
'[litecanvas] emit() 1st param must be a string'
|
|
1086
|
+
)
|
|
1032
1087
|
if (_initialized) {
|
|
1033
1088
|
eventName = eventName.toLowerCase()
|
|
1034
1089
|
|
|
@@ -1046,7 +1101,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1046
1101
|
pal(colors = defaultPalette) {
|
|
1047
1102
|
DEV: assert(
|
|
1048
1103
|
Array.isArray(colors) && colors.length > 0,
|
|
1049
|
-
'pal
|
|
1104
|
+
'[litecanvas] pal() 1st param must be a array of strings'
|
|
1050
1105
|
)
|
|
1051
1106
|
_colors = colors
|
|
1052
1107
|
},
|
|
@@ -1058,7 +1113,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1058
1113
|
* @param {*} value
|
|
1059
1114
|
*/
|
|
1060
1115
|
def(key, value) {
|
|
1061
|
-
DEV: assert('string' === typeof key, 'def
|
|
1116
|
+
DEV: assert('string' === typeof key, '[litecanvas] def() 1st param must be a string')
|
|
1062
1117
|
DEV: if (null == value) {
|
|
1063
1118
|
console.warn(`def: key "${key}" was defined as ${value} but now is null`)
|
|
1064
1119
|
}
|
|
@@ -1079,7 +1134,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1079
1134
|
timescale(value) {
|
|
1080
1135
|
DEV: assert(
|
|
1081
1136
|
isNumber(value) && value >= 0,
|
|
1082
|
-
'timescale
|
|
1137
|
+
'[litecanvas] timescale() 1st param must be a positive number or zero'
|
|
1083
1138
|
)
|
|
1084
1139
|
|
|
1085
1140
|
_timeScale = value
|
|
@@ -1093,7 +1148,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1093
1148
|
framerate(value) {
|
|
1094
1149
|
DEV: assert(
|
|
1095
1150
|
isNumber(value) && value >= 1,
|
|
1096
|
-
'framerate
|
|
1151
|
+
'[litecanvas] framerate() 1st param must be a positive number'
|
|
1097
1152
|
)
|
|
1098
1153
|
|
|
1099
1154
|
_deltaTime = 1 / ~~value
|
|
@@ -1106,7 +1161,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1106
1161
|
* @returns {any}
|
|
1107
1162
|
*/
|
|
1108
1163
|
stat(n) {
|
|
1109
|
-
DEV: assert(isNumber(n) && n >= 0, 'stat
|
|
1164
|
+
DEV: assert(isNumber(n) && n >= 0, '[litecanvas] stat() 1st param must be a number')
|
|
1110
1165
|
|
|
1111
1166
|
const list = [
|
|
1112
1167
|
// 0
|
|
@@ -1188,6 +1243,9 @@ export default function litecanvas(settings = {}) {
|
|
|
1188
1243
|
// setup default event listeners
|
|
1189
1244
|
const source = settings.loop ? settings.loop : root
|
|
1190
1245
|
for (const event of _coreEvents.split(',')) {
|
|
1246
|
+
DEV: if (root === source && source[event]) {
|
|
1247
|
+
console.info(`[litecanvas] using window.${event}()`)
|
|
1248
|
+
}
|
|
1191
1249
|
if (source[event]) instance.listen(event, source[event])
|
|
1192
1250
|
}
|
|
1193
1251
|
|
|
@@ -1423,7 +1481,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1423
1481
|
(key) => {
|
|
1424
1482
|
DEV: assert(
|
|
1425
1483
|
null == key || 'string' === typeof key,
|
|
1426
|
-
'iskeydown
|
|
1484
|
+
'[litecanvas] iskeydown() 1st param must be a string or undefined'
|
|
1427
1485
|
)
|
|
1428
1486
|
return keyCheck(_keysDown, key)
|
|
1429
1487
|
}
|
|
@@ -1441,7 +1499,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1441
1499
|
(key) => {
|
|
1442
1500
|
DEV: assert(
|
|
1443
1501
|
null == key || 'string' === typeof key,
|
|
1444
|
-
'iskeypressed
|
|
1502
|
+
'[litecanvas] iskeypressed() 1st param must be a string or undefined'
|
|
1445
1503
|
)
|
|
1446
1504
|
return keyCheck(_keysPress, key)
|
|
1447
1505
|
}
|
|
@@ -1498,7 +1556,10 @@ export default function litecanvas(settings = {}) {
|
|
|
1498
1556
|
function setupCanvas() {
|
|
1499
1557
|
if ('string' === typeof settings.canvas) {
|
|
1500
1558
|
_canvas = document.querySelector(settings.canvas)
|
|
1501
|
-
DEV: assert(
|
|
1559
|
+
DEV: assert(
|
|
1560
|
+
null != _canvas,
|
|
1561
|
+
'[litecanvas] litecanvas() option "canvas" is an invalid CSS selector'
|
|
1562
|
+
)
|
|
1502
1563
|
} else {
|
|
1503
1564
|
_canvas = settings.canvas
|
|
1504
1565
|
}
|
|
@@ -1507,7 +1568,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1507
1568
|
|
|
1508
1569
|
DEV: assert(
|
|
1509
1570
|
'CANVAS' === _canvas.tagName,
|
|
1510
|
-
'
|
|
1571
|
+
'[litecanvas] litecanvas() option "canvas" should be a canvas element or string (CSS selector)'
|
|
1511
1572
|
)
|
|
1512
1573
|
|
|
1513
1574
|
_ctx = _canvas.getContext('2d')
|
|
@@ -1527,15 +1588,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1527
1588
|
function resizeCanvas() {
|
|
1528
1589
|
DEV: assert(
|
|
1529
1590
|
null == settings.width || (isNumber(settings.width) && settings.width > 0),
|
|
1530
|
-
'
|
|
1591
|
+
'[litecanvas] litecanvas() option "width" should be a positive number when defined'
|
|
1531
1592
|
)
|
|
1532
1593
|
DEV: assert(
|
|
1533
1594
|
null == settings.height || (isNumber(settings.height) && settings.height > 0),
|
|
1534
|
-
'
|
|
1595
|
+
'[litecanvas] litecanvas() option "height" should be a positive number when defined'
|
|
1535
1596
|
)
|
|
1536
1597
|
DEV: assert(
|
|
1537
1598
|
null == settings.height || (settings.width > 0 && settings.height > 0),
|
|
1538
|
-
'
|
|
1599
|
+
'[litecanvas] litecanvas() option "width" is required when the option "height" is defined'
|
|
1539
1600
|
)
|
|
1540
1601
|
|
|
1541
1602
|
const width = settings.width || root.innerWidth,
|
|
@@ -1601,7 +1662,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1601
1662
|
|
|
1602
1663
|
DEV: assert(
|
|
1603
1664
|
null == pluginData || 'object' === typeof pluginData,
|
|
1604
|
-
'
|
|
1665
|
+
'[litecanvas] litecanvas() plugins should return an object or nothing'
|
|
1605
1666
|
)
|
|
1606
1667
|
|
|
1607
1668
|
for (const key in pluginData) {
|
|
@@ -1612,13 +1673,16 @@ export default function litecanvas(settings = {}) {
|
|
|
1612
1673
|
if (settings.global) {
|
|
1613
1674
|
// @ts-ignore
|
|
1614
1675
|
if (root.ENGINE) {
|
|
1615
|
-
throw new Error('
|
|
1676
|
+
throw new Error('only one global litecanvas is allowed')
|
|
1616
1677
|
}
|
|
1617
1678
|
Object.assign(root, instance)
|
|
1618
1679
|
// @ts-ignore
|
|
1619
1680
|
root.ENGINE = instance
|
|
1620
1681
|
}
|
|
1621
1682
|
|
|
1683
|
+
DEV: console.info(`[litecanvas] version ${version} started`)
|
|
1684
|
+
DEV: console.debug(`[litecanvas] litecanvas() options =`, settings)
|
|
1685
|
+
|
|
1622
1686
|
setupCanvas()
|
|
1623
1687
|
|
|
1624
1688
|
if ('loading' === document.readyState) {
|