litecanvas 0.78.2 → 0.79.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 +135 -155
- package/dist/dist.js +46 -58
- package/dist/dist.min.js +1 -1
- package/package.json +2 -2
- package/src/index.js +147 -171
- package/types/index.d.ts +0 -7
- package/types/types.d.ts +0 -7
package/src/index.js
CHANGED
|
@@ -12,8 +12,7 @@ import './types.js'
|
|
|
12
12
|
export default function litecanvas(settings = {}) {
|
|
13
13
|
const root = globalThis,
|
|
14
14
|
math = Math,
|
|
15
|
-
|
|
16
|
-
TWO_PI = PI * 2,
|
|
15
|
+
TWO_PI = math.PI * 2,
|
|
17
16
|
raf = requestAnimationFrame,
|
|
18
17
|
/** @type {Function[]} */
|
|
19
18
|
_browserEventListeners = [],
|
|
@@ -24,7 +23,7 @@ export default function litecanvas(settings = {}) {
|
|
|
24
23
|
elem.removeEventListener(evt, callback, false)
|
|
25
24
|
)
|
|
26
25
|
},
|
|
27
|
-
|
|
26
|
+
isNumber = Number.isFinite,
|
|
28
27
|
/** @type {LitecanvasOptions} */
|
|
29
28
|
defaults = {
|
|
30
29
|
width: null,
|
|
@@ -49,9 +48,7 @@ export default function litecanvas(settings = {}) {
|
|
|
49
48
|
/** @type {Function[]} */
|
|
50
49
|
_plugins = [],
|
|
51
50
|
/** @type {HTMLCanvasElement|string} _canvas */
|
|
52
|
-
_canvas
|
|
53
|
-
/** @type {boolean} */
|
|
54
|
-
_animated = settings.animate,
|
|
51
|
+
_canvas,
|
|
55
52
|
/** @type {number} */
|
|
56
53
|
_scale = 1,
|
|
57
54
|
/** @type {CanvasRenderingContext2D} */
|
|
@@ -62,7 +59,7 @@ export default function litecanvas(settings = {}) {
|
|
|
62
59
|
_timeScale = 1,
|
|
63
60
|
/** @type {number} */
|
|
64
61
|
_lastFrameTime,
|
|
65
|
-
/** @type {number} */
|
|
62
|
+
/** @type {number} duration of a frame at 60 FPS (default) */
|
|
66
63
|
_deltaTime = 1 / 60,
|
|
67
64
|
/** @type {number} */
|
|
68
65
|
_accumulated = 0,
|
|
@@ -71,24 +68,22 @@ export default function litecanvas(settings = {}) {
|
|
|
71
68
|
/** @type {string} */
|
|
72
69
|
_fontFamily = 'sans-serif',
|
|
73
70
|
/** @type {number} */
|
|
74
|
-
_fontSize =
|
|
71
|
+
_fontSize = 20,
|
|
75
72
|
/** @type {number} */
|
|
76
73
|
_rng_seed = Date.now(),
|
|
77
|
-
/** @type {boolean} */
|
|
78
|
-
_global = settings.global,
|
|
79
74
|
/**
|
|
80
75
|
* default game events
|
|
81
76
|
* @type {Object<string,Set<Function>>}
|
|
82
77
|
*/
|
|
83
78
|
_events = {
|
|
84
|
-
init:
|
|
85
|
-
update:
|
|
86
|
-
draw:
|
|
87
|
-
resized:
|
|
88
|
-
tap:
|
|
89
|
-
untap:
|
|
90
|
-
tapping:
|
|
91
|
-
tapped:
|
|
79
|
+
init: false,
|
|
80
|
+
update: false,
|
|
81
|
+
draw: false,
|
|
82
|
+
resized: false,
|
|
83
|
+
tap: false,
|
|
84
|
+
untap: false,
|
|
85
|
+
tapping: false,
|
|
86
|
+
tapped: false,
|
|
92
87
|
},
|
|
93
88
|
/**
|
|
94
89
|
* Helpers to be used by plugins
|
|
@@ -103,13 +98,13 @@ export default function litecanvas(settings = {}) {
|
|
|
103
98
|
/** @type {LitecanvasInstance} */
|
|
104
99
|
const instance = {
|
|
105
100
|
/** @type {number} */
|
|
106
|
-
WIDTH:
|
|
101
|
+
WIDTH: 0,
|
|
107
102
|
|
|
108
103
|
/** @type {number} */
|
|
109
|
-
HEIGHT:
|
|
104
|
+
HEIGHT: 0,
|
|
110
105
|
|
|
111
106
|
/** @type {HTMLCanvasElement} */
|
|
112
|
-
CANVAS:
|
|
107
|
+
CANVAS: false,
|
|
113
108
|
|
|
114
109
|
/** @type {number} */
|
|
115
110
|
ELAPSED: 0,
|
|
@@ -127,7 +122,7 @@ export default function litecanvas(settings = {}) {
|
|
|
127
122
|
MOUSEY: -1,
|
|
128
123
|
|
|
129
124
|
/** @type {number[]} */
|
|
130
|
-
DEFAULT_SFX: [0.5, ,
|
|
125
|
+
DEFAULT_SFX: [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1],
|
|
131
126
|
|
|
132
127
|
/** MATH API */
|
|
133
128
|
/**
|
|
@@ -147,7 +142,7 @@ export default function litecanvas(settings = {}) {
|
|
|
147
142
|
*
|
|
148
143
|
* @type {number}
|
|
149
144
|
*/
|
|
150
|
-
HALF_PI: PI / 2,
|
|
145
|
+
HALF_PI: math.PI / 2,
|
|
151
146
|
|
|
152
147
|
/**
|
|
153
148
|
* Calculates a linear (interpolation) value over t%.
|
|
@@ -159,9 +154,9 @@ export default function litecanvas(settings = {}) {
|
|
|
159
154
|
* @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
|
|
160
155
|
*/
|
|
161
156
|
lerp: (start, end, t) => {
|
|
162
|
-
DEV: assert(
|
|
163
|
-
DEV: assert(
|
|
164
|
-
DEV: assert(
|
|
157
|
+
DEV: assert(isNumber(start), 'lerp: 1st param must be a number')
|
|
158
|
+
DEV: assert(isNumber(end), 'lerp: 2nd param must be a number')
|
|
159
|
+
DEV: assert(isNumber(t), 'lerp: 3rd param must be a number')
|
|
165
160
|
|
|
166
161
|
return t * (end - start) + start
|
|
167
162
|
},
|
|
@@ -173,9 +168,9 @@ export default function litecanvas(settings = {}) {
|
|
|
173
168
|
* @returns {number} the value in radians
|
|
174
169
|
*/
|
|
175
170
|
deg2rad: (degs) => {
|
|
176
|
-
DEV: assert(
|
|
171
|
+
DEV: assert(isNumber(degs), 'deg2rad: 1st param must be a number')
|
|
177
172
|
|
|
178
|
-
return (PI / 180) * degs
|
|
173
|
+
return (math.PI / 180) * degs
|
|
179
174
|
},
|
|
180
175
|
|
|
181
176
|
/**
|
|
@@ -185,9 +180,9 @@ export default function litecanvas(settings = {}) {
|
|
|
185
180
|
* @returns {number} the value in degrees
|
|
186
181
|
*/
|
|
187
182
|
rad2deg: (rads) => {
|
|
188
|
-
DEV: assert(
|
|
183
|
+
DEV: assert(isNumber(rads), 'rad2deg: 1st param must be a number')
|
|
189
184
|
|
|
190
|
-
return (180 / PI) * rads
|
|
185
|
+
return (180 / math.PI) * rads
|
|
191
186
|
},
|
|
192
187
|
|
|
193
188
|
/**
|
|
@@ -200,9 +195,9 @@ export default function litecanvas(settings = {}) {
|
|
|
200
195
|
* @returns {number} rounded number.
|
|
201
196
|
*/
|
|
202
197
|
round: (n, precision = 0) => {
|
|
203
|
-
DEV: assert(
|
|
198
|
+
DEV: assert(isNumber(n), 'round: 1st param must be a number')
|
|
204
199
|
DEV: assert(
|
|
205
|
-
null
|
|
200
|
+
null == precision || (isNumber(precision) && precision >= 0),
|
|
206
201
|
'round: 2nd param must be a positive number or zero'
|
|
207
202
|
)
|
|
208
203
|
if (!precision) {
|
|
@@ -221,9 +216,9 @@ export default function litecanvas(settings = {}) {
|
|
|
221
216
|
* @returns {number}
|
|
222
217
|
*/
|
|
223
218
|
clamp: (value, min, max) => {
|
|
224
|
-
DEV: assert(
|
|
225
|
-
DEV: assert(
|
|
226
|
-
DEV: assert(
|
|
219
|
+
DEV: assert(isNumber(value), 'clamp: 1st param must be a number')
|
|
220
|
+
DEV: assert(isNumber(min), 'clamp: 2nd param must be a number')
|
|
221
|
+
DEV: assert(isNumber(max), 'clamp: 3rd param must be a number')
|
|
227
222
|
DEV: assert(
|
|
228
223
|
max > min,
|
|
229
224
|
'randi: the 2nd param must be less than the 3rd param'
|
|
@@ -243,9 +238,9 @@ export default function litecanvas(settings = {}) {
|
|
|
243
238
|
* @returns {number}
|
|
244
239
|
*/
|
|
245
240
|
wrap: (value, min, max) => {
|
|
246
|
-
DEV: assert(
|
|
247
|
-
DEV: assert(
|
|
248
|
-
DEV: assert(
|
|
241
|
+
DEV: assert(isNumber(value), 'wrap: 1st param must be a number')
|
|
242
|
+
DEV: assert(isNumber(min), 'wrap: 2nd param must be a number')
|
|
243
|
+
DEV: assert(isNumber(max), 'wrap: 3rd param must be a number')
|
|
249
244
|
DEV: assert(
|
|
250
245
|
max > min,
|
|
251
246
|
'randi: the 2nd param must be less than the 3rd param'
|
|
@@ -270,11 +265,11 @@ export default function litecanvas(settings = {}) {
|
|
|
270
265
|
* @returns {number} the remapped number
|
|
271
266
|
*/
|
|
272
267
|
map(value, start1, stop1, start2, stop2, withinBounds) {
|
|
273
|
-
DEV: assert(
|
|
274
|
-
DEV: assert(
|
|
275
|
-
DEV: assert(
|
|
276
|
-
DEV: assert(
|
|
277
|
-
DEV: assert(
|
|
268
|
+
DEV: assert(isNumber(value), 'map: 1st param must be a number')
|
|
269
|
+
DEV: assert(isNumber(start1), 'map: 2nd param must be a number')
|
|
270
|
+
DEV: assert(isNumber(stop1), 'map: 3rd param must be a number')
|
|
271
|
+
DEV: assert(isNumber(start2), 'map: 4th param must be a number')
|
|
272
|
+
DEV: assert(isNumber(stop2), 'map: 5th param must be a number')
|
|
278
273
|
|
|
279
274
|
// prettier-ignore
|
|
280
275
|
const result = ((value - start1) / (stop1 - start1)) * (stop2 - start2) + start2
|
|
@@ -292,9 +287,9 @@ export default function litecanvas(settings = {}) {
|
|
|
292
287
|
* @returns {number} the normalized number.
|
|
293
288
|
*/
|
|
294
289
|
norm: (value, start, stop) => {
|
|
295
|
-
DEV: assert(
|
|
296
|
-
DEV: assert(
|
|
297
|
-
DEV: assert(
|
|
290
|
+
DEV: assert(isNumber(value), 'norm: 1st param must be a number')
|
|
291
|
+
DEV: assert(isNumber(start), 'norm: 2nd param must be a number')
|
|
292
|
+
DEV: assert(isNumber(stop), 'norm: 3rd param must be a number')
|
|
298
293
|
|
|
299
294
|
return instance.map(value, start, stop, 0, 1)
|
|
300
295
|
},
|
|
@@ -309,8 +304,8 @@ export default function litecanvas(settings = {}) {
|
|
|
309
304
|
* @returns {number} the random number
|
|
310
305
|
*/
|
|
311
306
|
rand: (min = 0.0, max = 1.0) => {
|
|
312
|
-
DEV: assert(
|
|
313
|
-
DEV: assert(
|
|
307
|
+
DEV: assert(isNumber(min), 'rand: 1st param must be a number')
|
|
308
|
+
DEV: assert(isNumber(max), 'rand: 2nd param must be a number')
|
|
314
309
|
DEV: assert(
|
|
315
310
|
max > min,
|
|
316
311
|
'rand: the 1st param must be less than the 2nd param'
|
|
@@ -333,8 +328,8 @@ export default function litecanvas(settings = {}) {
|
|
|
333
328
|
* @returns {number} the random number
|
|
334
329
|
*/
|
|
335
330
|
randi: (min = 0, max = 1) => {
|
|
336
|
-
DEV: assert(
|
|
337
|
-
DEV: assert(
|
|
331
|
+
DEV: assert(isNumber(min), 'randi: 1st param must be a number')
|
|
332
|
+
DEV: assert(isNumber(max), 'randi: 2nd param must be a number')
|
|
338
333
|
DEV: assert(
|
|
339
334
|
max > min,
|
|
340
335
|
'randi: the 1st param must be less than the 2nd param'
|
|
@@ -352,7 +347,7 @@ export default function litecanvas(settings = {}) {
|
|
|
352
347
|
*/
|
|
353
348
|
seed: (value) => {
|
|
354
349
|
DEV: assert(
|
|
355
|
-
null == value || (
|
|
350
|
+
null == value || (isNumber(value) && value >= 0),
|
|
356
351
|
'seed: 1st param must be a positive number or zero'
|
|
357
352
|
)
|
|
358
353
|
|
|
@@ -363,11 +358,11 @@ export default function litecanvas(settings = {}) {
|
|
|
363
358
|
/**
|
|
364
359
|
* Clear the game screen with an optional color
|
|
365
360
|
*
|
|
366
|
-
* @param {number
|
|
361
|
+
* @param {number} [color] The background color (index) or null/undefined (for transparent)
|
|
367
362
|
*/
|
|
368
363
|
cls(color) {
|
|
369
364
|
DEV: assert(
|
|
370
|
-
null == color || (
|
|
365
|
+
null == color || (isNumber(color) && color >= 0),
|
|
371
366
|
'cls: 1st param must be a positive number or zero or undefined'
|
|
372
367
|
)
|
|
373
368
|
|
|
@@ -394,24 +389,24 @@ export default function litecanvas(settings = {}) {
|
|
|
394
389
|
* @param {number} [color=0] the color index
|
|
395
390
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
396
391
|
*/
|
|
397
|
-
rect(x, y, width, height, color, radii
|
|
398
|
-
DEV: assert(
|
|
399
|
-
DEV: assert(
|
|
392
|
+
rect(x, y, width, height, color, radii) {
|
|
393
|
+
DEV: assert(isNumber(x), 'rect: 1st param must be a number')
|
|
394
|
+
DEV: assert(isNumber(y), 'rect: 2nd param must be a number')
|
|
400
395
|
DEV: assert(
|
|
401
|
-
|
|
396
|
+
isNumber(width) && width > 0,
|
|
402
397
|
'rect: 3rd param must be a positive number'
|
|
403
398
|
)
|
|
404
399
|
DEV: assert(
|
|
405
|
-
|
|
400
|
+
isNumber(height) && height >= 0,
|
|
406
401
|
'rect: 4th param must be a positive number or zero'
|
|
407
402
|
)
|
|
408
403
|
DEV: assert(
|
|
409
|
-
null == color || (
|
|
404
|
+
null == color || (isNumber(color) && color >= 0),
|
|
410
405
|
'rect: 5th param must be a positive number or zero'
|
|
411
406
|
)
|
|
412
407
|
DEV: assert(
|
|
413
408
|
null == radii ||
|
|
414
|
-
|
|
409
|
+
isNumber(radii) ||
|
|
415
410
|
(Array.isArray(radii) && radii.length >= 1),
|
|
416
411
|
'rect: 6th param must be a number or array of numbers'
|
|
417
412
|
)
|
|
@@ -437,24 +432,24 @@ export default function litecanvas(settings = {}) {
|
|
|
437
432
|
* @param {number} [color=0] the color index
|
|
438
433
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
439
434
|
*/
|
|
440
|
-
rectfill(x, y, width, height, color, radii
|
|
441
|
-
DEV: assert(
|
|
442
|
-
DEV: assert(
|
|
435
|
+
rectfill(x, y, width, height, color, radii) {
|
|
436
|
+
DEV: assert(isNumber(x), 'rectfill: 1st param must be a number')
|
|
437
|
+
DEV: assert(isNumber(y), 'rectfill: 2nd param must be a number')
|
|
443
438
|
DEV: assert(
|
|
444
|
-
|
|
439
|
+
isNumber(width) && width >= 0,
|
|
445
440
|
'rectfill: 3rd param must be a positive number or zero'
|
|
446
441
|
)
|
|
447
442
|
DEV: assert(
|
|
448
|
-
|
|
443
|
+
isNumber(height) && height >= 0,
|
|
449
444
|
'rectfill: 4th param must be a positive number or zero'
|
|
450
445
|
)
|
|
451
446
|
DEV: assert(
|
|
452
|
-
null == color || (
|
|
447
|
+
null == color || (isNumber(color) && color >= 0),
|
|
453
448
|
'rectfill: 5th param must be a positive number or zero'
|
|
454
449
|
)
|
|
455
450
|
DEV: assert(
|
|
456
451
|
null == radii ||
|
|
457
|
-
|
|
452
|
+
isNumber(radii) ||
|
|
458
453
|
(Array.isArray(radii) && radii.length >= 1),
|
|
459
454
|
'rectfill: 6th param must be a number or array of at least 2 numbers'
|
|
460
455
|
)
|
|
@@ -479,14 +474,14 @@ export default function litecanvas(settings = {}) {
|
|
|
479
474
|
* @param {number} [color=0] the color index
|
|
480
475
|
*/
|
|
481
476
|
circ(x, y, radius, color) {
|
|
482
|
-
DEV: assert(
|
|
483
|
-
DEV: assert(
|
|
477
|
+
DEV: assert(isNumber(x), 'circ: 1st param must be a number')
|
|
478
|
+
DEV: assert(isNumber(y), 'circ: 2nd param must be a number')
|
|
484
479
|
DEV: assert(
|
|
485
|
-
|
|
480
|
+
isNumber(radius) && radius >= 0,
|
|
486
481
|
'circ: 3rd param must be a positive number or zero'
|
|
487
482
|
)
|
|
488
483
|
DEV: assert(
|
|
489
|
-
null == color || (
|
|
484
|
+
null == color || (isNumber(color) && color >= 0),
|
|
490
485
|
'circ: 4th param must be a positive number or zero'
|
|
491
486
|
)
|
|
492
487
|
|
|
@@ -504,14 +499,14 @@ export default function litecanvas(settings = {}) {
|
|
|
504
499
|
* @param {number} [color=0] the color index
|
|
505
500
|
*/
|
|
506
501
|
circfill(x, y, radius, color) {
|
|
507
|
-
DEV: assert(
|
|
508
|
-
DEV: assert(
|
|
502
|
+
DEV: assert(isNumber(x), 'circfill: 1st param must be a number')
|
|
503
|
+
DEV: assert(isNumber(y), 'circfill: 2nd param must be a number')
|
|
509
504
|
DEV: assert(
|
|
510
|
-
|
|
505
|
+
isNumber(radius) && radius >= 0,
|
|
511
506
|
'circfill: 3rd param must be a positive number or zero'
|
|
512
507
|
)
|
|
513
508
|
DEV: assert(
|
|
514
|
-
null == color || (
|
|
509
|
+
null == color || (isNumber(color) && color >= 0),
|
|
515
510
|
'circfill: 4th param must be a positive number or zero'
|
|
516
511
|
)
|
|
517
512
|
|
|
@@ -530,18 +525,18 @@ export default function litecanvas(settings = {}) {
|
|
|
530
525
|
* @param {number} [color=0] the color index
|
|
531
526
|
*/
|
|
532
527
|
line(x1, y1, x2, y2, color) {
|
|
533
|
-
DEV: assert(
|
|
534
|
-
DEV: assert(
|
|
528
|
+
DEV: assert(isNumber(x1), 'line: 1st param must be a number')
|
|
529
|
+
DEV: assert(isNumber(y1), 'line: 2nd param must be a number')
|
|
535
530
|
DEV: assert(
|
|
536
|
-
|
|
531
|
+
isNumber(x2),
|
|
537
532
|
'line: 3rd param must be a positive number or zero'
|
|
538
533
|
)
|
|
539
534
|
DEV: assert(
|
|
540
|
-
|
|
535
|
+
isNumber(y2),
|
|
541
536
|
'line: 4th param must be a positive number or zero'
|
|
542
537
|
)
|
|
543
538
|
DEV: assert(
|
|
544
|
-
null == color || (
|
|
539
|
+
null == color || (isNumber(color) && color >= 0),
|
|
545
540
|
'line: 5th param must be a positive number or zero'
|
|
546
541
|
)
|
|
547
542
|
|
|
@@ -564,12 +559,12 @@ export default function litecanvas(settings = {}) {
|
|
|
564
559
|
*/
|
|
565
560
|
linewidth(value) {
|
|
566
561
|
DEV: assert(
|
|
567
|
-
|
|
562
|
+
isNumber(value) && ~~value > 0,
|
|
568
563
|
'linewidth: 1st param must be a positive number'
|
|
569
564
|
)
|
|
570
565
|
|
|
571
566
|
_ctx.lineWidth = ~~value
|
|
572
|
-
_outline_fix = ~~value % 2
|
|
567
|
+
_outline_fix = 0 === ~~value % 2 ? 0 : 0.5
|
|
573
568
|
},
|
|
574
569
|
|
|
575
570
|
/**
|
|
@@ -586,7 +581,7 @@ export default function litecanvas(settings = {}) {
|
|
|
586
581
|
'linedash: 1st param must be an array of numbers'
|
|
587
582
|
)
|
|
588
583
|
DEV: assert(
|
|
589
|
-
|
|
584
|
+
isNumber(offset),
|
|
590
585
|
'linedash: 2nd param must be a number'
|
|
591
586
|
)
|
|
592
587
|
|
|
@@ -605,14 +600,14 @@ export default function litecanvas(settings = {}) {
|
|
|
605
600
|
* @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
|
|
606
601
|
*/
|
|
607
602
|
text(x, y, message, color = 3, fontStyle = 'normal') {
|
|
608
|
-
DEV: assert(
|
|
609
|
-
DEV: assert(
|
|
603
|
+
DEV: assert(isNumber(x), 'text: 1st param must be a number')
|
|
604
|
+
DEV: assert(isNumber(y), 'text: 2nd param must be a number')
|
|
610
605
|
// DEV: assert(
|
|
611
606
|
// 'string' === typeof message,
|
|
612
607
|
// 'text: 3rd param must be a string'
|
|
613
608
|
// )
|
|
614
609
|
DEV: assert(
|
|
615
|
-
null == color || (
|
|
610
|
+
null == color || (isNumber(color) && color >= 0),
|
|
616
611
|
'text: 4th param must be a positive number or zero'
|
|
617
612
|
)
|
|
618
613
|
DEV: assert(
|
|
@@ -645,7 +640,7 @@ export default function litecanvas(settings = {}) {
|
|
|
645
640
|
* @param {number} size
|
|
646
641
|
*/
|
|
647
642
|
textsize(size) {
|
|
648
|
-
DEV: assert(
|
|
643
|
+
DEV: assert(isNumber(size), 'textsize: 1st param must be a number')
|
|
649
644
|
|
|
650
645
|
_fontSize = size
|
|
651
646
|
},
|
|
@@ -690,8 +685,8 @@ export default function litecanvas(settings = {}) {
|
|
|
690
685
|
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
691
686
|
*/
|
|
692
687
|
image(x, y, source) {
|
|
693
|
-
DEV: assert(
|
|
694
|
-
DEV: assert(
|
|
688
|
+
DEV: assert(isNumber(x), 'image: 1st param must be a number')
|
|
689
|
+
DEV: assert(isNumber(y), 'image: 2nd param must be a number')
|
|
695
690
|
|
|
696
691
|
_ctx.drawImage(source, ~~x, ~~y)
|
|
697
692
|
},
|
|
@@ -709,14 +704,14 @@ export default function litecanvas(settings = {}) {
|
|
|
709
704
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
710
705
|
*/
|
|
711
706
|
paint(width, height, drawing, options = {}) {
|
|
712
|
-
DEV: assert(
|
|
713
|
-
DEV: assert(
|
|
707
|
+
DEV: assert(isNumber(width), 'paint: 1st param must be a number')
|
|
708
|
+
DEV: assert(isNumber(height), 'paint: 2nd param must be a number')
|
|
714
709
|
DEV: assert(
|
|
715
710
|
'function' === typeof drawing || Array.isArray(drawing),
|
|
716
711
|
'paint: 3rd param must be a function or array'
|
|
717
712
|
)
|
|
718
713
|
DEV: assert(
|
|
719
|
-
(options && !options.scale) ||
|
|
714
|
+
(options && !options.scale) || isNumber(options.scale),
|
|
720
715
|
'paint: 4th param (options.scale) must be a number'
|
|
721
716
|
)
|
|
722
717
|
|
|
@@ -793,8 +788,8 @@ export default function litecanvas(settings = {}) {
|
|
|
793
788
|
* @param {number} y
|
|
794
789
|
*/
|
|
795
790
|
translate: (x, y) => {
|
|
796
|
-
DEV: assert(
|
|
797
|
-
DEV: assert(
|
|
791
|
+
DEV: assert(isNumber(x), 'translate: 1st param must be a number')
|
|
792
|
+
DEV: assert(isNumber(y), 'translate: 2nd param must be a number')
|
|
798
793
|
|
|
799
794
|
return _ctx.translate(~~x, ~~y)
|
|
800
795
|
},
|
|
@@ -806,9 +801,9 @@ export default function litecanvas(settings = {}) {
|
|
|
806
801
|
* @param {number} [y]
|
|
807
802
|
*/
|
|
808
803
|
scale: (x, y) => {
|
|
809
|
-
DEV: assert(
|
|
804
|
+
DEV: assert(isNumber(x), 'scale: 1st param must be a number')
|
|
810
805
|
DEV: assert(
|
|
811
|
-
|
|
806
|
+
null == y || isNumber(y),
|
|
812
807
|
'scale: 2nd param must be a number'
|
|
813
808
|
)
|
|
814
809
|
|
|
@@ -821,7 +816,7 @@ export default function litecanvas(settings = {}) {
|
|
|
821
816
|
* @param {number} radians
|
|
822
817
|
*/
|
|
823
818
|
rotate: (radians) => {
|
|
824
|
-
DEV: assert(
|
|
819
|
+
DEV: assert(isNumber(radians), 'rotate: 1st param must be a number')
|
|
825
820
|
|
|
826
821
|
return _ctx.rotate(radians)
|
|
827
822
|
},
|
|
@@ -833,7 +828,7 @@ export default function litecanvas(settings = {}) {
|
|
|
833
828
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
834
829
|
*/
|
|
835
830
|
alpha(value) {
|
|
836
|
-
DEV: assert(
|
|
831
|
+
DEV: assert(isNumber(value), 'alpha: 1st param must be a number')
|
|
837
832
|
|
|
838
833
|
_ctx.globalAlpha = instance.clamp(value, 0, 1)
|
|
839
834
|
},
|
|
@@ -864,7 +859,7 @@ export default function litecanvas(settings = {}) {
|
|
|
864
859
|
*/
|
|
865
860
|
fill(color, path) {
|
|
866
861
|
DEV: assert(
|
|
867
|
-
null == color || (
|
|
862
|
+
null == color || (isNumber(color) && color >= 0),
|
|
868
863
|
'fill: 1st param must be a positive number or zero'
|
|
869
864
|
)
|
|
870
865
|
DEV: assert(
|
|
@@ -888,7 +883,7 @@ export default function litecanvas(settings = {}) {
|
|
|
888
883
|
*/
|
|
889
884
|
stroke(color, path) {
|
|
890
885
|
DEV: assert(
|
|
891
|
-
null == color || (
|
|
886
|
+
null == color || (isNumber(color) && color >= 0),
|
|
892
887
|
'stroke: 1st param must be a positive number or zero'
|
|
893
888
|
)
|
|
894
889
|
DEV: assert(
|
|
@@ -936,9 +931,9 @@ export default function litecanvas(settings = {}) {
|
|
|
936
931
|
null == zzfxParams || Array.isArray(zzfxParams),
|
|
937
932
|
'sfx: 1st param must be an array'
|
|
938
933
|
)
|
|
939
|
-
DEV: assert(
|
|
934
|
+
DEV: assert(isNumber(pitchSlide), 'sfx: 2nd param must be a number')
|
|
940
935
|
DEV: assert(
|
|
941
|
-
|
|
936
|
+
isNumber(volumeFactor),
|
|
942
937
|
'sfx: 3rd param must be a number'
|
|
943
938
|
)
|
|
944
939
|
|
|
@@ -971,7 +966,7 @@ export default function litecanvas(settings = {}) {
|
|
|
971
966
|
* @param {number} value
|
|
972
967
|
*/
|
|
973
968
|
volume(value) {
|
|
974
|
-
DEV: assert(
|
|
969
|
+
DEV: assert(isNumber(value), 'volume: 1st param must be a number')
|
|
975
970
|
|
|
976
971
|
root.zzfxV = value
|
|
977
972
|
},
|
|
@@ -1050,7 +1045,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1050
1045
|
*/
|
|
1051
1046
|
getcolor: (index) => {
|
|
1052
1047
|
DEV: assert(
|
|
1053
|
-
null == index || (
|
|
1048
|
+
null == index || (isNumber(index) && index >= 0),
|
|
1054
1049
|
'getcolor: 1st param must be a number'
|
|
1055
1050
|
)
|
|
1056
1051
|
|
|
@@ -1068,41 +1063,16 @@ export default function litecanvas(settings = {}) {
|
|
|
1068
1063
|
'string' === typeof key,
|
|
1069
1064
|
'setvar: 1st param must be a string'
|
|
1070
1065
|
)
|
|
1071
|
-
if (
|
|
1066
|
+
if (null == value) {
|
|
1072
1067
|
console.warn(`setvar: key "${key}" was defined as ${value}`)
|
|
1073
1068
|
}
|
|
1074
1069
|
|
|
1075
1070
|
instance[key] = value
|
|
1076
|
-
if (
|
|
1071
|
+
if (settings.global) {
|
|
1077
1072
|
root[key] = value
|
|
1078
1073
|
}
|
|
1079
1074
|
},
|
|
1080
1075
|
|
|
1081
|
-
/**
|
|
1082
|
-
* Resizes the game canvas and emit the "resized" event
|
|
1083
|
-
*
|
|
1084
|
-
* @param {number} width
|
|
1085
|
-
* @param {number} height
|
|
1086
|
-
*/
|
|
1087
|
-
resize(width, height) {
|
|
1088
|
-
DEV: assert(
|
|
1089
|
-
isFinite(width) && width > 0,
|
|
1090
|
-
'resize: 1st param must be a number'
|
|
1091
|
-
)
|
|
1092
|
-
DEV: assert(
|
|
1093
|
-
isFinite(height) && height > 0,
|
|
1094
|
-
'resize: 2nd param must be a number'
|
|
1095
|
-
)
|
|
1096
|
-
|
|
1097
|
-
instance.setvar('WIDTH', (_canvas.width = width))
|
|
1098
|
-
instance.setvar('HEIGHT', (_canvas.height = height))
|
|
1099
|
-
|
|
1100
|
-
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1101
|
-
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1102
|
-
|
|
1103
|
-
onResize()
|
|
1104
|
-
},
|
|
1105
|
-
|
|
1106
1076
|
/**
|
|
1107
1077
|
* The scale of the game's delta time (dt).
|
|
1108
1078
|
* Values higher than 1 increase the speed of time, while values smaller than 1 decrease it.
|
|
@@ -1112,7 +1082,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1112
1082
|
*/
|
|
1113
1083
|
timescale(value) {
|
|
1114
1084
|
DEV: assert(
|
|
1115
|
-
|
|
1085
|
+
isNumber(value),
|
|
1116
1086
|
'timescale: 1st param must be a number'
|
|
1117
1087
|
)
|
|
1118
1088
|
|
|
@@ -1126,7 +1096,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1126
1096
|
*/
|
|
1127
1097
|
setfps(value) {
|
|
1128
1098
|
DEV: assert(
|
|
1129
|
-
|
|
1099
|
+
isNumber(value) && value >= 1,
|
|
1130
1100
|
'setfps: 1st param must be a positive number'
|
|
1131
1101
|
)
|
|
1132
1102
|
|
|
@@ -1152,7 +1122,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1152
1122
|
}
|
|
1153
1123
|
|
|
1154
1124
|
// maybe clear global context
|
|
1155
|
-
if (
|
|
1125
|
+
if (settings.global) {
|
|
1156
1126
|
for (const key in instance) {
|
|
1157
1127
|
delete root[key]
|
|
1158
1128
|
}
|
|
@@ -1183,7 +1153,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1183
1153
|
|
|
1184
1154
|
// listen window resize event when "autoscale" is enabled
|
|
1185
1155
|
if (settings.autoscale) {
|
|
1186
|
-
on(root, 'resize',
|
|
1156
|
+
on(root, 'resize', resizeCanvas)
|
|
1187
1157
|
}
|
|
1188
1158
|
|
|
1189
1159
|
// default mouse/touch handlers
|
|
@@ -1318,7 +1288,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1318
1288
|
|
|
1319
1289
|
/**
|
|
1320
1290
|
* @param {Set<string>} keysSet
|
|
1321
|
-
* @param {string
|
|
1291
|
+
* @param {string} [key]
|
|
1322
1292
|
* @returns {boolean}
|
|
1323
1293
|
*/
|
|
1324
1294
|
const keyCheck = (keysSet, key) => {
|
|
@@ -1349,7 +1319,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1349
1319
|
* Checks if a which key is pressed (down) on the keyboard.
|
|
1350
1320
|
* Note: use `iskeydown()` to check for any key.
|
|
1351
1321
|
*
|
|
1352
|
-
* @param {string
|
|
1322
|
+
* @param {string} [key]
|
|
1353
1323
|
* @returns {boolean}
|
|
1354
1324
|
*/
|
|
1355
1325
|
(key) => {
|
|
@@ -1367,7 +1337,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1367
1337
|
* Checks if a which key just got pressed on the keyboard.
|
|
1368
1338
|
* Note: use `iskeypressed()` to check for any key.
|
|
1369
1339
|
*
|
|
1370
|
-
* @param {string
|
|
1340
|
+
* @param {string} [key]
|
|
1371
1341
|
* @returns {boolean}
|
|
1372
1342
|
*/
|
|
1373
1343
|
(key) => {
|
|
@@ -1409,7 +1379,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1409
1379
|
|
|
1410
1380
|
_lastFrameTime = now
|
|
1411
1381
|
|
|
1412
|
-
if (
|
|
1382
|
+
if (settings.animate) {
|
|
1413
1383
|
// request the next frame
|
|
1414
1384
|
_rafid = raf(drawFrame)
|
|
1415
1385
|
|
|
@@ -1435,7 +1405,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1435
1405
|
}
|
|
1436
1406
|
|
|
1437
1407
|
if (updated) {
|
|
1438
|
-
// by default the text
|
|
1439
1408
|
// always set default values for
|
|
1440
1409
|
// _ctx.textAlign and _ctx.textBaseline before draw
|
|
1441
1410
|
instance.textalign('start', 'top')
|
|
@@ -1444,6 +1413,8 @@ export default function litecanvas(settings = {}) {
|
|
|
1444
1413
|
}
|
|
1445
1414
|
|
|
1446
1415
|
function setupCanvas() {
|
|
1416
|
+
_canvas = settings.canvas || document.createElement('canvas')
|
|
1417
|
+
|
|
1447
1418
|
/** @type {HTMLCanvasElement} */
|
|
1448
1419
|
_canvas =
|
|
1449
1420
|
'string' === typeof _canvas
|
|
@@ -1454,19 +1425,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1454
1425
|
_canvas && _canvas.tagName === 'CANVAS',
|
|
1455
1426
|
'Invalid canvas element'
|
|
1456
1427
|
)
|
|
1457
|
-
DEV: assert(
|
|
1458
|
-
null == instance.WIDTH || instance.WIDTH > 0,
|
|
1459
|
-
'Litecanvas\' "width" option should be null or a positive number'
|
|
1460
|
-
)
|
|
1461
|
-
DEV: assert(
|
|
1462
|
-
null == instance.HEIGHT || instance.HEIGHT > 0,
|
|
1463
|
-
'Litecanvas\' "width" option should be null or a positive number'
|
|
1464
|
-
)
|
|
1465
|
-
DEV: assert(
|
|
1466
|
-
null == instance.HEIGHT ||
|
|
1467
|
-
(instance.WIDTH > 0 && instance.HEIGHT > 0),
|
|
1468
|
-
'Litecanvas\' "width" is required when "heigth" is passed'
|
|
1469
|
-
)
|
|
1470
1428
|
|
|
1471
1429
|
instance.setvar('CANVAS', _canvas)
|
|
1472
1430
|
_ctx = _canvas.getContext('2d')
|
|
@@ -1475,24 +1433,41 @@ export default function litecanvas(settings = {}) {
|
|
|
1475
1433
|
|
|
1476
1434
|
_canvas.style = ''
|
|
1477
1435
|
|
|
1478
|
-
|
|
1479
|
-
if (!instance.WIDTH) {
|
|
1480
|
-
instance.WIDTH = root.innerWidth
|
|
1481
|
-
instance.HEIGHT = root.innerHeight
|
|
1482
|
-
}
|
|
1483
|
-
|
|
1484
|
-
instance.resize(instance.WIDTH, instance.HEIGHT, false)
|
|
1436
|
+
resizeCanvas()
|
|
1485
1437
|
|
|
1486
1438
|
if (!_canvas.parentNode) document.body.appendChild(_canvas)
|
|
1487
1439
|
}
|
|
1488
1440
|
|
|
1489
|
-
function
|
|
1490
|
-
|
|
1441
|
+
function resizeCanvas() {
|
|
1442
|
+
DEV: assert(
|
|
1443
|
+
null == settings.width ||
|
|
1444
|
+
(isNumber(settings.width) && settings.width > 0),
|
|
1445
|
+
'Litecanvas\' option "width" should be a positive number when defined'
|
|
1446
|
+
)
|
|
1447
|
+
DEV: assert(
|
|
1448
|
+
null == settings.height ||
|
|
1449
|
+
(isNumber(settings.height) && settings.height > 0),
|
|
1450
|
+
'Litecanvas\' option "height" should be a positive number when defined'
|
|
1451
|
+
)
|
|
1452
|
+
DEV: assert(
|
|
1453
|
+
null == settings.height ||
|
|
1454
|
+
(settings.width > 0 && settings.height > 0),
|
|
1455
|
+
'Litecanvas\' option "width" is required when the option "height" is defined'
|
|
1456
|
+
)
|
|
1457
|
+
|
|
1458
|
+
const width = settings.width || root.innerWidth,
|
|
1459
|
+
height = settings.height || settings.width || root.innerHeight
|
|
1460
|
+
|
|
1461
|
+
instance.setvar('WIDTH', (_canvas.width = width))
|
|
1462
|
+
instance.setvar('HEIGHT', (_canvas.height = height))
|
|
1463
|
+
|
|
1464
|
+
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1465
|
+
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1491
1466
|
|
|
1492
1467
|
if (settings.autoscale) {
|
|
1493
|
-
if (!
|
|
1494
|
-
|
|
1495
|
-
|
|
1468
|
+
if (!_canvas.style.display) {
|
|
1469
|
+
_canvas.style.display = 'block'
|
|
1470
|
+
_canvas.style.margin = 'auto'
|
|
1496
1471
|
}
|
|
1497
1472
|
|
|
1498
1473
|
_scale = math.min(
|
|
@@ -1501,20 +1476,21 @@ export default function litecanvas(settings = {}) {
|
|
|
1501
1476
|
)
|
|
1502
1477
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1
|
|
1503
1478
|
|
|
1504
|
-
|
|
1505
|
-
|
|
1479
|
+
_canvas.style.width = instance.WIDTH * _scale + 'px'
|
|
1480
|
+
_canvas.style.height = instance.HEIGHT * _scale + 'px'
|
|
1506
1481
|
}
|
|
1507
1482
|
|
|
1508
1483
|
// restore canvas image rendering properties
|
|
1509
1484
|
if (!settings.antialias || settings.pixelart) {
|
|
1510
1485
|
_ctx.imageSmoothingEnabled = false
|
|
1511
|
-
|
|
1486
|
+
_canvas.style.imageRendering = 'pixelated'
|
|
1512
1487
|
}
|
|
1513
1488
|
|
|
1489
|
+
// trigger "resized" event
|
|
1514
1490
|
instance.emit('resized', _scale)
|
|
1515
1491
|
|
|
1516
1492
|
// force redraw
|
|
1517
|
-
if (!
|
|
1493
|
+
if (!settings.animate) {
|
|
1518
1494
|
raf(drawFrame)
|
|
1519
1495
|
}
|
|
1520
1496
|
}
|
|
@@ -1542,9 +1518,9 @@ export default function litecanvas(settings = {}) {
|
|
|
1542
1518
|
}
|
|
1543
1519
|
}
|
|
1544
1520
|
|
|
1545
|
-
if (
|
|
1521
|
+
if (settings.global) {
|
|
1546
1522
|
if (root.ENGINE) {
|
|
1547
|
-
throw 'two global litecanvas detected'
|
|
1523
|
+
throw new Error('two global litecanvas detected')
|
|
1548
1524
|
}
|
|
1549
1525
|
Object.assign(root, instance)
|
|
1550
1526
|
root.ENGINE = instance
|