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