litecanvas 0.207.1 → 0.208.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 +187 -347
- package/dist/dist.js +24 -15
- package/dist/dist.min.js +1 -1
- package/package.json +1 -1
- package/src/dev.js +1 -1
- package/src/index.js +210 -219
- package/src/version.js +1 -1
- package/types/global.d.ts +28 -2
- package/types/types.d.ts +30 -4
package/src/index.js
CHANGED
|
@@ -15,7 +15,6 @@ export default function litecanvas(settings = {}) {
|
|
|
15
15
|
math = Math,
|
|
16
16
|
perf = performance,
|
|
17
17
|
TWO_PI = math.PI * 2,
|
|
18
|
-
loggerPrefix = '[Litecanvas] ',
|
|
19
18
|
raf = requestAnimationFrame,
|
|
20
19
|
isNumber = Number.isFinite,
|
|
21
20
|
/** @type {Function[]} */
|
|
@@ -44,6 +43,11 @@ export default function litecanvas(settings = {}) {
|
|
|
44
43
|
keyboardEvents: true,
|
|
45
44
|
}
|
|
46
45
|
|
|
46
|
+
DEV: assert(
|
|
47
|
+
null == settings || 'object' === typeof settings,
|
|
48
|
+
'litecanvas() 1st parameter must be a object'
|
|
49
|
+
)
|
|
50
|
+
|
|
47
51
|
// setup the settings default values
|
|
48
52
|
settings = Object.assign(defaults, settings)
|
|
49
53
|
|
|
@@ -138,9 +142,9 @@ export default function litecanvas(settings = {}) {
|
|
|
138
142
|
* @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
|
|
139
143
|
*/
|
|
140
144
|
lerp: (start, end, t) => {
|
|
141
|
-
DEV: assert(isNumber(start),
|
|
142
|
-
DEV: assert(isNumber(end),
|
|
143
|
-
DEV: assert(isNumber(t),
|
|
145
|
+
DEV: assert(isNumber(start), 'lerp() 1st parameter must be a number')
|
|
146
|
+
DEV: assert(isNumber(end), 'lerp() 2nd parameter must be a number')
|
|
147
|
+
DEV: assert(isNumber(t), 'lerp() 3rd parameter must be a number')
|
|
144
148
|
|
|
145
149
|
return start + t * (end - start)
|
|
146
150
|
},
|
|
@@ -152,7 +156,7 @@ export default function litecanvas(settings = {}) {
|
|
|
152
156
|
* @returns {number} the value in radians
|
|
153
157
|
*/
|
|
154
158
|
deg2rad: (degs) => {
|
|
155
|
-
DEV: assert(isNumber(degs), 'deg2rad() 1st
|
|
159
|
+
DEV: assert(isNumber(degs), 'deg2rad() 1st parameter must be a number')
|
|
156
160
|
|
|
157
161
|
return (math.PI / 180) * degs
|
|
158
162
|
},
|
|
@@ -164,11 +168,30 @@ export default function litecanvas(settings = {}) {
|
|
|
164
168
|
* @returns {number} the value in degrees
|
|
165
169
|
*/
|
|
166
170
|
rad2deg: (rads) => {
|
|
167
|
-
DEV: assert(isNumber(rads), 'rad2deg() 1st
|
|
171
|
+
DEV: assert(isNumber(rads), 'rad2deg() 1st parameter must be a number')
|
|
168
172
|
|
|
169
173
|
return (180 / math.PI) * rads
|
|
170
174
|
},
|
|
171
175
|
|
|
176
|
+
/**
|
|
177
|
+
* Modulus (Euclidean division).
|
|
178
|
+
*
|
|
179
|
+
* Note: When `b == 0` returns `0`, rather than `NaN`.
|
|
180
|
+
*
|
|
181
|
+
* @param {number} a dividend
|
|
182
|
+
* @param {number} b divisor
|
|
183
|
+
* @returns {number} the remainder
|
|
184
|
+
* @example
|
|
185
|
+
* mod(-1, 5) // => 4
|
|
186
|
+
* -1 % 5 // => -1
|
|
187
|
+
*/
|
|
188
|
+
mod(a, b) {
|
|
189
|
+
DEV: assert(isNumber(a), 'mod() 1st parameter must be a number')
|
|
190
|
+
DEV: assert(isNumber(b) && b >= 0, 'mod() 2nd parameter must be a non-negative number')
|
|
191
|
+
|
|
192
|
+
return ((a % b) + b) % b || 0
|
|
193
|
+
},
|
|
194
|
+
|
|
172
195
|
/**
|
|
173
196
|
* Returns the rounded value of an number to optional precision (number of digits after the decimal point).
|
|
174
197
|
*
|
|
@@ -179,10 +202,10 @@ export default function litecanvas(settings = {}) {
|
|
|
179
202
|
* @returns {number} rounded number.
|
|
180
203
|
*/
|
|
181
204
|
round: (n, precision = 0) => {
|
|
182
|
-
DEV: assert(isNumber(n),
|
|
205
|
+
DEV: assert(isNumber(n), 'round() 1st parameter must be a number')
|
|
183
206
|
DEV: assert(
|
|
184
207
|
isNumber(precision) && precision >= 0,
|
|
185
|
-
|
|
208
|
+
'round() 2nd parameter must be a non-negative number'
|
|
186
209
|
)
|
|
187
210
|
|
|
188
211
|
if (!precision) {
|
|
@@ -201,13 +224,10 @@ export default function litecanvas(settings = {}) {
|
|
|
201
224
|
* @returns {number}
|
|
202
225
|
*/
|
|
203
226
|
clamp: (value, min, max) => {
|
|
204
|
-
DEV: assert(isNumber(value),
|
|
205
|
-
DEV: assert(isNumber(min),
|
|
206
|
-
DEV: assert(isNumber(max),
|
|
207
|
-
DEV: assert(
|
|
208
|
-
max >= min,
|
|
209
|
-
loggerPrefix + 'clamp() the 2nd param must be less than the 3rd param'
|
|
210
|
-
)
|
|
227
|
+
DEV: assert(isNumber(value), 'clamp() 1st parameter must be a number')
|
|
228
|
+
DEV: assert(isNumber(min), 'clamp() 2nd parameter must be a number')
|
|
229
|
+
DEV: assert(isNumber(max), 'clamp() 3rd parameter must be a number')
|
|
230
|
+
DEV: assert(max >= min, 'clamp() the 2nd parameter must be less than the 3rd parameter')
|
|
211
231
|
|
|
212
232
|
if (value < min) return min
|
|
213
233
|
if (value > max) return max
|
|
@@ -224,10 +244,10 @@ export default function litecanvas(settings = {}) {
|
|
|
224
244
|
* @returns {number}
|
|
225
245
|
*/
|
|
226
246
|
dist: (x1, y1, x2, y2) => {
|
|
227
|
-
DEV: assert(isNumber(x1),
|
|
228
|
-
DEV: assert(isNumber(y1),
|
|
229
|
-
DEV: assert(isNumber(x2),
|
|
230
|
-
DEV: assert(isNumber(y2),
|
|
247
|
+
DEV: assert(isNumber(x1), 'dist() 1st parameter must be a number')
|
|
248
|
+
DEV: assert(isNumber(y1), 'dist() 2nd parameter must be a number')
|
|
249
|
+
DEV: assert(isNumber(x2), 'dist() 3rd parameter must be a number')
|
|
250
|
+
DEV: assert(isNumber(y2), 'dist() 4th parameter must be a number')
|
|
231
251
|
|
|
232
252
|
return math.hypot(x2 - x1, y2 - y1)
|
|
233
253
|
},
|
|
@@ -241,13 +261,10 @@ export default function litecanvas(settings = {}) {
|
|
|
241
261
|
* @returns {number}
|
|
242
262
|
*/
|
|
243
263
|
wrap: (value, min, max) => {
|
|
244
|
-
DEV: assert(isNumber(value),
|
|
245
|
-
DEV: assert(isNumber(min),
|
|
246
|
-
DEV: assert(isNumber(max),
|
|
247
|
-
DEV: assert(
|
|
248
|
-
max > min,
|
|
249
|
-
loggerPrefix + 'wrap() the 2nd param must be less than the 3rd param'
|
|
250
|
-
)
|
|
264
|
+
DEV: assert(isNumber(value), 'wrap() 1st parameter must be a number')
|
|
265
|
+
DEV: assert(isNumber(min), 'wrap() 2nd parameter must be a number')
|
|
266
|
+
DEV: assert(isNumber(max), 'wrap() 3rd parameter must be a number')
|
|
267
|
+
DEV: assert(max > min, 'wrap() the 2nd parameter must be less than the 3rd parameter')
|
|
251
268
|
|
|
252
269
|
return value - (max - min) * math.floor((value - min) / (max - min))
|
|
253
270
|
},
|
|
@@ -264,14 +281,14 @@ export default function litecanvas(settings = {}) {
|
|
|
264
281
|
* @returns {number} the remapped number
|
|
265
282
|
*/
|
|
266
283
|
map(value, start1, stop1, start2, stop2, withinBounds) {
|
|
267
|
-
DEV: assert(isNumber(value),
|
|
268
|
-
DEV: assert(isNumber(start1),
|
|
269
|
-
DEV: assert(isNumber(stop1),
|
|
270
|
-
DEV: assert(isNumber(start2),
|
|
271
|
-
DEV: assert(isNumber(stop2),
|
|
284
|
+
DEV: assert(isNumber(value), 'map() 1st parameter must be a number')
|
|
285
|
+
DEV: assert(isNumber(start1), 'map() 2nd parameter must be a number')
|
|
286
|
+
DEV: assert(isNumber(stop1), 'map() 3rd parameter must be a number')
|
|
287
|
+
DEV: assert(isNumber(start2), 'map() 4th parameter must be a number')
|
|
288
|
+
DEV: assert(isNumber(stop2), 'map() 5th parameter must be a number')
|
|
272
289
|
DEV: assert(
|
|
273
290
|
stop1 !== start1,
|
|
274
|
-
|
|
291
|
+
'map() the 2nd parameter must be different than the 3rd parameter'
|
|
275
292
|
)
|
|
276
293
|
|
|
277
294
|
// prettier-ignore
|
|
@@ -290,12 +307,12 @@ export default function litecanvas(settings = {}) {
|
|
|
290
307
|
* @returns {number} the normalized number.
|
|
291
308
|
*/
|
|
292
309
|
norm: (value, start, stop) => {
|
|
293
|
-
DEV: assert(isNumber(value),
|
|
294
|
-
DEV: assert(isNumber(start),
|
|
295
|
-
DEV: assert(isNumber(stop),
|
|
310
|
+
DEV: assert(isNumber(value), 'norm() 1st parameter must be a number')
|
|
311
|
+
DEV: assert(isNumber(start), 'norm() 2nd parameter must be a number')
|
|
312
|
+
DEV: assert(isNumber(stop), 'norm() 3rd parameter must be a number')
|
|
296
313
|
DEV: assert(
|
|
297
314
|
start !== stop,
|
|
298
|
-
|
|
315
|
+
'norm() the 2nd parameter must be different than the 3rd parameter'
|
|
299
316
|
)
|
|
300
317
|
|
|
301
318
|
return instance.map(value, start, stop, 0, 1)
|
|
@@ -311,12 +328,9 @@ export default function litecanvas(settings = {}) {
|
|
|
311
328
|
* @returns {number} the random number
|
|
312
329
|
*/
|
|
313
330
|
rand: (min = 0.0, max = 1.0) => {
|
|
314
|
-
DEV: assert(isNumber(min),
|
|
315
|
-
DEV: assert(isNumber(max),
|
|
316
|
-
DEV: assert(
|
|
317
|
-
max >= min,
|
|
318
|
-
loggerPrefix + 'rand() the 1st param must be less than the 2nd param'
|
|
319
|
-
)
|
|
331
|
+
DEV: assert(isNumber(min), 'rand() 1st parameter must be a number')
|
|
332
|
+
DEV: assert(isNumber(max), 'rand() 2nd parameter must be a number')
|
|
333
|
+
DEV: assert(max >= min, 'rand() the 1st parameter must be less than the 2nd parameter')
|
|
320
334
|
|
|
321
335
|
const a = 1664525
|
|
322
336
|
const c = 1013904223
|
|
@@ -335,12 +349,9 @@ export default function litecanvas(settings = {}) {
|
|
|
335
349
|
* @returns {number} the random number
|
|
336
350
|
*/
|
|
337
351
|
randi: (min = 0, max = 1) => {
|
|
338
|
-
DEV: assert(isNumber(min),
|
|
339
|
-
DEV: assert(isNumber(max),
|
|
340
|
-
DEV: assert(
|
|
341
|
-
max >= min,
|
|
342
|
-
loggerPrefix + 'randi() the 1st param must be less than the 2nd param'
|
|
343
|
-
)
|
|
352
|
+
DEV: assert(isNumber(min), 'randi() 1st parameter must be a number')
|
|
353
|
+
DEV: assert(isNumber(max), 'randi() 2nd parameter must be a number')
|
|
354
|
+
DEV: assert(max >= min, 'randi() the 1st parameter must be less than the 2nd parameter')
|
|
344
355
|
|
|
345
356
|
return ~~instance.rand(min, max + 1)
|
|
346
357
|
},
|
|
@@ -355,7 +366,7 @@ export default function litecanvas(settings = {}) {
|
|
|
355
366
|
rseed(value) {
|
|
356
367
|
DEV: assert(
|
|
357
368
|
isNumber(value) && value >= 0,
|
|
358
|
-
|
|
369
|
+
'rseed() 1st parameter must be a non-negative integer'
|
|
359
370
|
)
|
|
360
371
|
|
|
361
372
|
_rngSeed = ~~value
|
|
@@ -363,14 +374,14 @@ export default function litecanvas(settings = {}) {
|
|
|
363
374
|
|
|
364
375
|
/** BASIC GRAPHICS API */
|
|
365
376
|
/**
|
|
366
|
-
* Clear the game screen with an optional color
|
|
377
|
+
* Clear the game screen with an optional color.
|
|
367
378
|
*
|
|
368
379
|
* @param {number} [color] The background color (index) or null/undefined (for transparent)
|
|
369
380
|
*/
|
|
370
381
|
cls(color) {
|
|
371
382
|
DEV: assert(
|
|
372
383
|
null == color || (isNumber(color) && color >= 0),
|
|
373
|
-
|
|
384
|
+
'cls() 1st parameter must be a non-negative number'
|
|
374
385
|
)
|
|
375
386
|
|
|
376
387
|
if (null == color) {
|
|
@@ -393,26 +404,27 @@ export default function litecanvas(settings = {}) {
|
|
|
393
404
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
394
405
|
*/
|
|
395
406
|
rect(x, y, width, height, color, radii) {
|
|
396
|
-
DEV: assert(isNumber(x),
|
|
397
|
-
DEV: assert(isNumber(y),
|
|
407
|
+
DEV: assert(isNumber(x), 'rect() 1st parameter must be a number')
|
|
408
|
+
DEV: assert(isNumber(y), 'rect() 2nd parameter must be a number')
|
|
398
409
|
DEV: assert(
|
|
399
410
|
isNumber(width) && width > 0,
|
|
400
|
-
|
|
411
|
+
'rect() 3rd parameter must be a positive number'
|
|
401
412
|
)
|
|
402
413
|
DEV: assert(
|
|
403
414
|
isNumber(height) && height >= 0,
|
|
404
|
-
|
|
415
|
+
'rect() 4th parameter must be a non-negative number'
|
|
405
416
|
)
|
|
406
417
|
DEV: assert(
|
|
407
418
|
null == color || (isNumber(color) && color >= 0),
|
|
408
|
-
|
|
419
|
+
'rect() 5th parameter must be a non-negative number'
|
|
409
420
|
)
|
|
410
421
|
DEV: assert(
|
|
411
422
|
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
412
|
-
|
|
423
|
+
'rect() 6th parameter must be a number or array of numbers'
|
|
413
424
|
)
|
|
414
425
|
|
|
415
426
|
beginPath(_ctx)
|
|
427
|
+
|
|
416
428
|
_ctx[radii ? 'roundRect' : 'rect'](
|
|
417
429
|
~~x - _outline_fix,
|
|
418
430
|
~~y - _outline_fix,
|
|
@@ -434,27 +446,28 @@ export default function litecanvas(settings = {}) {
|
|
|
434
446
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
435
447
|
*/
|
|
436
448
|
rectfill(x, y, width, height, color, radii) {
|
|
437
|
-
DEV: assert(isNumber(x),
|
|
438
|
-
DEV: assert(isNumber(y),
|
|
449
|
+
DEV: assert(isNumber(x), 'rectfill() 1st parameter must be a number')
|
|
450
|
+
DEV: assert(isNumber(y), 'rectfill() 2nd parameter must be a number')
|
|
439
451
|
DEV: assert(
|
|
440
452
|
isNumber(width) && width >= 0,
|
|
441
|
-
|
|
453
|
+
'rectfill() 3rd parameter must be a non-negative number'
|
|
442
454
|
)
|
|
443
455
|
DEV: assert(
|
|
444
456
|
isNumber(height) && height >= 0,
|
|
445
|
-
|
|
457
|
+
'rectfill() 4th parameter must be a non-negative number'
|
|
446
458
|
)
|
|
447
459
|
DEV: assert(
|
|
448
460
|
null == color || (isNumber(color) && color >= 0),
|
|
449
|
-
|
|
461
|
+
'rectfill() 5th parameter must be a non-negative number'
|
|
450
462
|
)
|
|
451
463
|
DEV: assert(
|
|
452
464
|
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
453
|
-
|
|
454
|
-
|
|
465
|
+
|
|
466
|
+
'rectfill() 6th parameter must be a number or array of at least 2 numbers'
|
|
455
467
|
)
|
|
456
468
|
|
|
457
469
|
beginPath(_ctx)
|
|
470
|
+
|
|
458
471
|
_ctx[radii ? 'roundRect' : 'rect'](~~x, ~~y, ~~width, ~~height, radii)
|
|
459
472
|
instance.fill(color)
|
|
460
473
|
},
|
|
@@ -469,22 +482,23 @@ export default function litecanvas(settings = {}) {
|
|
|
469
482
|
* @param {number} [color=0] the color index
|
|
470
483
|
*/
|
|
471
484
|
oval(x, y, radiusX, radiusY, color) {
|
|
472
|
-
DEV: assert(isNumber(x),
|
|
473
|
-
DEV: assert(isNumber(y),
|
|
485
|
+
DEV: assert(isNumber(x), 'oval() 1st parameter must be a number')
|
|
486
|
+
DEV: assert(isNumber(y), 'oval() 2nd parameter must be a number')
|
|
474
487
|
DEV: assert(
|
|
475
488
|
isNumber(radiusX) && radiusX >= 0,
|
|
476
|
-
|
|
489
|
+
'oval() 3rd parameter must be a non-negative number'
|
|
477
490
|
)
|
|
478
491
|
DEV: assert(
|
|
479
492
|
isNumber(radiusY) && radiusY >= 0,
|
|
480
|
-
|
|
493
|
+
'oval() 4th parameter must be a non-negative number'
|
|
481
494
|
)
|
|
482
495
|
DEV: assert(
|
|
483
496
|
null == color || (isNumber(color) && color >= 0),
|
|
484
|
-
|
|
497
|
+
'oval() 5th parameter must be a non-negative number'
|
|
485
498
|
)
|
|
486
499
|
|
|
487
500
|
beginPath(_ctx)
|
|
501
|
+
|
|
488
502
|
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI)
|
|
489
503
|
instance.stroke(color)
|
|
490
504
|
},
|
|
@@ -499,22 +513,23 @@ export default function litecanvas(settings = {}) {
|
|
|
499
513
|
* @param {number} [color=0] the color index
|
|
500
514
|
*/
|
|
501
515
|
ovalfill(x, y, radiusX, radiusY, color) {
|
|
502
|
-
DEV: assert(isNumber(x),
|
|
503
|
-
DEV: assert(isNumber(y),
|
|
516
|
+
DEV: assert(isNumber(x), 'ovalfill() 1st parameter must be a number')
|
|
517
|
+
DEV: assert(isNumber(y), 'ovalfill() 2nd parameter must be a number')
|
|
504
518
|
DEV: assert(
|
|
505
519
|
isNumber(radiusX) && radiusX >= 0,
|
|
506
|
-
|
|
520
|
+
'ovalfill() 3rd parameter must be a non-negative number'
|
|
507
521
|
)
|
|
508
522
|
DEV: assert(
|
|
509
523
|
isNumber(radiusY) && radiusY >= 0,
|
|
510
|
-
|
|
524
|
+
'ovalfill() 4th parameter must be a non-negative number'
|
|
511
525
|
)
|
|
512
526
|
DEV: assert(
|
|
513
527
|
null == color || (isNumber(color) && color >= 0),
|
|
514
|
-
|
|
528
|
+
'ovalfill() 5th parameter must be a non-negative number'
|
|
515
529
|
)
|
|
516
530
|
|
|
517
531
|
beginPath(_ctx)
|
|
532
|
+
|
|
518
533
|
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI)
|
|
519
534
|
instance.fill(color)
|
|
520
535
|
},
|
|
@@ -528,15 +543,15 @@ export default function litecanvas(settings = {}) {
|
|
|
528
543
|
* @param {number} [color=0] the color index
|
|
529
544
|
*/
|
|
530
545
|
circ(x, y, radius, color) {
|
|
531
|
-
DEV: assert(isNumber(x),
|
|
532
|
-
DEV: assert(isNumber(y),
|
|
546
|
+
DEV: assert(isNumber(x), 'circ() 1st parameter must be a number')
|
|
547
|
+
DEV: assert(isNumber(y), 'circ() 2nd parameter must be a number')
|
|
533
548
|
DEV: assert(
|
|
534
549
|
isNumber(radius) && radius >= 0,
|
|
535
|
-
|
|
550
|
+
'circ() 3rd parameter must be a non-negative number'
|
|
536
551
|
)
|
|
537
552
|
DEV: assert(
|
|
538
553
|
null == color || (isNumber(color) && color >= 0),
|
|
539
|
-
|
|
554
|
+
'circ() 4th parameter must be a non-negative number'
|
|
540
555
|
)
|
|
541
556
|
|
|
542
557
|
instance.oval(x, y, radius, radius, color)
|
|
@@ -551,15 +566,15 @@ export default function litecanvas(settings = {}) {
|
|
|
551
566
|
* @param {number} [color=0] the color index
|
|
552
567
|
*/
|
|
553
568
|
circfill(x, y, radius, color) {
|
|
554
|
-
DEV: assert(isNumber(x),
|
|
555
|
-
DEV: assert(isNumber(y),
|
|
569
|
+
DEV: assert(isNumber(x), 'circfill() 1st parameter must be a number')
|
|
570
|
+
DEV: assert(isNumber(y), 'circfill() 2nd parameter must be a number')
|
|
556
571
|
DEV: assert(
|
|
557
572
|
isNumber(radius) && radius >= 0,
|
|
558
|
-
|
|
573
|
+
'circfill() 3rd parameter must be a non-negative number'
|
|
559
574
|
)
|
|
560
575
|
DEV: assert(
|
|
561
576
|
null == color || (isNumber(color) && color >= 0),
|
|
562
|
-
|
|
577
|
+
'circfill() 4th parameter must be a non-negative number'
|
|
563
578
|
)
|
|
564
579
|
|
|
565
580
|
instance.ovalfill(x, y, radius, radius, color)
|
|
@@ -572,21 +587,19 @@ export default function litecanvas(settings = {}) {
|
|
|
572
587
|
* @param {number[]} points an array of Xs and Ys coordinates
|
|
573
588
|
*/
|
|
574
589
|
shape(points) {
|
|
575
|
-
DEV: assert(
|
|
576
|
-
Array.isArray(points),
|
|
577
|
-
loggerPrefix + 'shape() 1st param must be an array of numbers'
|
|
578
|
-
)
|
|
590
|
+
DEV: assert(Array.isArray(points), 'shape() 1st parameter must be an array of numbers')
|
|
579
591
|
DEV: assert(
|
|
580
592
|
points.length >= 6,
|
|
581
|
-
|
|
582
|
-
'shape() 1st param must be an array with at least 6 numbers (3 points)'
|
|
593
|
+
'shape() 1st parameter must be an array with at least 6 numbers (3 points)'
|
|
583
594
|
)
|
|
595
|
+
|
|
584
596
|
beginPath(_ctx)
|
|
597
|
+
|
|
585
598
|
for (let i = 0; i < points.length; i += 2) {
|
|
586
|
-
if (
|
|
587
|
-
_ctx.moveTo(~~points[i], ~~points[i + 1])
|
|
588
|
-
} else {
|
|
599
|
+
if (i) {
|
|
589
600
|
_ctx.lineTo(~~points[i], ~~points[i + 1])
|
|
601
|
+
} else {
|
|
602
|
+
_ctx.moveTo(~~points[i], ~~points[i + 1])
|
|
590
603
|
}
|
|
591
604
|
}
|
|
592
605
|
_ctx.lineTo(~~points[0], ~~points[1])
|
|
@@ -602,25 +615,19 @@ export default function litecanvas(settings = {}) {
|
|
|
602
615
|
* @param {number} [color=0] the color index
|
|
603
616
|
*/
|
|
604
617
|
line(x1, y1, x2, y2, color) {
|
|
605
|
-
DEV: assert(isNumber(x1),
|
|
606
|
-
DEV: assert(isNumber(y1),
|
|
607
|
-
DEV: assert(
|
|
608
|
-
|
|
609
|
-
loggerPrefix + 'line() 3rd param must be a positive number or zero'
|
|
610
|
-
)
|
|
611
|
-
DEV: assert(
|
|
612
|
-
isNumber(y2),
|
|
613
|
-
loggerPrefix + 'line() 4th param must be a positive number or zero'
|
|
614
|
-
)
|
|
618
|
+
DEV: assert(isNumber(x1), 'line() 1st parameter must be a number')
|
|
619
|
+
DEV: assert(isNumber(y1), 'line() 2nd parameter must be a number')
|
|
620
|
+
DEV: assert(isNumber(x2), 'line() 3rd parameter must be a non-negative number')
|
|
621
|
+
DEV: assert(isNumber(y2), 'line() 4th parameter must be a non-negative number')
|
|
615
622
|
DEV: assert(
|
|
616
623
|
null == color || (isNumber(color) && color >= 0),
|
|
617
|
-
|
|
624
|
+
'line() 5th parameter must be a non-negative number'
|
|
618
625
|
)
|
|
619
626
|
|
|
620
627
|
beginPath(_ctx)
|
|
621
628
|
|
|
622
|
-
let xfix = _outline_fix
|
|
623
|
-
let yfix = _outline_fix
|
|
629
|
+
let xfix = _outline_fix && ~~x1 === ~~x2 ? 0.5 : 0
|
|
630
|
+
let yfix = _outline_fix && ~~y1 === ~~y2 ? 0.5 : 0
|
|
624
631
|
|
|
625
632
|
_ctx.moveTo(~~x1 + xfix, ~~y1 + yfix)
|
|
626
633
|
_ctx.lineTo(~~x2 + xfix, ~~y2 + yfix)
|
|
@@ -637,11 +644,11 @@ export default function litecanvas(settings = {}) {
|
|
|
637
644
|
linewidth(value) {
|
|
638
645
|
DEV: assert(
|
|
639
646
|
isNumber(value) && value >= 0,
|
|
640
|
-
|
|
647
|
+
'linewidth() 1st parameter must be a non-negative integer'
|
|
641
648
|
)
|
|
642
649
|
|
|
643
650
|
_ctx.lineWidth = ~~value
|
|
644
|
-
_outline_fix =
|
|
651
|
+
_outline_fix = ~~value % 2 ? 0.5 : 0
|
|
645
652
|
},
|
|
646
653
|
|
|
647
654
|
/**
|
|
@@ -655,9 +662,9 @@ export default function litecanvas(settings = {}) {
|
|
|
655
662
|
linedash(segments, offset = 0) {
|
|
656
663
|
DEV: assert(
|
|
657
664
|
Array.isArray(segments) && segments.length > 0,
|
|
658
|
-
|
|
665
|
+
'linedash() 1st parameter must be an array of numbers'
|
|
659
666
|
)
|
|
660
|
-
DEV: assert(isNumber(offset),
|
|
667
|
+
DEV: assert(isNumber(offset), 'linedash() 2nd parameter must be a number')
|
|
661
668
|
|
|
662
669
|
_ctx.setLineDash(segments)
|
|
663
670
|
_ctx.lineDashOffset = offset
|
|
@@ -674,16 +681,13 @@ export default function litecanvas(settings = {}) {
|
|
|
674
681
|
* @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
|
|
675
682
|
*/
|
|
676
683
|
text(x, y, message, color = _defaultTextColor, fontStyle = 'normal') {
|
|
677
|
-
DEV: assert(isNumber(x),
|
|
678
|
-
DEV: assert(isNumber(y),
|
|
684
|
+
DEV: assert(isNumber(x), 'text() 1st parameter must be a number')
|
|
685
|
+
DEV: assert(isNumber(y), 'text() 2nd parameter must be a number')
|
|
679
686
|
DEV: assert(
|
|
680
687
|
null == color || (isNumber(color) && color >= 0),
|
|
681
|
-
|
|
682
|
-
)
|
|
683
|
-
DEV: assert(
|
|
684
|
-
'string' === typeof fontStyle,
|
|
685
|
-
loggerPrefix + 'text() 5th param must be a string'
|
|
688
|
+
'text() 4th parameter must be a non-negative number'
|
|
686
689
|
)
|
|
690
|
+
DEV: assert('string' === typeof fontStyle, 'text() 5th parameter must be a string')
|
|
687
691
|
|
|
688
692
|
_ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`
|
|
689
693
|
_ctx.fillStyle = getColor(color)
|
|
@@ -702,7 +706,7 @@ export default function litecanvas(settings = {}) {
|
|
|
702
706
|
* @param {number} value
|
|
703
707
|
*/
|
|
704
708
|
textgap(value) {
|
|
705
|
-
DEV: assert(isNumber(value),
|
|
709
|
+
DEV: assert(isNumber(value), 'textgap() 1st parameter must be a number')
|
|
706
710
|
|
|
707
711
|
_fontLineHeight = value
|
|
708
712
|
},
|
|
@@ -713,10 +717,7 @@ export default function litecanvas(settings = {}) {
|
|
|
713
717
|
* @param {string} family
|
|
714
718
|
*/
|
|
715
719
|
textfont(family) {
|
|
716
|
-
DEV: assert(
|
|
717
|
-
'string' === typeof family,
|
|
718
|
-
loggerPrefix + 'textfont() 1st param must be a string'
|
|
719
|
-
)
|
|
720
|
+
DEV: assert('string' === typeof family, 'textfont() 1st parameter must be a string')
|
|
720
721
|
|
|
721
722
|
_fontFamily = family
|
|
722
723
|
},
|
|
@@ -727,7 +728,7 @@ export default function litecanvas(settings = {}) {
|
|
|
727
728
|
* @param {number} size
|
|
728
729
|
*/
|
|
729
730
|
textsize(size) {
|
|
730
|
-
DEV: assert(isNumber(size),
|
|
731
|
+
DEV: assert(isNumber(size), 'textsize() 1st parameter must be a number')
|
|
731
732
|
|
|
732
733
|
_fontSize = size
|
|
733
734
|
},
|
|
@@ -743,16 +744,16 @@ export default function litecanvas(settings = {}) {
|
|
|
743
744
|
textalign(align, baseline) {
|
|
744
745
|
DEV: assert(
|
|
745
746
|
null == align || ['left', 'right', 'center', 'start', 'end'].includes(align),
|
|
746
|
-
|
|
747
|
-
|
|
747
|
+
|
|
748
|
+
'textalign() 1st parameter must be null or one of the following strings: center, left, right, start or end.'
|
|
748
749
|
)
|
|
749
750
|
DEV: assert(
|
|
750
751
|
null == baseline ||
|
|
751
752
|
['top', 'bottom', 'middle', 'hanging', 'alphabetic', 'ideographic'].includes(
|
|
752
753
|
baseline
|
|
753
754
|
),
|
|
754
|
-
|
|
755
|
-
|
|
755
|
+
|
|
756
|
+
'textalign() 2nd parameter must be null or one of the following strings: middle, top, bottom, hanging, alphabetic or ideographic.'
|
|
756
757
|
)
|
|
757
758
|
|
|
758
759
|
if (align) _ctx.textAlign = align
|
|
@@ -768,8 +769,8 @@ export default function litecanvas(settings = {}) {
|
|
|
768
769
|
* @param {CanvasImageSource} source
|
|
769
770
|
*/
|
|
770
771
|
image(x, y, source) {
|
|
771
|
-
DEV: assert(isNumber(x),
|
|
772
|
-
DEV: assert(isNumber(y),
|
|
772
|
+
DEV: assert(isNumber(x), 'image() 1st parameter must be a number')
|
|
773
|
+
DEV: assert(isNumber(y), 'image() 2nd parameter must be a number')
|
|
773
774
|
|
|
774
775
|
_ctx.drawImage(source, ~~x, ~~y)
|
|
775
776
|
},
|
|
@@ -782,12 +783,9 @@ export default function litecanvas(settings = {}) {
|
|
|
782
783
|
* @param {string} pixels
|
|
783
784
|
*/
|
|
784
785
|
spr(x, y, pixels) {
|
|
785
|
-
DEV: assert(isNumber(x),
|
|
786
|
-
DEV: assert(isNumber(y),
|
|
787
|
-
DEV: assert(
|
|
788
|
-
'string' === typeof pixels,
|
|
789
|
-
loggerPrefix + 'spr() 3rd param must be a string'
|
|
790
|
-
)
|
|
786
|
+
DEV: assert(isNumber(x), 'spr() 1st parameter must be a number')
|
|
787
|
+
DEV: assert(isNumber(y), 'spr() 2nd parameter must be a number')
|
|
788
|
+
DEV: assert('string' === typeof pixels, 'spr() 3rd parameter must be a string')
|
|
791
789
|
|
|
792
790
|
const rows = pixels.trim().split('\n')
|
|
793
791
|
|
|
@@ -817,24 +815,21 @@ export default function litecanvas(settings = {}) {
|
|
|
817
815
|
paint(width, height, callback, options = {}) {
|
|
818
816
|
DEV: assert(
|
|
819
817
|
isNumber(width) && width >= 1,
|
|
820
|
-
|
|
818
|
+
'paint() 1st parameter must be a positive number'
|
|
821
819
|
)
|
|
822
820
|
DEV: assert(
|
|
823
821
|
isNumber(height) && height >= 1,
|
|
824
|
-
|
|
825
|
-
)
|
|
826
|
-
DEV: assert(
|
|
827
|
-
'function' === typeof callback,
|
|
828
|
-
loggerPrefix + 'paint() 3rd param must be a function'
|
|
822
|
+
'paint() 2nd parameter must be a positive number'
|
|
829
823
|
)
|
|
824
|
+
DEV: assert('function' === typeof callback, 'paint() 3rd parameter must be a function')
|
|
830
825
|
DEV: assert(
|
|
831
826
|
(options && null == options.scale) ||
|
|
832
827
|
(isNumber(options.scale) && options.scale > 0),
|
|
833
|
-
|
|
828
|
+
'paint() 4th parameter (options.scale) must be a positive number'
|
|
834
829
|
)
|
|
835
830
|
DEV: assert(
|
|
836
831
|
(options && null == options.canvas) || options.canvas instanceof OffscreenCanvas,
|
|
837
|
-
|
|
832
|
+
'paint() 4th parameter (options.canvas) must be an OffscreenCanvas'
|
|
838
833
|
)
|
|
839
834
|
|
|
840
835
|
const /** @type {OffscreenCanvas} */
|
|
@@ -867,22 +862,38 @@ export default function litecanvas(settings = {}) {
|
|
|
867
862
|
null == context ||
|
|
868
863
|
context instanceof CanvasRenderingContext2D ||
|
|
869
864
|
context instanceof OffscreenCanvasRenderingContext2D,
|
|
870
|
-
|
|
865
|
+
'ctx() 1st parameter must be an [Offscreen]CanvasRenderingContext2D'
|
|
871
866
|
)
|
|
872
867
|
|
|
873
868
|
if (context) {
|
|
874
869
|
_ctx = context
|
|
875
870
|
}
|
|
871
|
+
|
|
876
872
|
return _ctx
|
|
877
873
|
},
|
|
878
874
|
|
|
879
875
|
/**
|
|
880
|
-
*
|
|
876
|
+
* Saves the current drawing style settings and, optionally, transforms (translate/rotate/scale) the canvas.
|
|
877
|
+
*
|
|
878
|
+
* @param {number} [translateX]
|
|
879
|
+
* @param {number} [translateY]
|
|
880
|
+
* @param {number} [rotation] in radians
|
|
881
|
+
* @param {number} [scaleX]
|
|
882
|
+
* @param {number} [scaleY]
|
|
881
883
|
*
|
|
882
884
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
|
|
883
885
|
*/
|
|
884
|
-
push() {
|
|
886
|
+
push(translateX = 0, translateY = translateX, rotation = 0, scaleX = 1, scaleY = scaleX) {
|
|
887
|
+
DEV: assert(isNumber(translateX), 'push() 1st parameter must be a number')
|
|
888
|
+
DEV: assert(isNumber(translateY), 'push() 2nd parameter must be a number')
|
|
889
|
+
DEV: assert(isNumber(rotation), 'push() 3rd parameter must be a number')
|
|
890
|
+
DEV: assert(isNumber(scaleX), 'push() 4th parameter must be a number')
|
|
891
|
+
DEV: assert(isNumber(scaleY), 'push() 5th parameter must be a number')
|
|
892
|
+
|
|
885
893
|
_ctx.save()
|
|
894
|
+
instance.translate(translateX, translateY)
|
|
895
|
+
instance.rotate(rotation)
|
|
896
|
+
instance.scale(scaleX, scaleY)
|
|
886
897
|
},
|
|
887
898
|
|
|
888
899
|
/**
|
|
@@ -901,8 +912,8 @@ export default function litecanvas(settings = {}) {
|
|
|
901
912
|
* @param {number} y
|
|
902
913
|
*/
|
|
903
914
|
translate(x, y) {
|
|
904
|
-
DEV: assert(isNumber(x),
|
|
905
|
-
DEV: assert(isNumber(y),
|
|
915
|
+
DEV: assert(isNumber(x), 'translate() 1st parameter must be a number')
|
|
916
|
+
DEV: assert(isNumber(y), 'translate() 2nd parameter must be a number')
|
|
906
917
|
|
|
907
918
|
_ctx.translate(~~x, ~~y)
|
|
908
919
|
},
|
|
@@ -914,8 +925,8 @@ export default function litecanvas(settings = {}) {
|
|
|
914
925
|
* @param {number} [y]
|
|
915
926
|
*/
|
|
916
927
|
scale(x, y = x) {
|
|
917
|
-
DEV: assert(isNumber(x),
|
|
918
|
-
DEV: assert(isNumber(y),
|
|
928
|
+
DEV: assert(isNumber(x), 'scale() 1st parameter must be a number')
|
|
929
|
+
DEV: assert(isNumber(y), 'scale() 2nd parameter must be a number')
|
|
919
930
|
|
|
920
931
|
_ctx.scale(x, y)
|
|
921
932
|
},
|
|
@@ -926,7 +937,7 @@ export default function litecanvas(settings = {}) {
|
|
|
926
937
|
* @param {number} radians
|
|
927
938
|
*/
|
|
928
939
|
rotate(radians) {
|
|
929
|
-
DEV: assert(isNumber(radians),
|
|
940
|
+
DEV: assert(isNumber(radians), 'rotate() 1st parameter must be a number')
|
|
930
941
|
|
|
931
942
|
_ctx.rotate(radians)
|
|
932
943
|
},
|
|
@@ -938,7 +949,7 @@ export default function litecanvas(settings = {}) {
|
|
|
938
949
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
939
950
|
*/
|
|
940
951
|
alpha(value) {
|
|
941
|
-
DEV: assert(isNumber(value),
|
|
952
|
+
DEV: assert(isNumber(value), 'alpha() 1st parameter must be a number')
|
|
942
953
|
|
|
943
954
|
_ctx.globalAlpha = instance.clamp(value, 0, 1)
|
|
944
955
|
},
|
|
@@ -951,7 +962,7 @@ export default function litecanvas(settings = {}) {
|
|
|
951
962
|
fill(color) {
|
|
952
963
|
DEV: assert(
|
|
953
964
|
null == color || (isNumber(color) && color >= 0),
|
|
954
|
-
|
|
965
|
+
'fill() 1st parameter must be a non-negative number'
|
|
955
966
|
)
|
|
956
967
|
|
|
957
968
|
_ctx.fillStyle = getColor(color)
|
|
@@ -966,7 +977,7 @@ export default function litecanvas(settings = {}) {
|
|
|
966
977
|
stroke(color) {
|
|
967
978
|
DEV: assert(
|
|
968
979
|
null == color || (isNumber(color) && color >= 0),
|
|
969
|
-
|
|
980
|
+
'stroke() 1st parameter must be a non-negative number'
|
|
970
981
|
)
|
|
971
982
|
|
|
972
983
|
_ctx.strokeStyle = getColor(color)
|
|
@@ -982,7 +993,7 @@ export default function litecanvas(settings = {}) {
|
|
|
982
993
|
clip(callback) {
|
|
983
994
|
DEV: assert(
|
|
984
995
|
'function' === typeof callback,
|
|
985
|
-
|
|
996
|
+
'clip() 1st parameter must be a function (ctx) => void'
|
|
986
997
|
)
|
|
987
998
|
|
|
988
999
|
beginPath(_ctx)
|
|
@@ -995,7 +1006,7 @@ export default function litecanvas(settings = {}) {
|
|
|
995
1006
|
* Play a sound effects using ZzFX library.
|
|
996
1007
|
* If the first argument is omitted, plays an default sound.
|
|
997
1008
|
*
|
|
998
|
-
* @param {number[]} [zzfxParams] a ZzFX array of
|
|
1009
|
+
* @param {number[]} [zzfxParams] a ZzFX array of parameters
|
|
999
1010
|
* @param {number} [pitchSlide] a value to increment/decrement the pitch
|
|
1000
1011
|
* @param {number} [volumeFactor] the volume factor
|
|
1001
1012
|
* @returns {number[] | boolean} The sound that was played or `false`
|
|
@@ -1005,15 +1016,15 @@ export default function litecanvas(settings = {}) {
|
|
|
1005
1016
|
sfx(zzfxParams, pitchSlide, volumeFactor) {
|
|
1006
1017
|
DEV: assert(
|
|
1007
1018
|
null == zzfxParams || Array.isArray(zzfxParams),
|
|
1008
|
-
|
|
1019
|
+
'sfx() 1st parameter must be an array'
|
|
1009
1020
|
)
|
|
1010
1021
|
DEV: assert(
|
|
1011
1022
|
null == pitchSlide || isNumber(pitchSlide),
|
|
1012
|
-
|
|
1023
|
+
'sfx() 2nd parameter must be a number'
|
|
1013
1024
|
)
|
|
1014
1025
|
DEV: assert(
|
|
1015
1026
|
null == volumeFactor || isNumber(volumeFactor),
|
|
1016
|
-
|
|
1027
|
+
'sfx() 3rd parameter must be a number'
|
|
1017
1028
|
)
|
|
1018
1029
|
|
|
1019
1030
|
if (
|
|
@@ -1030,7 +1041,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1030
1041
|
zzfxParams = zzfxParams.slice()
|
|
1031
1042
|
zzfxParams[0] = volumeFactor * (zzfxParams[0] || 1)
|
|
1032
1043
|
zzfxParams[10] = ~~zzfxParams[10] + pitchSlide
|
|
1033
|
-
console.log(zzfxParams)
|
|
1034
1044
|
}
|
|
1035
1045
|
|
|
1036
1046
|
zzfx.apply(0, zzfxParams)
|
|
@@ -1047,7 +1057,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1047
1057
|
volume(value) {
|
|
1048
1058
|
DEV: assert(
|
|
1049
1059
|
isNumber(value) && value >= 0,
|
|
1050
|
-
|
|
1060
|
+
'volume() 1st parameter must be a non-negative number'
|
|
1051
1061
|
)
|
|
1052
1062
|
|
|
1053
1063
|
root.zzfxV = value
|
|
@@ -1070,12 +1080,9 @@ export default function litecanvas(settings = {}) {
|
|
|
1070
1080
|
use(callback, config = {}) {
|
|
1071
1081
|
DEV: assert(
|
|
1072
1082
|
'function' === typeof callback,
|
|
1073
|
-
|
|
1074
|
-
)
|
|
1075
|
-
DEV: assert(
|
|
1076
|
-
'object' === typeof config,
|
|
1077
|
-
loggerPrefix + 'use() 2nd param must be an object'
|
|
1083
|
+
'use() 1st parameter must be a function (instance, config) => any'
|
|
1078
1084
|
)
|
|
1085
|
+
DEV: assert('object' === typeof config, 'use() 2nd parameter must be an object')
|
|
1079
1086
|
|
|
1080
1087
|
loadPlugin(callback, config)
|
|
1081
1088
|
},
|
|
@@ -1087,14 +1094,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1087
1094
|
* @param {Function} callback the function that is called when the event occurs
|
|
1088
1095
|
*/
|
|
1089
1096
|
listen: (eventName, callback) => {
|
|
1090
|
-
DEV: assert(
|
|
1091
|
-
|
|
1092
|
-
loggerPrefix + 'listen() 1st param must be a string'
|
|
1093
|
-
)
|
|
1094
|
-
DEV: assert(
|
|
1095
|
-
'function' === typeof callback,
|
|
1096
|
-
loggerPrefix + 'listen() 2nd param must be a function'
|
|
1097
|
-
)
|
|
1097
|
+
DEV: assert('string' === typeof eventName, 'listen() 1st parameter must be a string')
|
|
1098
|
+
DEV: assert('function' === typeof callback, 'listen() 2nd parameter must be a function')
|
|
1098
1099
|
|
|
1099
1100
|
eventName = lowerCase(eventName)
|
|
1100
1101
|
|
|
@@ -1109,13 +1110,10 @@ export default function litecanvas(settings = {}) {
|
|
|
1109
1110
|
* @param {Function} callback the function that is called when the event occurs
|
|
1110
1111
|
*/
|
|
1111
1112
|
unlisten: (eventName, callback) => {
|
|
1112
|
-
DEV: assert(
|
|
1113
|
-
'string' === typeof eventName,
|
|
1114
|
-
loggerPrefix + 'unlisten() 1st param must be a string'
|
|
1115
|
-
)
|
|
1113
|
+
DEV: assert('string' === typeof eventName, 'unlisten() 1st parameter must be a string')
|
|
1116
1114
|
DEV: assert(
|
|
1117
1115
|
'function' === typeof callback,
|
|
1118
|
-
|
|
1116
|
+
'unlisten() 2nd parameter must be a function'
|
|
1119
1117
|
)
|
|
1120
1118
|
|
|
1121
1119
|
eventName = lowerCase(eventName)
|
|
@@ -1140,10 +1138,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1140
1138
|
* @returns {any} always returns the second argument
|
|
1141
1139
|
*/
|
|
1142
1140
|
emit(eventName, arg1, arg2, arg3, arg4) {
|
|
1143
|
-
DEV: assert(
|
|
1144
|
-
'string' === typeof eventName,
|
|
1145
|
-
loggerPrefix + 'emit() 1st param must be a string'
|
|
1146
|
-
)
|
|
1141
|
+
DEV: assert('string' === typeof eventName, 'emit() 1st parameter must be a string')
|
|
1147
1142
|
|
|
1148
1143
|
if (_initialized) {
|
|
1149
1144
|
eventName = lowerCase(eventName)
|
|
@@ -1178,11 +1173,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1178
1173
|
pal(colors, textColor = 3) {
|
|
1179
1174
|
DEV: assert(
|
|
1180
1175
|
null == colors || (Array.isArray(colors) && colors.length > 0),
|
|
1181
|
-
|
|
1176
|
+
'pal() 1st parameter must be null or an array of colors'
|
|
1182
1177
|
)
|
|
1183
1178
|
DEV: assert(
|
|
1184
1179
|
isNumber(textColor) && textColor >= 0,
|
|
1185
|
-
|
|
1180
|
+
'pal() 2nd parameter must be a non-negative number'
|
|
1186
1181
|
)
|
|
1187
1182
|
|
|
1188
1183
|
_colorPalette = colors || defaultPalette
|
|
@@ -1205,14 +1200,14 @@ export default function litecanvas(settings = {}) {
|
|
|
1205
1200
|
palc(a, b) {
|
|
1206
1201
|
DEV: assert(
|
|
1207
1202
|
null == a || (isNumber(a) && a >= 0),
|
|
1208
|
-
|
|
1203
|
+
'palc() 1st parameter must be a positive number'
|
|
1209
1204
|
)
|
|
1210
1205
|
DEV: assert(
|
|
1211
1206
|
isNumber(a) ? isNumber(b) && b >= 0 : null == b,
|
|
1212
|
-
|
|
1207
|
+
'palc() 2nd parameter must be a positive number'
|
|
1213
1208
|
)
|
|
1214
1209
|
|
|
1215
|
-
if (
|
|
1210
|
+
if (null == a) {
|
|
1216
1211
|
_colorPaletteState = []
|
|
1217
1212
|
} else {
|
|
1218
1213
|
_colorPaletteState[a] = b
|
|
@@ -1231,11 +1226,10 @@ export default function litecanvas(settings = {}) {
|
|
|
1231
1226
|
* @param {any} value the property value
|
|
1232
1227
|
*/
|
|
1233
1228
|
def(key, value) {
|
|
1234
|
-
DEV: assert('string' === typeof key,
|
|
1229
|
+
DEV: assert('string' === typeof key, 'def() 1st parameter must be a string')
|
|
1235
1230
|
DEV: if (null == value) {
|
|
1236
1231
|
console.warn(
|
|
1237
|
-
|
|
1238
|
-
`def() changed the key "${key}" to null (previous value was ${instance[key]})`
|
|
1232
|
+
`[litecanvas] def() changed the key "${key}" to null (previous value was ${instance[key]})`
|
|
1239
1233
|
)
|
|
1240
1234
|
}
|
|
1241
1235
|
|
|
@@ -1255,7 +1249,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1255
1249
|
timescale(value) {
|
|
1256
1250
|
DEV: assert(
|
|
1257
1251
|
isNumber(value) && value >= 0,
|
|
1258
|
-
|
|
1252
|
+
'timescale() 1st parameter must be a non-negative number'
|
|
1259
1253
|
)
|
|
1260
1254
|
|
|
1261
1255
|
_timeScale = value
|
|
@@ -1269,7 +1263,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1269
1263
|
framerate(value) {
|
|
1270
1264
|
DEV: assert(
|
|
1271
1265
|
isNumber(value) && value >= 1,
|
|
1272
|
-
|
|
1266
|
+
'framerate() 1st parameter must be a positive number'
|
|
1273
1267
|
)
|
|
1274
1268
|
|
|
1275
1269
|
_fpsInterval = 1000 / ~~value
|
|
@@ -1282,7 +1276,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1282
1276
|
* @returns {any}
|
|
1283
1277
|
*/
|
|
1284
1278
|
stat(index) {
|
|
1285
|
-
DEV: assert(isNumber(index),
|
|
1279
|
+
DEV: assert(isNumber(index), 'stat() 1st parameter must be a number')
|
|
1286
1280
|
|
|
1287
1281
|
const internals = [
|
|
1288
1282
|
// 0
|
|
@@ -1330,9 +1324,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1330
1324
|
|
|
1331
1325
|
DEV: assert(
|
|
1332
1326
|
index >= 0 && index < internals.length,
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
(internals.length - 1)
|
|
1327
|
+
|
|
1328
|
+
'stat() 1st parameter must be a number between 0 and ' + (internals.length - 1)
|
|
1336
1329
|
)
|
|
1337
1330
|
|
|
1338
1331
|
return internals[index]
|
|
@@ -1355,8 +1348,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1355
1348
|
resume() {
|
|
1356
1349
|
DEV: assert(
|
|
1357
1350
|
_initialized,
|
|
1358
|
-
|
|
1359
|
-
|
|
1351
|
+
|
|
1352
|
+
'resume() cannot be called before the "init" event and neither after the quit() function'
|
|
1360
1353
|
)
|
|
1361
1354
|
if (_initialized && _paused) {
|
|
1362
1355
|
startGameLoop()
|
|
@@ -1403,7 +1396,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1403
1396
|
delete root.ENGINE
|
|
1404
1397
|
}
|
|
1405
1398
|
|
|
1406
|
-
DEV: console.warn(
|
|
1399
|
+
DEV: console.warn('[litecanvas] quit() terminated a Litecanvas instance.')
|
|
1407
1400
|
},
|
|
1408
1401
|
}
|
|
1409
1402
|
|
|
@@ -1438,6 +1431,9 @@ export default function litecanvas(settings = {}) {
|
|
|
1438
1431
|
(ev.pageX - _canvas.offsetLeft) / _canvasScale,
|
|
1439
1432
|
(ev.pageY - _canvas.offsetTop) / _canvasScale,
|
|
1440
1433
|
],
|
|
1434
|
+
/**
|
|
1435
|
+
* @type {Map<number,{x:number, y:number, xi: number, yi: number, t:number}>}
|
|
1436
|
+
*/
|
|
1441
1437
|
_taps = new Map(),
|
|
1442
1438
|
_registerTap =
|
|
1443
1439
|
/**
|
|
@@ -1487,7 +1483,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1487
1483
|
* @param {MouseEvent} ev
|
|
1488
1484
|
*/
|
|
1489
1485
|
(ev) => {
|
|
1490
|
-
if (ev.button
|
|
1486
|
+
if (!ev.button) {
|
|
1491
1487
|
preventDefault(ev)
|
|
1492
1488
|
const [x, y] = _getXY(ev)
|
|
1493
1489
|
instance.emit('tap', x, y, 0)
|
|
@@ -1504,7 +1500,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1504
1500
|
* @param {MouseEvent} ev
|
|
1505
1501
|
*/
|
|
1506
1502
|
(ev) => {
|
|
1507
|
-
if (ev.button
|
|
1503
|
+
if (!ev.button) {
|
|
1508
1504
|
preventDefault(ev)
|
|
1509
1505
|
const tap = _taps.get(0)
|
|
1510
1506
|
const [x, y] = _getXY(ev)
|
|
@@ -1580,10 +1576,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1580
1576
|
preventDefault(ev)
|
|
1581
1577
|
const existing = []
|
|
1582
1578
|
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
existing.push(touch.identifier + 1)
|
|
1586
|
-
}
|
|
1579
|
+
for (const touch of ev.targetTouches) {
|
|
1580
|
+
existing.push(touch.identifier + 1)
|
|
1587
1581
|
}
|
|
1588
1582
|
|
|
1589
1583
|
for (const [id, tap] of _taps) {
|
|
@@ -1654,7 +1648,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1654
1648
|
(key) => {
|
|
1655
1649
|
DEV: assert(
|
|
1656
1650
|
null == key || 'string' === typeof key,
|
|
1657
|
-
|
|
1651
|
+
'iskeydown() 1st parameter must be a string or undefined'
|
|
1658
1652
|
)
|
|
1659
1653
|
return keyCheck(_keysDown, key)
|
|
1660
1654
|
}
|
|
@@ -1669,7 +1663,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1669
1663
|
(key) => {
|
|
1670
1664
|
DEV: assert(
|
|
1671
1665
|
null == key || 'string' === typeof key,
|
|
1672
|
-
|
|
1666
|
+
'iskeypressed() 1st parameter must be a string or undefined'
|
|
1673
1667
|
)
|
|
1674
1668
|
return keyCheck(_keysPress, key)
|
|
1675
1669
|
}
|
|
@@ -1732,10 +1726,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1732
1726
|
|
|
1733
1727
|
if ('string' === typeof settings.canvas) {
|
|
1734
1728
|
_canvas = d.querySelector(settings.canvas)
|
|
1735
|
-
DEV: assert(
|
|
1736
|
-
null != _canvas,
|
|
1737
|
-
loggerPrefix + 'litecanvas() option "canvas" is an invalid CSS selector'
|
|
1738
|
-
)
|
|
1729
|
+
DEV: assert(null != _canvas, 'litecanvas() option "canvas" is an invalid CSS selector')
|
|
1739
1730
|
} else {
|
|
1740
1731
|
_canvas = settings.canvas
|
|
1741
1732
|
}
|
|
@@ -1744,8 +1735,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1744
1735
|
|
|
1745
1736
|
DEV: assert(
|
|
1746
1737
|
_canvas instanceof HTMLElement && 'CANVAS' === _canvas.tagName,
|
|
1747
|
-
|
|
1748
|
-
|
|
1738
|
+
|
|
1739
|
+
'litecanvas() option "canvas" should be a canvas element or string (CSS selector of a canvas)'
|
|
1749
1740
|
)
|
|
1750
1741
|
|
|
1751
1742
|
_ctx = _canvas.getContext('2d')
|
|
@@ -1767,16 +1758,16 @@ export default function litecanvas(settings = {}) {
|
|
|
1767
1758
|
function resizeCanvas() {
|
|
1768
1759
|
DEV: assert(
|
|
1769
1760
|
null == settings.width || (isNumber(settings.width) && settings.width > 0),
|
|
1770
|
-
|
|
1761
|
+
'litecanvas() option "width" should be a positive number when defined'
|
|
1771
1762
|
)
|
|
1772
1763
|
DEV: assert(
|
|
1773
1764
|
null == settings.height || (isNumber(settings.height) && settings.height > 0),
|
|
1774
|
-
|
|
1765
|
+
'litecanvas() option "height" should be a positive number when defined'
|
|
1775
1766
|
)
|
|
1776
1767
|
DEV: assert(
|
|
1777
1768
|
null == settings.height || (settings.width > 0 && settings.height > 0),
|
|
1778
|
-
|
|
1779
|
-
|
|
1769
|
+
|
|
1770
|
+
'litecanvas() option "width" is required when the option "height" is defined'
|
|
1780
1771
|
)
|
|
1781
1772
|
|
|
1782
1773
|
const width = settings.width > 0 ? settings.width : innerWidth,
|
|
@@ -1837,7 +1828,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1837
1828
|
|
|
1838
1829
|
DEV: assert(
|
|
1839
1830
|
null == pluginData || 'object' === typeof pluginData,
|
|
1840
|
-
|
|
1831
|
+
'litecanvas() plugins should return an object or nothing'
|
|
1841
1832
|
)
|
|
1842
1833
|
|
|
1843
1834
|
for (const key in pluginData) {
|
|
@@ -1862,8 +1853,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1862
1853
|
root.ENGINE = instance
|
|
1863
1854
|
}
|
|
1864
1855
|
|
|
1865
|
-
DEV: console.info(
|
|
1866
|
-
DEV: console.debug(
|
|
1856
|
+
DEV: console.info(`[litecanvas] version ${version} started`)
|
|
1857
|
+
DEV: console.debug(`[litecanvas] litecanvas() options =`, settings)
|
|
1867
1858
|
|
|
1868
1859
|
// setup the canvas
|
|
1869
1860
|
setupCanvas()
|