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