litecanvas 0.75.0 → 0.76.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 CHANGED
@@ -36,7 +36,6 @@
36
36
  () => elem.removeEventListener(evt, callback, false)
37
37
  );
38
38
  }, isFinite = Number.isFinite, defaults = {
39
- fullscreen: true,
40
39
  width: null,
41
40
  height: null,
42
41
  autoscale: true,
@@ -51,7 +50,7 @@
51
50
  animate: true
52
51
  };
53
52
  settings = Object.assign(defaults, settings);
54
- let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _fullscreen = settings.fullscreen, _autoscale = settings.autoscale, _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 32, _rng_seed = Date.now(), _global = settings.global, _events = {
53
+ let _initialized = false, _plugins = [], _canvas = settings.canvas || document.createElement("canvas"), _autoscale = settings.autoscale, _animated = settings.animate, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 32, _rng_seed = Date.now(), _global = settings.global, _events = {
55
54
  init: null,
56
55
  update: null,
57
56
  draw: null,
@@ -111,11 +110,9 @@
111
110
  * @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
112
111
  */
113
112
  lerp: (start, end, t) => {
114
- if (true) {
115
- assert(isFinite(start), "lerp: 1st param must be a number");
116
- assert(isFinite(end), "lerp: 2nd param must be a number");
117
- assert(isFinite(t), "lerp: 3rd param must be a number");
118
- }
113
+ DEV: assert(isFinite(start), "lerp: 1st param must be a number");
114
+ DEV: assert(isFinite(end), "lerp: 2nd param must be a number");
115
+ DEV: assert(isFinite(t), "lerp: 3rd param must be a number");
119
116
  return t * (end - start) + start;
120
117
  },
121
118
  /**
@@ -125,9 +122,7 @@
125
122
  * @returns {number} the value in radians
126
123
  */
127
124
  deg2rad: (degs) => {
128
- if (true) {
129
- assert(isFinite(degs), "deg2rad: 1st param must be a number");
130
- }
125
+ DEV: assert(isFinite(degs), "deg2rad: 1st param must be a number");
131
126
  return PI / 180 * degs;
132
127
  },
133
128
  /**
@@ -137,9 +132,7 @@
137
132
  * @returns {number} the value in degrees
138
133
  */
139
134
  rad2deg: (rads) => {
140
- if (true) {
141
- assert(isFinite(rads), "rad2deg: 1st param must be a number");
142
- }
135
+ DEV: assert(isFinite(rads), "rad2deg: 1st param must be a number");
143
136
  return 180 / PI * rads;
144
137
  },
145
138
  /**
@@ -151,15 +144,13 @@
151
144
  * @returns {number}
152
145
  */
153
146
  clamp: (value, min, max) => {
154
- if (true) {
155
- assert(isFinite(value), "clamp: 1st param must be a number");
156
- assert(isFinite(min), "clamp: 2nd param must be a number");
157
- assert(isFinite(max), "clamp: 3rd param must be a number");
158
- assert(
159
- max > min,
160
- "randi: the 2nd param must be less than the 3rd param"
161
- );
162
- }
147
+ DEV: assert(isFinite(value), "clamp: 1st param must be a number");
148
+ DEV: assert(isFinite(min), "clamp: 2nd param must be a number");
149
+ DEV: assert(isFinite(max), "clamp: 3rd param must be a number");
150
+ DEV: assert(
151
+ max > min,
152
+ "randi: the 2nd param must be less than the 3rd param"
153
+ );
163
154
  if (value < min) return min;
164
155
  if (value > max) return max;
165
156
  return value;
@@ -173,19 +164,17 @@
173
164
  * @returns {number}
174
165
  */
175
166
  wrap: (value, min, max) => {
176
- if (true) {
177
- assert(isFinite(value), "wrap: 1st param must be a number");
178
- assert(isFinite(min), "wrap: 2nd param must be a number");
179
- assert(isFinite(max), "wrap: 3rd param must be a number");
180
- assert(
181
- max > min,
182
- "randi: the 2nd param must be less than the 3rd param"
183
- );
184
- assert(
185
- max !== min,
186
- "randi: the 2nd param must be not equal to the 3rd param"
187
- );
188
- }
167
+ DEV: assert(isFinite(value), "wrap: 1st param must be a number");
168
+ DEV: assert(isFinite(min), "wrap: 2nd param must be a number");
169
+ DEV: assert(isFinite(max), "wrap: 3rd param must be a number");
170
+ DEV: assert(
171
+ max > min,
172
+ "randi: the 2nd param must be less than the 3rd param"
173
+ );
174
+ DEV: assert(
175
+ max !== min,
176
+ "randi: the 2nd param must be not equal to the 3rd param"
177
+ );
189
178
  return value - (max - min) * Math.floor((value - min) / (max - min));
190
179
  },
191
180
  /**
@@ -200,13 +189,11 @@
200
189
  * @returns {number} the remapped number
201
190
  */
202
191
  map(value, start1, stop1, start2, stop2, withinBounds) {
203
- if (true) {
204
- assert(isFinite(value), "map: 1st param must be a number");
205
- assert(isFinite(start1), "map: 2nd param must be a number");
206
- assert(isFinite(stop1), "map: 3rd param must be a number");
207
- assert(isFinite(start2), "map: 4th param must be a number");
208
- assert(isFinite(stop2), "map: 5th param must be a number");
209
- }
192
+ DEV: assert(isFinite(value), "map: 1st param must be a number");
193
+ DEV: assert(isFinite(start1), "map: 2nd param must be a number");
194
+ DEV: assert(isFinite(stop1), "map: 3rd param must be a number");
195
+ DEV: assert(isFinite(start2), "map: 4th param must be a number");
196
+ DEV: assert(isFinite(stop2), "map: 5th param must be a number");
210
197
  const result = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2;
211
198
  return withinBounds ? instance.clamp(result, start2, stop2) : result;
212
199
  },
@@ -221,11 +208,9 @@
221
208
  * @returns {number} the normalized number.
222
209
  */
223
210
  norm: (value, start, stop) => {
224
- if (true) {
225
- assert(isFinite(value), "norm: 1st param must be a number");
226
- assert(isFinite(start), "norm: 2nd param must be a number");
227
- assert(isFinite(stop), "norm: 3rd param must be a number");
228
- }
211
+ DEV: assert(isFinite(value), "norm: 1st param must be a number");
212
+ DEV: assert(isFinite(start), "norm: 2nd param must be a number");
213
+ DEV: assert(isFinite(stop), "norm: 3rd param must be a number");
229
214
  return instance.map(value, start, stop, 0, 1);
230
215
  },
231
216
  /** RNG API */
@@ -238,14 +223,12 @@
238
223
  * @returns {number} the random number
239
224
  */
240
225
  rand: (min = 0, max = 1) => {
241
- if (true) {
242
- assert(isFinite(min), "rand: 1st param must be a number");
243
- assert(isFinite(max), "rand: 2nd param must be a number");
244
- assert(
245
- max > min,
246
- "rand: the 1st param must be less than the 2nd param"
247
- );
248
- }
226
+ DEV: assert(isFinite(min), "rand: 1st param must be a number");
227
+ DEV: assert(isFinite(max), "rand: 2nd param must be a number");
228
+ DEV: assert(
229
+ max > min,
230
+ "rand: the 1st param must be less than the 2nd param"
231
+ );
249
232
  const a = 1664525;
250
233
  const c = 1013904223;
251
234
  const m = 4294967296;
@@ -260,14 +243,12 @@
260
243
  * @returns {number} the random number
261
244
  */
262
245
  randi: (min = 0, max = 1) => {
263
- if (true) {
264
- assert(isFinite(min), "randi: 1st param must be a number");
265
- assert(isFinite(max), "randi: 2nd param must be a number");
266
- assert(
267
- max > min,
268
- "randi: the 1st param must be less than the 2nd param"
269
- );
270
- }
246
+ DEV: assert(isFinite(min), "randi: 1st param must be a number");
247
+ DEV: assert(isFinite(max), "randi: 2nd param must be a number");
248
+ DEV: assert(
249
+ max > min,
250
+ "randi: the 1st param must be less than the 2nd param"
251
+ );
271
252
  return Math.floor(instance.rand(min, max + 1));
272
253
  },
273
254
  /**
@@ -278,12 +259,10 @@
278
259
  * @returns {number} the seed state
279
260
  */
280
261
  seed: (value) => {
281
- if (true) {
282
- assert(
283
- null == value || isFinite(value) && value >= 0,
284
- "seed: 1st param must be a positive number or zero"
285
- );
286
- }
262
+ DEV: assert(
263
+ null == value || isFinite(value) && value >= 0,
264
+ "seed: 1st param must be a positive number or zero"
265
+ );
287
266
  return null == value ? _rng_seed : _rng_seed = ~~value;
288
267
  },
289
268
  /** BASIC GRAPHICS API */
@@ -293,12 +272,10 @@
293
272
  * @param {number?} color The background color (index) or null (for transparent)
294
273
  */
295
274
  cls(color) {
296
- if (true) {
297
- assert(
298
- null == color || isFinite(color) && color >= 0,
299
- "cls: 1st param must be a positive number or zero or null"
300
- );
301
- }
275
+ DEV: assert(
276
+ null == color || isFinite(color) && color >= 0,
277
+ "cls: 1st param must be a positive number or zero or null"
278
+ );
302
279
  if (null == color) {
303
280
  _ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height);
304
281
  } else {
@@ -322,26 +299,24 @@
322
299
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
323
300
  */
324
301
  rect(x, y, width, height, color, radii = null) {
325
- if (true) {
326
- assert(isFinite(x), "rect: 1st param must be a number");
327
- assert(isFinite(y), "rect: 2nd param must be a number");
328
- assert(
329
- isFinite(width) && width > 0,
330
- "rect: 3rd param must be a positive number"
331
- );
332
- assert(
333
- isFinite(height) && height >= 0,
334
- "rect: 4th param must be a positive number or zero"
335
- );
336
- assert(
337
- null == color || isFinite(color) && color >= 0,
338
- "rect: 5th param must be a positive number or zero"
339
- );
340
- assert(
341
- null == radii || isFinite(radii) || Array.isArray(radii) && radii.length >= 1,
342
- "rect: 6th param must be a number or array of numbers"
343
- );
344
- }
302
+ DEV: assert(isFinite(x), "rect: 1st param must be a number");
303
+ DEV: assert(isFinite(y), "rect: 2nd param must be a number");
304
+ DEV: assert(
305
+ isFinite(width) && width > 0,
306
+ "rect: 3rd param must be a positive number"
307
+ );
308
+ DEV: assert(
309
+ isFinite(height) && height >= 0,
310
+ "rect: 4th param must be a positive number or zero"
311
+ );
312
+ DEV: assert(
313
+ null == color || isFinite(color) && color >= 0,
314
+ "rect: 5th param must be a positive number or zero"
315
+ );
316
+ DEV: assert(
317
+ null == radii || isFinite(radii) || Array.isArray(radii) && radii.length >= 1,
318
+ "rect: 6th param must be a number or array of numbers"
319
+ );
345
320
  _ctx.beginPath();
346
321
  _ctx[radii ? "roundRect" : "rect"](
347
322
  ~~x - _outline_fix,
@@ -363,26 +338,24 @@
363
338
  * @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
364
339
  */
365
340
  rectfill(x, y, width, height, color, radii = null) {
366
- if (true) {
367
- assert(isFinite(x), "rectfill: 1st param must be a number");
368
- assert(isFinite(y), "rectfill: 2nd param must be a number");
369
- assert(
370
- isFinite(width) && width >= 0,
371
- "rectfill: 3rd param must be a positive number or zero"
372
- );
373
- assert(
374
- isFinite(height) && height >= 0,
375
- "rectfill: 4th param must be a positive number or zero"
376
- );
377
- assert(
378
- null == color || isFinite(color) && color >= 0,
379
- "rectfill: 5th param must be a positive number or zero"
380
- );
381
- assert(
382
- null == radii || isFinite(radii) || Array.isArray(radii) && radii.length >= 1,
383
- "rectfill: 6th param must be a number or array of at least 2 numbers"
384
- );
385
- }
341
+ DEV: assert(isFinite(x), "rectfill: 1st param must be a number");
342
+ DEV: assert(isFinite(y), "rectfill: 2nd param must be a number");
343
+ DEV: assert(
344
+ isFinite(width) && width >= 0,
345
+ "rectfill: 3rd param must be a positive number or zero"
346
+ );
347
+ DEV: assert(
348
+ isFinite(height) && height >= 0,
349
+ "rectfill: 4th param must be a positive number or zero"
350
+ );
351
+ DEV: assert(
352
+ null == color || isFinite(color) && color >= 0,
353
+ "rectfill: 5th param must be a positive number or zero"
354
+ );
355
+ DEV: assert(
356
+ null == radii || isFinite(radii) || Array.isArray(radii) && radii.length >= 1,
357
+ "rectfill: 6th param must be a number or array of at least 2 numbers"
358
+ );
386
359
  _ctx.beginPath();
387
360
  _ctx[radii ? "roundRect" : "rect"](
388
361
  ~~x,
@@ -402,18 +375,16 @@
402
375
  * @param {number} [color=0] the color index
403
376
  */
404
377
  circ(x, y, radius, color) {
405
- if (true) {
406
- assert(isFinite(x), "circ: 1st param must be a number");
407
- assert(isFinite(y), "circ: 2nd param must be a number");
408
- assert(
409
- isFinite(radius) && radius >= 0,
410
- "circ: 3rd param must be a positive number or zero"
411
- );
412
- assert(
413
- null == color || isFinite(color) && color >= 0,
414
- "circ: 4th param must be a positive number or zero"
415
- );
416
- }
378
+ DEV: assert(isFinite(x), "circ: 1st param must be a number");
379
+ DEV: assert(isFinite(y), "circ: 2nd param must be a number");
380
+ DEV: assert(
381
+ isFinite(radius) && radius >= 0,
382
+ "circ: 3rd param must be a positive number or zero"
383
+ );
384
+ DEV: assert(
385
+ null == color || isFinite(color) && color >= 0,
386
+ "circ: 4th param must be a positive number or zero"
387
+ );
417
388
  _ctx.beginPath();
418
389
  _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
419
390
  instance.stroke(color);
@@ -427,18 +398,16 @@
427
398
  * @param {number} [color=0] the color index
428
399
  */
429
400
  circfill(x, y, radius, color) {
430
- if (true) {
431
- assert(isFinite(x), "circfill: 1st param must be a number");
432
- assert(isFinite(y), "circfill: 2nd param must be a number");
433
- assert(
434
- isFinite(radius) && radius >= 0,
435
- "circfill: 3rd param must be a positive number or zero"
436
- );
437
- assert(
438
- null == color || isFinite(color) && color >= 0,
439
- "circfill: 4th param must be a positive number or zero"
440
- );
441
- }
401
+ DEV: assert(isFinite(x), "circfill: 1st param must be a number");
402
+ DEV: assert(isFinite(y), "circfill: 2nd param must be a number");
403
+ DEV: assert(
404
+ isFinite(radius) && radius >= 0,
405
+ "circfill: 3rd param must be a positive number or zero"
406
+ );
407
+ DEV: assert(
408
+ null == color || isFinite(color) && color >= 0,
409
+ "circfill: 4th param must be a positive number or zero"
410
+ );
442
411
  _ctx.beginPath();
443
412
  _ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
444
413
  instance.fill(color);
@@ -453,22 +422,20 @@
453
422
  * @param {number} [color=0] the color index
454
423
  */
455
424
  line(x1, y1, x2, y2, color) {
456
- if (true) {
457
- assert(isFinite(x1), "line: 1st param must be a number");
458
- assert(isFinite(y1), "line: 2nd param must be a number");
459
- assert(
460
- isFinite(x2),
461
- "line: 3rd param must be a positive number or zero"
462
- );
463
- assert(
464
- isFinite(y2),
465
- "line: 4th param must be a positive number or zero"
466
- );
467
- assert(
468
- null == color || isFinite(color) && color >= 0,
469
- "line: 5th param must be a positive number or zero"
470
- );
471
- }
425
+ DEV: assert(isFinite(x1), "line: 1st param must be a number");
426
+ DEV: assert(isFinite(y1), "line: 2nd param must be a number");
427
+ DEV: assert(
428
+ isFinite(x2),
429
+ "line: 3rd param must be a positive number or zero"
430
+ );
431
+ DEV: assert(
432
+ isFinite(y2),
433
+ "line: 4th param must be a positive number or zero"
434
+ );
435
+ DEV: assert(
436
+ null == color || isFinite(color) && color >= 0,
437
+ "line: 5th param must be a positive number or zero"
438
+ );
472
439
  _ctx.beginPath();
473
440
  let xfix = _outline_fix !== 0 && ~~x1 === ~~x2 ? 0.5 : 0;
474
441
  let yfix = _outline_fix !== 0 && ~~y1 === ~~y2 ? 0.5 : 0;
@@ -483,12 +450,10 @@
483
450
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
484
451
  */
485
452
  linewidth(value) {
486
- if (true) {
487
- assert(
488
- isFinite(value) && ~~value > 0,
489
- "linewidth: 1st param must be a positive number"
490
- );
491
- }
453
+ DEV: assert(
454
+ isFinite(value) && ~~value > 0,
455
+ "linewidth: 1st param must be a positive number"
456
+ );
492
457
  _ctx.lineWidth = ~~value;
493
458
  _outline_fix = ~~value % 2 === 0 ? 0 : 0.5;
494
459
  },
@@ -501,13 +466,14 @@
501
466
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
502
467
  */
503
468
  linedash(segments, offset = 0) {
504
- if (true) {
505
- assert(
506
- Array.isArray(segments) && segments.length > 0,
507
- "linedash: 1st param must be an array of numbers"
508
- );
509
- assert(isFinite(offset), "linedash: 2nd param must be a number");
510
- }
469
+ DEV: assert(
470
+ Array.isArray(segments) && segments.length > 0,
471
+ "linedash: 1st param must be an array of numbers"
472
+ );
473
+ DEV: assert(
474
+ isFinite(offset),
475
+ "linedash: 2nd param must be a number"
476
+ );
511
477
  _ctx.setLineDash(segments);
512
478
  _ctx.lineDashOffset = offset;
513
479
  },
@@ -522,18 +488,16 @@
522
488
  * @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
523
489
  */
524
490
  text(x, y, message, color = 3, fontStyle = "normal") {
525
- if (true) {
526
- assert(isFinite(x), "text: 1st param must be a number");
527
- assert(isFinite(y), "text: 2nd param must be a number");
528
- assert(
529
- null == color || isFinite(color) && color >= 0,
530
- "text: 4th param must be a positive number or zero"
531
- );
532
- assert(
533
- "string" === typeof fontStyle,
534
- "text: 5th param must be a string"
535
- );
536
- }
491
+ DEV: assert(isFinite(x), "text: 1st param must be a number");
492
+ DEV: assert(isFinite(y), "text: 2nd param must be a number");
493
+ DEV: assert(
494
+ null == color || isFinite(color) && color >= 0,
495
+ "text: 4th param must be a positive number or zero"
496
+ );
497
+ DEV: assert(
498
+ "string" === typeof fontStyle,
499
+ "text: 5th param must be a string"
500
+ );
537
501
  _ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`;
538
502
  _ctx.fillStyle = instance.getcolor(color);
539
503
  _ctx.fillText(message, ~~x, ~~y);
@@ -544,12 +508,10 @@
544
508
  * @param {string} family
545
509
  */
546
510
  textfont(family) {
547
- if (true) {
548
- assert(
549
- "string" === typeof family,
550
- "textfont: 1st param must be a string"
551
- );
552
- }
511
+ DEV: assert(
512
+ "string" === typeof family,
513
+ "textfont: 1st param must be a string"
514
+ );
553
515
  _fontFamily = family;
554
516
  },
555
517
  /**
@@ -558,9 +520,7 @@
558
520
  * @param {number} size
559
521
  */
560
522
  textsize(size) {
561
- if (true) {
562
- assert(isFinite(size), "textsize: 1st param must be a number");
563
- }
523
+ DEV: assert(isFinite(size), "textsize: 1st param must be a number");
564
524
  _fontSize = size;
565
525
  },
566
526
  /**
@@ -572,25 +532,21 @@
572
532
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
573
533
  */
574
534
  textalign(align, baseline) {
575
- if (true) {
576
- assert(
577
- null == align || ["left", "right", "center", "start", "end"].includes(
578
- align
579
- ),
580
- "textalign: 1st param must be a string"
581
- );
582
- assert(
583
- null == baseline || [
584
- "top",
585
- "bottom",
586
- "middle",
587
- "hanging",
588
- "alphabetic",
589
- "ideographic"
590
- ].includes(baseline),
591
- "textalign: 2nd param must be a string"
592
- );
593
- }
535
+ DEV: assert(
536
+ null == align || ["left", "right", "center", "start", "end"].includes(align),
537
+ "textalign: 1st param must be a string"
538
+ );
539
+ DEV: assert(
540
+ null == baseline || [
541
+ "top",
542
+ "bottom",
543
+ "middle",
544
+ "hanging",
545
+ "alphabetic",
546
+ "ideographic"
547
+ ].includes(baseline),
548
+ "textalign: 2nd param must be a string"
549
+ );
594
550
  if (align) _ctx.textAlign = align;
595
551
  if (baseline) _ctx.textBaseline = baseline;
596
552
  },
@@ -603,10 +559,8 @@
603
559
  * @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
604
560
  */
605
561
  image(x, y, source) {
606
- if (true) {
607
- assert(isFinite(x), "image: 1st param must be a number");
608
- assert(isFinite(y), "image: 2nd param must be a number");
609
- }
562
+ DEV: assert(isFinite(x), "image: 1st param must be a number");
563
+ DEV: assert(isFinite(y), "image: 2nd param must be a number");
610
564
  _ctx.drawImage(source, ~~x, ~~y);
611
565
  },
612
566
  /**
@@ -622,18 +576,16 @@
622
576
  * @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
623
577
  */
624
578
  paint(width, height, drawing, options = {}) {
625
- if (true) {
626
- assert(isFinite(width), "paint: 1st param must be a number");
627
- assert(isFinite(height), "paint: 2nd param must be a number");
628
- assert(
629
- "function" === typeof drawing || Array.isArray(drawing),
630
- "paint: 3rd param must be a function or array"
631
- );
632
- assert(
633
- options && !options.scale || isFinite(options.scale),
634
- "paint: 4th param (options.scale) must be a number"
635
- );
636
- }
579
+ DEV: assert(isFinite(width), "paint: 1st param must be a number");
580
+ DEV: assert(isFinite(height), "paint: 2nd param must be a number");
581
+ DEV: assert(
582
+ "function" === typeof drawing || Array.isArray(drawing),
583
+ "paint: 3rd param must be a function or array"
584
+ );
585
+ DEV: assert(
586
+ options && !options.scale || isFinite(options.scale),
587
+ "paint: 4th param (options.scale) must be a number"
588
+ );
637
589
  const canvas = options.canvas || new OffscreenCanvas(1, 1), scale = options.scale || 1, contextOriginal = _ctx;
638
590
  canvas.width = width * scale;
639
591
  canvas.height = height * scale;
@@ -667,8 +619,6 @@
667
619
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
668
620
  */
669
621
  ctx(context) {
670
- if (true) {
671
- }
672
622
  if (context) {
673
623
  _ctx = context;
674
624
  }
@@ -693,10 +643,8 @@
693
643
  * @param {number} y
694
644
  */
695
645
  translate: (x, y) => {
696
- if (true) {
697
- assert(isFinite(x), "translate: 1st param must be a number");
698
- assert(isFinite(y), "translate: 2nd param must be a number");
699
- }
646
+ DEV: assert(isFinite(x), "translate: 1st param must be a number");
647
+ DEV: assert(isFinite(y), "translate: 2nd param must be a number");
700
648
  return _ctx.translate(~~x, ~~y);
701
649
  },
702
650
  /**
@@ -706,13 +654,11 @@
706
654
  * @param {number} [y]
707
655
  */
708
656
  scale: (x, y) => {
709
- if (true) {
710
- assert(isFinite(x), "scale: 1st param must be a number");
711
- assert(
712
- y == null || isFinite(y),
713
- "scale: 2nd param must be a number"
714
- );
715
- }
657
+ DEV: assert(isFinite(x), "scale: 1st param must be a number");
658
+ DEV: assert(
659
+ y == null || isFinite(y),
660
+ "scale: 2nd param must be a number"
661
+ );
716
662
  return _ctx.scale(x, y || x);
717
663
  },
718
664
  /**
@@ -721,9 +667,7 @@
721
667
  * @param {number} radians
722
668
  */
723
669
  rotate: (radians) => {
724
- if (true) {
725
- assert(isFinite(radians), "rotate: 1st param must be a number");
726
- }
670
+ DEV: assert(isFinite(radians), "rotate: 1st param must be a number");
727
671
  return _ctx.rotate(radians);
728
672
  },
729
673
  /**
@@ -733,9 +677,7 @@
733
677
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
734
678
  */
735
679
  alpha(value) {
736
- if (true) {
737
- assert(isFinite(value), "alpha: 1st param must be a number");
738
- }
680
+ DEV: assert(isFinite(value), "alpha: 1st param must be a number");
739
681
  _ctx.globalAlpha = instance.clamp(value, 0, 1);
740
682
  },
741
683
  /**
@@ -748,12 +690,10 @@
748
690
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path2D
749
691
  */
750
692
  path: (arg) => {
751
- if (true) {
752
- assert(
753
- null == arg || "string" === typeof arg || arg instanceof Path2D,
754
- "path: 1st param must be a string or a Path2D instance"
755
- );
756
- }
693
+ DEV: assert(
694
+ null == arg || "string" === typeof arg || arg instanceof Path2D,
695
+ "path: 1st param must be a string or a Path2D instance"
696
+ );
757
697
  return new Path2D(arg);
758
698
  },
759
699
  /**
@@ -763,16 +703,14 @@
763
703
  * @param {Path2D} [path]
764
704
  */
765
705
  fill(color, path) {
766
- if (true) {
767
- assert(
768
- null == color || isFinite(color) && color >= 0,
769
- "fill: 1st param must be a positive number or zero"
770
- );
771
- assert(
772
- null == path || path instanceof Path2D,
773
- "fill: 2nd param must be a Path2D instance"
774
- );
775
- }
706
+ DEV: assert(
707
+ null == color || isFinite(color) && color >= 0,
708
+ "fill: 1st param must be a positive number or zero"
709
+ );
710
+ DEV: assert(
711
+ null == path || path instanceof Path2D,
712
+ "fill: 2nd param must be a Path2D instance"
713
+ );
776
714
  _ctx.fillStyle = instance.getcolor(color);
777
715
  if (path) {
778
716
  _ctx.fill(path);
@@ -787,16 +725,14 @@
787
725
  * @param {Path2D} [path]
788
726
  */
789
727
  stroke(color, path) {
790
- if (true) {
791
- assert(
792
- null == color || isFinite(color) && color >= 0,
793
- "stroke: 1st param must be a positive number or zero"
794
- );
795
- assert(
796
- null == path || path instanceof Path2D,
797
- "stroke: 2nd param must be a Path2D instance"
798
- );
799
- }
728
+ DEV: assert(
729
+ null == color || isFinite(color) && color >= 0,
730
+ "stroke: 1st param must be a positive number or zero"
731
+ );
732
+ DEV: assert(
733
+ null == path || path instanceof Path2D,
734
+ "stroke: 2nd param must be a Path2D instance"
735
+ );
800
736
  _ctx.strokeStyle = instance.getcolor(color);
801
737
  if (path) {
802
738
  _ctx.stroke(path);
@@ -811,12 +747,10 @@
811
747
  * @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
812
748
  */
813
749
  clip(path) {
814
- if (true) {
815
- assert(
816
- path instanceof Path2D,
817
- "clip: 1st param must be a Path2D instance"
818
- );
819
- }
750
+ DEV: assert(
751
+ path instanceof Path2D,
752
+ "clip: 1st param must be a Path2D instance"
753
+ );
820
754
  _ctx.clip(path);
821
755
  },
822
756
  /** SOUND API */
@@ -832,17 +766,15 @@
832
766
  * @see https://github.com/KilledByAPixel/ZzFX
833
767
  */
834
768
  sfx(zzfxParams, pitchSlide = 0, volumeFactor = 1) {
835
- if (true) {
836
- assert(
837
- null == zzfxParams || Array.isArray(zzfxParams),
838
- "sfx: 1st param must be an array"
839
- );
840
- assert(isFinite(pitchSlide), "sfx: 2nd param must be a number");
841
- assert(
842
- isFinite(volumeFactor),
843
- "sfx: 3rd param must be a number"
844
- );
845
- }
769
+ DEV: assert(
770
+ null == zzfxParams || Array.isArray(zzfxParams),
771
+ "sfx: 1st param must be an array"
772
+ );
773
+ DEV: assert(isFinite(pitchSlide), "sfx: 2nd param must be a number");
774
+ DEV: assert(
775
+ isFinite(volumeFactor),
776
+ "sfx: 3rd param must be a number"
777
+ );
846
778
  if (root.zzfxV <= 0 || navigator.userActivation && !navigator.userActivation.hasBeenActive) {
847
779
  return false;
848
780
  }
@@ -862,9 +794,7 @@
862
794
  * @param {number} value
863
795
  */
864
796
  volume(value) {
865
- if (true) {
866
- assert(isFinite(value), "volume: 1st param must be a number");
867
- }
797
+ DEV: assert(isFinite(value), "volume: 1st param must be a number");
868
798
  root.zzfxV = value;
869
799
  },
870
800
  /** UTILS API */
@@ -882,16 +812,14 @@
882
812
  * @returns {boolean}
883
813
  */
884
814
  colrect: (x1, y1, w1, h1, x2, y2, w2, h2) => {
885
- if (true) {
886
- assert(isFinite(x1), "colrect: 1st param must be a number");
887
- assert(isFinite(y1), "colrect: 2nd param must be a number");
888
- assert(isFinite(w1), "colrect: 3rd param must be a number");
889
- assert(isFinite(h1), "colrect: 4th param must be a number");
890
- assert(isFinite(x2), "colrect: 5th param must be a number");
891
- assert(isFinite(y2), "colrect: 6th param must be a number");
892
- assert(isFinite(w2), "colrect: 7th param must be a number");
893
- assert(isFinite(h2), "colrect: 8th param must be a number");
894
- }
815
+ DEV: assert(isFinite(x1), "colrect: 1st param must be a number");
816
+ DEV: assert(isFinite(y1), "colrect: 2nd param must be a number");
817
+ DEV: assert(isFinite(w1), "colrect: 3rd param must be a number");
818
+ DEV: assert(isFinite(h1), "colrect: 4th param must be a number");
819
+ DEV: assert(isFinite(x2), "colrect: 5th param must be a number");
820
+ DEV: assert(isFinite(y2), "colrect: 6th param must be a number");
821
+ DEV: assert(isFinite(w2), "colrect: 7th param must be a number");
822
+ DEV: assert(isFinite(h2), "colrect: 8th param must be a number");
895
823
  return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2;
896
824
  },
897
825
  /**
@@ -906,14 +834,12 @@
906
834
  * @returns {boolean}
907
835
  */
908
836
  colcirc: (x1, y1, r1, x2, y2, r2) => {
909
- if (true) {
910
- assert(isFinite(x1), "colcirc: 1st param must be a number");
911
- assert(isFinite(y1), "colcirc: 2nd param must be a number");
912
- assert(isFinite(r1), "colcirc: 3rd param must be a number");
913
- assert(isFinite(x2), "colcirc: 4th param must be a number");
914
- assert(isFinite(y2), "colcirc: 5th param must be a number");
915
- assert(isFinite(r2), "colcirc: 6th param must be a number");
916
- }
837
+ DEV: assert(isFinite(x1), "colcirc: 1st param must be a number");
838
+ DEV: assert(isFinite(y1), "colcirc: 2nd param must be a number");
839
+ DEV: assert(isFinite(r1), "colcirc: 3rd param must be a number");
840
+ DEV: assert(isFinite(x2), "colcirc: 4th param must be a number");
841
+ DEV: assert(isFinite(y2), "colcirc: 5th param must be a number");
842
+ DEV: assert(isFinite(r2), "colcirc: 6th param must be a number");
917
843
  return (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= (r1 + r2) * (r1 + r2);
918
844
  },
919
845
  /** PLUGINS API */
@@ -923,16 +849,14 @@
923
849
  * @param {pluginCallback} callback
924
850
  */
925
851
  use(callback, config = {}) {
926
- if (true) {
927
- assert(
928
- "function" === typeof callback,
929
- "use: 1st param must be a function"
930
- );
931
- assert(
932
- "object" === typeof config,
933
- "use: 2nd param must be an object"
934
- );
935
- }
852
+ DEV: assert(
853
+ "function" === typeof callback,
854
+ "use: 1st param must be a function"
855
+ );
856
+ DEV: assert(
857
+ "object" === typeof config,
858
+ "use: 2nd param must be an object"
859
+ );
936
860
  _initialized ? loadPlugin(callback, config) : _plugins.push([callback, config]);
937
861
  },
938
862
  /**
@@ -943,16 +867,14 @@
943
867
  * @returns {Function} a function to remove the listener
944
868
  */
945
869
  listen(eventName, callback) {
946
- if (true) {
947
- assert(
948
- "string" === typeof eventName,
949
- "listen: 1st param must be a string"
950
- );
951
- assert(
952
- "function" === typeof callback,
953
- "listen: 2nd param must be a function"
954
- );
955
- }
870
+ DEV: assert(
871
+ "string" === typeof eventName,
872
+ "listen: 1st param must be a string"
873
+ );
874
+ DEV: assert(
875
+ "function" === typeof callback,
876
+ "listen: 2nd param must be a function"
877
+ );
956
878
  _events[eventName] = _events[eventName] || /* @__PURE__ */ new Set();
957
879
  _events[eventName].add(callback);
958
880
  return () => _events[eventName].delete(callback);
@@ -967,15 +889,15 @@
967
889
  * @param {*} [arg4] any data to be passed over the listeners
968
890
  */
969
891
  emit(eventName, arg1, arg2, arg3, arg4) {
970
- if (true) {
971
- assert(
972
- "string" === typeof eventName,
973
- "emit: 1st param must be a string"
974
- );
892
+ DEV: assert(
893
+ "string" === typeof eventName,
894
+ "emit: 1st param must be a string"
895
+ );
896
+ if (_initialized) {
897
+ triggerEvent("before:" + eventName, arg1, arg2, arg3, arg4);
898
+ triggerEvent(eventName, arg1, arg2, arg3, arg4);
899
+ triggerEvent("after:" + eventName, arg1, arg2, arg3, arg4);
975
900
  }
976
- triggerEvent("before:" + eventName, arg1, arg2, arg3, arg4);
977
- triggerEvent(eventName, arg1, arg2, arg3, arg4);
978
- triggerEvent("after:" + eventName, arg1, arg2, arg3, arg4);
979
901
  },
980
902
  /**
981
903
  * Get a color by index
@@ -984,12 +906,10 @@
984
906
  * @returns {string} the color code
985
907
  */
986
908
  getcolor: (index) => {
987
- if (true) {
988
- assert(
989
- null == index || isFinite(index) && index >= 0,
990
- "getcolor: 1st param must be a number"
991
- );
992
- }
909
+ DEV: assert(
910
+ null == index || isFinite(index) && index >= 0,
911
+ "getcolor: 1st param must be a number"
912
+ );
993
913
  return colors[~~index % colors.length];
994
914
  },
995
915
  /**
@@ -999,13 +919,12 @@
999
919
  * @param {*} value
1000
920
  */
1001
921
  setvar(key, value) {
1002
- if (true) {
1003
- assert(
1004
- "string" === typeof key,
1005
- "setvar: 1st param must be a string"
1006
- );
1007
- if (value == null) {
1008
- }
922
+ DEV: assert(
923
+ "string" === typeof key,
924
+ "setvar: 1st param must be a string"
925
+ );
926
+ if (value == null) {
927
+ console.warn(`setvar: key "${key}" was defined as ${value}`);
1009
928
  }
1010
929
  instance[key] = value;
1011
930
  if (_global) {
@@ -1019,13 +938,19 @@
1019
938
  * @param {number} height
1020
939
  */
1021
940
  resize(width, height) {
1022
- if (true) {
1023
- assert(isFinite(width), "resize: 1st param must be a number");
1024
- assert(isFinite(height), "resize: 2nd param must be a number");
1025
- }
941
+ DEV: assert(
942
+ isFinite(width) && width > 0,
943
+ "resize: 1st param must be a number"
944
+ );
945
+ DEV: assert(
946
+ isFinite(height) && height > 0,
947
+ "resize: 2nd param must be a number"
948
+ );
1026
949
  instance.setvar("WIDTH", _canvas.width = width);
1027
950
  instance.setvar("HEIGHT", _canvas.height = height);
1028
- pageResized();
951
+ instance.setvar("CENTERX", instance.WIDTH / 2);
952
+ instance.setvar("CENTERY", instance.HEIGHT / 2);
953
+ onResize();
1029
954
  },
1030
955
  /**
1031
956
  * The scale of the game's delta time (dt).
@@ -1035,9 +960,10 @@
1035
960
  * @param {number} value
1036
961
  */
1037
962
  timescale(value) {
1038
- if (true) {
1039
- assert(isFinite(value), "timescale: 1st param must be a number");
1040
- }
963
+ DEV: assert(
964
+ isFinite(value),
965
+ "timescale: 1st param must be a number"
966
+ );
1041
967
  _timeScale = value;
1042
968
  },
1043
969
  /**
@@ -1046,12 +972,10 @@
1046
972
  * @param {number} value
1047
973
  */
1048
974
  setfps(value) {
1049
- if (true) {
1050
- assert(
1051
- isFinite(value) && value >= 1,
1052
- "setfps: 1st param must be a positive number"
1053
- );
1054
- }
975
+ DEV: assert(
976
+ isFinite(value) && value >= 1,
977
+ "setfps: 1st param must be a positive number"
978
+ );
1055
979
  _deltaTime = 1 / ~~value;
1056
980
  },
1057
981
  /**
@@ -1084,10 +1008,9 @@
1084
1008
  for (const [callback, config] of _plugins) {
1085
1009
  loadPlugin(callback, config);
1086
1010
  }
1087
- if (_fullscreen || _autoscale) {
1088
- on(root, "resize", pageResized);
1011
+ if (_autoscale) {
1012
+ on(root, "resize", onResize);
1089
1013
  }
1090
- pageResized();
1091
1014
  if (settings.tapEvents) {
1092
1015
  const _getXY = (pageX, pageY) => [
1093
1016
  (pageX - _canvas.offsetLeft) / _scale,
@@ -1188,12 +1111,10 @@
1188
1111
  if (settings.keyboardEvents) {
1189
1112
  const _keys = /* @__PURE__ */ new Set();
1190
1113
  const iskeydown = (key) => {
1191
- if (true) {
1192
- assert(
1193
- "string" === typeof key,
1194
- "iskeydown: 1st param must be a string"
1195
- );
1196
- }
1114
+ DEV: assert(
1115
+ "string" === typeof key,
1116
+ "iskeydown: 1st param must be a string"
1117
+ );
1197
1118
  return "any" === key ? _keys.size > 0 : _keys.has(key.toLowerCase());
1198
1119
  };
1199
1120
  instance.setvar("iskeydown", iskeydown);
@@ -1226,72 +1147,71 @@
1226
1147
  }
1227
1148
  let updated = 0, frameTime = (now - _lastFrameTime) / 1e3;
1228
1149
  _lastFrameTime = now;
1229
- if (frameTime > _deltaTime * 30)
1230
- return void 0;
1231
- _accumulated += frameTime;
1232
- if (!_animated) {
1233
- _accumulated = _deltaTime;
1234
- }
1235
- for (; _accumulated >= _deltaTime; _accumulated -= _deltaTime) {
1236
- instance.emit("update", _deltaTime * _timeScale);
1237
- instance.setvar(
1238
- "ELAPSED",
1239
- instance.ELAPSED + _deltaTime * _timeScale
1240
- );
1241
- updated++;
1150
+ if (frameTime > _deltaTime * 30) {
1151
+ console.log("skipping too long frame");
1152
+ } else {
1153
+ _accumulated += frameTime;
1154
+ if (!_animated) {
1155
+ _accumulated = _deltaTime;
1156
+ }
1157
+ for (; _accumulated >= _deltaTime; _accumulated -= _deltaTime) {
1158
+ instance.emit("update", _deltaTime * _timeScale);
1159
+ instance.setvar(
1160
+ "ELAPSED",
1161
+ instance.ELAPSED + _deltaTime * _timeScale
1162
+ );
1163
+ updated++;
1164
+ }
1242
1165
  }
1243
- if (updated) {
1166
+ if (updated || !_animated) {
1244
1167
  instance.textalign("start", "top");
1245
1168
  instance.emit("draw");
1246
1169
  }
1247
1170
  }
1248
1171
  function setupCanvas() {
1249
1172
  _canvas = "string" === typeof _canvas ? document.querySelector(_canvas) : _canvas;
1250
- if (true) {
1251
- assert(
1252
- _canvas && _canvas.tagName === "CANVAS",
1253
- "Invalid canvas element"
1254
- );
1255
- assert(
1256
- null === instance.WIDTH || instance.WIDTH > 0,
1257
- `Litecanvas' "width" option should be null or a positive number`
1258
- );
1259
- assert(
1260
- null === instance.HEIGHT || instance.HEIGHT > 0,
1261
- `Litecanvas' "width" option should be null or a positive number`
1262
- );
1263
- }
1173
+ DEV: assert(
1174
+ _canvas && _canvas.tagName === "CANVAS",
1175
+ "Invalid canvas element"
1176
+ );
1177
+ DEV: assert(
1178
+ null == instance.WIDTH || instance.WIDTH > 0,
1179
+ `Litecanvas' "width" option should be null or a positive number`
1180
+ );
1181
+ DEV: assert(
1182
+ null == instance.HEIGHT || instance.HEIGHT > 0,
1183
+ `Litecanvas' "width" option should be null or a positive number`
1184
+ );
1185
+ DEV: assert(
1186
+ null == instance.HEIGHT || instance.WIDTH > 0 && instance.HEIGHT > 0,
1187
+ `Litecanvas' "width" is required when "heigth" is passed`
1188
+ );
1264
1189
  instance.setvar("CANVAS", _canvas);
1265
1190
  _ctx = _canvas.getContext("2d");
1266
1191
  on(_canvas, "click", () => root.focus());
1267
- if (instance.WIDTH > 0) {
1268
- _fullscreen = false;
1269
- }
1270
1192
  _canvas.style = "";
1271
- _canvas.width = instance.WIDTH;
1272
- _canvas.height = instance.HEIGHT || instance.WIDTH;
1193
+ if (!instance.WIDTH) {
1194
+ instance.WIDTH = root.innerWidth;
1195
+ instance.HEIGHT = root.innerHeight;
1196
+ }
1197
+ instance.resize(instance.WIDTH, instance.HEIGHT, false);
1273
1198
  if (!_canvas.parentNode) document.body.appendChild(_canvas);
1274
1199
  }
1275
- function pageResized() {
1276
- const pageWidth = root.innerWidth, pageHeight = root.innerHeight, styles = _canvas.style;
1277
- styles.display = "block";
1278
- if (_fullscreen) {
1279
- styles.position = "absolute";
1280
- styles.inset = 0;
1281
- instance.setvar("WIDTH", _canvas.width = pageWidth);
1282
- instance.setvar("HEIGHT", _canvas.height = pageHeight);
1283
- } else if (_autoscale) {
1284
- styles.margin = "auto";
1200
+ function onResize() {
1201
+ const styles = _canvas.style;
1202
+ if (_autoscale) {
1203
+ if (!styles.display) {
1204
+ styles.display = "block";
1205
+ styles.margin = "auto";
1206
+ }
1285
1207
  _scale = Math.min(
1286
- pageWidth / instance.WIDTH,
1287
- pageHeight / instance.HEIGHT
1208
+ root.innerWidth / instance.WIDTH,
1209
+ root.innerHeight / instance.HEIGHT
1288
1210
  );
1289
1211
  _scale = (settings.pixelart ? ~~_scale : _scale) || 1;
1290
1212
  styles.width = instance.WIDTH * _scale + "px";
1291
1213
  styles.height = instance.HEIGHT * _scale + "px";
1292
1214
  }
1293
- instance.setvar("CENTERX", instance.WIDTH / 2);
1294
- instance.setvar("CENTERY", instance.HEIGHT / 2);
1295
1215
  if (!settings.antialias || settings.pixelart) {
1296
1216
  _ctx.imageSmoothingEnabled = false;
1297
1217
  styles.imageRendering = "pixelated";
@@ -1309,12 +1229,10 @@
1309
1229
  }
1310
1230
  function loadPlugin(callback, config) {
1311
1231
  const pluginData = callback(instance, _helpers, config);
1312
- if (true) {
1313
- assert(
1314
- null == pluginData || "object" === typeof pluginData,
1315
- "Litecanvas plugins should return an object or nothing"
1316
- );
1317
- }
1232
+ DEV: assert(
1233
+ null == pluginData || "object" === typeof pluginData,
1234
+ "Litecanvas plugins should return an object or nothing"
1235
+ );
1318
1236
  for (const key in pluginData) {
1319
1237
  instance.setvar(key, pluginData[key]);
1320
1238
  }