litecanvas 0.75.0 → 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
@@ -163,11 +163,10 @@ export default function litecanvas(settings = {}) {
163
163
  * @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
164
164
  */
165
165
  lerp: (start, end, t) => {
166
- if (DEV_BUILD) {
167
- assert(isFinite(start), 'lerp: 1st param must be a number')
168
- assert(isFinite(end), 'lerp: 2nd param must be a number')
169
- assert(isFinite(t), 'lerp: 3rd param must be a number')
170
- }
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
+
171
170
  return t * (end - start) + start
172
171
  },
173
172
 
@@ -178,9 +177,8 @@ export default function litecanvas(settings = {}) {
178
177
  * @returns {number} the value in radians
179
178
  */
180
179
  deg2rad: (degs) => {
181
- if (DEV_BUILD) {
182
- assert(isFinite(degs), 'deg2rad: 1st param must be a number')
183
- }
180
+ DEV: assert(isFinite(degs), 'deg2rad: 1st param must be a number')
181
+
184
182
  return (PI / 180) * degs
185
183
  },
186
184
 
@@ -191,9 +189,8 @@ export default function litecanvas(settings = {}) {
191
189
  * @returns {number} the value in degrees
192
190
  */
193
191
  rad2deg: (rads) => {
194
- if (DEV_BUILD) {
195
- assert(isFinite(rads), 'rad2deg: 1st param must be a number')
196
- }
192
+ DEV: assert(isFinite(rads), 'rad2deg: 1st param must be a number')
193
+
197
194
  return (180 / PI) * rads
198
195
  },
199
196
 
@@ -206,15 +203,14 @@ export default function litecanvas(settings = {}) {
206
203
  * @returns {number}
207
204
  */
208
205
  clamp: (value, min, max) => {
209
- if (DEV_BUILD) {
210
- assert(isFinite(value), 'clamp: 1st param must be a number')
211
- assert(isFinite(min), 'clamp: 2nd param must be a number')
212
- assert(isFinite(max), 'clamp: 3rd param must be a number')
213
- assert(
214
- max > min,
215
- 'randi: the 2nd param must be less than the 3rd param'
216
- )
217
- }
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
+
218
214
  if (value < min) return min
219
215
  if (value > max) return max
220
216
  return value
@@ -229,19 +225,18 @@ export default function litecanvas(settings = {}) {
229
225
  * @returns {number}
230
226
  */
231
227
  wrap: (value, min, max) => {
232
- if (DEV_BUILD) {
233
- assert(isFinite(value), 'wrap: 1st param must be a number')
234
- assert(isFinite(min), 'wrap: 2nd param must be a number')
235
- assert(isFinite(max), 'wrap: 3rd param must be a number')
236
- assert(
237
- max > min,
238
- 'randi: the 2nd param must be less than the 3rd param'
239
- )
240
- assert(
241
- max !== min,
242
- 'randi: the 2nd param must be not equal to the 3rd param'
243
- )
244
- }
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
+
245
240
  return value - (max - min) * Math.floor((value - min) / (max - min))
246
241
  },
247
242
 
@@ -257,13 +252,12 @@ export default function litecanvas(settings = {}) {
257
252
  * @returns {number} the remapped number
258
253
  */
259
254
  map(value, start1, stop1, start2, stop2, withinBounds) {
260
- if (DEV_BUILD) {
261
- assert(isFinite(value), 'map: 1st param must be a number')
262
- assert(isFinite(start1), 'map: 2nd param must be a number')
263
- assert(isFinite(stop1), 'map: 3rd param must be a number')
264
- assert(isFinite(start2), 'map: 4th param must be a number')
265
- assert(isFinite(stop2), 'map: 5th param must be a number')
266
- }
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
+
267
261
  // prettier-ignore
268
262
  const result = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
269
263
  return withinBounds ? instance.clamp(result, start2, stop2) : result
@@ -280,11 +274,10 @@ export default function litecanvas(settings = {}) {
280
274
  * @returns {number} the normalized number.
281
275
  */
282
276
  norm: (value, start, stop) => {
283
- if (DEV_BUILD) {
284
- assert(isFinite(value), 'norm: 1st param must be a number')
285
- assert(isFinite(start), 'norm: 2nd param must be a number')
286
- assert(isFinite(stop), 'norm: 3rd param must be a number')
287
- }
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
+
288
281
  return instance.map(value, start, stop, 0, 1)
289
282
  },
290
283
 
@@ -298,14 +291,13 @@ export default function litecanvas(settings = {}) {
298
291
  * @returns {number} the random number
299
292
  */
300
293
  rand: (min = 0.0, max = 1.0) => {
301
- if (DEV_BUILD) {
302
- assert(isFinite(min), 'rand: 1st param must be a number')
303
- assert(isFinite(max), 'rand: 2nd param must be a number')
304
- assert(
305
- max > min,
306
- 'rand: the 1st param must be less than the 2nd param'
307
- )
308
- }
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
+
309
301
  const a = 1664525
310
302
  const c = 1013904223
311
303
  const m = 4294967296
@@ -323,14 +315,13 @@ export default function litecanvas(settings = {}) {
323
315
  * @returns {number} the random number
324
316
  */
325
317
  randi: (min = 0, max = 1) => {
326
- if (DEV_BUILD) {
327
- assert(isFinite(min), 'randi: 1st param must be a number')
328
- assert(isFinite(max), 'randi: 2nd param must be a number')
329
- assert(
330
- max > min,
331
- 'randi: the 1st param must be less than the 2nd param'
332
- )
333
- }
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
+
334
325
  return Math.floor(instance.rand(min, max + 1))
335
326
  },
336
327
 
@@ -342,12 +333,11 @@ export default function litecanvas(settings = {}) {
342
333
  * @returns {number} the seed state
343
334
  */
344
335
  seed: (value) => {
345
- if (DEV_BUILD) {
346
- assert(
347
- null == value || (isFinite(value) && value >= 0),
348
- 'seed: 1st param must be a positive number or zero'
349
- )
350
- }
336
+ DEV: assert(
337
+ null == value || (isFinite(value) && value >= 0),
338
+ 'seed: 1st param must be a positive number or zero'
339
+ )
340
+
351
341
  return null == value ? _rng_seed : (_rng_seed = ~~value)
352
342
  },
353
343
 
@@ -358,12 +348,11 @@ export default function litecanvas(settings = {}) {
358
348
  * @param {number?} color The background color (index) or null (for transparent)
359
349
  */
360
350
  cls(color) {
361
- if (DEV_BUILD) {
362
- assert(
363
- null == color || (isFinite(color) && color >= 0),
364
- 'cls: 1st param must be a positive number or zero or null'
365
- )
366
- }
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
+
367
356
  if (null == color) {
368
357
  _ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height)
369
358
  } else {
@@ -388,28 +377,27 @@ export default function litecanvas(settings = {}) {
388
377
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
389
378
  */
390
379
  rect(x, y, width, height, color, radii = null) {
391
- if (DEV_BUILD) {
392
- assert(isFinite(x), 'rect: 1st param must be a number')
393
- assert(isFinite(y), 'rect: 2nd param must be a number')
394
- assert(
395
- isFinite(width) && width > 0,
396
- 'rect: 3rd param must be a positive number'
397
- )
398
- assert(
399
- isFinite(height) && height >= 0,
400
- 'rect: 4th param must be a positive number or zero'
401
- )
402
- assert(
403
- null == color || (isFinite(color) && color >= 0),
404
- 'rect: 5th param must be a positive number or zero'
405
- )
406
- assert(
407
- null == radii ||
408
- isFinite(radii) ||
409
- (Array.isArray(radii) && radii.length >= 1),
410
- 'rect: 6th param must be a number or array of numbers'
411
- )
412
- }
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
+
413
401
  _ctx.beginPath()
414
402
  _ctx[radii ? 'roundRect' : 'rect'](
415
403
  ~~x - _outline_fix,
@@ -432,28 +420,27 @@ export default function litecanvas(settings = {}) {
432
420
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
433
421
  */
434
422
  rectfill(x, y, width, height, color, radii = null) {
435
- if (DEV_BUILD) {
436
- assert(isFinite(x), 'rectfill: 1st param must be a number')
437
- assert(isFinite(y), 'rectfill: 2nd param must be a number')
438
- assert(
439
- isFinite(width) && width >= 0,
440
- 'rectfill: 3rd param must be a positive number or zero'
441
- )
442
- assert(
443
- isFinite(height) && height >= 0,
444
- 'rectfill: 4th param must be a positive number or zero'
445
- )
446
- assert(
447
- null == color || (isFinite(color) && color >= 0),
448
- 'rectfill: 5th param must be a positive number or zero'
449
- )
450
- assert(
451
- null == radii ||
452
- isFinite(radii) ||
453
- (Array.isArray(radii) && radii.length >= 1),
454
- 'rectfill: 6th param must be a number or array of at least 2 numbers'
455
- )
456
- }
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
+
457
444
  _ctx.beginPath()
458
445
  _ctx[radii ? 'roundRect' : 'rect'](
459
446
  ~~x,
@@ -474,18 +461,17 @@ export default function litecanvas(settings = {}) {
474
461
  * @param {number} [color=0] the color index
475
462
  */
476
463
  circ(x, y, radius, color) {
477
- if (DEV_BUILD) {
478
- assert(isFinite(x), 'circ: 1st param must be a number')
479
- assert(isFinite(y), 'circ: 2nd param must be a number')
480
- assert(
481
- isFinite(radius) && radius >= 0,
482
- 'circ: 3rd param must be a positive number or zero'
483
- )
484
- assert(
485
- null == color || (isFinite(color) && color >= 0),
486
- 'circ: 4th param must be a positive number or zero'
487
- )
488
- }
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
+
489
475
  _ctx.beginPath()
490
476
  _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
491
477
  instance.stroke(color)
@@ -500,18 +486,17 @@ export default function litecanvas(settings = {}) {
500
486
  * @param {number} [color=0] the color index
501
487
  */
502
488
  circfill(x, y, radius, color) {
503
- if (DEV_BUILD) {
504
- assert(isFinite(x), 'circfill: 1st param must be a number')
505
- assert(isFinite(y), 'circfill: 2nd param must be a number')
506
- assert(
507
- isFinite(radius) && radius >= 0,
508
- 'circfill: 3rd param must be a positive number or zero'
509
- )
510
- assert(
511
- null == color || (isFinite(color) && color >= 0),
512
- 'circfill: 4th param must be a positive number or zero'
513
- )
514
- }
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
+
515
500
  _ctx.beginPath()
516
501
  _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
517
502
  instance.fill(color)
@@ -527,22 +512,21 @@ export default function litecanvas(settings = {}) {
527
512
  * @param {number} [color=0] the color index
528
513
  */
529
514
  line(x1, y1, x2, y2, color) {
530
- if (DEV_BUILD) {
531
- assert(isFinite(x1), 'line: 1st param must be a number')
532
- assert(isFinite(y1), 'line: 2nd param must be a number')
533
- assert(
534
- isFinite(x2),
535
- 'line: 3rd param must be a positive number or zero'
536
- )
537
- assert(
538
- isFinite(y2),
539
- 'line: 4th param must be a positive number or zero'
540
- )
541
- assert(
542
- null == color || (isFinite(color) && color >= 0),
543
- 'line: 5th param must be a positive number or zero'
544
- )
545
- }
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
+
546
530
  _ctx.beginPath()
547
531
 
548
532
  let xfix = _outline_fix !== 0 && ~~x1 === ~~x2 ? 0.5 : 0
@@ -561,12 +545,11 @@ export default function litecanvas(settings = {}) {
561
545
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
562
546
  */
563
547
  linewidth(value) {
564
- if (DEV_BUILD) {
565
- assert(
566
- isFinite(value) && ~~value > 0,
567
- 'linewidth: 1st param must be a positive number'
568
- )
569
- }
548
+ DEV: assert(
549
+ isFinite(value) && ~~value > 0,
550
+ 'linewidth: 1st param must be a positive number'
551
+ )
552
+
570
553
  _ctx.lineWidth = ~~value
571
554
  _outline_fix = ~~value % 2 === 0 ? 0 : 0.5
572
555
  },
@@ -580,13 +563,15 @@ export default function litecanvas(settings = {}) {
580
563
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
581
564
  */
582
565
  linedash(segments, offset = 0) {
583
- if (DEV_BUILD) {
584
- assert(
585
- Array.isArray(segments) && segments.length > 0,
586
- 'linedash: 1st param must be an array of numbers'
587
- )
588
- assert(isFinite(offset), 'linedash: 2nd param must be a number')
589
- }
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
+
590
575
  _ctx.setLineDash(segments)
591
576
  _ctx.lineDashOffset = offset
592
577
  },
@@ -602,22 +587,21 @@ export default function litecanvas(settings = {}) {
602
587
  * @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
603
588
  */
604
589
  text(x, y, message, color = 3, fontStyle = 'normal') {
605
- if (DEV_BUILD) {
606
- assert(isFinite(x), 'text: 1st param must be a number')
607
- assert(isFinite(y), 'text: 2nd param must be a number')
608
- // assert(
609
- // 'string' === typeof message,
610
- // 'text: 3rd param must be a string'
611
- // )
612
- assert(
613
- null == color || (isFinite(color) && color >= 0),
614
- 'text: 4th param must be a positive number or zero'
615
- )
616
- assert(
617
- 'string' === typeof fontStyle,
618
- 'text: 5th param must be a string'
619
- )
620
- }
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
+
621
605
  _ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`
622
606
  _ctx.fillStyle = instance.getcolor(color)
623
607
  _ctx.fillText(message, ~~x, ~~y)
@@ -629,12 +613,11 @@ export default function litecanvas(settings = {}) {
629
613
  * @param {string} family
630
614
  */
631
615
  textfont(family) {
632
- if (DEV_BUILD) {
633
- assert(
634
- 'string' === typeof family,
635
- 'textfont: 1st param must be a string'
636
- )
637
- }
616
+ DEV: assert(
617
+ 'string' === typeof family,
618
+ 'textfont: 1st param must be a string'
619
+ )
620
+
638
621
  _fontFamily = family
639
622
  },
640
623
 
@@ -644,9 +627,8 @@ export default function litecanvas(settings = {}) {
644
627
  * @param {number} size
645
628
  */
646
629
  textsize(size) {
647
- if (DEV_BUILD) {
648
- assert(isFinite(size), 'textsize: 1st param must be a number')
649
- }
630
+ DEV: assert(isFinite(size), 'textsize: 1st param must be a number')
631
+
650
632
  _fontSize = size
651
633
  },
652
634
 
@@ -659,27 +641,24 @@ export default function litecanvas(settings = {}) {
659
641
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
660
642
  */
661
643
  textalign(align, baseline) {
662
- if (DEV_BUILD) {
663
- assert(
664
- null == align ||
665
- ['left', 'right', 'center', 'start', 'end'].includes(
666
- align
667
- ),
668
- 'textalign: 1st param must be a string'
669
- )
670
- assert(
671
- null == baseline ||
672
- [
673
- 'top',
674
- 'bottom',
675
- 'middle',
676
- 'hanging',
677
- 'alphabetic',
678
- 'ideographic',
679
- ].includes(baseline),
680
- 'textalign: 2nd param must be a string'
681
- )
682
- }
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
+
683
662
  if (align) _ctx.textAlign = align
684
663
  if (baseline) _ctx.textBaseline = baseline
685
664
  },
@@ -693,10 +672,9 @@ export default function litecanvas(settings = {}) {
693
672
  * @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
694
673
  */
695
674
  image(x, y, source) {
696
- if (DEV_BUILD) {
697
- assert(isFinite(x), 'image: 1st param must be a number')
698
- assert(isFinite(y), 'image: 2nd param must be a number')
699
- }
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
+
700
678
  _ctx.drawImage(source, ~~x, ~~y)
701
679
  },
702
680
 
@@ -713,18 +691,17 @@ export default function litecanvas(settings = {}) {
713
691
  * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
714
692
  */
715
693
  paint(width, height, drawing, options = {}) {
716
- if (DEV_BUILD) {
717
- assert(isFinite(width), 'paint: 1st param must be a number')
718
- assert(isFinite(height), 'paint: 2nd param must be a number')
719
- assert(
720
- 'function' === typeof drawing || Array.isArray(drawing),
721
- 'paint: 3rd param must be a function or array'
722
- )
723
- assert(
724
- (options && !options.scale) || isFinite(options.scale),
725
- 'paint: 4th param (options.scale) must be a number'
726
- )
727
- }
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
+
728
705
  const canvas = options.canvas || new OffscreenCanvas(1, 1),
729
706
  scale = options.scale || 1,
730
707
  contextOriginal = _ctx
@@ -771,9 +748,6 @@ export default function litecanvas(settings = {}) {
771
748
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
772
749
  */
773
750
  ctx(context) {
774
- if (DEV_BUILD) {
775
- // TODO
776
- }
777
751
  if (context) {
778
752
  _ctx = context
779
753
  }
@@ -801,10 +775,9 @@ export default function litecanvas(settings = {}) {
801
775
  * @param {number} y
802
776
  */
803
777
  translate: (x, y) => {
804
- if (DEV_BUILD) {
805
- assert(isFinite(x), 'translate: 1st param must be a number')
806
- assert(isFinite(y), 'translate: 2nd param must be a number')
807
- }
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
+
808
781
  return _ctx.translate(~~x, ~~y)
809
782
  },
810
783
 
@@ -815,13 +788,12 @@ export default function litecanvas(settings = {}) {
815
788
  * @param {number} [y]
816
789
  */
817
790
  scale: (x, y) => {
818
- if (DEV_BUILD) {
819
- assert(isFinite(x), 'scale: 1st param must be a number')
820
- assert(
821
- y == null || isFinite(y),
822
- 'scale: 2nd param must be a number'
823
- )
824
- }
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
+
825
797
  return _ctx.scale(x, y || x)
826
798
  },
827
799
 
@@ -831,9 +803,8 @@ export default function litecanvas(settings = {}) {
831
803
  * @param {number} radians
832
804
  */
833
805
  rotate: (radians) => {
834
- if (DEV_BUILD) {
835
- assert(isFinite(radians), 'rotate: 1st param must be a number')
836
- }
806
+ DEV: assert(isFinite(radians), 'rotate: 1st param must be a number')
807
+
837
808
  return _ctx.rotate(radians)
838
809
  },
839
810
 
@@ -844,9 +815,8 @@ export default function litecanvas(settings = {}) {
844
815
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
845
816
  */
846
817
  alpha(value) {
847
- if (DEV_BUILD) {
848
- assert(isFinite(value), 'alpha: 1st param must be a number')
849
- }
818
+ DEV: assert(isFinite(value), 'alpha: 1st param must be a number')
819
+
850
820
  _ctx.globalAlpha = instance.clamp(value, 0, 1)
851
821
  },
852
822
 
@@ -860,14 +830,11 @@ export default function litecanvas(settings = {}) {
860
830
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path2D
861
831
  */
862
832
  path: (arg) => {
863
- if (DEV_BUILD) {
864
- assert(
865
- null == arg ||
866
- 'string' === typeof arg ||
867
- arg instanceof Path2D,
868
- 'path: 1st param must be a string or a Path2D instance'
869
- )
870
- }
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
+
871
838
  return new Path2D(arg)
872
839
  },
873
840
 
@@ -878,16 +845,15 @@ export default function litecanvas(settings = {}) {
878
845
  * @param {Path2D} [path]
879
846
  */
880
847
  fill(color, path) {
881
- if (DEV_BUILD) {
882
- assert(
883
- null == color || (isFinite(color) && color >= 0),
884
- 'fill: 1st param must be a positive number or zero'
885
- )
886
- assert(
887
- null == path || path instanceof Path2D,
888
- 'fill: 2nd param must be a Path2D instance'
889
- )
890
- }
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
+
891
857
  _ctx.fillStyle = instance.getcolor(color)
892
858
  if (path) {
893
859
  _ctx.fill(path)
@@ -903,16 +869,15 @@ export default function litecanvas(settings = {}) {
903
869
  * @param {Path2D} [path]
904
870
  */
905
871
  stroke(color, path) {
906
- if (DEV_BUILD) {
907
- assert(
908
- null == color || (isFinite(color) && color >= 0),
909
- 'stroke: 1st param must be a positive number or zero'
910
- )
911
- assert(
912
- null == path || path instanceof Path2D,
913
- 'stroke: 2nd param must be a Path2D instance'
914
- )
915
- }
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
+
916
881
  _ctx.strokeStyle = instance.getcolor(color)
917
882
  if (path) {
918
883
  _ctx.stroke(path)
@@ -928,12 +893,11 @@ export default function litecanvas(settings = {}) {
928
893
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
929
894
  */
930
895
  clip(path) {
931
- if (DEV_BUILD) {
932
- assert(
933
- path instanceof Path2D,
934
- 'clip: 1st param must be a Path2D instance'
935
- )
936
- }
896
+ DEV: assert(
897
+ path instanceof Path2D,
898
+ 'clip: 1st param must be a Path2D instance'
899
+ )
900
+
937
901
  _ctx.clip(path)
938
902
  },
939
903
 
@@ -950,17 +914,16 @@ export default function litecanvas(settings = {}) {
950
914
  * @see https://github.com/KilledByAPixel/ZzFX
951
915
  */
952
916
  sfx(zzfxParams, pitchSlide = 0, volumeFactor = 1) {
953
- if (DEV_BUILD) {
954
- assert(
955
- null == zzfxParams || Array.isArray(zzfxParams),
956
- 'sfx: 1st param must be an array'
957
- )
958
- assert(isFinite(pitchSlide), 'sfx: 2nd param must be a number')
959
- assert(
960
- isFinite(volumeFactor),
961
- 'sfx: 3rd param must be a number'
962
- )
963
- }
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
+
964
927
  if (
965
928
  root.zzfxV <= 0 ||
966
929
  (navigator.userActivation &&
@@ -990,9 +953,8 @@ export default function litecanvas(settings = {}) {
990
953
  * @param {number} value
991
954
  */
992
955
  volume(value) {
993
- if (DEV_BUILD) {
994
- assert(isFinite(value), 'volume: 1st param must be a number')
995
- }
956
+ DEV: assert(isFinite(value), 'volume: 1st param must be a number')
957
+
996
958
  root.zzfxV = value
997
959
  },
998
960
 
@@ -1011,16 +973,15 @@ export default function litecanvas(settings = {}) {
1011
973
  * @returns {boolean}
1012
974
  */
1013
975
  colrect: (x1, y1, w1, h1, x2, y2, w2, h2) => {
1014
- if (DEV_BUILD) {
1015
- assert(isFinite(x1), 'colrect: 1st param must be a number')
1016
- assert(isFinite(y1), 'colrect: 2nd param must be a number')
1017
- assert(isFinite(w1), 'colrect: 3rd param must be a number')
1018
- assert(isFinite(h1), 'colrect: 4th param must be a number')
1019
- assert(isFinite(x2), 'colrect: 5th param must be a number')
1020
- assert(isFinite(y2), 'colrect: 6th param must be a number')
1021
- assert(isFinite(w2), 'colrect: 7th param must be a number')
1022
- assert(isFinite(h2), 'colrect: 8th param must be a number')
1023
- }
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
+
1024
985
  return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
1025
986
  },
1026
987
 
@@ -1036,14 +997,13 @@ export default function litecanvas(settings = {}) {
1036
997
  * @returns {boolean}
1037
998
  */
1038
999
  colcirc: (x1, y1, r1, x2, y2, r2) => {
1039
- if (DEV_BUILD) {
1040
- assert(isFinite(x1), 'colcirc: 1st param must be a number')
1041
- assert(isFinite(y1), 'colcirc: 2nd param must be a number')
1042
- assert(isFinite(r1), 'colcirc: 3rd param must be a number')
1043
- assert(isFinite(x2), 'colcirc: 4th param must be a number')
1044
- assert(isFinite(y2), 'colcirc: 5th param must be a number')
1045
- assert(isFinite(r2), 'colcirc: 6th param must be a number')
1046
- }
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
+
1047
1007
  return (
1048
1008
  (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <=
1049
1009
  (r1 + r2) * (r1 + r2)
@@ -1057,16 +1017,15 @@ export default function litecanvas(settings = {}) {
1057
1017
  * @param {pluginCallback} callback
1058
1018
  */
1059
1019
  use(callback, config = {}) {
1060
- if (DEV_BUILD) {
1061
- assert(
1062
- 'function' === typeof callback,
1063
- 'use: 1st param must be a function'
1064
- )
1065
- assert(
1066
- 'object' === typeof config,
1067
- 'use: 2nd param must be an object'
1068
- )
1069
- }
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
+
1070
1029
  _initialized
1071
1030
  ? loadPlugin(callback, config)
1072
1031
  : _plugins.push([callback, config])
@@ -1080,16 +1039,15 @@ export default function litecanvas(settings = {}) {
1080
1039
  * @returns {Function} a function to remove the listener
1081
1040
  */
1082
1041
  listen(eventName, callback) {
1083
- if (DEV_BUILD) {
1084
- assert(
1085
- 'string' === typeof eventName,
1086
- 'listen: 1st param must be a string'
1087
- )
1088
- assert(
1089
- 'function' === typeof callback,
1090
- 'listen: 2nd param must be a function'
1091
- )
1092
- }
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
+
1093
1051
  _events[eventName] = _events[eventName] || new Set()
1094
1052
  _events[eventName].add(callback)
1095
1053
 
@@ -1107,12 +1065,11 @@ export default function litecanvas(settings = {}) {
1107
1065
  * @param {*} [arg4] any data to be passed over the listeners
1108
1066
  */
1109
1067
  emit(eventName, arg1, arg2, arg3, arg4) {
1110
- if (DEV_BUILD) {
1111
- assert(
1112
- 'string' === typeof eventName,
1113
- 'emit: 1st param must be a string'
1114
- )
1115
- }
1068
+ DEV: assert(
1069
+ 'string' === typeof eventName,
1070
+ 'emit: 1st param must be a string'
1071
+ )
1072
+
1116
1073
  triggerEvent('before:' + eventName, arg1, arg2, arg3, arg4)
1117
1074
  triggerEvent(eventName, arg1, arg2, arg3, arg4)
1118
1075
  triggerEvent('after:' + eventName, arg1, arg2, arg3, arg4)
@@ -1125,12 +1082,11 @@ export default function litecanvas(settings = {}) {
1125
1082
  * @returns {string} the color code
1126
1083
  */
1127
1084
  getcolor: (index) => {
1128
- if (DEV_BUILD) {
1129
- assert(
1130
- null == index || (isFinite(index) && index >= 0),
1131
- 'getcolor: 1st param must be a number'
1132
- )
1133
- }
1085
+ DEV: assert(
1086
+ null == index || (isFinite(index) && index >= 0),
1087
+ 'getcolor: 1st param must be a number'
1088
+ )
1089
+
1134
1090
  return colors[~~index % colors.length]
1135
1091
  },
1136
1092
 
@@ -1141,15 +1097,14 @@ export default function litecanvas(settings = {}) {
1141
1097
  * @param {*} value
1142
1098
  */
1143
1099
  setvar(key, value) {
1144
- if (DEV_BUILD) {
1145
- assert(
1146
- 'string' === typeof key,
1147
- 'setvar: 1st param must be a string'
1148
- )
1149
- if (value == null) {
1150
- console.warn(`setvar: key "${key}" was defined as ${value}`)
1151
- }
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}`)
1152
1106
  }
1107
+
1153
1108
  instance[key] = value
1154
1109
  if (_global) {
1155
1110
  root[key] = value
@@ -1163,10 +1118,9 @@ export default function litecanvas(settings = {}) {
1163
1118
  * @param {number} height
1164
1119
  */
1165
1120
  resize(width, height) {
1166
- if (DEV_BUILD) {
1167
- assert(isFinite(width), 'resize: 1st param must be a number')
1168
- assert(isFinite(height), 'resize: 2nd param must be a number')
1169
- }
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
+
1170
1124
  instance.setvar('WIDTH', (_canvas.width = width))
1171
1125
  instance.setvar('HEIGHT', (_canvas.height = height))
1172
1126
  pageResized()
@@ -1180,9 +1134,11 @@ export default function litecanvas(settings = {}) {
1180
1134
  * @param {number} value
1181
1135
  */
1182
1136
  timescale(value) {
1183
- if (DEV_BUILD) {
1184
- assert(isFinite(value), 'timescale: 1st param must be a number')
1185
- }
1137
+ DEV: assert(
1138
+ isFinite(value),
1139
+ 'timescale: 1st param must be a number'
1140
+ )
1141
+
1186
1142
  _timeScale = value
1187
1143
  },
1188
1144
 
@@ -1192,12 +1148,11 @@ export default function litecanvas(settings = {}) {
1192
1148
  * @param {number} value
1193
1149
  */
1194
1150
  setfps(value) {
1195
- if (DEV_BUILD) {
1196
- assert(
1197
- isFinite(value) && value >= 1,
1198
- 'setfps: 1st param must be a positive number'
1199
- )
1200
- }
1151
+ DEV: assert(
1152
+ isFinite(value) && value >= 1,
1153
+ 'setfps: 1st param must be a positive number'
1154
+ )
1155
+
1201
1156
  _deltaTime = 1 / ~~value
1202
1157
  },
1203
1158
 
@@ -1382,12 +1337,11 @@ export default function litecanvas(settings = {}) {
1382
1337
  * @returns {boolean}
1383
1338
  */
1384
1339
  const iskeydown = (key) => {
1385
- if (DEV_BUILD) {
1386
- assert(
1387
- 'string' === typeof key,
1388
- 'iskeydown: 1st param must be a string'
1389
- )
1390
- }
1340
+ DEV: assert(
1341
+ 'string' === typeof key,
1342
+ 'iskeydown: 1st param must be a string'
1343
+ )
1344
+
1391
1345
  return 'any' === key
1392
1346
  ? _keys.size > 0
1393
1347
  : _keys.has(key.toLowerCase())
@@ -1472,20 +1426,18 @@ export default function litecanvas(settings = {}) {
1472
1426
  ? document.querySelector(_canvas)
1473
1427
  : _canvas
1474
1428
 
1475
- if (DEV_BUILD) {
1476
- assert(
1477
- _canvas && _canvas.tagName === 'CANVAS',
1478
- 'Invalid canvas element'
1479
- )
1480
- assert(
1481
- null === instance.WIDTH || instance.WIDTH > 0,
1482
- 'Litecanvas\' "width" option should be null or a positive number'
1483
- )
1484
- assert(
1485
- null === instance.HEIGHT || instance.HEIGHT > 0,
1486
- 'Litecanvas\' "width" option should be null or a positive number'
1487
- )
1488
- }
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
+ )
1489
1441
 
1490
1442
  instance.setvar('CANVAS', _canvas)
1491
1443
  _ctx = _canvas.getContext('2d')
@@ -1555,12 +1507,12 @@ export default function litecanvas(settings = {}) {
1555
1507
  */
1556
1508
  function loadPlugin(callback, config) {
1557
1509
  const pluginData = callback(instance, _helpers, config)
1558
- if (DEV_BUILD) {
1559
- assert(
1560
- null == pluginData || 'object' === typeof pluginData,
1561
- 'Litecanvas plugins should return an object or nothing'
1562
- )
1563
- }
1510
+
1511
+ DEV: assert(
1512
+ null == pluginData || 'object' === typeof pluginData,
1513
+ 'Litecanvas plugins should return an object or nothing'
1514
+ )
1515
+
1564
1516
  for (const key in pluginData) {
1565
1517
  instance.setvar(key, pluginData[key])
1566
1518
  }