litecanvas 0.74.3 → 0.75.1

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/src/index.js CHANGED
@@ -134,14 +134,6 @@ export default function litecanvas(settings = {}) {
134
134
  DEFAULT_SFX: [0.5, , 1675, , 0.06, 0.2, 1, 1.8, , , 637, 0.06],
135
135
 
136
136
  /** MATH API */
137
- /**
138
- * The value of the mathematical constant PI (π).
139
- * Approximately 3.14159
140
- *
141
- * @type {number}
142
- */
143
- PI,
144
-
145
137
  /**
146
138
  * Twice the value of the mathematical constant PI (π).
147
139
  * Approximately 6.28318
@@ -159,7 +151,7 @@ export default function litecanvas(settings = {}) {
159
151
  *
160
152
  * @type {number}
161
153
  */
162
- HALF_PI: PI * 0.5,
154
+ HALF_PI: PI / 2,
163
155
 
164
156
  /**
165
157
  * Calculates a linear (interpolation) value over t%.
@@ -171,12 +163,11 @@ export default function litecanvas(settings = {}) {
171
163
  * @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
172
164
  */
173
165
  lerp: (start, end, t) => {
174
- if (DEV_BUILD) {
175
- assert(isFinite(start), 'lerp: 1st param must be a number')
176
- assert(isFinite(end), 'lerp: 2nd param must be a number')
177
- assert(isFinite(t), 'lerp: 3rd param must be a number')
178
- }
179
- return start + t * (end - start)
166
+ DEV: assert(isFinite(start), 'lerp: 1st param must be a number')
167
+ DEV: assert(isFinite(end), 'lerp: 2nd param must be a number')
168
+ DEV: assert(isFinite(t), 'lerp: 3rd param must be a number')
169
+
170
+ return t * (end - start) + start
180
171
  },
181
172
 
182
173
  /**
@@ -186,9 +177,8 @@ export default function litecanvas(settings = {}) {
186
177
  * @returns {number} the value in radians
187
178
  */
188
179
  deg2rad: (degs) => {
189
- if (DEV_BUILD) {
190
- assert(isFinite(degs), 'deg2rad: 1st param must be a number')
191
- }
180
+ DEV: assert(isFinite(degs), 'deg2rad: 1st param must be a number')
181
+
192
182
  return (PI / 180) * degs
193
183
  },
194
184
 
@@ -199,9 +189,8 @@ export default function litecanvas(settings = {}) {
199
189
  * @returns {number} the value in degrees
200
190
  */
201
191
  rad2deg: (rads) => {
202
- if (DEV_BUILD) {
203
- assert(isFinite(rads), 'rad2deg: 1st param must be a number')
204
- }
192
+ DEV: assert(isFinite(rads), 'rad2deg: 1st param must be a number')
193
+
205
194
  return (180 / PI) * rads
206
195
  },
207
196
 
@@ -214,15 +203,14 @@ export default function litecanvas(settings = {}) {
214
203
  * @returns {number}
215
204
  */
216
205
  clamp: (value, min, max) => {
217
- if (DEV_BUILD) {
218
- assert(isFinite(value), 'clamp: 1st param must be a number')
219
- assert(isFinite(min), 'clamp: 2nd param must be a number')
220
- assert(isFinite(max), 'clamp: 3rd param must be a number')
221
- assert(
222
- max > min,
223
- 'randi: the 2nd param must be less than the 3rd param'
224
- )
225
- }
206
+ DEV: assert(isFinite(value), 'clamp: 1st param must be a number')
207
+ DEV: assert(isFinite(min), 'clamp: 2nd param must be a number')
208
+ DEV: assert(isFinite(max), 'clamp: 3rd param must be a number')
209
+ DEV: assert(
210
+ max > min,
211
+ 'randi: the 2nd param must be less than the 3rd param'
212
+ )
213
+
226
214
  if (value < min) return min
227
215
  if (value > max) return max
228
216
  return value
@@ -237,19 +225,18 @@ export default function litecanvas(settings = {}) {
237
225
  * @returns {number}
238
226
  */
239
227
  wrap: (value, min, max) => {
240
- if (DEV_BUILD) {
241
- assert(isFinite(value), 'wrap: 1st param must be a number')
242
- assert(isFinite(min), 'wrap: 2nd param must be a number')
243
- assert(isFinite(max), 'wrap: 3rd param must be a number')
244
- assert(
245
- max > min,
246
- 'randi: the 2nd param must be less than the 3rd param'
247
- )
248
- assert(
249
- max !== min,
250
- 'randi: the 2nd param must be not equal to the 3rd param'
251
- )
252
- }
228
+ DEV: assert(isFinite(value), 'wrap: 1st param must be a number')
229
+ DEV: assert(isFinite(min), 'wrap: 2nd param must be a number')
230
+ DEV: assert(isFinite(max), 'wrap: 3rd param must be a number')
231
+ DEV: assert(
232
+ max > min,
233
+ 'randi: the 2nd param must be less than the 3rd param'
234
+ )
235
+ DEV: assert(
236
+ max !== min,
237
+ 'randi: the 2nd param must be not equal to the 3rd param'
238
+ )
239
+
253
240
  return value - (max - min) * Math.floor((value - min) / (max - min))
254
241
  },
255
242
 
@@ -265,13 +252,12 @@ export default function litecanvas(settings = {}) {
265
252
  * @returns {number} the remapped number
266
253
  */
267
254
  map(value, start1, stop1, start2, stop2, withinBounds) {
268
- if (DEV_BUILD) {
269
- assert(isFinite(value), 'map: 1st param must be a number')
270
- assert(isFinite(start1), 'map: 2nd param must be a number')
271
- assert(isFinite(stop1), 'map: 3rd param must be a number')
272
- assert(isFinite(start2), 'map: 4th param must be a number')
273
- assert(isFinite(stop2), 'map: 5th param must be a number')
274
- }
255
+ DEV: assert(isFinite(value), 'map: 1st param must be a number')
256
+ DEV: assert(isFinite(start1), 'map: 2nd param must be a number')
257
+ DEV: assert(isFinite(stop1), 'map: 3rd param must be a number')
258
+ DEV: assert(isFinite(start2), 'map: 4th param must be a number')
259
+ DEV: assert(isFinite(stop2), 'map: 5th param must be a number')
260
+
275
261
  // prettier-ignore
276
262
  const result = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
277
263
  return withinBounds ? instance.clamp(result, start2, stop2) : result
@@ -288,11 +274,10 @@ export default function litecanvas(settings = {}) {
288
274
  * @returns {number} the normalized number.
289
275
  */
290
276
  norm: (value, start, stop) => {
291
- if (DEV_BUILD) {
292
- assert(isFinite(value), 'norm: 1st param must be a number')
293
- assert(isFinite(start), 'norm: 2nd param must be a number')
294
- assert(isFinite(stop), 'norm: 3rd param must be a number')
295
- }
277
+ DEV: assert(isFinite(value), 'norm: 1st param must be a number')
278
+ DEV: assert(isFinite(start), 'norm: 2nd param must be a number')
279
+ DEV: assert(isFinite(stop), 'norm: 3rd param must be a number')
280
+
296
281
  return instance.map(value, start, stop, 0, 1)
297
282
  },
298
283
 
@@ -306,14 +291,13 @@ export default function litecanvas(settings = {}) {
306
291
  * @returns {number} the random number
307
292
  */
308
293
  rand: (min = 0.0, max = 1.0) => {
309
- if (DEV_BUILD) {
310
- assert(isFinite(min), 'rand: 1st param must be a number')
311
- assert(isFinite(max), 'rand: 2nd param must be a number')
312
- assert(
313
- max > min,
314
- 'rand: the 1st param must be less than the 2nd param'
315
- )
316
- }
294
+ DEV: assert(isFinite(min), 'rand: 1st param must be a number')
295
+ DEV: assert(isFinite(max), 'rand: 2nd param must be a number')
296
+ DEV: assert(
297
+ max > min,
298
+ 'rand: the 1st param must be less than the 2nd param'
299
+ )
300
+
317
301
  const a = 1664525
318
302
  const c = 1013904223
319
303
  const m = 4294967296
@@ -331,14 +315,13 @@ export default function litecanvas(settings = {}) {
331
315
  * @returns {number} the random number
332
316
  */
333
317
  randi: (min = 0, max = 1) => {
334
- if (DEV_BUILD) {
335
- assert(isFinite(min), 'randi: 1st param must be a number')
336
- assert(isFinite(max), 'randi: 2nd param must be a number')
337
- assert(
338
- max > min,
339
- 'randi: the 1st param must be less than the 2nd param'
340
- )
341
- }
318
+ DEV: assert(isFinite(min), 'randi: 1st param must be a number')
319
+ DEV: assert(isFinite(max), 'randi: 2nd param must be a number')
320
+ DEV: assert(
321
+ max > min,
322
+ 'randi: the 1st param must be less than the 2nd param'
323
+ )
324
+
342
325
  return Math.floor(instance.rand(min, max + 1))
343
326
  },
344
327
 
@@ -350,12 +333,11 @@ export default function litecanvas(settings = {}) {
350
333
  * @returns {number} the seed state
351
334
  */
352
335
  seed: (value) => {
353
- if (DEV_BUILD) {
354
- assert(
355
- null == value || (isFinite(value) && value >= 0),
356
- 'seed: 1st param must be a positive number or zero'
357
- )
358
- }
336
+ DEV: assert(
337
+ null == value || (isFinite(value) && value >= 0),
338
+ 'seed: 1st param must be a positive number or zero'
339
+ )
340
+
359
341
  return null == value ? _rng_seed : (_rng_seed = ~~value)
360
342
  },
361
343
 
@@ -366,12 +348,11 @@ export default function litecanvas(settings = {}) {
366
348
  * @param {number?} color The background color (index) or null (for transparent)
367
349
  */
368
350
  cls(color) {
369
- if (DEV_BUILD) {
370
- assert(
371
- null == color || (isFinite(color) && color >= 0),
372
- 'cls: 1st param must be a positive number or zero or null'
373
- )
374
- }
351
+ DEV: assert(
352
+ null == color || (isFinite(color) && color >= 0),
353
+ 'cls: 1st param must be a positive number or zero or null'
354
+ )
355
+
375
356
  if (null == color) {
376
357
  _ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height)
377
358
  } else {
@@ -396,28 +377,27 @@ export default function litecanvas(settings = {}) {
396
377
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
397
378
  */
398
379
  rect(x, y, width, height, color, radii = null) {
399
- if (DEV_BUILD) {
400
- assert(isFinite(x), 'rect: 1st param must be a number')
401
- assert(isFinite(y), 'rect: 2nd param must be a number')
402
- assert(
403
- isFinite(width) && width > 0,
404
- 'rect: 3rd param must be a positive number'
405
- )
406
- assert(
407
- isFinite(height) && height >= 0,
408
- 'rect: 4th param must be a positive number or zero'
409
- )
410
- assert(
411
- null == color || (isFinite(color) && color >= 0),
412
- 'rect: 5th param must be a positive number or zero'
413
- )
414
- assert(
415
- null == radii ||
416
- isFinite(radii) ||
417
- (Array.isArray(radii) && radii.length >= 1),
418
- 'rect: 6th param must be a number or array of numbers'
419
- )
420
- }
380
+ DEV: assert(isFinite(x), 'rect: 1st param must be a number')
381
+ DEV: assert(isFinite(y), 'rect: 2nd param must be a number')
382
+ DEV: assert(
383
+ isFinite(width) && width > 0,
384
+ 'rect: 3rd param must be a positive number'
385
+ )
386
+ DEV: assert(
387
+ isFinite(height) && height >= 0,
388
+ 'rect: 4th param must be a positive number or zero'
389
+ )
390
+ DEV: assert(
391
+ null == color || (isFinite(color) && color >= 0),
392
+ 'rect: 5th param must be a positive number or zero'
393
+ )
394
+ DEV: assert(
395
+ null == radii ||
396
+ isFinite(radii) ||
397
+ (Array.isArray(radii) && radii.length >= 1),
398
+ 'rect: 6th param must be a number or array of numbers'
399
+ )
400
+
421
401
  _ctx.beginPath()
422
402
  _ctx[radii ? 'roundRect' : 'rect'](
423
403
  ~~x - _outline_fix,
@@ -440,28 +420,27 @@ export default function litecanvas(settings = {}) {
440
420
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
441
421
  */
442
422
  rectfill(x, y, width, height, color, radii = null) {
443
- if (DEV_BUILD) {
444
- assert(isFinite(x), 'rectfill: 1st param must be a number')
445
- assert(isFinite(y), 'rectfill: 2nd param must be a number')
446
- assert(
447
- isFinite(width) && width >= 0,
448
- 'rectfill: 3rd param must be a positive number or zero'
449
- )
450
- assert(
451
- isFinite(height) && height >= 0,
452
- 'rectfill: 4th param must be a positive number or zero'
453
- )
454
- assert(
455
- null == color || (isFinite(color) && color >= 0),
456
- 'rectfill: 5th param must be a positive number or zero'
457
- )
458
- assert(
459
- null == radii ||
460
- isFinite(radii) ||
461
- (Array.isArray(radii) && radii.length >= 1),
462
- 'rectfill: 6th param must be a number or array of at least 2 numbers'
463
- )
464
- }
423
+ DEV: assert(isFinite(x), 'rectfill: 1st param must be a number')
424
+ DEV: assert(isFinite(y), 'rectfill: 2nd param must be a number')
425
+ DEV: assert(
426
+ isFinite(width) && width >= 0,
427
+ 'rectfill: 3rd param must be a positive number or zero'
428
+ )
429
+ DEV: assert(
430
+ isFinite(height) && height >= 0,
431
+ 'rectfill: 4th param must be a positive number or zero'
432
+ )
433
+ DEV: assert(
434
+ null == color || (isFinite(color) && color >= 0),
435
+ 'rectfill: 5th param must be a positive number or zero'
436
+ )
437
+ DEV: assert(
438
+ null == radii ||
439
+ isFinite(radii) ||
440
+ (Array.isArray(radii) && radii.length >= 1),
441
+ 'rectfill: 6th param must be a number or array of at least 2 numbers'
442
+ )
443
+
465
444
  _ctx.beginPath()
466
445
  _ctx[radii ? 'roundRect' : 'rect'](
467
446
  ~~x,
@@ -482,18 +461,17 @@ export default function litecanvas(settings = {}) {
482
461
  * @param {number} [color=0] the color index
483
462
  */
484
463
  circ(x, y, radius, color) {
485
- if (DEV_BUILD) {
486
- assert(isFinite(x), 'circ: 1st param must be a number')
487
- assert(isFinite(y), 'circ: 2nd param must be a number')
488
- assert(
489
- isFinite(radius) && radius >= 0,
490
- 'circ: 3rd param must be a positive number or zero'
491
- )
492
- assert(
493
- null == color || (isFinite(color) && color >= 0),
494
- 'circ: 4th param must be a positive number or zero'
495
- )
496
- }
464
+ DEV: assert(isFinite(x), 'circ: 1st param must be a number')
465
+ DEV: assert(isFinite(y), 'circ: 2nd param must be a number')
466
+ DEV: assert(
467
+ isFinite(radius) && radius >= 0,
468
+ 'circ: 3rd param must be a positive number or zero'
469
+ )
470
+ DEV: assert(
471
+ null == color || (isFinite(color) && color >= 0),
472
+ 'circ: 4th param must be a positive number or zero'
473
+ )
474
+
497
475
  _ctx.beginPath()
498
476
  _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
499
477
  instance.stroke(color)
@@ -508,18 +486,17 @@ export default function litecanvas(settings = {}) {
508
486
  * @param {number} [color=0] the color index
509
487
  */
510
488
  circfill(x, y, radius, color) {
511
- if (DEV_BUILD) {
512
- assert(isFinite(x), 'circfill: 1st param must be a number')
513
- assert(isFinite(y), 'circfill: 2nd param must be a number')
514
- assert(
515
- isFinite(radius) && radius >= 0,
516
- 'circfill: 3rd param must be a positive number or zero'
517
- )
518
- assert(
519
- null == color || (isFinite(color) && color >= 0),
520
- 'circfill: 4th param must be a positive number or zero'
521
- )
522
- }
489
+ DEV: assert(isFinite(x), 'circfill: 1st param must be a number')
490
+ DEV: assert(isFinite(y), 'circfill: 2nd param must be a number')
491
+ DEV: assert(
492
+ isFinite(radius) && radius >= 0,
493
+ 'circfill: 3rd param must be a positive number or zero'
494
+ )
495
+ DEV: assert(
496
+ null == color || (isFinite(color) && color >= 0),
497
+ 'circfill: 4th param must be a positive number or zero'
498
+ )
499
+
523
500
  _ctx.beginPath()
524
501
  _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
525
502
  instance.fill(color)
@@ -535,22 +512,21 @@ export default function litecanvas(settings = {}) {
535
512
  * @param {number} [color=0] the color index
536
513
  */
537
514
  line(x1, y1, x2, y2, color) {
538
- if (DEV_BUILD) {
539
- assert(isFinite(x1), 'line: 1st param must be a number')
540
- assert(isFinite(y1), 'line: 2nd param must be a number')
541
- assert(
542
- isFinite(x2),
543
- 'line: 3rd param must be a positive number or zero'
544
- )
545
- assert(
546
- isFinite(y2),
547
- 'line: 4th param must be a positive number or zero'
548
- )
549
- assert(
550
- null == color || (isFinite(color) && color >= 0),
551
- 'line: 5th param must be a positive number or zero'
552
- )
553
- }
515
+ DEV: assert(isFinite(x1), 'line: 1st param must be a number')
516
+ DEV: assert(isFinite(y1), 'line: 2nd param must be a number')
517
+ DEV: assert(
518
+ isFinite(x2),
519
+ 'line: 3rd param must be a positive number or zero'
520
+ )
521
+ DEV: assert(
522
+ isFinite(y2),
523
+ 'line: 4th param must be a positive number or zero'
524
+ )
525
+ DEV: assert(
526
+ null == color || (isFinite(color) && color >= 0),
527
+ 'line: 5th param must be a positive number or zero'
528
+ )
529
+
554
530
  _ctx.beginPath()
555
531
 
556
532
  let xfix = _outline_fix !== 0 && ~~x1 === ~~x2 ? 0.5 : 0
@@ -569,12 +545,11 @@ export default function litecanvas(settings = {}) {
569
545
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
570
546
  */
571
547
  linewidth(value) {
572
- if (DEV_BUILD) {
573
- assert(
574
- isFinite(value) && ~~value > 0,
575
- 'linewidth: 1st param must be a positive number'
576
- )
577
- }
548
+ DEV: assert(
549
+ isFinite(value) && ~~value > 0,
550
+ 'linewidth: 1st param must be a positive number'
551
+ )
552
+
578
553
  _ctx.lineWidth = ~~value
579
554
  _outline_fix = ~~value % 2 === 0 ? 0 : 0.5
580
555
  },
@@ -588,13 +563,15 @@ export default function litecanvas(settings = {}) {
588
563
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
589
564
  */
590
565
  linedash(segments, offset = 0) {
591
- if (DEV_BUILD) {
592
- assert(
593
- Array.isArray(segments) && segments.length > 0,
594
- 'linedash: 1st param must be an array of numbers'
595
- )
596
- assert(isFinite(offset), 'linedash: 2nd param must be a number')
597
- }
566
+ DEV: assert(
567
+ Array.isArray(segments) && segments.length > 0,
568
+ 'linedash: 1st param must be an array of numbers'
569
+ )
570
+ DEV: assert(
571
+ isFinite(offset),
572
+ 'linedash: 2nd param must be a number'
573
+ )
574
+
598
575
  _ctx.setLineDash(segments)
599
576
  _ctx.lineDashOffset = offset
600
577
  },
@@ -610,22 +587,21 @@ export default function litecanvas(settings = {}) {
610
587
  * @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
611
588
  */
612
589
  text(x, y, message, color = 3, fontStyle = 'normal') {
613
- if (DEV_BUILD) {
614
- assert(isFinite(x), 'text: 1st param must be a number')
615
- assert(isFinite(y), 'text: 2nd param must be a number')
616
- // assert(
617
- // 'string' === typeof message,
618
- // 'text: 3rd param must be a string'
619
- // )
620
- assert(
621
- null == color || (isFinite(color) && color >= 0),
622
- 'text: 4th param must be a positive number or zero'
623
- )
624
- assert(
625
- 'string' === typeof fontStyle,
626
- 'text: 5th param must be a string'
627
- )
628
- }
590
+ DEV: assert(isFinite(x), 'text: 1st param must be a number')
591
+ DEV: assert(isFinite(y), 'text: 2nd param must be a number')
592
+ // DEV: assert(
593
+ // 'string' === typeof message,
594
+ // 'text: 3rd param must be a string'
595
+ // )
596
+ DEV: assert(
597
+ null == color || (isFinite(color) && color >= 0),
598
+ 'text: 4th param must be a positive number or zero'
599
+ )
600
+ DEV: assert(
601
+ 'string' === typeof fontStyle,
602
+ 'text: 5th param must be a string'
603
+ )
604
+
629
605
  _ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`
630
606
  _ctx.fillStyle = instance.getcolor(color)
631
607
  _ctx.fillText(message, ~~x, ~~y)
@@ -637,12 +613,11 @@ export default function litecanvas(settings = {}) {
637
613
  * @param {string} family
638
614
  */
639
615
  textfont(family) {
640
- if (DEV_BUILD) {
641
- assert(
642
- 'string' === typeof family,
643
- 'textfont: 1st param must be a string'
644
- )
645
- }
616
+ DEV: assert(
617
+ 'string' === typeof family,
618
+ 'textfont: 1st param must be a string'
619
+ )
620
+
646
621
  _fontFamily = family
647
622
  },
648
623
 
@@ -652,9 +627,8 @@ export default function litecanvas(settings = {}) {
652
627
  * @param {number} size
653
628
  */
654
629
  textsize(size) {
655
- if (DEV_BUILD) {
656
- assert(isFinite(size), 'textsize: 1st param must be a number')
657
- }
630
+ DEV: assert(isFinite(size), 'textsize: 1st param must be a number')
631
+
658
632
  _fontSize = size
659
633
  },
660
634
 
@@ -667,27 +641,24 @@ export default function litecanvas(settings = {}) {
667
641
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
668
642
  */
669
643
  textalign(align, baseline) {
670
- if (DEV_BUILD) {
671
- assert(
672
- null == align ||
673
- ['left', 'right', 'center', 'start', 'end'].includes(
674
- align
675
- ),
676
- 'textalign: 1st param must be a string'
677
- )
678
- assert(
679
- null == baseline ||
680
- [
681
- 'top',
682
- 'bottom',
683
- 'middle',
684
- 'hanging',
685
- 'alphabetic',
686
- 'ideographic',
687
- ].includes(baseline),
688
- 'textalign: 2nd param must be a string'
689
- )
690
- }
644
+ DEV: assert(
645
+ null == align ||
646
+ ['left', 'right', 'center', 'start', 'end'].includes(align),
647
+ 'textalign: 1st param must be a string'
648
+ )
649
+ DEV: assert(
650
+ null == baseline ||
651
+ [
652
+ 'top',
653
+ 'bottom',
654
+ 'middle',
655
+ 'hanging',
656
+ 'alphabetic',
657
+ 'ideographic',
658
+ ].includes(baseline),
659
+ 'textalign: 2nd param must be a string'
660
+ )
661
+
691
662
  if (align) _ctx.textAlign = align
692
663
  if (baseline) _ctx.textBaseline = baseline
693
664
  },
@@ -701,10 +672,9 @@ export default function litecanvas(settings = {}) {
701
672
  * @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
702
673
  */
703
674
  image(x, y, source) {
704
- if (DEV_BUILD) {
705
- assert(isFinite(x), 'image: 1st param must be a number')
706
- assert(isFinite(y), 'image: 2nd param must be a number')
707
- }
675
+ DEV: assert(isFinite(x), 'image: 1st param must be a number')
676
+ DEV: assert(isFinite(y), 'image: 2nd param must be a number')
677
+
708
678
  _ctx.drawImage(source, ~~x, ~~y)
709
679
  },
710
680
 
@@ -721,18 +691,17 @@ export default function litecanvas(settings = {}) {
721
691
  * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
722
692
  */
723
693
  paint(width, height, drawing, options = {}) {
724
- if (DEV_BUILD) {
725
- assert(isFinite(width), 'paint: 1st param must be a number')
726
- assert(isFinite(height), 'paint: 2nd param must be a number')
727
- assert(
728
- 'function' === typeof drawing || Array.isArray(drawing),
729
- 'paint: 3rd param must be a function or array'
730
- )
731
- assert(
732
- (options && !options.scale) || isFinite(options.scale),
733
- 'paint: 4th param (options.scale) must be a number'
734
- )
735
- }
694
+ DEV: assert(isFinite(width), 'paint: 1st param must be a number')
695
+ DEV: assert(isFinite(height), 'paint: 2nd param must be a number')
696
+ DEV: assert(
697
+ 'function' === typeof drawing || Array.isArray(drawing),
698
+ 'paint: 3rd param must be a function or array'
699
+ )
700
+ DEV: assert(
701
+ (options && !options.scale) || isFinite(options.scale),
702
+ 'paint: 4th param (options.scale) must be a number'
703
+ )
704
+
736
705
  const canvas = options.canvas || new OffscreenCanvas(1, 1),
737
706
  scale = options.scale || 1,
738
707
  contextOriginal = _ctx
@@ -779,9 +748,6 @@ export default function litecanvas(settings = {}) {
779
748
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
780
749
  */
781
750
  ctx(context) {
782
- if (DEV_BUILD) {
783
- // TODO
784
- }
785
751
  if (context) {
786
752
  _ctx = context
787
753
  }
@@ -809,10 +775,9 @@ export default function litecanvas(settings = {}) {
809
775
  * @param {number} y
810
776
  */
811
777
  translate: (x, y) => {
812
- if (DEV_BUILD) {
813
- assert(isFinite(x), 'translate: 1st param must be a number')
814
- assert(isFinite(y), 'translate: 2nd param must be a number')
815
- }
778
+ DEV: assert(isFinite(x), 'translate: 1st param must be a number')
779
+ DEV: assert(isFinite(y), 'translate: 2nd param must be a number')
780
+
816
781
  return _ctx.translate(~~x, ~~y)
817
782
  },
818
783
 
@@ -823,13 +788,12 @@ export default function litecanvas(settings = {}) {
823
788
  * @param {number} [y]
824
789
  */
825
790
  scale: (x, y) => {
826
- if (DEV_BUILD) {
827
- assert(isFinite(x), 'scale: 1st param must be a number')
828
- assert(
829
- y == null || isFinite(y),
830
- 'scale: 2nd param must be a number'
831
- )
832
- }
791
+ DEV: assert(isFinite(x), 'scale: 1st param must be a number')
792
+ DEV: assert(
793
+ y == null || isFinite(y),
794
+ 'scale: 2nd param must be a number'
795
+ )
796
+
833
797
  return _ctx.scale(x, y || x)
834
798
  },
835
799
 
@@ -839,9 +803,8 @@ export default function litecanvas(settings = {}) {
839
803
  * @param {number} radians
840
804
  */
841
805
  rotate: (radians) => {
842
- if (DEV_BUILD) {
843
- assert(isFinite(radians), 'rotate: 1st param must be a number')
844
- }
806
+ DEV: assert(isFinite(radians), 'rotate: 1st param must be a number')
807
+
845
808
  return _ctx.rotate(radians)
846
809
  },
847
810
 
@@ -852,9 +815,8 @@ export default function litecanvas(settings = {}) {
852
815
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
853
816
  */
854
817
  alpha(value) {
855
- if (DEV_BUILD) {
856
- assert(isFinite(value), 'alpha: 1st param must be a number')
857
- }
818
+ DEV: assert(isFinite(value), 'alpha: 1st param must be a number')
819
+
858
820
  _ctx.globalAlpha = instance.clamp(value, 0, 1)
859
821
  },
860
822
 
@@ -868,14 +830,11 @@ export default function litecanvas(settings = {}) {
868
830
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path2D
869
831
  */
870
832
  path: (arg) => {
871
- if (DEV_BUILD) {
872
- assert(
873
- null == arg ||
874
- 'string' === typeof arg ||
875
- arg instanceof Path2D,
876
- 'path: 1st param must be a string or a Path2D instance'
877
- )
878
- }
833
+ DEV: assert(
834
+ null == arg || 'string' === typeof arg || arg instanceof Path2D,
835
+ 'path: 1st param must be a string or a Path2D instance'
836
+ )
837
+
879
838
  return new Path2D(arg)
880
839
  },
881
840
 
@@ -886,16 +845,15 @@ export default function litecanvas(settings = {}) {
886
845
  * @param {Path2D} [path]
887
846
  */
888
847
  fill(color, path) {
889
- if (DEV_BUILD) {
890
- assert(
891
- null == color || (isFinite(color) && color >= 0),
892
- 'fill: 1st param must be a positive number or zero'
893
- )
894
- assert(
895
- null == path || path instanceof Path2D,
896
- 'fill: 2nd param must be a Path2D instance'
897
- )
898
- }
848
+ DEV: assert(
849
+ null == color || (isFinite(color) && color >= 0),
850
+ 'fill: 1st param must be a positive number or zero'
851
+ )
852
+ DEV: assert(
853
+ null == path || path instanceof Path2D,
854
+ 'fill: 2nd param must be a Path2D instance'
855
+ )
856
+
899
857
  _ctx.fillStyle = instance.getcolor(color)
900
858
  if (path) {
901
859
  _ctx.fill(path)
@@ -911,16 +869,15 @@ export default function litecanvas(settings = {}) {
911
869
  * @param {Path2D} [path]
912
870
  */
913
871
  stroke(color, path) {
914
- if (DEV_BUILD) {
915
- assert(
916
- null == color || (isFinite(color) && color >= 0),
917
- 'stroke: 1st param must be a positive number or zero'
918
- )
919
- assert(
920
- null == path || path instanceof Path2D,
921
- 'stroke: 2nd param must be a Path2D instance'
922
- )
923
- }
872
+ DEV: assert(
873
+ null == color || (isFinite(color) && color >= 0),
874
+ 'stroke: 1st param must be a positive number or zero'
875
+ )
876
+ DEV: assert(
877
+ null == path || path instanceof Path2D,
878
+ 'stroke: 2nd param must be a Path2D instance'
879
+ )
880
+
924
881
  _ctx.strokeStyle = instance.getcolor(color)
925
882
  if (path) {
926
883
  _ctx.stroke(path)
@@ -936,12 +893,11 @@ export default function litecanvas(settings = {}) {
936
893
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
937
894
  */
938
895
  clip(path) {
939
- if (DEV_BUILD) {
940
- assert(
941
- path instanceof Path2D,
942
- 'clip: 1st param must be a Path2D instance'
943
- )
944
- }
896
+ DEV: assert(
897
+ path instanceof Path2D,
898
+ 'clip: 1st param must be a Path2D instance'
899
+ )
900
+
945
901
  _ctx.clip(path)
946
902
  },
947
903
 
@@ -958,17 +914,16 @@ export default function litecanvas(settings = {}) {
958
914
  * @see https://github.com/KilledByAPixel/ZzFX
959
915
  */
960
916
  sfx(zzfxParams, pitchSlide = 0, volumeFactor = 1) {
961
- if (DEV_BUILD) {
962
- assert(
963
- null == zzfxParams || Array.isArray(zzfxParams),
964
- 'sfx: 1st param must be an array'
965
- )
966
- assert(isFinite(pitchSlide), 'sfx: 2nd param must be a number')
967
- assert(
968
- isFinite(volumeFactor),
969
- 'sfx: 3rd param must be a number'
970
- )
971
- }
917
+ DEV: assert(
918
+ null == zzfxParams || Array.isArray(zzfxParams),
919
+ 'sfx: 1st param must be an array'
920
+ )
921
+ DEV: assert(isFinite(pitchSlide), 'sfx: 2nd param must be a number')
922
+ DEV: assert(
923
+ isFinite(volumeFactor),
924
+ 'sfx: 3rd param must be a number'
925
+ )
926
+
972
927
  if (
973
928
  root.zzfxV <= 0 ||
974
929
  (navigator.userActivation &&
@@ -998,9 +953,8 @@ export default function litecanvas(settings = {}) {
998
953
  * @param {number} value
999
954
  */
1000
955
  volume(value) {
1001
- if (DEV_BUILD) {
1002
- assert(isFinite(value), 'volume: 1st param must be a number')
1003
- }
956
+ DEV: assert(isFinite(value), 'volume: 1st param must be a number')
957
+
1004
958
  root.zzfxV = value
1005
959
  },
1006
960
 
@@ -1019,16 +973,15 @@ export default function litecanvas(settings = {}) {
1019
973
  * @returns {boolean}
1020
974
  */
1021
975
  colrect: (x1, y1, w1, h1, x2, y2, w2, h2) => {
1022
- if (DEV_BUILD) {
1023
- assert(isFinite(x1), 'colrect: 1st param must be a number')
1024
- assert(isFinite(y1), 'colrect: 2nd param must be a number')
1025
- assert(isFinite(w1), 'colrect: 3rd param must be a number')
1026
- assert(isFinite(h1), 'colrect: 4th param must be a number')
1027
- assert(isFinite(x2), 'colrect: 5th param must be a number')
1028
- assert(isFinite(y2), 'colrect: 6th param must be a number')
1029
- assert(isFinite(w2), 'colrect: 7th param must be a number')
1030
- assert(isFinite(h2), 'colrect: 8th param must be a number')
1031
- }
976
+ DEV: assert(isFinite(x1), 'colrect: 1st param must be a number')
977
+ DEV: assert(isFinite(y1), 'colrect: 2nd param must be a number')
978
+ DEV: assert(isFinite(w1), 'colrect: 3rd param must be a number')
979
+ DEV: assert(isFinite(h1), 'colrect: 4th param must be a number')
980
+ DEV: assert(isFinite(x2), 'colrect: 5th param must be a number')
981
+ DEV: assert(isFinite(y2), 'colrect: 6th param must be a number')
982
+ DEV: assert(isFinite(w2), 'colrect: 7th param must be a number')
983
+ DEV: assert(isFinite(h2), 'colrect: 8th param must be a number')
984
+
1032
985
  return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
1033
986
  },
1034
987
 
@@ -1044,14 +997,13 @@ export default function litecanvas(settings = {}) {
1044
997
  * @returns {boolean}
1045
998
  */
1046
999
  colcirc: (x1, y1, r1, x2, y2, r2) => {
1047
- if (DEV_BUILD) {
1048
- assert(isFinite(x1), 'colcirc: 1st param must be a number')
1049
- assert(isFinite(y1), 'colcirc: 2nd param must be a number')
1050
- assert(isFinite(r1), 'colcirc: 3rd param must be a number')
1051
- assert(isFinite(x2), 'colcirc: 4th param must be a number')
1052
- assert(isFinite(y2), 'colcirc: 5th param must be a number')
1053
- assert(isFinite(r2), 'colcirc: 6th param must be a number')
1054
- }
1000
+ DEV: assert(isFinite(x1), 'colcirc: 1st param must be a number')
1001
+ DEV: assert(isFinite(y1), 'colcirc: 2nd param must be a number')
1002
+ DEV: assert(isFinite(r1), 'colcirc: 3rd param must be a number')
1003
+ DEV: assert(isFinite(x2), 'colcirc: 4th param must be a number')
1004
+ DEV: assert(isFinite(y2), 'colcirc: 5th param must be a number')
1005
+ DEV: assert(isFinite(r2), 'colcirc: 6th param must be a number')
1006
+
1055
1007
  return (
1056
1008
  (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <=
1057
1009
  (r1 + r2) * (r1 + r2)
@@ -1065,16 +1017,15 @@ export default function litecanvas(settings = {}) {
1065
1017
  * @param {pluginCallback} callback
1066
1018
  */
1067
1019
  use(callback, config = {}) {
1068
- if (DEV_BUILD) {
1069
- assert(
1070
- 'function' === typeof callback,
1071
- 'use: 1st param must be a function'
1072
- )
1073
- assert(
1074
- 'object' === typeof config,
1075
- 'use: 2nd param must be an object'
1076
- )
1077
- }
1020
+ DEV: assert(
1021
+ 'function' === typeof callback,
1022
+ 'use: 1st param must be a function'
1023
+ )
1024
+ DEV: assert(
1025
+ 'object' === typeof config,
1026
+ 'use: 2nd param must be an object'
1027
+ )
1028
+
1078
1029
  _initialized
1079
1030
  ? loadPlugin(callback, config)
1080
1031
  : _plugins.push([callback, config])
@@ -1088,16 +1039,15 @@ export default function litecanvas(settings = {}) {
1088
1039
  * @returns {Function} a function to remove the listener
1089
1040
  */
1090
1041
  listen(eventName, callback) {
1091
- if (DEV_BUILD) {
1092
- assert(
1093
- 'string' === typeof eventName,
1094
- 'listen: 1st param must be a string'
1095
- )
1096
- assert(
1097
- 'function' === typeof callback,
1098
- 'listen: 2nd param must be a function'
1099
- )
1100
- }
1042
+ DEV: assert(
1043
+ 'string' === typeof eventName,
1044
+ 'listen: 1st param must be a string'
1045
+ )
1046
+ DEV: assert(
1047
+ 'function' === typeof callback,
1048
+ 'listen: 2nd param must be a function'
1049
+ )
1050
+
1101
1051
  _events[eventName] = _events[eventName] || new Set()
1102
1052
  _events[eventName].add(callback)
1103
1053
 
@@ -1115,12 +1065,11 @@ export default function litecanvas(settings = {}) {
1115
1065
  * @param {*} [arg4] any data to be passed over the listeners
1116
1066
  */
1117
1067
  emit(eventName, arg1, arg2, arg3, arg4) {
1118
- if (DEV_BUILD) {
1119
- assert(
1120
- 'string' === typeof eventName,
1121
- 'emit: 1st param must be a string'
1122
- )
1123
- }
1068
+ DEV: assert(
1069
+ 'string' === typeof eventName,
1070
+ 'emit: 1st param must be a string'
1071
+ )
1072
+
1124
1073
  triggerEvent('before:' + eventName, arg1, arg2, arg3, arg4)
1125
1074
  triggerEvent(eventName, arg1, arg2, arg3, arg4)
1126
1075
  triggerEvent('after:' + eventName, arg1, arg2, arg3, arg4)
@@ -1133,12 +1082,11 @@ export default function litecanvas(settings = {}) {
1133
1082
  * @returns {string} the color code
1134
1083
  */
1135
1084
  getcolor: (index) => {
1136
- if (DEV_BUILD) {
1137
- assert(
1138
- null == index || (isFinite(index) && index >= 0),
1139
- 'getcolor: 1st param must be a number'
1140
- )
1141
- }
1085
+ DEV: assert(
1086
+ null == index || (isFinite(index) && index >= 0),
1087
+ 'getcolor: 1st param must be a number'
1088
+ )
1089
+
1142
1090
  return colors[~~index % colors.length]
1143
1091
  },
1144
1092
 
@@ -1149,15 +1097,14 @@ export default function litecanvas(settings = {}) {
1149
1097
  * @param {*} value
1150
1098
  */
1151
1099
  setvar(key, value) {
1152
- if (DEV_BUILD) {
1153
- assert(
1154
- 'string' === typeof key,
1155
- 'setvar: 1st param must be a string'
1156
- )
1157
- if (value == null) {
1158
- console.warn(`setvar: key "${key}" was defined as ${value}`)
1159
- }
1100
+ DEV: assert(
1101
+ 'string' === typeof key,
1102
+ 'setvar: 1st param must be a string'
1103
+ )
1104
+ if (value == null) {
1105
+ console.warn(`setvar: key "${key}" was defined as ${value}`)
1160
1106
  }
1107
+
1161
1108
  instance[key] = value
1162
1109
  if (_global) {
1163
1110
  root[key] = value
@@ -1171,10 +1118,9 @@ export default function litecanvas(settings = {}) {
1171
1118
  * @param {number} height
1172
1119
  */
1173
1120
  resize(width, height) {
1174
- if (DEV_BUILD) {
1175
- assert(isFinite(width), 'resize: 1st param must be a number')
1176
- assert(isFinite(height), 'resize: 2nd param must be a number')
1177
- }
1121
+ DEV: assert(isFinite(width), 'resize: 1st param must be a number')
1122
+ DEV: assert(isFinite(height), 'resize: 2nd param must be a number')
1123
+
1178
1124
  instance.setvar('WIDTH', (_canvas.width = width))
1179
1125
  instance.setvar('HEIGHT', (_canvas.height = height))
1180
1126
  pageResized()
@@ -1188,9 +1134,11 @@ export default function litecanvas(settings = {}) {
1188
1134
  * @param {number} value
1189
1135
  */
1190
1136
  timescale(value) {
1191
- if (DEV_BUILD) {
1192
- assert(isFinite(value), 'timescale: 1st param must be a number')
1193
- }
1137
+ DEV: assert(
1138
+ isFinite(value),
1139
+ 'timescale: 1st param must be a number'
1140
+ )
1141
+
1194
1142
  _timeScale = value
1195
1143
  },
1196
1144
 
@@ -1200,12 +1148,11 @@ export default function litecanvas(settings = {}) {
1200
1148
  * @param {number} value
1201
1149
  */
1202
1150
  setfps(value) {
1203
- if (DEV_BUILD) {
1204
- assert(
1205
- isFinite(value) && value >= 1,
1206
- 'setfps: 1st param must be a positive number'
1207
- )
1208
- }
1151
+ DEV: assert(
1152
+ isFinite(value) && value >= 1,
1153
+ 'setfps: 1st param must be a positive number'
1154
+ )
1155
+
1209
1156
  _deltaTime = 1 / ~~value
1210
1157
  },
1211
1158
 
@@ -1228,26 +1175,9 @@ export default function litecanvas(settings = {}) {
1228
1175
  },
1229
1176
  }
1230
1177
 
1231
- /** Copy some functions from native `Math` object */
1232
- for (const k of [
1233
- 'sin',
1234
- 'cos',
1235
- 'atan2',
1236
- 'hypot',
1237
- 'tan',
1238
- 'abs',
1239
- 'ceil',
1240
- 'round',
1241
- 'floor',
1242
- 'trunc',
1243
- 'min',
1244
- 'max',
1245
- 'pow',
1246
- 'sqrt',
1247
- 'sign',
1248
- 'exp',
1249
- ]) {
1250
- // import some native Math functions
1178
+ // prettier-ignore
1179
+ for (const k of 'PI,sin,cos,atan2,hypot,tan,abs,ceil,round,floor,trunc,min,max,pow,sqrt,sign,exp'.split(',')) {
1180
+ // import native Math functions
1251
1181
  instance[k] = Math[k]
1252
1182
  }
1253
1183
 
@@ -1296,20 +1226,38 @@ export default function litecanvas(settings = {}) {
1296
1226
  tap.x = x
1297
1227
  tap.y = y
1298
1228
  },
1299
- _checkTapped = (tap) => tap && performance.now() - tap.ts <= 200
1229
+ _checkTapped = (tap) =>
1230
+ tap && performance.now() - tap.ts <= 200,
1231
+ preventDefault = (ev) => ev.preventDefault()
1300
1232
 
1301
1233
  let _pressingMouse = false
1302
1234
 
1303
1235
  on(_canvas, 'mousedown', (ev) => {
1304
- ev.preventDefault()
1305
- const [x, y] = _getXY(ev.pageX, ev.pageY)
1306
- instance.emit('tap', x, y, 0)
1307
- _registerTap(0, x, y)
1308
- _pressingMouse = true
1236
+ if (ev.button === 0) {
1237
+ preventDefault(ev)
1238
+ const [x, y] = _getXY(ev.pageX, ev.pageY)
1239
+ instance.emit('tap', x, y, 0)
1240
+ _registerTap(0, x, y)
1241
+ _pressingMouse = true
1242
+ }
1243
+ })
1244
+
1245
+ on(_canvas, 'mouseup', (ev) => {
1246
+ if (ev.button === 0) {
1247
+ preventDefault(ev)
1248
+ const tap = _taps.get(0)
1249
+ const [x, y] = _getXY(ev.pageX, ev.pageY)
1250
+ if (_checkTapped(tap)) {
1251
+ instance.emit('tapped', tap.startX, tap.startY, 0)
1252
+ }
1253
+ instance.emit('untap', x, y, 0)
1254
+ _taps.delete(0)
1255
+ _pressingMouse = false
1256
+ }
1309
1257
  })
1310
1258
 
1311
1259
  on(_canvas, 'mousemove', (ev) => {
1312
- ev.preventDefault()
1260
+ preventDefault(ev)
1313
1261
 
1314
1262
  const [x, y] = _getXY(ev.pageX, ev.pageY)
1315
1263
  instance.setvar('MOUSEX', x)
@@ -1321,20 +1269,8 @@ export default function litecanvas(settings = {}) {
1321
1269
  _updateTap(0, x, y)
1322
1270
  })
1323
1271
 
1324
- on(_canvas, 'mouseup', (ev) => {
1325
- ev.preventDefault()
1326
- const tap = _taps.get(0)
1327
- const [x, y] = _getXY(ev.pageX, ev.pageY)
1328
- if (_checkTapped(tap)) {
1329
- instance.emit('tapped', tap.startX, tap.startY, 0)
1330
- }
1331
- instance.emit('untap', x, y, 0)
1332
- _taps.delete(0)
1333
- _pressingMouse = false
1334
- })
1335
-
1336
1272
  on(_canvas, 'touchstart', (ev) => {
1337
- ev.preventDefault()
1273
+ preventDefault(ev)
1338
1274
  /** @type {TouchList} touches */
1339
1275
  const touches = ev.changedTouches
1340
1276
  for (const touch of touches) {
@@ -1345,7 +1281,7 @@ export default function litecanvas(settings = {}) {
1345
1281
  })
1346
1282
 
1347
1283
  on(_canvas, 'touchmove', (ev) => {
1348
- ev.preventDefault()
1284
+ preventDefault(ev)
1349
1285
  /** @type {TouchList} touches */
1350
1286
  const touches = ev.changedTouches
1351
1287
  for (const touch of touches) {
@@ -1356,7 +1292,7 @@ export default function litecanvas(settings = {}) {
1356
1292
  })
1357
1293
 
1358
1294
  const _touchEndHandler = (ev) => {
1359
- ev.preventDefault()
1295
+ preventDefault(ev)
1360
1296
  const existing = []
1361
1297
 
1362
1298
  if (ev.targetTouches.length > 0) {
@@ -1401,12 +1337,11 @@ export default function litecanvas(settings = {}) {
1401
1337
  * @returns {boolean}
1402
1338
  */
1403
1339
  const iskeydown = (key) => {
1404
- if (DEV_BUILD) {
1405
- assert(
1406
- 'string' === typeof key,
1407
- 'iskeydown: 1st param must be a string'
1408
- )
1409
- }
1340
+ DEV: assert(
1341
+ 'string' === typeof key,
1342
+ 'iskeydown: 1st param must be a string'
1343
+ )
1344
+
1410
1345
  return 'any' === key
1411
1346
  ? _keys.size > 0
1412
1347
  : _keys.has(key.toLowerCase())
@@ -1460,7 +1395,8 @@ export default function litecanvas(settings = {}) {
1460
1395
 
1461
1396
  _lastFrameTime = now
1462
1397
 
1463
- if (frameTime > 1) return
1398
+ if (frameTime > _deltaTime * 30)
1399
+ return console.log('skipping too long frame')
1464
1400
 
1465
1401
  _accumulated += frameTime
1466
1402
 
@@ -1484,11 +1420,25 @@ export default function litecanvas(settings = {}) {
1484
1420
  }
1485
1421
 
1486
1422
  function setupCanvas() {
1423
+ /** @type {HTMLCanvasElement} */
1487
1424
  _canvas =
1488
1425
  'string' === typeof _canvas
1489
1426
  ? document.querySelector(_canvas)
1490
1427
  : _canvas
1491
1428
 
1429
+ DEV: assert(
1430
+ _canvas && _canvas.tagName === 'CANVAS',
1431
+ 'Invalid canvas element'
1432
+ )
1433
+ DEV: assert(
1434
+ null === instance.WIDTH || instance.WIDTH > 0,
1435
+ 'Litecanvas\' "width" option should be null or a positive number'
1436
+ )
1437
+ DEV: assert(
1438
+ null === instance.HEIGHT || instance.HEIGHT > 0,
1439
+ 'Litecanvas\' "width" option should be null or a positive number'
1440
+ )
1441
+
1492
1442
  instance.setvar('CANVAS', _canvas)
1493
1443
  _ctx = _canvas.getContext('2d')
1494
1444
 
@@ -1535,7 +1485,7 @@ export default function litecanvas(settings = {}) {
1535
1485
  // restore canvas image rendering properties
1536
1486
  if (!settings.antialias || settings.pixelart) {
1537
1487
  _ctx.imageSmoothingEnabled = false
1538
- _canvas.style.imageRendering = 'pixelated'
1488
+ styles.imageRendering = 'pixelated'
1539
1489
  }
1540
1490
 
1541
1491
  instance.emit('resized', _scale)
@@ -1557,10 +1507,14 @@ export default function litecanvas(settings = {}) {
1557
1507
  */
1558
1508
  function loadPlugin(callback, config) {
1559
1509
  const pluginData = callback(instance, _helpers, config)
1560
- if ('object' === typeof pluginData) {
1561
- for (const key of Object.keys(pluginData)) {
1562
- instance.setvar(key, pluginData[key])
1563
- }
1510
+
1511
+ DEV: assert(
1512
+ null == pluginData || 'object' === typeof pluginData,
1513
+ 'Litecanvas plugins should return an object or nothing'
1514
+ )
1515
+
1516
+ for (const key in pluginData) {
1517
+ instance.setvar(key, pluginData[key])
1564
1518
  }
1565
1519
  }
1566
1520