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/README.md +1 -1
- package/dist/dist.dev.js +342 -424
- package/dist/dist.js +40 -424
- package/dist/dist.min.js +1 -1
- package/package.json +4 -4
- package/src/index.js +406 -447
- package/src/types.js +0 -2
- package/types/types.d.ts +1 -9
package/src/index.js
CHANGED
|
@@ -26,7 +26,6 @@ export default function litecanvas(settings = {}) {
|
|
|
26
26
|
isFinite = Number.isFinite,
|
|
27
27
|
/** @type {LitecanvasOptions} */
|
|
28
28
|
defaults = {
|
|
29
|
-
fullscreen: true,
|
|
30
29
|
width: null,
|
|
31
30
|
height: null,
|
|
32
31
|
autoscale: true,
|
|
@@ -51,8 +50,6 @@ export default function litecanvas(settings = {}) {
|
|
|
51
50
|
/** @type {HTMLCanvasElement|string} _canvas */
|
|
52
51
|
_canvas = settings.canvas || document.createElement('canvas'),
|
|
53
52
|
/** @type {boolean} */
|
|
54
|
-
_fullscreen = settings.fullscreen,
|
|
55
|
-
/** @type {boolean} */
|
|
56
53
|
_autoscale = settings.autoscale,
|
|
57
54
|
/** @type {boolean} */
|
|
58
55
|
_animated = settings.animate,
|
|
@@ -163,11 +160,10 @@ export default function litecanvas(settings = {}) {
|
|
|
163
160
|
* @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
|
|
164
161
|
*/
|
|
165
162
|
lerp: (start, end, t) => {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
}
|
|
163
|
+
DEV: assert(isFinite(start), 'lerp: 1st param must be a number')
|
|
164
|
+
DEV: assert(isFinite(end), 'lerp: 2nd param must be a number')
|
|
165
|
+
DEV: assert(isFinite(t), 'lerp: 3rd param must be a number')
|
|
166
|
+
|
|
171
167
|
return t * (end - start) + start
|
|
172
168
|
},
|
|
173
169
|
|
|
@@ -178,9 +174,8 @@ export default function litecanvas(settings = {}) {
|
|
|
178
174
|
* @returns {number} the value in radians
|
|
179
175
|
*/
|
|
180
176
|
deg2rad: (degs) => {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
177
|
+
DEV: assert(isFinite(degs), 'deg2rad: 1st param must be a number')
|
|
178
|
+
|
|
184
179
|
return (PI / 180) * degs
|
|
185
180
|
},
|
|
186
181
|
|
|
@@ -191,9 +186,8 @@ export default function litecanvas(settings = {}) {
|
|
|
191
186
|
* @returns {number} the value in degrees
|
|
192
187
|
*/
|
|
193
188
|
rad2deg: (rads) => {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
189
|
+
DEV: assert(isFinite(rads), 'rad2deg: 1st param must be a number')
|
|
190
|
+
|
|
197
191
|
return (180 / PI) * rads
|
|
198
192
|
},
|
|
199
193
|
|
|
@@ -206,15 +200,14 @@ export default function litecanvas(settings = {}) {
|
|
|
206
200
|
* @returns {number}
|
|
207
201
|
*/
|
|
208
202
|
clamp: (value, min, max) => {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
}
|
|
203
|
+
DEV: assert(isFinite(value), 'clamp: 1st param must be a number')
|
|
204
|
+
DEV: assert(isFinite(min), 'clamp: 2nd param must be a number')
|
|
205
|
+
DEV: assert(isFinite(max), 'clamp: 3rd param must be a number')
|
|
206
|
+
DEV: assert(
|
|
207
|
+
max > min,
|
|
208
|
+
'randi: the 2nd param must be less than the 3rd param'
|
|
209
|
+
)
|
|
210
|
+
|
|
218
211
|
if (value < min) return min
|
|
219
212
|
if (value > max) return max
|
|
220
213
|
return value
|
|
@@ -229,19 +222,18 @@ export default function litecanvas(settings = {}) {
|
|
|
229
222
|
* @returns {number}
|
|
230
223
|
*/
|
|
231
224
|
wrap: (value, min, max) => {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
}
|
|
225
|
+
DEV: assert(isFinite(value), 'wrap: 1st param must be a number')
|
|
226
|
+
DEV: assert(isFinite(min), 'wrap: 2nd param must be a number')
|
|
227
|
+
DEV: assert(isFinite(max), 'wrap: 3rd param must be a number')
|
|
228
|
+
DEV: assert(
|
|
229
|
+
max > min,
|
|
230
|
+
'randi: the 2nd param must be less than the 3rd param'
|
|
231
|
+
)
|
|
232
|
+
DEV: assert(
|
|
233
|
+
max !== min,
|
|
234
|
+
'randi: the 2nd param must be not equal to the 3rd param'
|
|
235
|
+
)
|
|
236
|
+
|
|
245
237
|
return value - (max - min) * Math.floor((value - min) / (max - min))
|
|
246
238
|
},
|
|
247
239
|
|
|
@@ -257,13 +249,12 @@ export default function litecanvas(settings = {}) {
|
|
|
257
249
|
* @returns {number} the remapped number
|
|
258
250
|
*/
|
|
259
251
|
map(value, start1, stop1, start2, stop2, withinBounds) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
}
|
|
252
|
+
DEV: assert(isFinite(value), 'map: 1st param must be a number')
|
|
253
|
+
DEV: assert(isFinite(start1), 'map: 2nd param must be a number')
|
|
254
|
+
DEV: assert(isFinite(stop1), 'map: 3rd param must be a number')
|
|
255
|
+
DEV: assert(isFinite(start2), 'map: 4th param must be a number')
|
|
256
|
+
DEV: assert(isFinite(stop2), 'map: 5th param must be a number')
|
|
257
|
+
|
|
267
258
|
// prettier-ignore
|
|
268
259
|
const result = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
|
|
269
260
|
return withinBounds ? instance.clamp(result, start2, stop2) : result
|
|
@@ -280,11 +271,10 @@ export default function litecanvas(settings = {}) {
|
|
|
280
271
|
* @returns {number} the normalized number.
|
|
281
272
|
*/
|
|
282
273
|
norm: (value, start, stop) => {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
274
|
+
DEV: assert(isFinite(value), 'norm: 1st param must be a number')
|
|
275
|
+
DEV: assert(isFinite(start), 'norm: 2nd param must be a number')
|
|
276
|
+
DEV: assert(isFinite(stop), 'norm: 3rd param must be a number')
|
|
277
|
+
|
|
288
278
|
return instance.map(value, start, stop, 0, 1)
|
|
289
279
|
},
|
|
290
280
|
|
|
@@ -298,14 +288,13 @@ export default function litecanvas(settings = {}) {
|
|
|
298
288
|
* @returns {number} the random number
|
|
299
289
|
*/
|
|
300
290
|
rand: (min = 0.0, max = 1.0) => {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
}
|
|
291
|
+
DEV: assert(isFinite(min), 'rand: 1st param must be a number')
|
|
292
|
+
DEV: assert(isFinite(max), 'rand: 2nd param must be a number')
|
|
293
|
+
DEV: assert(
|
|
294
|
+
max > min,
|
|
295
|
+
'rand: the 1st param must be less than the 2nd param'
|
|
296
|
+
)
|
|
297
|
+
|
|
309
298
|
const a = 1664525
|
|
310
299
|
const c = 1013904223
|
|
311
300
|
const m = 4294967296
|
|
@@ -323,14 +312,13 @@ export default function litecanvas(settings = {}) {
|
|
|
323
312
|
* @returns {number} the random number
|
|
324
313
|
*/
|
|
325
314
|
randi: (min = 0, max = 1) => {
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
315
|
+
DEV: assert(isFinite(min), 'randi: 1st param must be a number')
|
|
316
|
+
DEV: assert(isFinite(max), 'randi: 2nd param must be a number')
|
|
317
|
+
DEV: assert(
|
|
318
|
+
max > min,
|
|
319
|
+
'randi: the 1st param must be less than the 2nd param'
|
|
320
|
+
)
|
|
321
|
+
|
|
334
322
|
return Math.floor(instance.rand(min, max + 1))
|
|
335
323
|
},
|
|
336
324
|
|
|
@@ -342,12 +330,11 @@ export default function litecanvas(settings = {}) {
|
|
|
342
330
|
* @returns {number} the seed state
|
|
343
331
|
*/
|
|
344
332
|
seed: (value) => {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
333
|
+
DEV: assert(
|
|
334
|
+
null == value || (isFinite(value) && value >= 0),
|
|
335
|
+
'seed: 1st param must be a positive number or zero'
|
|
336
|
+
)
|
|
337
|
+
|
|
351
338
|
return null == value ? _rng_seed : (_rng_seed = ~~value)
|
|
352
339
|
},
|
|
353
340
|
|
|
@@ -358,12 +345,11 @@ export default function litecanvas(settings = {}) {
|
|
|
358
345
|
* @param {number?} color The background color (index) or null (for transparent)
|
|
359
346
|
*/
|
|
360
347
|
cls(color) {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
348
|
+
DEV: assert(
|
|
349
|
+
null == color || (isFinite(color) && color >= 0),
|
|
350
|
+
'cls: 1st param must be a positive number or zero or null'
|
|
351
|
+
)
|
|
352
|
+
|
|
367
353
|
if (null == color) {
|
|
368
354
|
_ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height)
|
|
369
355
|
} else {
|
|
@@ -388,28 +374,27 @@ export default function litecanvas(settings = {}) {
|
|
|
388
374
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
389
375
|
*/
|
|
390
376
|
rect(x, y, width, height, color, radii = null) {
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
377
|
+
DEV: assert(isFinite(x), 'rect: 1st param must be a number')
|
|
378
|
+
DEV: assert(isFinite(y), 'rect: 2nd param must be a number')
|
|
379
|
+
DEV: assert(
|
|
380
|
+
isFinite(width) && width > 0,
|
|
381
|
+
'rect: 3rd param must be a positive number'
|
|
382
|
+
)
|
|
383
|
+
DEV: assert(
|
|
384
|
+
isFinite(height) && height >= 0,
|
|
385
|
+
'rect: 4th param must be a positive number or zero'
|
|
386
|
+
)
|
|
387
|
+
DEV: assert(
|
|
388
|
+
null == color || (isFinite(color) && color >= 0),
|
|
389
|
+
'rect: 5th param must be a positive number or zero'
|
|
390
|
+
)
|
|
391
|
+
DEV: assert(
|
|
392
|
+
null == radii ||
|
|
393
|
+
isFinite(radii) ||
|
|
394
|
+
(Array.isArray(radii) && radii.length >= 1),
|
|
395
|
+
'rect: 6th param must be a number or array of numbers'
|
|
396
|
+
)
|
|
397
|
+
|
|
413
398
|
_ctx.beginPath()
|
|
414
399
|
_ctx[radii ? 'roundRect' : 'rect'](
|
|
415
400
|
~~x - _outline_fix,
|
|
@@ -432,28 +417,27 @@ export default function litecanvas(settings = {}) {
|
|
|
432
417
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
433
418
|
*/
|
|
434
419
|
rectfill(x, y, width, height, color, radii = null) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
}
|
|
420
|
+
DEV: assert(isFinite(x), 'rectfill: 1st param must be a number')
|
|
421
|
+
DEV: assert(isFinite(y), 'rectfill: 2nd param must be a number')
|
|
422
|
+
DEV: assert(
|
|
423
|
+
isFinite(width) && width >= 0,
|
|
424
|
+
'rectfill: 3rd param must be a positive number or zero'
|
|
425
|
+
)
|
|
426
|
+
DEV: assert(
|
|
427
|
+
isFinite(height) && height >= 0,
|
|
428
|
+
'rectfill: 4th param must be a positive number or zero'
|
|
429
|
+
)
|
|
430
|
+
DEV: assert(
|
|
431
|
+
null == color || (isFinite(color) && color >= 0),
|
|
432
|
+
'rectfill: 5th param must be a positive number or zero'
|
|
433
|
+
)
|
|
434
|
+
DEV: assert(
|
|
435
|
+
null == radii ||
|
|
436
|
+
isFinite(radii) ||
|
|
437
|
+
(Array.isArray(radii) && radii.length >= 1),
|
|
438
|
+
'rectfill: 6th param must be a number or array of at least 2 numbers'
|
|
439
|
+
)
|
|
440
|
+
|
|
457
441
|
_ctx.beginPath()
|
|
458
442
|
_ctx[radii ? 'roundRect' : 'rect'](
|
|
459
443
|
~~x,
|
|
@@ -474,18 +458,17 @@ export default function litecanvas(settings = {}) {
|
|
|
474
458
|
* @param {number} [color=0] the color index
|
|
475
459
|
*/
|
|
476
460
|
circ(x, y, radius, color) {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
461
|
+
DEV: assert(isFinite(x), 'circ: 1st param must be a number')
|
|
462
|
+
DEV: assert(isFinite(y), 'circ: 2nd param must be a number')
|
|
463
|
+
DEV: assert(
|
|
464
|
+
isFinite(radius) && radius >= 0,
|
|
465
|
+
'circ: 3rd param must be a positive number or zero'
|
|
466
|
+
)
|
|
467
|
+
DEV: assert(
|
|
468
|
+
null == color || (isFinite(color) && color >= 0),
|
|
469
|
+
'circ: 4th param must be a positive number or zero'
|
|
470
|
+
)
|
|
471
|
+
|
|
489
472
|
_ctx.beginPath()
|
|
490
473
|
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
|
|
491
474
|
instance.stroke(color)
|
|
@@ -500,18 +483,17 @@ export default function litecanvas(settings = {}) {
|
|
|
500
483
|
* @param {number} [color=0] the color index
|
|
501
484
|
*/
|
|
502
485
|
circfill(x, y, radius, color) {
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
}
|
|
486
|
+
DEV: assert(isFinite(x), 'circfill: 1st param must be a number')
|
|
487
|
+
DEV: assert(isFinite(y), 'circfill: 2nd param must be a number')
|
|
488
|
+
DEV: assert(
|
|
489
|
+
isFinite(radius) && radius >= 0,
|
|
490
|
+
'circfill: 3rd param must be a positive number or zero'
|
|
491
|
+
)
|
|
492
|
+
DEV: assert(
|
|
493
|
+
null == color || (isFinite(color) && color >= 0),
|
|
494
|
+
'circfill: 4th param must be a positive number or zero'
|
|
495
|
+
)
|
|
496
|
+
|
|
515
497
|
_ctx.beginPath()
|
|
516
498
|
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI)
|
|
517
499
|
instance.fill(color)
|
|
@@ -527,22 +509,21 @@ export default function litecanvas(settings = {}) {
|
|
|
527
509
|
* @param {number} [color=0] the color index
|
|
528
510
|
*/
|
|
529
511
|
line(x1, y1, x2, y2, color) {
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
}
|
|
512
|
+
DEV: assert(isFinite(x1), 'line: 1st param must be a number')
|
|
513
|
+
DEV: assert(isFinite(y1), 'line: 2nd param must be a number')
|
|
514
|
+
DEV: assert(
|
|
515
|
+
isFinite(x2),
|
|
516
|
+
'line: 3rd param must be a positive number or zero'
|
|
517
|
+
)
|
|
518
|
+
DEV: assert(
|
|
519
|
+
isFinite(y2),
|
|
520
|
+
'line: 4th param must be a positive number or zero'
|
|
521
|
+
)
|
|
522
|
+
DEV: assert(
|
|
523
|
+
null == color || (isFinite(color) && color >= 0),
|
|
524
|
+
'line: 5th param must be a positive number or zero'
|
|
525
|
+
)
|
|
526
|
+
|
|
546
527
|
_ctx.beginPath()
|
|
547
528
|
|
|
548
529
|
let xfix = _outline_fix !== 0 && ~~x1 === ~~x2 ? 0.5 : 0
|
|
@@ -561,12 +542,11 @@ export default function litecanvas(settings = {}) {
|
|
|
561
542
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
|
|
562
543
|
*/
|
|
563
544
|
linewidth(value) {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
}
|
|
545
|
+
DEV: assert(
|
|
546
|
+
isFinite(value) && ~~value > 0,
|
|
547
|
+
'linewidth: 1st param must be a positive number'
|
|
548
|
+
)
|
|
549
|
+
|
|
570
550
|
_ctx.lineWidth = ~~value
|
|
571
551
|
_outline_fix = ~~value % 2 === 0 ? 0 : 0.5
|
|
572
552
|
},
|
|
@@ -580,13 +560,15 @@ export default function litecanvas(settings = {}) {
|
|
|
580
560
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineDashOffset
|
|
581
561
|
*/
|
|
582
562
|
linedash(segments, offset = 0) {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
563
|
+
DEV: assert(
|
|
564
|
+
Array.isArray(segments) && segments.length > 0,
|
|
565
|
+
'linedash: 1st param must be an array of numbers'
|
|
566
|
+
)
|
|
567
|
+
DEV: assert(
|
|
568
|
+
isFinite(offset),
|
|
569
|
+
'linedash: 2nd param must be a number'
|
|
570
|
+
)
|
|
571
|
+
|
|
590
572
|
_ctx.setLineDash(segments)
|
|
591
573
|
_ctx.lineDashOffset = offset
|
|
592
574
|
},
|
|
@@ -602,22 +584,21 @@ export default function litecanvas(settings = {}) {
|
|
|
602
584
|
* @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
|
|
603
585
|
*/
|
|
604
586
|
text(x, y, message, color = 3, fontStyle = 'normal') {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
}
|
|
587
|
+
DEV: assert(isFinite(x), 'text: 1st param must be a number')
|
|
588
|
+
DEV: assert(isFinite(y), 'text: 2nd param must be a number')
|
|
589
|
+
// DEV: assert(
|
|
590
|
+
// 'string' === typeof message,
|
|
591
|
+
// 'text: 3rd param must be a string'
|
|
592
|
+
// )
|
|
593
|
+
DEV: assert(
|
|
594
|
+
null == color || (isFinite(color) && color >= 0),
|
|
595
|
+
'text: 4th param must be a positive number or zero'
|
|
596
|
+
)
|
|
597
|
+
DEV: assert(
|
|
598
|
+
'string' === typeof fontStyle,
|
|
599
|
+
'text: 5th param must be a string'
|
|
600
|
+
)
|
|
601
|
+
|
|
621
602
|
_ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`
|
|
622
603
|
_ctx.fillStyle = instance.getcolor(color)
|
|
623
604
|
_ctx.fillText(message, ~~x, ~~y)
|
|
@@ -629,12 +610,11 @@ export default function litecanvas(settings = {}) {
|
|
|
629
610
|
* @param {string} family
|
|
630
611
|
*/
|
|
631
612
|
textfont(family) {
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
}
|
|
613
|
+
DEV: assert(
|
|
614
|
+
'string' === typeof family,
|
|
615
|
+
'textfont: 1st param must be a string'
|
|
616
|
+
)
|
|
617
|
+
|
|
638
618
|
_fontFamily = family
|
|
639
619
|
},
|
|
640
620
|
|
|
@@ -644,9 +624,8 @@ export default function litecanvas(settings = {}) {
|
|
|
644
624
|
* @param {number} size
|
|
645
625
|
*/
|
|
646
626
|
textsize(size) {
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
}
|
|
627
|
+
DEV: assert(isFinite(size), 'textsize: 1st param must be a number')
|
|
628
|
+
|
|
650
629
|
_fontSize = size
|
|
651
630
|
},
|
|
652
631
|
|
|
@@ -659,27 +638,24 @@ export default function litecanvas(settings = {}) {
|
|
|
659
638
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/textAlign
|
|
660
639
|
*/
|
|
661
640
|
textalign(align, baseline) {
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
'textalign: 2nd param must be a string'
|
|
681
|
-
)
|
|
682
|
-
}
|
|
641
|
+
DEV: assert(
|
|
642
|
+
null == align ||
|
|
643
|
+
['left', 'right', 'center', 'start', 'end'].includes(align),
|
|
644
|
+
'textalign: 1st param must be a string'
|
|
645
|
+
)
|
|
646
|
+
DEV: assert(
|
|
647
|
+
null == baseline ||
|
|
648
|
+
[
|
|
649
|
+
'top',
|
|
650
|
+
'bottom',
|
|
651
|
+
'middle',
|
|
652
|
+
'hanging',
|
|
653
|
+
'alphabetic',
|
|
654
|
+
'ideographic',
|
|
655
|
+
].includes(baseline),
|
|
656
|
+
'textalign: 2nd param must be a string'
|
|
657
|
+
)
|
|
658
|
+
|
|
683
659
|
if (align) _ctx.textAlign = align
|
|
684
660
|
if (baseline) _ctx.textBaseline = baseline
|
|
685
661
|
},
|
|
@@ -693,10 +669,9 @@ export default function litecanvas(settings = {}) {
|
|
|
693
669
|
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
694
670
|
*/
|
|
695
671
|
image(x, y, source) {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
}
|
|
672
|
+
DEV: assert(isFinite(x), 'image: 1st param must be a number')
|
|
673
|
+
DEV: assert(isFinite(y), 'image: 2nd param must be a number')
|
|
674
|
+
|
|
700
675
|
_ctx.drawImage(source, ~~x, ~~y)
|
|
701
676
|
},
|
|
702
677
|
|
|
@@ -713,18 +688,17 @@ export default function litecanvas(settings = {}) {
|
|
|
713
688
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
714
689
|
*/
|
|
715
690
|
paint(width, height, drawing, options = {}) {
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
}
|
|
691
|
+
DEV: assert(isFinite(width), 'paint: 1st param must be a number')
|
|
692
|
+
DEV: assert(isFinite(height), 'paint: 2nd param must be a number')
|
|
693
|
+
DEV: assert(
|
|
694
|
+
'function' === typeof drawing || Array.isArray(drawing),
|
|
695
|
+
'paint: 3rd param must be a function or array'
|
|
696
|
+
)
|
|
697
|
+
DEV: assert(
|
|
698
|
+
(options && !options.scale) || isFinite(options.scale),
|
|
699
|
+
'paint: 4th param (options.scale) must be a number'
|
|
700
|
+
)
|
|
701
|
+
|
|
728
702
|
const canvas = options.canvas || new OffscreenCanvas(1, 1),
|
|
729
703
|
scale = options.scale || 1,
|
|
730
704
|
contextOriginal = _ctx
|
|
@@ -771,9 +745,6 @@ export default function litecanvas(settings = {}) {
|
|
|
771
745
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D
|
|
772
746
|
*/
|
|
773
747
|
ctx(context) {
|
|
774
|
-
if (DEV_BUILD) {
|
|
775
|
-
// TODO
|
|
776
|
-
}
|
|
777
748
|
if (context) {
|
|
778
749
|
_ctx = context
|
|
779
750
|
}
|
|
@@ -801,10 +772,9 @@ export default function litecanvas(settings = {}) {
|
|
|
801
772
|
* @param {number} y
|
|
802
773
|
*/
|
|
803
774
|
translate: (x, y) => {
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
}
|
|
775
|
+
DEV: assert(isFinite(x), 'translate: 1st param must be a number')
|
|
776
|
+
DEV: assert(isFinite(y), 'translate: 2nd param must be a number')
|
|
777
|
+
|
|
808
778
|
return _ctx.translate(~~x, ~~y)
|
|
809
779
|
},
|
|
810
780
|
|
|
@@ -815,13 +785,12 @@ export default function litecanvas(settings = {}) {
|
|
|
815
785
|
* @param {number} [y]
|
|
816
786
|
*/
|
|
817
787
|
scale: (x, y) => {
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
}
|
|
788
|
+
DEV: assert(isFinite(x), 'scale: 1st param must be a number')
|
|
789
|
+
DEV: assert(
|
|
790
|
+
y == null || isFinite(y),
|
|
791
|
+
'scale: 2nd param must be a number'
|
|
792
|
+
)
|
|
793
|
+
|
|
825
794
|
return _ctx.scale(x, y || x)
|
|
826
795
|
},
|
|
827
796
|
|
|
@@ -831,9 +800,8 @@ export default function litecanvas(settings = {}) {
|
|
|
831
800
|
* @param {number} radians
|
|
832
801
|
*/
|
|
833
802
|
rotate: (radians) => {
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
}
|
|
803
|
+
DEV: assert(isFinite(radians), 'rotate: 1st param must be a number')
|
|
804
|
+
|
|
837
805
|
return _ctx.rotate(radians)
|
|
838
806
|
},
|
|
839
807
|
|
|
@@ -844,9 +812,8 @@ export default function litecanvas(settings = {}) {
|
|
|
844
812
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
845
813
|
*/
|
|
846
814
|
alpha(value) {
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
}
|
|
815
|
+
DEV: assert(isFinite(value), 'alpha: 1st param must be a number')
|
|
816
|
+
|
|
850
817
|
_ctx.globalAlpha = instance.clamp(value, 0, 1)
|
|
851
818
|
},
|
|
852
819
|
|
|
@@ -860,14 +827,11 @@ export default function litecanvas(settings = {}) {
|
|
|
860
827
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path2D
|
|
861
828
|
*/
|
|
862
829
|
path: (arg) => {
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
'path: 1st param must be a string or a Path2D instance'
|
|
869
|
-
)
|
|
870
|
-
}
|
|
830
|
+
DEV: assert(
|
|
831
|
+
null == arg || 'string' === typeof arg || arg instanceof Path2D,
|
|
832
|
+
'path: 1st param must be a string or a Path2D instance'
|
|
833
|
+
)
|
|
834
|
+
|
|
871
835
|
return new Path2D(arg)
|
|
872
836
|
},
|
|
873
837
|
|
|
@@ -878,16 +842,15 @@ export default function litecanvas(settings = {}) {
|
|
|
878
842
|
* @param {Path2D} [path]
|
|
879
843
|
*/
|
|
880
844
|
fill(color, path) {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
}
|
|
845
|
+
DEV: assert(
|
|
846
|
+
null == color || (isFinite(color) && color >= 0),
|
|
847
|
+
'fill: 1st param must be a positive number or zero'
|
|
848
|
+
)
|
|
849
|
+
DEV: assert(
|
|
850
|
+
null == path || path instanceof Path2D,
|
|
851
|
+
'fill: 2nd param must be a Path2D instance'
|
|
852
|
+
)
|
|
853
|
+
|
|
891
854
|
_ctx.fillStyle = instance.getcolor(color)
|
|
892
855
|
if (path) {
|
|
893
856
|
_ctx.fill(path)
|
|
@@ -903,16 +866,15 @@ export default function litecanvas(settings = {}) {
|
|
|
903
866
|
* @param {Path2D} [path]
|
|
904
867
|
*/
|
|
905
868
|
stroke(color, path) {
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
}
|
|
869
|
+
DEV: assert(
|
|
870
|
+
null == color || (isFinite(color) && color >= 0),
|
|
871
|
+
'stroke: 1st param must be a positive number or zero'
|
|
872
|
+
)
|
|
873
|
+
DEV: assert(
|
|
874
|
+
null == path || path instanceof Path2D,
|
|
875
|
+
'stroke: 2nd param must be a Path2D instance'
|
|
876
|
+
)
|
|
877
|
+
|
|
916
878
|
_ctx.strokeStyle = instance.getcolor(color)
|
|
917
879
|
if (path) {
|
|
918
880
|
_ctx.stroke(path)
|
|
@@ -928,12 +890,11 @@ export default function litecanvas(settings = {}) {
|
|
|
928
890
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
|
|
929
891
|
*/
|
|
930
892
|
clip(path) {
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
}
|
|
893
|
+
DEV: assert(
|
|
894
|
+
path instanceof Path2D,
|
|
895
|
+
'clip: 1st param must be a Path2D instance'
|
|
896
|
+
)
|
|
897
|
+
|
|
937
898
|
_ctx.clip(path)
|
|
938
899
|
},
|
|
939
900
|
|
|
@@ -950,17 +911,16 @@ export default function litecanvas(settings = {}) {
|
|
|
950
911
|
* @see https://github.com/KilledByAPixel/ZzFX
|
|
951
912
|
*/
|
|
952
913
|
sfx(zzfxParams, pitchSlide = 0, volumeFactor = 1) {
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
}
|
|
914
|
+
DEV: assert(
|
|
915
|
+
null == zzfxParams || Array.isArray(zzfxParams),
|
|
916
|
+
'sfx: 1st param must be an array'
|
|
917
|
+
)
|
|
918
|
+
DEV: assert(isFinite(pitchSlide), 'sfx: 2nd param must be a number')
|
|
919
|
+
DEV: assert(
|
|
920
|
+
isFinite(volumeFactor),
|
|
921
|
+
'sfx: 3rd param must be a number'
|
|
922
|
+
)
|
|
923
|
+
|
|
964
924
|
if (
|
|
965
925
|
root.zzfxV <= 0 ||
|
|
966
926
|
(navigator.userActivation &&
|
|
@@ -990,9 +950,8 @@ export default function litecanvas(settings = {}) {
|
|
|
990
950
|
* @param {number} value
|
|
991
951
|
*/
|
|
992
952
|
volume(value) {
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
}
|
|
953
|
+
DEV: assert(isFinite(value), 'volume: 1st param must be a number')
|
|
954
|
+
|
|
996
955
|
root.zzfxV = value
|
|
997
956
|
},
|
|
998
957
|
|
|
@@ -1011,16 +970,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1011
970
|
* @returns {boolean}
|
|
1012
971
|
*/
|
|
1013
972
|
colrect: (x1, y1, w1, h1, x2, y2, w2, h2) => {
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
}
|
|
973
|
+
DEV: assert(isFinite(x1), 'colrect: 1st param must be a number')
|
|
974
|
+
DEV: assert(isFinite(y1), 'colrect: 2nd param must be a number')
|
|
975
|
+
DEV: assert(isFinite(w1), 'colrect: 3rd param must be a number')
|
|
976
|
+
DEV: assert(isFinite(h1), 'colrect: 4th param must be a number')
|
|
977
|
+
DEV: assert(isFinite(x2), 'colrect: 5th param must be a number')
|
|
978
|
+
DEV: assert(isFinite(y2), 'colrect: 6th param must be a number')
|
|
979
|
+
DEV: assert(isFinite(w2), 'colrect: 7th param must be a number')
|
|
980
|
+
DEV: assert(isFinite(h2), 'colrect: 8th param must be a number')
|
|
981
|
+
|
|
1024
982
|
return x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2
|
|
1025
983
|
},
|
|
1026
984
|
|
|
@@ -1036,14 +994,13 @@ export default function litecanvas(settings = {}) {
|
|
|
1036
994
|
* @returns {boolean}
|
|
1037
995
|
*/
|
|
1038
996
|
colcirc: (x1, y1, r1, x2, y2, r2) => {
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
}
|
|
997
|
+
DEV: assert(isFinite(x1), 'colcirc: 1st param must be a number')
|
|
998
|
+
DEV: assert(isFinite(y1), 'colcirc: 2nd param must be a number')
|
|
999
|
+
DEV: assert(isFinite(r1), 'colcirc: 3rd param must be a number')
|
|
1000
|
+
DEV: assert(isFinite(x2), 'colcirc: 4th param must be a number')
|
|
1001
|
+
DEV: assert(isFinite(y2), 'colcirc: 5th param must be a number')
|
|
1002
|
+
DEV: assert(isFinite(r2), 'colcirc: 6th param must be a number')
|
|
1003
|
+
|
|
1047
1004
|
return (
|
|
1048
1005
|
(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <=
|
|
1049
1006
|
(r1 + r2) * (r1 + r2)
|
|
@@ -1057,16 +1014,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1057
1014
|
* @param {pluginCallback} callback
|
|
1058
1015
|
*/
|
|
1059
1016
|
use(callback, config = {}) {
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
}
|
|
1017
|
+
DEV: assert(
|
|
1018
|
+
'function' === typeof callback,
|
|
1019
|
+
'use: 1st param must be a function'
|
|
1020
|
+
)
|
|
1021
|
+
DEV: assert(
|
|
1022
|
+
'object' === typeof config,
|
|
1023
|
+
'use: 2nd param must be an object'
|
|
1024
|
+
)
|
|
1025
|
+
|
|
1070
1026
|
_initialized
|
|
1071
1027
|
? loadPlugin(callback, config)
|
|
1072
1028
|
: _plugins.push([callback, config])
|
|
@@ -1080,16 +1036,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1080
1036
|
* @returns {Function} a function to remove the listener
|
|
1081
1037
|
*/
|
|
1082
1038
|
listen(eventName, callback) {
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
}
|
|
1039
|
+
DEV: assert(
|
|
1040
|
+
'string' === typeof eventName,
|
|
1041
|
+
'listen: 1st param must be a string'
|
|
1042
|
+
)
|
|
1043
|
+
DEV: assert(
|
|
1044
|
+
'function' === typeof callback,
|
|
1045
|
+
'listen: 2nd param must be a function'
|
|
1046
|
+
)
|
|
1047
|
+
|
|
1093
1048
|
_events[eventName] = _events[eventName] || new Set()
|
|
1094
1049
|
_events[eventName].add(callback)
|
|
1095
1050
|
|
|
@@ -1107,15 +1062,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1107
1062
|
* @param {*} [arg4] any data to be passed over the listeners
|
|
1108
1063
|
*/
|
|
1109
1064
|
emit(eventName, arg1, arg2, arg3, arg4) {
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1065
|
+
DEV: assert(
|
|
1066
|
+
'string' === typeof eventName,
|
|
1067
|
+
'emit: 1st param must be a string'
|
|
1068
|
+
)
|
|
1069
|
+
if (_initialized) {
|
|
1070
|
+
triggerEvent('before:' + eventName, arg1, arg2, arg3, arg4)
|
|
1071
|
+
triggerEvent(eventName, arg1, arg2, arg3, arg4)
|
|
1072
|
+
triggerEvent('after:' + eventName, arg1, arg2, arg3, arg4)
|
|
1115
1073
|
}
|
|
1116
|
-
triggerEvent('before:' + eventName, arg1, arg2, arg3, arg4)
|
|
1117
|
-
triggerEvent(eventName, arg1, arg2, arg3, arg4)
|
|
1118
|
-
triggerEvent('after:' + eventName, arg1, arg2, arg3, arg4)
|
|
1119
1074
|
},
|
|
1120
1075
|
|
|
1121
1076
|
/**
|
|
@@ -1125,12 +1080,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1125
1080
|
* @returns {string} the color code
|
|
1126
1081
|
*/
|
|
1127
1082
|
getcolor: (index) => {
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
}
|
|
1083
|
+
DEV: assert(
|
|
1084
|
+
null == index || (isFinite(index) && index >= 0),
|
|
1085
|
+
'getcolor: 1st param must be a number'
|
|
1086
|
+
)
|
|
1087
|
+
|
|
1134
1088
|
return colors[~~index % colors.length]
|
|
1135
1089
|
},
|
|
1136
1090
|
|
|
@@ -1141,15 +1095,14 @@ export default function litecanvas(settings = {}) {
|
|
|
1141
1095
|
* @param {*} value
|
|
1142
1096
|
*/
|
|
1143
1097
|
setvar(key, value) {
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
console.warn(`setvar: key "${key}" was defined as ${value}`)
|
|
1151
|
-
}
|
|
1098
|
+
DEV: assert(
|
|
1099
|
+
'string' === typeof key,
|
|
1100
|
+
'setvar: 1st param must be a string'
|
|
1101
|
+
)
|
|
1102
|
+
if (value == null) {
|
|
1103
|
+
console.warn(`setvar: key "${key}" was defined as ${value}`)
|
|
1152
1104
|
}
|
|
1105
|
+
|
|
1153
1106
|
instance[key] = value
|
|
1154
1107
|
if (_global) {
|
|
1155
1108
|
root[key] = value
|
|
@@ -1163,13 +1116,22 @@ export default function litecanvas(settings = {}) {
|
|
|
1163
1116
|
* @param {number} height
|
|
1164
1117
|
*/
|
|
1165
1118
|
resize(width, height) {
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1119
|
+
DEV: assert(
|
|
1120
|
+
isFinite(width) && width > 0,
|
|
1121
|
+
'resize: 1st param must be a number'
|
|
1122
|
+
)
|
|
1123
|
+
DEV: assert(
|
|
1124
|
+
isFinite(height) && height > 0,
|
|
1125
|
+
'resize: 2nd param must be a number'
|
|
1126
|
+
)
|
|
1127
|
+
|
|
1170
1128
|
instance.setvar('WIDTH', (_canvas.width = width))
|
|
1171
1129
|
instance.setvar('HEIGHT', (_canvas.height = height))
|
|
1172
|
-
|
|
1130
|
+
|
|
1131
|
+
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1132
|
+
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1133
|
+
|
|
1134
|
+
onResize()
|
|
1173
1135
|
},
|
|
1174
1136
|
|
|
1175
1137
|
/**
|
|
@@ -1180,9 +1142,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1180
1142
|
* @param {number} value
|
|
1181
1143
|
*/
|
|
1182
1144
|
timescale(value) {
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1145
|
+
DEV: assert(
|
|
1146
|
+
isFinite(value),
|
|
1147
|
+
'timescale: 1st param must be a number'
|
|
1148
|
+
)
|
|
1149
|
+
|
|
1186
1150
|
_timeScale = value
|
|
1187
1151
|
},
|
|
1188
1152
|
|
|
@@ -1192,12 +1156,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1192
1156
|
* @param {number} value
|
|
1193
1157
|
*/
|
|
1194
1158
|
setfps(value) {
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
}
|
|
1159
|
+
DEV: assert(
|
|
1160
|
+
isFinite(value) && value >= 1,
|
|
1161
|
+
'setfps: 1st param must be a positive number'
|
|
1162
|
+
)
|
|
1163
|
+
|
|
1201
1164
|
_deltaTime = 1 / ~~value
|
|
1202
1165
|
},
|
|
1203
1166
|
|
|
@@ -1229,7 +1192,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1229
1192
|
function init() {
|
|
1230
1193
|
_initialized = true
|
|
1231
1194
|
|
|
1232
|
-
//
|
|
1195
|
+
// setup default event listeners
|
|
1233
1196
|
const source = settings.loop ? settings.loop : root
|
|
1234
1197
|
for (const event in _events) {
|
|
1235
1198
|
if (source[event]) instance.listen(event, source[event])
|
|
@@ -1240,13 +1203,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1240
1203
|
loadPlugin(callback, config)
|
|
1241
1204
|
}
|
|
1242
1205
|
|
|
1243
|
-
// listen window resize event
|
|
1244
|
-
if (
|
|
1245
|
-
on(root, 'resize',
|
|
1206
|
+
// listen window resize event when "autoscale" is enabled
|
|
1207
|
+
if (_autoscale) {
|
|
1208
|
+
on(root, 'resize', onResize)
|
|
1246
1209
|
}
|
|
1247
1210
|
|
|
1248
|
-
pageResized()
|
|
1249
|
-
|
|
1250
1211
|
// default mouse/touch handlers
|
|
1251
1212
|
if (settings.tapEvents) {
|
|
1252
1213
|
const _getXY = (pageX, pageY) => [
|
|
@@ -1382,12 +1343,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1382
1343
|
* @returns {boolean}
|
|
1383
1344
|
*/
|
|
1384
1345
|
const iskeydown = (key) => {
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
}
|
|
1346
|
+
DEV: assert(
|
|
1347
|
+
'string' === typeof key,
|
|
1348
|
+
'iskeydown: 1st param must be a string'
|
|
1349
|
+
)
|
|
1350
|
+
|
|
1391
1351
|
return 'any' === key
|
|
1392
1352
|
? _keys.size > 0
|
|
1393
1353
|
: _keys.has(key.toLowerCase())
|
|
@@ -1441,25 +1401,26 @@ export default function litecanvas(settings = {}) {
|
|
|
1441
1401
|
|
|
1442
1402
|
_lastFrameTime = now
|
|
1443
1403
|
|
|
1444
|
-
if (frameTime > _deltaTime * 30)
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1404
|
+
if (frameTime > _deltaTime * 30) {
|
|
1405
|
+
console.log('skipping too long frame')
|
|
1406
|
+
} else {
|
|
1407
|
+
_accumulated += frameTime
|
|
1448
1408
|
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1409
|
+
if (!_animated) {
|
|
1410
|
+
_accumulated = _deltaTime
|
|
1411
|
+
}
|
|
1452
1412
|
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1413
|
+
for (; _accumulated >= _deltaTime; _accumulated -= _deltaTime) {
|
|
1414
|
+
instance.emit('update', _deltaTime * _timeScale)
|
|
1415
|
+
instance.setvar(
|
|
1416
|
+
'ELAPSED',
|
|
1417
|
+
instance.ELAPSED + _deltaTime * _timeScale
|
|
1418
|
+
)
|
|
1419
|
+
updated++
|
|
1420
|
+
}
|
|
1460
1421
|
}
|
|
1461
1422
|
|
|
1462
|
-
if (updated) {
|
|
1423
|
+
if (updated || !_animated) {
|
|
1463
1424
|
instance.textalign('start', 'top') // default values for textAlign & textBaseline
|
|
1464
1425
|
instance.emit('draw')
|
|
1465
1426
|
}
|
|
@@ -1472,64 +1433,61 @@ export default function litecanvas(settings = {}) {
|
|
|
1472
1433
|
? document.querySelector(_canvas)
|
|
1473
1434
|
: _canvas
|
|
1474
1435
|
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1436
|
+
DEV: assert(
|
|
1437
|
+
_canvas && _canvas.tagName === 'CANVAS',
|
|
1438
|
+
'Invalid canvas element'
|
|
1439
|
+
)
|
|
1440
|
+
DEV: assert(
|
|
1441
|
+
null == instance.WIDTH || instance.WIDTH > 0,
|
|
1442
|
+
'Litecanvas\' "width" option should be null or a positive number'
|
|
1443
|
+
)
|
|
1444
|
+
DEV: assert(
|
|
1445
|
+
null == instance.HEIGHT || instance.HEIGHT > 0,
|
|
1446
|
+
'Litecanvas\' "width" option should be null or a positive number'
|
|
1447
|
+
)
|
|
1448
|
+
DEV: assert(
|
|
1449
|
+
null == instance.HEIGHT ||
|
|
1450
|
+
(instance.WIDTH > 0 && instance.HEIGHT > 0),
|
|
1451
|
+
'Litecanvas\' "width" is required when "heigth" is passed'
|
|
1452
|
+
)
|
|
1489
1453
|
|
|
1490
1454
|
instance.setvar('CANVAS', _canvas)
|
|
1491
1455
|
_ctx = _canvas.getContext('2d')
|
|
1492
1456
|
|
|
1493
1457
|
on(_canvas, 'click', () => root.focus())
|
|
1494
1458
|
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1459
|
+
_canvas.style = ''
|
|
1460
|
+
|
|
1461
|
+
// If width is not set, the canvas will have the size of the page width.
|
|
1462
|
+
if (!instance.WIDTH) {
|
|
1463
|
+
instance.WIDTH = root.innerWidth
|
|
1464
|
+
instance.HEIGHT = root.innerHeight
|
|
1498
1465
|
}
|
|
1499
1466
|
|
|
1500
|
-
|
|
1501
|
-
_canvas.width = instance.WIDTH
|
|
1502
|
-
_canvas.height = instance.HEIGHT || instance.WIDTH
|
|
1467
|
+
instance.resize(instance.WIDTH, instance.HEIGHT, false)
|
|
1503
1468
|
|
|
1504
1469
|
if (!_canvas.parentNode) document.body.appendChild(_canvas)
|
|
1505
1470
|
}
|
|
1506
1471
|
|
|
1507
|
-
function
|
|
1508
|
-
const
|
|
1509
|
-
pageHeight = root.innerHeight,
|
|
1510
|
-
styles = _canvas.style
|
|
1472
|
+
function onResize() {
|
|
1473
|
+
const styles = _canvas.style
|
|
1511
1474
|
|
|
1512
|
-
|
|
1475
|
+
if (_autoscale) {
|
|
1476
|
+
if (!styles.display) {
|
|
1477
|
+
styles.display = 'block'
|
|
1478
|
+
styles.margin = 'auto'
|
|
1479
|
+
}
|
|
1513
1480
|
|
|
1514
|
-
if (_fullscreen) {
|
|
1515
|
-
styles.position = 'absolute'
|
|
1516
|
-
styles.inset = 0
|
|
1517
|
-
instance.setvar('WIDTH', (_canvas.width = pageWidth))
|
|
1518
|
-
instance.setvar('HEIGHT', (_canvas.height = pageHeight))
|
|
1519
|
-
} else if (_autoscale) {
|
|
1520
|
-
styles.margin = 'auto'
|
|
1521
1481
|
_scale = Math.min(
|
|
1522
|
-
|
|
1523
|
-
|
|
1482
|
+
root.innerWidth / instance.WIDTH,
|
|
1483
|
+
root.innerHeight / instance.HEIGHT
|
|
1524
1484
|
)
|
|
1525
1485
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1
|
|
1486
|
+
|
|
1526
1487
|
styles.width = instance.WIDTH * _scale + 'px'
|
|
1527
1488
|
styles.height = instance.HEIGHT * _scale + 'px'
|
|
1528
1489
|
}
|
|
1529
1490
|
|
|
1530
|
-
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1531
|
-
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1532
|
-
|
|
1533
1491
|
// restore canvas image rendering properties
|
|
1534
1492
|
if (!settings.antialias || settings.pixelart) {
|
|
1535
1493
|
_ctx.imageSmoothingEnabled = false
|
|
@@ -1538,6 +1496,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1538
1496
|
|
|
1539
1497
|
instance.emit('resized', _scale)
|
|
1540
1498
|
|
|
1499
|
+
// force redraw
|
|
1541
1500
|
if (!_animated) {
|
|
1542
1501
|
raf(drawFrame)
|
|
1543
1502
|
}
|
|
@@ -1555,12 +1514,12 @@ export default function litecanvas(settings = {}) {
|
|
|
1555
1514
|
*/
|
|
1556
1515
|
function loadPlugin(callback, config) {
|
|
1557
1516
|
const pluginData = callback(instance, _helpers, config)
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1517
|
+
|
|
1518
|
+
DEV: assert(
|
|
1519
|
+
null == pluginData || 'object' === typeof pluginData,
|
|
1520
|
+
'Litecanvas plugins should return an object or nothing'
|
|
1521
|
+
)
|
|
1522
|
+
|
|
1564
1523
|
for (const key in pluginData) {
|
|
1565
1524
|
instance.setvar(key, pluginData[key])
|
|
1566
1525
|
}
|