litecanvas 0.85.1 → 0.87.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 +241 -117
- package/dist/dist.js +36 -6
- package/dist/dist.min.js +1 -1
- package/package.json +8 -5
- package/src/index.js +245 -117
- package/types/index.d.ts +20 -0
- package/types/types.d.ts +20 -0
package/dist/dist.dev.js
CHANGED
|
@@ -31,6 +31,9 @@
|
|
|
31
31
|
if (!condition) throw new Error(message);
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
+
// version.js
|
|
35
|
+
var version = "0.87.0";
|
|
36
|
+
|
|
34
37
|
// src/index.js
|
|
35
38
|
function litecanvas(settings = {}) {
|
|
36
39
|
const root = window, math = Math, TWO_PI = math.PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => {
|
|
@@ -90,9 +93,9 @@
|
|
|
90
93
|
* @tutorial https://gamedev.net/tutorials/programming/general-and-gameplay-programming/a-brief-introduction-to-lerp-r4954/
|
|
91
94
|
*/
|
|
92
95
|
lerp: (start, end, t) => {
|
|
93
|
-
DEV: assert(isNumber(start), "lerp
|
|
94
|
-
DEV: assert(isNumber(end), "lerp
|
|
95
|
-
DEV: assert(isNumber(t), "lerp
|
|
96
|
+
DEV: assert(isNumber(start), "[litecanvas] lerp() 1st param must be a number");
|
|
97
|
+
DEV: assert(isNumber(end), "[litecanvas] lerp() 2nd param must be a number");
|
|
98
|
+
DEV: assert(isNumber(t), "[litecanvas] lerp() 3rd param must be a number");
|
|
96
99
|
return t * (end - start) + start;
|
|
97
100
|
},
|
|
98
101
|
/**
|
|
@@ -125,10 +128,10 @@
|
|
|
125
128
|
* @returns {number} rounded number.
|
|
126
129
|
*/
|
|
127
130
|
round: (n, precision = 0) => {
|
|
128
|
-
DEV: assert(isNumber(n), "round
|
|
131
|
+
DEV: assert(isNumber(n), "[litecanvas] round() 1st param must be a number");
|
|
129
132
|
DEV: assert(
|
|
130
133
|
null == precision || isNumber(precision) && precision >= 0,
|
|
131
|
-
"round
|
|
134
|
+
"[litecanvas] round() 2nd param must be a positive number or zero"
|
|
132
135
|
);
|
|
133
136
|
if (!precision) {
|
|
134
137
|
return math.round(n);
|
|
@@ -145,10 +148,13 @@
|
|
|
145
148
|
* @returns {number}
|
|
146
149
|
*/
|
|
147
150
|
clamp: (value, min, max) => {
|
|
148
|
-
DEV: assert(isNumber(value), "clamp
|
|
149
|
-
DEV: assert(isNumber(min), "clamp
|
|
150
|
-
DEV: assert(isNumber(max), "clamp
|
|
151
|
-
DEV: assert(
|
|
151
|
+
DEV: assert(isNumber(value), "[litecanvas] clamp() 1st param must be a number");
|
|
152
|
+
DEV: assert(isNumber(min), "[litecanvas] clamp() 2nd param must be a number");
|
|
153
|
+
DEV: assert(isNumber(max), "[litecanvas] clamp() 3rd param must be a number");
|
|
154
|
+
DEV: assert(
|
|
155
|
+
max > min,
|
|
156
|
+
"[litecanvas] clamp() the 2nd param must be less than the 3rd param"
|
|
157
|
+
);
|
|
152
158
|
if (value < min) return min;
|
|
153
159
|
if (value > max) return max;
|
|
154
160
|
return value;
|
|
@@ -162,10 +168,13 @@
|
|
|
162
168
|
* @returns {number}
|
|
163
169
|
*/
|
|
164
170
|
wrap: (value, min, max) => {
|
|
165
|
-
DEV: assert(isNumber(value), "wrap
|
|
166
|
-
DEV: assert(isNumber(min), "wrap
|
|
167
|
-
DEV: assert(isNumber(max), "wrap
|
|
168
|
-
DEV: assert(
|
|
171
|
+
DEV: assert(isNumber(value), "[litecanvas] wrap() 1st param must be a number");
|
|
172
|
+
DEV: assert(isNumber(min), "[litecanvas] wrap() 2nd param must be a number");
|
|
173
|
+
DEV: assert(isNumber(max), "[litecanvas] wrap() 3rd param must be a number");
|
|
174
|
+
DEV: assert(
|
|
175
|
+
max > min,
|
|
176
|
+
"[litecanvas] wrap() the 2nd param must be less than the 3rd param"
|
|
177
|
+
);
|
|
169
178
|
return value - (max - min) * math.floor((value - min) / (max - min));
|
|
170
179
|
},
|
|
171
180
|
/**
|
|
@@ -180,12 +189,15 @@
|
|
|
180
189
|
* @returns {number} the remapped number
|
|
181
190
|
*/
|
|
182
191
|
map(value, start1, stop1, start2, stop2, withinBounds) {
|
|
183
|
-
DEV: assert(isNumber(value), "map
|
|
184
|
-
DEV: assert(isNumber(start1), "map
|
|
185
|
-
DEV: assert(isNumber(stop1), "map
|
|
186
|
-
DEV: assert(isNumber(start2), "map
|
|
187
|
-
DEV: assert(isNumber(stop2), "map
|
|
188
|
-
DEV: assert(
|
|
192
|
+
DEV: assert(isNumber(value), "[litecanvas] map() 1st param must be a number");
|
|
193
|
+
DEV: assert(isNumber(start1), "[litecanvas] map() 2nd param must be a number");
|
|
194
|
+
DEV: assert(isNumber(stop1), "[litecanvas] map() 3rd param must be a number");
|
|
195
|
+
DEV: assert(isNumber(start2), "[litecanvas] map() 4th param must be a number");
|
|
196
|
+
DEV: assert(isNumber(stop2), "[litecanvas] map() 5th param must be a number");
|
|
197
|
+
DEV: assert(
|
|
198
|
+
stop1 !== start1,
|
|
199
|
+
"[litecanvas] map() the 2nd param must be different than the 3rd param"
|
|
200
|
+
);
|
|
189
201
|
const result = (value - start1) / (stop1 - start1) * (stop2 - start2) + start2;
|
|
190
202
|
return withinBounds ? instance.clamp(result, start2, stop2) : result;
|
|
191
203
|
},
|
|
@@ -200,9 +212,13 @@
|
|
|
200
212
|
* @returns {number} the normalized number.
|
|
201
213
|
*/
|
|
202
214
|
norm: (value, start, stop) => {
|
|
203
|
-
DEV: assert(isNumber(value), "norm
|
|
204
|
-
DEV: assert(isNumber(start), "norm
|
|
205
|
-
DEV: assert(isNumber(stop), "norm
|
|
215
|
+
DEV: assert(isNumber(value), "[litecanvas] norm() 1st param must be a number");
|
|
216
|
+
DEV: assert(isNumber(start), "[litecanvas] norm() 2nd param must be a number");
|
|
217
|
+
DEV: assert(isNumber(stop), "[litecanvas] norm() 3rd param must be a number");
|
|
218
|
+
DEV: assert(
|
|
219
|
+
start !== stop,
|
|
220
|
+
"[litecanvas] norm() the 2nd param must be different than the 3rd param"
|
|
221
|
+
);
|
|
206
222
|
return instance.map(value, start, stop, 0, 1);
|
|
207
223
|
},
|
|
208
224
|
/**
|
|
@@ -214,12 +230,12 @@
|
|
|
214
230
|
* @param {(n: number) => number} [fn] - the periodic function (which default to `Math.sin`)
|
|
215
231
|
*/
|
|
216
232
|
wave: (from, to, t, fn = Math.sin) => {
|
|
217
|
-
DEV: assert(isNumber(from), "wave
|
|
218
|
-
DEV: assert(isNumber(to), "wave
|
|
219
|
-
DEV: assert(isNumber(t), "wave
|
|
233
|
+
DEV: assert(isNumber(from), "[litecanvas] wave() 1st param must be a number");
|
|
234
|
+
DEV: assert(isNumber(to), "[litecanvas] wave() 2nd param must be a number");
|
|
235
|
+
DEV: assert(isNumber(t), "[litecanvas] wave() 3rd param must be a number");
|
|
220
236
|
DEV: assert(
|
|
221
237
|
"function" === typeof fn,
|
|
222
|
-
"wave
|
|
238
|
+
"[litecanvas] wave() 4rd param must be a function (n: number) => number"
|
|
223
239
|
);
|
|
224
240
|
return from + (fn(t) + 1) / 2 * (to - from);
|
|
225
241
|
},
|
|
@@ -233,9 +249,12 @@
|
|
|
233
249
|
* @returns {number} the random number
|
|
234
250
|
*/
|
|
235
251
|
rand: (min = 0, max = 1) => {
|
|
236
|
-
DEV: assert(isNumber(min), "rand
|
|
237
|
-
DEV: assert(isNumber(max), "rand
|
|
238
|
-
DEV: assert(
|
|
252
|
+
DEV: assert(isNumber(min), "[litecanvas] rand() 1st param must be a number");
|
|
253
|
+
DEV: assert(isNumber(max), "[litecanvas] rand() 2nd param must be a number");
|
|
254
|
+
DEV: assert(
|
|
255
|
+
max > min,
|
|
256
|
+
"[litecanvas] rand() the 1st param must be less than the 2nd param"
|
|
257
|
+
);
|
|
239
258
|
const a = 1664525;
|
|
240
259
|
const c = 1013904223;
|
|
241
260
|
const m = 4294967296;
|
|
@@ -250,9 +269,12 @@
|
|
|
250
269
|
* @returns {number} the random number
|
|
251
270
|
*/
|
|
252
271
|
randi: (min = 0, max = 1) => {
|
|
253
|
-
DEV: assert(isNumber(min), "randi
|
|
254
|
-
DEV: assert(isNumber(max), "randi
|
|
255
|
-
DEV: assert(
|
|
272
|
+
DEV: assert(isNumber(min), "[litecanvas] randi() 1st param must be a number");
|
|
273
|
+
DEV: assert(isNumber(max), "[litecanvas] randi() 2nd param must be a number");
|
|
274
|
+
DEV: assert(
|
|
275
|
+
max > min,
|
|
276
|
+
"[litecanvas] randi() the 1st param must be less than the 2nd param"
|
|
277
|
+
);
|
|
256
278
|
return math.floor(instance.rand(min, max + 1));
|
|
257
279
|
},
|
|
258
280
|
/**
|
|
@@ -265,7 +287,7 @@
|
|
|
265
287
|
rseed(value) {
|
|
266
288
|
DEV: assert(
|
|
267
289
|
null == value || isNumber(value) && value >= 0,
|
|
268
|
-
"rseed
|
|
290
|
+
"[litecanvas] rseed() 1st param must be a positive number or zero"
|
|
269
291
|
);
|
|
270
292
|
_rngSeed = ~~value;
|
|
271
293
|
},
|
|
@@ -278,7 +300,7 @@
|
|
|
278
300
|
cls(color) {
|
|
279
301
|
DEV: assert(
|
|
280
302
|
null == color || isNumber(color) && color >= 0,
|
|
281
|
-
"cls
|
|
303
|
+
"[litecanvas] cls() 1st param must be a positive number or zero or undefined"
|
|
282
304
|
);
|
|
283
305
|
if (null == color) {
|
|
284
306
|
_ctx.clearRect(0, 0, _ctx.canvas.width, _ctx.canvas.height);
|
|
@@ -299,20 +321,23 @@
|
|
|
299
321
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
300
322
|
*/
|
|
301
323
|
rect(x, y, width, height, color, radii) {
|
|
302
|
-
DEV: assert(isNumber(x), "rect
|
|
303
|
-
DEV: assert(isNumber(y), "rect
|
|
304
|
-
DEV: assert(
|
|
324
|
+
DEV: assert(isNumber(x), "[litecanvas] rect() 1st param must be a number");
|
|
325
|
+
DEV: assert(isNumber(y), "[litecanvas] rect() 2nd param must be a number");
|
|
326
|
+
DEV: assert(
|
|
327
|
+
isNumber(width) && width > 0,
|
|
328
|
+
"[litecanvas] rect() 3rd param must be a positive number"
|
|
329
|
+
);
|
|
305
330
|
DEV: assert(
|
|
306
331
|
isNumber(height) && height >= 0,
|
|
307
|
-
"rect
|
|
332
|
+
"[litecanvas] rect() 4th param must be a positive number or zero"
|
|
308
333
|
);
|
|
309
334
|
DEV: assert(
|
|
310
335
|
null == color || isNumber(color) && color >= 0,
|
|
311
|
-
"rect
|
|
336
|
+
"[litecanvas] rect() 5th param must be a positive number or zero"
|
|
312
337
|
);
|
|
313
338
|
DEV: assert(
|
|
314
339
|
null == radii || isNumber(radii) || Array.isArray(radii) && radii.length >= 1,
|
|
315
|
-
"rect
|
|
340
|
+
"[litecanvas] rect() 6th param must be a number or array of numbers"
|
|
316
341
|
);
|
|
317
342
|
_ctx.beginPath();
|
|
318
343
|
_ctx[radii ? "roundRect" : "rect"](
|
|
@@ -335,23 +360,23 @@
|
|
|
335
360
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
336
361
|
*/
|
|
337
362
|
rectfill(x, y, width, height, color, radii) {
|
|
338
|
-
DEV: assert(isNumber(x), "rectfill
|
|
339
|
-
DEV: assert(isNumber(y), "rectfill
|
|
363
|
+
DEV: assert(isNumber(x), "[litecanvas] rectfill() 1st param must be a number");
|
|
364
|
+
DEV: assert(isNumber(y), "[litecanvas] rectfill() 2nd param must be a number");
|
|
340
365
|
DEV: assert(
|
|
341
366
|
isNumber(width) && width >= 0,
|
|
342
|
-
"rectfill
|
|
367
|
+
"[litecanvas] rectfill() 3rd param must be a positive number or zero"
|
|
343
368
|
);
|
|
344
369
|
DEV: assert(
|
|
345
370
|
isNumber(height) && height >= 0,
|
|
346
|
-
"rectfill
|
|
371
|
+
"[litecanvas] rectfill() 4th param must be a positive number or zero"
|
|
347
372
|
);
|
|
348
373
|
DEV: assert(
|
|
349
374
|
null == color || isNumber(color) && color >= 0,
|
|
350
|
-
"rectfill
|
|
375
|
+
"[litecanvas] rectfill() 5th param must be a positive number or zero"
|
|
351
376
|
);
|
|
352
377
|
DEV: assert(
|
|
353
378
|
null == radii || isNumber(radii) || Array.isArray(radii) && radii.length >= 1,
|
|
354
|
-
"rectfill
|
|
379
|
+
"[litecanvas] rectfill() 6th param must be a number or array of at least 2 numbers"
|
|
355
380
|
);
|
|
356
381
|
_ctx.beginPath();
|
|
357
382
|
_ctx[radii ? "roundRect" : "rect"](~~x, ~~y, ~~width, ~~height, radii);
|
|
@@ -366,15 +391,15 @@
|
|
|
366
391
|
* @param {number} [color=0] the color index
|
|
367
392
|
*/
|
|
368
393
|
circ(x, y, radius, color) {
|
|
369
|
-
DEV: assert(isNumber(x), "circ
|
|
370
|
-
DEV: assert(isNumber(y), "circ
|
|
394
|
+
DEV: assert(isNumber(x), "[litecanvas] circ() 1st param must be a number");
|
|
395
|
+
DEV: assert(isNumber(y), "[litecanvas] circ() 2nd param must be a number");
|
|
371
396
|
DEV: assert(
|
|
372
397
|
isNumber(radius) && radius >= 0,
|
|
373
|
-
"circ
|
|
398
|
+
"[litecanvas] circ() 3rd param must be a positive number or zero"
|
|
374
399
|
);
|
|
375
400
|
DEV: assert(
|
|
376
401
|
null == color || isNumber(color) && color >= 0,
|
|
377
|
-
"circ
|
|
402
|
+
"[litecanvas] circ() 4th param must be a positive number or zero"
|
|
378
403
|
);
|
|
379
404
|
_ctx.beginPath();
|
|
380
405
|
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
|
|
@@ -389,20 +414,76 @@
|
|
|
389
414
|
* @param {number} [color=0] the color index
|
|
390
415
|
*/
|
|
391
416
|
circfill(x, y, radius, color) {
|
|
392
|
-
DEV: assert(isNumber(x), "circfill
|
|
393
|
-
DEV: assert(isNumber(y), "circfill
|
|
417
|
+
DEV: assert(isNumber(x), "[litecanvas] circfill() 1st param must be a number");
|
|
418
|
+
DEV: assert(isNumber(y), "[litecanvas] circfill() 2nd param must be a number");
|
|
394
419
|
DEV: assert(
|
|
395
420
|
isNumber(radius) && radius >= 0,
|
|
396
|
-
"circfill
|
|
421
|
+
"[litecanvas] circfill() 3rd param must be a positive number or zero"
|
|
397
422
|
);
|
|
398
423
|
DEV: assert(
|
|
399
424
|
null == color || isNumber(color) && color >= 0,
|
|
400
|
-
"circfill
|
|
425
|
+
"[litecanvas] circfill() 4th param must be a positive number or zero"
|
|
401
426
|
);
|
|
402
427
|
_ctx.beginPath();
|
|
403
428
|
_ctx.arc(~~x, ~~y, ~~radius, 0, TWO_PI);
|
|
404
429
|
instance.fill(color);
|
|
405
430
|
},
|
|
431
|
+
/**
|
|
432
|
+
* Draw a ellipse outline
|
|
433
|
+
*
|
|
434
|
+
* @param {number} x
|
|
435
|
+
* @param {number} y
|
|
436
|
+
* @param {number} radiusX
|
|
437
|
+
* @param {number} radiusY
|
|
438
|
+
* @param {number} [color=0] the color index
|
|
439
|
+
*/
|
|
440
|
+
oval(x, y, radiusX, radiusY, color) {
|
|
441
|
+
DEV: assert(isNumber(x), "[litecanvas] oval() 1st param must be a number");
|
|
442
|
+
DEV: assert(isNumber(y), "[litecanvas] oval() 2nd param must be a number");
|
|
443
|
+
DEV: assert(
|
|
444
|
+
isNumber(radiusX) && radiusX >= 0,
|
|
445
|
+
"[litecanvas] oval() 3rd param must be a positive number or zero"
|
|
446
|
+
);
|
|
447
|
+
DEV: assert(
|
|
448
|
+
isNumber(radiusY) && radiusY >= 0,
|
|
449
|
+
"[litecanvas] oval() 4th param must be a positive number or zero"
|
|
450
|
+
);
|
|
451
|
+
DEV: assert(
|
|
452
|
+
null == color || isNumber(color) && color >= 0,
|
|
453
|
+
"[litecanvas] oval() 5th param must be a positive number or zero"
|
|
454
|
+
);
|
|
455
|
+
_ctx.beginPath();
|
|
456
|
+
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
457
|
+
instance.stroke(color);
|
|
458
|
+
},
|
|
459
|
+
/**
|
|
460
|
+
* Draw a color-filled ellipse
|
|
461
|
+
*
|
|
462
|
+
* @param {number} x
|
|
463
|
+
* @param {number} y
|
|
464
|
+
* @param {number} radiusX
|
|
465
|
+
* @param {number} radiusY
|
|
466
|
+
* @param {number} [color=0] the color index
|
|
467
|
+
*/
|
|
468
|
+
ovalfill(x, y, radiusX, radiusY, color) {
|
|
469
|
+
DEV: assert(isNumber(x), "[litecanvas] ovalfill() 1st param must be a number");
|
|
470
|
+
DEV: assert(isNumber(y), "[litecanvas] ovalfill() 2nd param must be a number");
|
|
471
|
+
DEV: assert(
|
|
472
|
+
isNumber(radiusX) && radiusX >= 0,
|
|
473
|
+
"[litecanvas] ovalfill() 3rd param must be a positive number or zero"
|
|
474
|
+
);
|
|
475
|
+
DEV: assert(
|
|
476
|
+
isNumber(radiusY) && radiusY >= 0,
|
|
477
|
+
"[litecanvas] ovalfill() 4th param must be a positive number or zero"
|
|
478
|
+
);
|
|
479
|
+
DEV: assert(
|
|
480
|
+
null == color || isNumber(color) && color >= 0,
|
|
481
|
+
"[litecanvas] ovalfill() 5th param must be a positive number or zero"
|
|
482
|
+
);
|
|
483
|
+
_ctx.beginPath();
|
|
484
|
+
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TWO_PI);
|
|
485
|
+
instance.fill(color);
|
|
486
|
+
},
|
|
406
487
|
/**
|
|
407
488
|
* Draw a line
|
|
408
489
|
*
|
|
@@ -413,13 +494,19 @@
|
|
|
413
494
|
* @param {number} [color=0] the color index
|
|
414
495
|
*/
|
|
415
496
|
line(x1, y1, x2, y2, color) {
|
|
416
|
-
DEV: assert(isNumber(x1), "line
|
|
417
|
-
DEV: assert(isNumber(y1), "line
|
|
418
|
-
DEV: assert(
|
|
419
|
-
|
|
497
|
+
DEV: assert(isNumber(x1), "[litecanvas] line() 1st param must be a number");
|
|
498
|
+
DEV: assert(isNumber(y1), "[litecanvas] line() 2nd param must be a number");
|
|
499
|
+
DEV: assert(
|
|
500
|
+
isNumber(x2),
|
|
501
|
+
"[litecanvas] line() 3rd param must be a positive number or zero"
|
|
502
|
+
);
|
|
503
|
+
DEV: assert(
|
|
504
|
+
isNumber(y2),
|
|
505
|
+
"[litecanvas] line() 4th param must be a positive number or zero"
|
|
506
|
+
);
|
|
420
507
|
DEV: assert(
|
|
421
508
|
null == color || isNumber(color) && color >= 0,
|
|
422
|
-
"line
|
|
509
|
+
"[litecanvas] line() 5th param must be a positive number or zero"
|
|
423
510
|
);
|
|
424
511
|
_ctx.beginPath();
|
|
425
512
|
let xfix = _outline_fix !== 0 && ~~x1 === ~~x2 ? 0.5 : 0;
|
|
@@ -437,7 +524,7 @@
|
|
|
437
524
|
linewidth(value) {
|
|
438
525
|
DEV: assert(
|
|
439
526
|
isNumber(value) && ~~value > 0,
|
|
440
|
-
"linewidth
|
|
527
|
+
"[litecanvas] linewidth() 1st param must be a positive number"
|
|
441
528
|
);
|
|
442
529
|
_ctx.lineWidth = ~~value;
|
|
443
530
|
_outline_fix = 0 === ~~value % 2 ? 0 : 0.5;
|
|
@@ -453,9 +540,9 @@
|
|
|
453
540
|
linedash(segments, offset = 0) {
|
|
454
541
|
DEV: assert(
|
|
455
542
|
Array.isArray(segments) && segments.length > 0,
|
|
456
|
-
"linedash
|
|
543
|
+
"[litecanvas] linedash() 1st param must be an array of numbers"
|
|
457
544
|
);
|
|
458
|
-
DEV: assert(isNumber(offset), "linedash
|
|
545
|
+
DEV: assert(isNumber(offset), "[litecanvas] linedash() 2nd param must be a number");
|
|
459
546
|
_ctx.setLineDash(segments);
|
|
460
547
|
_ctx.lineDashOffset = offset;
|
|
461
548
|
},
|
|
@@ -470,13 +557,16 @@
|
|
|
470
557
|
* @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
|
|
471
558
|
*/
|
|
472
559
|
text(x, y, message, color = 3, fontStyle = "normal") {
|
|
473
|
-
DEV: assert(isNumber(x), "text
|
|
474
|
-
DEV: assert(isNumber(y), "text
|
|
560
|
+
DEV: assert(isNumber(x), "[litecanvas] text() 1st param must be a number");
|
|
561
|
+
DEV: assert(isNumber(y), "[litecanvas] text() 2nd param must be a number");
|
|
475
562
|
DEV: assert(
|
|
476
563
|
null == color || isNumber(color) && color >= 0,
|
|
477
|
-
"text
|
|
564
|
+
"[litecanvas] text() 4th param must be a positive number or zero"
|
|
565
|
+
);
|
|
566
|
+
DEV: assert(
|
|
567
|
+
"string" === typeof fontStyle,
|
|
568
|
+
"[litecanvas] text() 5th param must be a string"
|
|
478
569
|
);
|
|
479
|
-
DEV: assert("string" === typeof fontStyle, "text: 5th param must be a string");
|
|
480
570
|
_ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`;
|
|
481
571
|
_ctx.fillStyle = _colors[~~color % _colors.length];
|
|
482
572
|
_ctx.fillText(message, ~~x, ~~y);
|
|
@@ -487,7 +577,10 @@
|
|
|
487
577
|
* @param {string} family
|
|
488
578
|
*/
|
|
489
579
|
textfont(family) {
|
|
490
|
-
DEV: assert(
|
|
580
|
+
DEV: assert(
|
|
581
|
+
"string" === typeof family,
|
|
582
|
+
"[litecanvas] textfont() 1st param must be a string"
|
|
583
|
+
);
|
|
491
584
|
_fontFamily = family;
|
|
492
585
|
},
|
|
493
586
|
/**
|
|
@@ -496,7 +589,7 @@
|
|
|
496
589
|
* @param {number} size
|
|
497
590
|
*/
|
|
498
591
|
textsize(size) {
|
|
499
|
-
DEV: assert(isNumber(size), "textsize
|
|
592
|
+
DEV: assert(isNumber(size), "[litecanvas] textsize() 1st param must be a number");
|
|
500
593
|
_fontSize = size;
|
|
501
594
|
},
|
|
502
595
|
/**
|
|
@@ -510,13 +603,13 @@
|
|
|
510
603
|
textalign(align, baseline) {
|
|
511
604
|
DEV: assert(
|
|
512
605
|
null == align || ["left", "right", "center", "start", "end"].includes(align),
|
|
513
|
-
"textalign
|
|
606
|
+
"[litecanvas] textalign() 1st param must be null or one of the following strings: center, left, right, start or end."
|
|
514
607
|
);
|
|
515
608
|
DEV: assert(
|
|
516
609
|
null == baseline || ["top", "bottom", "middle", "hanging", "alphabetic", "ideographic"].includes(
|
|
517
610
|
baseline
|
|
518
611
|
),
|
|
519
|
-
"textalign
|
|
612
|
+
"[litecanvas] textalign() 2nd param must be null or one of the following strings: middle, top, bottom, hanging, alphabetic or ideographic."
|
|
520
613
|
);
|
|
521
614
|
if (align) _ctx.textAlign = align;
|
|
522
615
|
if (baseline) _ctx.textBaseline = baseline;
|
|
@@ -530,8 +623,8 @@
|
|
|
530
623
|
* @param {OffscreenCanvas|HTMLImageElement|HTMLCanvasElement} source
|
|
531
624
|
*/
|
|
532
625
|
image(x, y, source) {
|
|
533
|
-
DEV: assert(isNumber(x), "image
|
|
534
|
-
DEV: assert(isNumber(y), "image
|
|
626
|
+
DEV: assert(isNumber(x), "[litecanvas] image() 1st param must be a number");
|
|
627
|
+
DEV: assert(isNumber(y), "[litecanvas] image() 2nd param must be a number");
|
|
535
628
|
_ctx.drawImage(source, ~~x, ~~y);
|
|
536
629
|
},
|
|
537
630
|
/**
|
|
@@ -547,22 +640,25 @@
|
|
|
547
640
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
548
641
|
*/
|
|
549
642
|
paint(width, height, drawing, options = {}) {
|
|
550
|
-
DEV: assert(
|
|
643
|
+
DEV: assert(
|
|
644
|
+
isNumber(width) && width >= 1,
|
|
645
|
+
"[litecanvas] paint() 1st param must be a positive number"
|
|
646
|
+
);
|
|
551
647
|
DEV: assert(
|
|
552
648
|
isNumber(height) && height >= 1,
|
|
553
|
-
"paint
|
|
649
|
+
"[litecanvas] paint() 2nd param must be a positive number"
|
|
554
650
|
);
|
|
555
651
|
DEV: assert(
|
|
556
652
|
"function" === typeof drawing || Array.isArray(drawing),
|
|
557
|
-
"paint
|
|
653
|
+
"[litecanvas] paint() 3rd param must be a function or array"
|
|
558
654
|
);
|
|
559
655
|
DEV: assert(
|
|
560
656
|
options && null == options.scale || isNumber(options.scale),
|
|
561
|
-
"paint
|
|
657
|
+
"[litecanvas] paint() 4th param (options.scale) must be a number"
|
|
562
658
|
);
|
|
563
659
|
DEV: assert(
|
|
564
660
|
options && null == options.canvas || options.canvas instanceof OffscreenCanvas,
|
|
565
|
-
"paint
|
|
661
|
+
"[litecanvas] paint() 4th param (options.canvas) must be an OffscreenCanvas"
|
|
566
662
|
);
|
|
567
663
|
const canvas = options.canvas || new OffscreenCanvas(1, 1), scale = options.scale || 1, contextOriginal = _ctx;
|
|
568
664
|
canvas.width = width * scale;
|
|
@@ -621,8 +717,8 @@
|
|
|
621
717
|
* @param {number} y
|
|
622
718
|
*/
|
|
623
719
|
translate: (x, y) => {
|
|
624
|
-
DEV: assert(isNumber(x), "translate
|
|
625
|
-
DEV: assert(isNumber(y), "translate
|
|
720
|
+
DEV: assert(isNumber(x), "[litecanvas] translate() 1st param must be a number");
|
|
721
|
+
DEV: assert(isNumber(y), "[litecanvas] translate() 2nd param must be a number");
|
|
626
722
|
return _ctx.translate(~~x, ~~y);
|
|
627
723
|
},
|
|
628
724
|
/**
|
|
@@ -632,8 +728,8 @@
|
|
|
632
728
|
* @param {number} [y]
|
|
633
729
|
*/
|
|
634
730
|
scale: (x, y) => {
|
|
635
|
-
DEV: assert(isNumber(x), "scale
|
|
636
|
-
DEV: assert(null == y || isNumber(y), "scale
|
|
731
|
+
DEV: assert(isNumber(x), "[litecanvas] scale() 1st param must be a number");
|
|
732
|
+
DEV: assert(null == y || isNumber(y), "[litecanvas] scale() 2nd param must be a number");
|
|
637
733
|
return _ctx.scale(x, y || x);
|
|
638
734
|
},
|
|
639
735
|
/**
|
|
@@ -642,7 +738,7 @@
|
|
|
642
738
|
* @param {number} radians
|
|
643
739
|
*/
|
|
644
740
|
rotate: (radians) => {
|
|
645
|
-
DEV: assert(isNumber(radians), "rotate
|
|
741
|
+
DEV: assert(isNumber(radians), "[litecanvas] rotate() 1st param must be a number");
|
|
646
742
|
return _ctx.rotate(radians);
|
|
647
743
|
},
|
|
648
744
|
/**
|
|
@@ -652,7 +748,7 @@
|
|
|
652
748
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/globalAlpha
|
|
653
749
|
*/
|
|
654
750
|
alpha(value) {
|
|
655
|
-
DEV: assert(isNumber(value), "alpha
|
|
751
|
+
DEV: assert(isNumber(value), "[litecanvas] alpha() 1st param must be a number");
|
|
656
752
|
_ctx.globalAlpha = instance.clamp(value, 0, 1);
|
|
657
753
|
},
|
|
658
754
|
/**
|
|
@@ -667,7 +763,7 @@
|
|
|
667
763
|
path: (arg) => {
|
|
668
764
|
DEV: assert(
|
|
669
765
|
null == arg || "string" === typeof arg || arg instanceof Path2D,
|
|
670
|
-
"path
|
|
766
|
+
"[litecanvas] path() 1st param must be a string or a Path2D instance"
|
|
671
767
|
);
|
|
672
768
|
return new Path2D(arg);
|
|
673
769
|
},
|
|
@@ -680,11 +776,11 @@
|
|
|
680
776
|
fill(color, path) {
|
|
681
777
|
DEV: assert(
|
|
682
778
|
null == color || isNumber(color) && color >= 0,
|
|
683
|
-
"fill
|
|
779
|
+
"[litecanvas] fill() 1st param must be a positive number or zero"
|
|
684
780
|
);
|
|
685
781
|
DEV: assert(
|
|
686
782
|
null == path || path instanceof Path2D,
|
|
687
|
-
"fill
|
|
783
|
+
"[litecanvas] fill() 2nd param must be a Path2D instance"
|
|
688
784
|
);
|
|
689
785
|
_ctx.fillStyle = _colors[~~color % _colors.length];
|
|
690
786
|
if (path) {
|
|
@@ -702,11 +798,11 @@
|
|
|
702
798
|
stroke(color, path) {
|
|
703
799
|
DEV: assert(
|
|
704
800
|
null == color || isNumber(color) && color >= 0,
|
|
705
|
-
"stroke
|
|
801
|
+
"[litecanvas] stroke() 1st param must be a positive number or zero"
|
|
706
802
|
);
|
|
707
803
|
DEV: assert(
|
|
708
804
|
null == path || path instanceof Path2D,
|
|
709
|
-
"stroke
|
|
805
|
+
"[litecanvas] stroke() 2nd param must be a Path2D instance"
|
|
710
806
|
);
|
|
711
807
|
_ctx.strokeStyle = _colors[~~color % _colors.length];
|
|
712
808
|
if (path) {
|
|
@@ -724,7 +820,10 @@
|
|
|
724
820
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/clip
|
|
725
821
|
*/
|
|
726
822
|
clip(path) {
|
|
727
|
-
DEV: assert(
|
|
823
|
+
DEV: assert(
|
|
824
|
+
path instanceof Path2D,
|
|
825
|
+
"[litecanvas] clip() 1st param must be a Path2D instance"
|
|
826
|
+
);
|
|
728
827
|
_ctx.clip(path);
|
|
729
828
|
},
|
|
730
829
|
/** SOUND API */
|
|
@@ -742,10 +841,10 @@
|
|
|
742
841
|
sfx(zzfxParams, pitchSlide = 0, volumeFactor = 1) {
|
|
743
842
|
DEV: assert(
|
|
744
843
|
null == zzfxParams || Array.isArray(zzfxParams),
|
|
745
|
-
"sfx
|
|
844
|
+
"[litecanvas] sfx() 1st param must be an array"
|
|
746
845
|
);
|
|
747
|
-
DEV: assert(isNumber(pitchSlide), "sfx
|
|
748
|
-
DEV: assert(isNumber(volumeFactor), "sfx
|
|
846
|
+
DEV: assert(isNumber(pitchSlide), "[litecanvas] sfx() 2nd param must be a number");
|
|
847
|
+
DEV: assert(isNumber(volumeFactor), "[litecanvas] sfx() 3rd param must be a number");
|
|
749
848
|
if (
|
|
750
849
|
// @ts-ignore
|
|
751
850
|
root.zzfxV <= 0 || navigator.userActivation && !navigator.userActivation.hasBeenActive
|
|
@@ -768,7 +867,7 @@
|
|
|
768
867
|
* @param {number} value
|
|
769
868
|
*/
|
|
770
869
|
volume(value) {
|
|
771
|
-
DEV: assert(isNumber(value), "volume
|
|
870
|
+
DEV: assert(isNumber(value), "[litecanvas] volume() 1st param must be a number");
|
|
772
871
|
root.zzfxV = value;
|
|
773
872
|
},
|
|
774
873
|
/** PLUGINS API */
|
|
@@ -784,8 +883,14 @@
|
|
|
784
883
|
* @param {pluginCallback} callback
|
|
785
884
|
*/
|
|
786
885
|
use(callback, config = {}) {
|
|
787
|
-
DEV: assert(
|
|
788
|
-
|
|
886
|
+
DEV: assert(
|
|
887
|
+
"function" === typeof callback,
|
|
888
|
+
"[litecanvas] use() 1st param must be a function"
|
|
889
|
+
);
|
|
890
|
+
DEV: assert(
|
|
891
|
+
"object" === typeof config,
|
|
892
|
+
"[litecanvas] use() 2nd param must be an object"
|
|
893
|
+
);
|
|
789
894
|
if (_initialized) {
|
|
790
895
|
loadPlugin(callback, config);
|
|
791
896
|
} else {
|
|
@@ -800,8 +905,14 @@
|
|
|
800
905
|
* @returns {Function} a function to remove the listener
|
|
801
906
|
*/
|
|
802
907
|
listen(eventName, callback) {
|
|
803
|
-
DEV: assert(
|
|
804
|
-
|
|
908
|
+
DEV: assert(
|
|
909
|
+
"string" === typeof eventName,
|
|
910
|
+
"[litecanvas] listen() 1st param must be a string"
|
|
911
|
+
);
|
|
912
|
+
DEV: assert(
|
|
913
|
+
"function" === typeof callback,
|
|
914
|
+
"[litecanvas] listen() 2nd param must be a function"
|
|
915
|
+
);
|
|
805
916
|
eventName = eventName.toLowerCase();
|
|
806
917
|
_eventListeners[eventName] = _eventListeners[eventName] || /* @__PURE__ */ new Set();
|
|
807
918
|
_eventListeners[eventName].add(callback);
|
|
@@ -817,7 +928,10 @@
|
|
|
817
928
|
* @param {*} [arg4] any data to be passed over the listeners
|
|
818
929
|
*/
|
|
819
930
|
emit(eventName, arg1, arg2, arg3, arg4) {
|
|
820
|
-
DEV: assert(
|
|
931
|
+
DEV: assert(
|
|
932
|
+
"string" === typeof eventName,
|
|
933
|
+
"[litecanvas] emit() 1st param must be a string"
|
|
934
|
+
);
|
|
821
935
|
if (_initialized) {
|
|
822
936
|
eventName = eventName.toLowerCase();
|
|
823
937
|
triggerEvent("before:" + eventName, arg1, arg2, arg3, arg4);
|
|
@@ -833,7 +947,7 @@
|
|
|
833
947
|
pal(colors = defaultPalette) {
|
|
834
948
|
DEV: assert(
|
|
835
949
|
Array.isArray(colors) && colors.length > 0,
|
|
836
|
-
"pal
|
|
950
|
+
"[litecanvas] pal() 1st param must be a array of strings"
|
|
837
951
|
);
|
|
838
952
|
_colors = colors;
|
|
839
953
|
},
|
|
@@ -844,7 +958,7 @@
|
|
|
844
958
|
* @param {*} value
|
|
845
959
|
*/
|
|
846
960
|
def(key, value) {
|
|
847
|
-
DEV: assert("string" === typeof key, "def
|
|
961
|
+
DEV: assert("string" === typeof key, "[litecanvas] def() 1st param must be a string");
|
|
848
962
|
DEV: if (null == value) {
|
|
849
963
|
console.warn(`def: key "${key}" was defined as ${value} but now is null`);
|
|
850
964
|
}
|
|
@@ -863,7 +977,7 @@
|
|
|
863
977
|
timescale(value) {
|
|
864
978
|
DEV: assert(
|
|
865
979
|
isNumber(value) && value >= 0,
|
|
866
|
-
"timescale
|
|
980
|
+
"[litecanvas] timescale() 1st param must be a positive number or zero"
|
|
867
981
|
);
|
|
868
982
|
_timeScale = value;
|
|
869
983
|
},
|
|
@@ -875,7 +989,7 @@
|
|
|
875
989
|
framerate(value) {
|
|
876
990
|
DEV: assert(
|
|
877
991
|
isNumber(value) && value >= 1,
|
|
878
|
-
"framerate
|
|
992
|
+
"[litecanvas] framerate() 1st param must be a positive number"
|
|
879
993
|
);
|
|
880
994
|
_deltaTime = 1 / ~~value;
|
|
881
995
|
},
|
|
@@ -886,7 +1000,7 @@
|
|
|
886
1000
|
* @returns {any}
|
|
887
1001
|
*/
|
|
888
1002
|
stat(n) {
|
|
889
|
-
DEV: assert(isNumber(n) && n >= 0, "stat
|
|
1003
|
+
DEV: assert(isNumber(n) && n >= 0, "[litecanvas] stat() 1st param must be a number");
|
|
890
1004
|
const list = [
|
|
891
1005
|
// 0
|
|
892
1006
|
settings,
|
|
@@ -944,6 +1058,9 @@
|
|
|
944
1058
|
function init() {
|
|
945
1059
|
const source = settings.loop ? settings.loop : root;
|
|
946
1060
|
for (const event of _coreEvents.split(",")) {
|
|
1061
|
+
DEV: if (root === source && source[event]) {
|
|
1062
|
+
console.info(`[litecanvas] using window.${event}()`);
|
|
1063
|
+
}
|
|
947
1064
|
if (source[event]) instance.listen(event, source[event]);
|
|
948
1065
|
}
|
|
949
1066
|
for (const [callback, config] of _plugins) {
|
|
@@ -1140,7 +1257,7 @@
|
|
|
1140
1257
|
(key) => {
|
|
1141
1258
|
DEV: assert(
|
|
1142
1259
|
null == key || "string" === typeof key,
|
|
1143
|
-
"iskeydown
|
|
1260
|
+
"[litecanvas] iskeydown() 1st param must be a string or undefined"
|
|
1144
1261
|
);
|
|
1145
1262
|
return keyCheck(_keysDown, key);
|
|
1146
1263
|
}
|
|
@@ -1157,7 +1274,7 @@
|
|
|
1157
1274
|
(key) => {
|
|
1158
1275
|
DEV: assert(
|
|
1159
1276
|
null == key || "string" === typeof key,
|
|
1160
|
-
"iskeypressed
|
|
1277
|
+
"[litecanvas] iskeypressed() 1st param must be a string or undefined"
|
|
1161
1278
|
);
|
|
1162
1279
|
return keyCheck(_keysPress, key);
|
|
1163
1280
|
}
|
|
@@ -1193,14 +1310,17 @@
|
|
|
1193
1310
|
function setupCanvas() {
|
|
1194
1311
|
if ("string" === typeof settings.canvas) {
|
|
1195
1312
|
_canvas = document.querySelector(settings.canvas);
|
|
1196
|
-
DEV: assert(
|
|
1313
|
+
DEV: assert(
|
|
1314
|
+
null != _canvas,
|
|
1315
|
+
'[litecanvas] litecanvas() option "canvas" is an invalid CSS selector'
|
|
1316
|
+
);
|
|
1197
1317
|
} else {
|
|
1198
1318
|
_canvas = settings.canvas;
|
|
1199
1319
|
}
|
|
1200
1320
|
_canvas = _canvas || document.createElement("canvas");
|
|
1201
1321
|
DEV: assert(
|
|
1202
1322
|
"CANVAS" === _canvas.tagName,
|
|
1203
|
-
|
|
1323
|
+
'[litecanvas] litecanvas() option "canvas" should be a canvas element or string (CSS selector)'
|
|
1204
1324
|
);
|
|
1205
1325
|
_ctx = _canvas.getContext("2d");
|
|
1206
1326
|
on(_canvas, "click", () => root.focus());
|
|
@@ -1213,28 +1333,30 @@
|
|
|
1213
1333
|
function resizeCanvas() {
|
|
1214
1334
|
DEV: assert(
|
|
1215
1335
|
null == settings.width || isNumber(settings.width) && settings.width > 0,
|
|
1216
|
-
|
|
1336
|
+
'[litecanvas] litecanvas() option "width" should be a positive number when defined'
|
|
1217
1337
|
);
|
|
1218
1338
|
DEV: assert(
|
|
1219
1339
|
null == settings.height || isNumber(settings.height) && settings.height > 0,
|
|
1220
|
-
|
|
1340
|
+
'[litecanvas] litecanvas() option "height" should be a positive number when defined'
|
|
1221
1341
|
);
|
|
1222
1342
|
DEV: assert(
|
|
1223
1343
|
null == settings.height || settings.width > 0 && settings.height > 0,
|
|
1224
|
-
|
|
1344
|
+
'[litecanvas] litecanvas() option "width" is required when the option "height" is defined'
|
|
1225
1345
|
);
|
|
1226
1346
|
const width = settings.width || root.innerWidth, height = settings.height || settings.width || root.innerHeight;
|
|
1227
|
-
instance.def("W",
|
|
1228
|
-
instance.def("H",
|
|
1347
|
+
instance.def("W", width);
|
|
1348
|
+
instance.def("H", height);
|
|
1349
|
+
_canvas.width = width;
|
|
1350
|
+
_canvas.height = height;
|
|
1229
1351
|
if (settings.autoscale) {
|
|
1230
1352
|
if (!_canvas.style.display) {
|
|
1231
1353
|
_canvas.style.display = "block";
|
|
1232
1354
|
_canvas.style.margin = "auto";
|
|
1233
1355
|
}
|
|
1234
|
-
_scale = math.min(root.innerWidth /
|
|
1356
|
+
_scale = math.min(root.innerWidth / width, root.innerHeight / height);
|
|
1235
1357
|
_scale = (settings.pixelart ? ~~_scale : _scale) || 1;
|
|
1236
|
-
_canvas.style.width =
|
|
1237
|
-
_canvas.style.height =
|
|
1358
|
+
_canvas.style.width = width * _scale + "px";
|
|
1359
|
+
_canvas.style.height = height * _scale + "px";
|
|
1238
1360
|
}
|
|
1239
1361
|
if (!settings.antialias || settings.pixelart) {
|
|
1240
1362
|
_ctx.imageSmoothingEnabled = false;
|
|
@@ -1256,7 +1378,7 @@
|
|
|
1256
1378
|
const pluginData = callback(instance, config);
|
|
1257
1379
|
DEV: assert(
|
|
1258
1380
|
null == pluginData || "object" === typeof pluginData,
|
|
1259
|
-
"
|
|
1381
|
+
"[litecanvas] litecanvas() plugins should return an object or nothing"
|
|
1260
1382
|
);
|
|
1261
1383
|
for (const key in pluginData) {
|
|
1262
1384
|
instance.def(key, pluginData[key]);
|
|
@@ -1264,11 +1386,13 @@
|
|
|
1264
1386
|
}
|
|
1265
1387
|
if (settings.global) {
|
|
1266
1388
|
if (root.ENGINE) {
|
|
1267
|
-
throw new Error("
|
|
1389
|
+
throw new Error("only one global litecanvas is allowed");
|
|
1268
1390
|
}
|
|
1269
1391
|
Object.assign(root, instance);
|
|
1270
1392
|
root.ENGINE = instance;
|
|
1271
1393
|
}
|
|
1394
|
+
DEV: console.info(`[litecanvas] version ${version} started`);
|
|
1395
|
+
DEV: console.debug(`[litecanvas] litecanvas() options =`, settings);
|
|
1272
1396
|
setupCanvas();
|
|
1273
1397
|
if ("loading" === document.readyState) {
|
|
1274
1398
|
on(root, "DOMContentLoaded", () => raf(init));
|