litecanvas 0.303.0 → 0.401.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 +19 -5
- package/dist/dist.dev.js +87 -83
- package/dist/dist.js +42 -60
- package/dist/dist.min.js +1 -1
- package/package.json +1 -1
- package/src/index.js +79 -137
- package/src/version.js +1 -1
- package/types/global.d.ts +30 -12
- package/types/types.d.ts +11 -11
package/README.md
CHANGED
|
@@ -100,15 +100,28 @@ Litecanvas has a default palette with 4 colors:
|
|
|
100
100
|
|
|
101
101
|
<img src=".github/_assets/palette.png" src="The litecanvas color palette" style="2px solid #fff">
|
|
102
102
|
|
|
103
|
-
Each time a Litecanvas' function ask for a color, you
|
|
103
|
+
Each time a Litecanvas' function ask for a color, you can use an of theses colors by its index (from 0 to 3, by default). But, you can customize the color palette using the `pal()` function. [Example](samples/custom-palette/custom-palette.js)
|
|
104
104
|
|
|
105
105
|
```js
|
|
106
|
-
|
|
106
|
+
litecanvas()
|
|
107
|
+
|
|
108
|
+
// example: draw a white (color #3) rectangle
|
|
107
109
|
let color = 3
|
|
108
110
|
rectfill(0, 0, 32, 32, color)
|
|
109
111
|
```
|
|
110
112
|
|
|
111
|
-
|
|
113
|
+
However, you can also use any other color:
|
|
114
|
+
|
|
115
|
+
```js
|
|
116
|
+
litecanvas()
|
|
117
|
+
|
|
118
|
+
// example: draw three "orange" rectangles
|
|
119
|
+
rectfill(0, 0, 32, 32, 'orange')
|
|
120
|
+
rectfill(32, 32, 32, 32, '#ffa500')
|
|
121
|
+
rectfill(64, 64, 32, 32, 'rgb(255, 165, 0)')
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
[Live Demo](https://litecanvas.js.org?c=eJxNytEKgzAMBdD3fkVwD22hYKerD%2Fub6FIVMjdimX6%2BFZkb5IYL9%2FCYqMPpg7OxSpUl0IrPN9MdHoILpEGIoHgJTj0VINSl3Jhmtdc4MhvvIF9dHdEH1fYHvtNJLjFi8P7fNDcHe04jfWuqEBxcm%2Fy81XYDYQwvNQ%3D%3D)
|
|
112
125
|
|
|
113
126
|
### Printing messages
|
|
114
127
|
|
|
@@ -120,9 +133,10 @@ function draw() {
|
|
|
120
133
|
cls(0)
|
|
121
134
|
|
|
122
135
|
// print a gray (color #1) text at x=0, y=0
|
|
123
|
-
|
|
136
|
+
const color = 1
|
|
137
|
+
text(0, 0, 'Hello!!!', color)
|
|
124
138
|
|
|
125
|
-
// use \n to break text lines
|
|
139
|
+
// use \n to break text lines (by default uses the color #3)
|
|
126
140
|
text(0, 30, 'multi\nline\ntext')
|
|
127
141
|
}
|
|
128
142
|
```
|
package/dist/dist.dev.js
CHANGED
|
@@ -115,7 +115,7 @@
|
|
|
115
115
|
var assert = (condition, message = "Assertion failed") => {
|
|
116
116
|
if (!condition) throw new Error("[Litecanvas] " + message);
|
|
117
117
|
};
|
|
118
|
-
var version = "0.
|
|
118
|
+
var version = "0.401.0";
|
|
119
119
|
function litecanvas(settings = {}) {
|
|
120
120
|
const root = window,
|
|
121
121
|
math = Math,
|
|
@@ -296,8 +296,10 @@
|
|
|
296
296
|
},
|
|
297
297
|
cls(color) {
|
|
298
298
|
DEV: assert(
|
|
299
|
-
null == color ||
|
|
300
|
-
|
|
299
|
+
null == color ||
|
|
300
|
+
(isNumber(color) && color >= 0) ||
|
|
301
|
+
"string" === typeof color,
|
|
302
|
+
"cls() 1st argument must be a non-negative number or string",
|
|
301
303
|
);
|
|
302
304
|
if (null == color) {
|
|
303
305
|
_ctx.clearRect(0, 0, instance.W, instance.H);
|
|
@@ -317,8 +319,10 @@
|
|
|
317
319
|
"rect() 4th argument must be a non-negative number",
|
|
318
320
|
);
|
|
319
321
|
DEV: assert(
|
|
320
|
-
null == color ||
|
|
321
|
-
|
|
322
|
+
null == color ||
|
|
323
|
+
(isNumber(color) && color >= 0) ||
|
|
324
|
+
"string" === typeof color,
|
|
325
|
+
"rect() 5th argument must be a non-negative number or string",
|
|
322
326
|
);
|
|
323
327
|
DEV: assert(
|
|
324
328
|
null == radii ||
|
|
@@ -348,8 +352,10 @@
|
|
|
348
352
|
"rectfill() 4th argument must be a non-negative number",
|
|
349
353
|
);
|
|
350
354
|
DEV: assert(
|
|
351
|
-
null == color ||
|
|
352
|
-
|
|
355
|
+
null == color ||
|
|
356
|
+
(isNumber(color) && color >= 0) ||
|
|
357
|
+
"string" === typeof color,
|
|
358
|
+
"rectfill() 5th argument must be a non-negative number or string",
|
|
353
359
|
);
|
|
354
360
|
DEV: assert(
|
|
355
361
|
null == radii ||
|
|
@@ -373,8 +379,10 @@
|
|
|
373
379
|
"oval() 4th argument must be a non-negative number",
|
|
374
380
|
);
|
|
375
381
|
DEV: assert(
|
|
376
|
-
null == color ||
|
|
377
|
-
|
|
382
|
+
null == color ||
|
|
383
|
+
(isNumber(color) && color >= 0) ||
|
|
384
|
+
"string" === typeof color,
|
|
385
|
+
"oval() 5th argument must be a non-negative number or string",
|
|
378
386
|
);
|
|
379
387
|
beginPath(_ctx);
|
|
380
388
|
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TAU);
|
|
@@ -392,8 +400,10 @@
|
|
|
392
400
|
"ovalfill() 4th argument must be a non-negative number",
|
|
393
401
|
);
|
|
394
402
|
DEV: assert(
|
|
395
|
-
null == color ||
|
|
396
|
-
|
|
403
|
+
null == color ||
|
|
404
|
+
(isNumber(color) && color >= 0) ||
|
|
405
|
+
"string" === typeof color,
|
|
406
|
+
"ovalfill() 5th argument must be a non-negative number or string",
|
|
397
407
|
);
|
|
398
408
|
beginPath(_ctx);
|
|
399
409
|
_ctx.ellipse(~~x, ~~y, ~~radiusX, ~~radiusY, 0, 0, TAU);
|
|
@@ -407,8 +417,10 @@
|
|
|
407
417
|
"circ() 3rd argument must be a non-negative number",
|
|
408
418
|
);
|
|
409
419
|
DEV: assert(
|
|
410
|
-
null == color ||
|
|
411
|
-
|
|
420
|
+
null == color ||
|
|
421
|
+
(isNumber(color) && color >= 0) ||
|
|
422
|
+
"string" === typeof color,
|
|
423
|
+
"circ() 4th argument must be a non-negative number or string",
|
|
412
424
|
);
|
|
413
425
|
instance.oval(x, y, radius, radius, color);
|
|
414
426
|
},
|
|
@@ -420,8 +432,10 @@
|
|
|
420
432
|
"circfill() 3rd argument must be a non-negative number",
|
|
421
433
|
);
|
|
422
434
|
DEV: assert(
|
|
423
|
-
null == color ||
|
|
424
|
-
|
|
435
|
+
null == color ||
|
|
436
|
+
(isNumber(color) && color >= 0) ||
|
|
437
|
+
"string" === typeof color,
|
|
438
|
+
"circfill() 4th argument must be a non-negative number or string",
|
|
425
439
|
);
|
|
426
440
|
instance.ovalfill(x, y, radius, radius, color);
|
|
427
441
|
},
|
|
@@ -456,8 +470,10 @@
|
|
|
456
470
|
"line() 4th argument must be a non-negative number",
|
|
457
471
|
);
|
|
458
472
|
DEV: assert(
|
|
459
|
-
null == color ||
|
|
460
|
-
|
|
473
|
+
null == color ||
|
|
474
|
+
(isNumber(color) && color >= 0) ||
|
|
475
|
+
"string" === typeof color,
|
|
476
|
+
"line() 5th argument must be a non-negative number or string",
|
|
461
477
|
);
|
|
462
478
|
beginPath(_ctx);
|
|
463
479
|
let xfix = _outline_fix && ~~x1 === ~~x2 ? 0.5 : 0;
|
|
@@ -490,8 +506,10 @@
|
|
|
490
506
|
DEV: assert(isNumber(x), "text() 1st argument must be a number");
|
|
491
507
|
DEV: assert(isNumber(y), "text() 2nd argument must be a number");
|
|
492
508
|
DEV: assert(
|
|
493
|
-
null == color ||
|
|
494
|
-
|
|
509
|
+
null == color ||
|
|
510
|
+
(isNumber(color) && color >= 0) ||
|
|
511
|
+
"string" === typeof color,
|
|
512
|
+
"text() 4th argument must be a non-negative number or string",
|
|
495
513
|
);
|
|
496
514
|
DEV: assert(
|
|
497
515
|
"string" === typeof fontStyle,
|
|
@@ -669,16 +687,20 @@
|
|
|
669
687
|
},
|
|
670
688
|
fill(color) {
|
|
671
689
|
DEV: assert(
|
|
672
|
-
null == color ||
|
|
673
|
-
|
|
690
|
+
null == color ||
|
|
691
|
+
(isNumber(color) && color >= 0) ||
|
|
692
|
+
"string" === typeof color,
|
|
693
|
+
"fill() 1st argument must be a non-negative number or string",
|
|
674
694
|
);
|
|
675
695
|
_ctx.fillStyle = getColor(color);
|
|
676
696
|
_ctx.fill();
|
|
677
697
|
},
|
|
678
698
|
stroke(color) {
|
|
679
699
|
DEV: assert(
|
|
680
|
-
null == color ||
|
|
681
|
-
|
|
700
|
+
null == color ||
|
|
701
|
+
(isNumber(color) && color >= 0) ||
|
|
702
|
+
"string" === typeof color,
|
|
703
|
+
"stroke() 1st argument must be a non-negative number or string",
|
|
682
704
|
);
|
|
683
705
|
_ctx.strokeStyle = getColor(color);
|
|
684
706
|
_ctx.stroke();
|
|
@@ -968,77 +990,56 @@
|
|
|
968
990
|
},
|
|
969
991
|
_checkTapped = (tap) => tap && perf.now() - tap.t <= 300;
|
|
970
992
|
let _pressingMouse = false;
|
|
971
|
-
on(_canvas, "
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
instance.emit("tap", x, y, 0);
|
|
976
|
-
_registerTap(0, x, y);
|
|
993
|
+
on(_canvas, "pointerdown", (ev) => {
|
|
994
|
+
const [x, y] = _getXY(ev);
|
|
995
|
+
if ("mouse" === ev.pointerType) {
|
|
996
|
+
if (ev.button) return;
|
|
977
997
|
_pressingMouse = true;
|
|
978
998
|
}
|
|
999
|
+
const id = ev.pointerId;
|
|
1000
|
+
_registerTap(id, x, y);
|
|
1001
|
+
instance.emit("tap", x, y, id);
|
|
1002
|
+
_canvas.setPointerCapture(id);
|
|
979
1003
|
});
|
|
980
|
-
on(_canvas, "
|
|
981
|
-
if (!ev.button) {
|
|
982
|
-
preventDefault(ev);
|
|
983
|
-
const tap = _taps.get(0);
|
|
984
|
-
const [x, y] = _getXY(ev);
|
|
985
|
-
if (_checkTapped(tap)) {
|
|
986
|
-
instance.emit("tapped", tap.xi, tap.yi, 0);
|
|
987
|
-
}
|
|
988
|
-
instance.emit("untap", x, y, 0);
|
|
989
|
-
_taps.delete(0);
|
|
990
|
-
_pressingMouse = false;
|
|
991
|
-
}
|
|
992
|
-
});
|
|
993
|
-
on(root, "mousemove", (ev) => {
|
|
994
|
-
preventDefault(ev);
|
|
1004
|
+
on(_canvas, "pointermove", (ev) => {
|
|
995
1005
|
const [x, y] = _getXY(ev);
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
_updateTap(0, x, y);
|
|
1001
|
-
});
|
|
1002
|
-
on(_canvas, "touchstart", (ev) => {
|
|
1003
|
-
preventDefault(ev);
|
|
1004
|
-
const touches = ev.changedTouches;
|
|
1005
|
-
for (const touch of touches) {
|
|
1006
|
-
const [x, y] = _getXY(touch);
|
|
1007
|
-
instance.emit("tap", x, y, touch.identifier + 1);
|
|
1008
|
-
_registerTap(touch.identifier + 1, x, y);
|
|
1006
|
+
if ("mouse" === ev.pointerType) {
|
|
1007
|
+
instance.def("MX", x);
|
|
1008
|
+
instance.def("MY", y);
|
|
1009
|
+
if (!_pressingMouse) return;
|
|
1009
1010
|
}
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
for (const touch of touches) {
|
|
1015
|
-
const [x, y] = _getXY(touch);
|
|
1016
|
-
instance.emit("tapping", x, y, touch.identifier + 1);
|
|
1017
|
-
_updateTap(touch.identifier + 1, x, y);
|
|
1011
|
+
const id = ev.pointerId;
|
|
1012
|
+
if (_taps.get(id)) {
|
|
1013
|
+
instance.emit("tapping", x, y, id);
|
|
1014
|
+
_updateTap(id, x, y);
|
|
1018
1015
|
}
|
|
1019
1016
|
});
|
|
1020
|
-
const
|
|
1021
|
-
|
|
1022
|
-
const
|
|
1023
|
-
|
|
1024
|
-
|
|
1017
|
+
const _pointerUpHandler = (ev) => {
|
|
1018
|
+
const id = ev.pointerId;
|
|
1019
|
+
const tap = _taps.get(id);
|
|
1020
|
+
const [x, y] = _getXY(ev);
|
|
1021
|
+
if (!tap) return;
|
|
1022
|
+
if ("mouse" === ev.pointerType) {
|
|
1023
|
+
if (ev.button) return;
|
|
1024
|
+
_pressingMouse = false;
|
|
1025
1025
|
}
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
if (_checkTapped(tap)) {
|
|
1029
|
-
instance.emit("tapped", tap.xi, tap.yi, id);
|
|
1030
|
-
}
|
|
1031
|
-
instance.emit("untap", tap.x, tap.y, id);
|
|
1032
|
-
_taps.delete(id);
|
|
1026
|
+
if (_checkTapped(tap)) {
|
|
1027
|
+
instance.emit("tapped", tap.xi, tap.yi, id);
|
|
1033
1028
|
}
|
|
1029
|
+
instance.emit("untap", x, y, id);
|
|
1030
|
+
_taps.delete(id);
|
|
1031
|
+
_canvas.releasePointerCapture(id);
|
|
1034
1032
|
};
|
|
1035
|
-
on(_canvas, "
|
|
1036
|
-
on(_canvas, "
|
|
1033
|
+
on(_canvas, "pointerup", _pointerUpHandler);
|
|
1034
|
+
on(_canvas, "pointercancel", _pointerUpHandler);
|
|
1037
1035
|
on(root, "blur", () => {
|
|
1038
1036
|
_pressingMouse = false;
|
|
1039
1037
|
for (const [id, tap] of _taps) {
|
|
1040
1038
|
instance.emit("untap", tap.x, tap.y, id);
|
|
1041
1039
|
_taps.delete(id);
|
|
1040
|
+
try {
|
|
1041
|
+
_canvas.releasePointerCapture(id);
|
|
1042
|
+
} catch (_) {}
|
|
1042
1043
|
}
|
|
1043
1044
|
});
|
|
1044
1045
|
}
|
|
@@ -1158,7 +1159,7 @@
|
|
|
1158
1159
|
instance.def("H", height);
|
|
1159
1160
|
_canvas.width = width;
|
|
1160
1161
|
_canvas.height = height;
|
|
1161
|
-
_canvas.style = "image-rendering:pixelated";
|
|
1162
|
+
_canvas.style = "image-rendering:pixelated;touch-action:none";
|
|
1162
1163
|
if (settings.autoscale) {
|
|
1163
1164
|
let maxScale = +settings.autoscale;
|
|
1164
1165
|
if (!_canvas.style.display) {
|
|
@@ -1192,9 +1193,12 @@
|
|
|
1192
1193
|
instance.def(key, pluginData[key]);
|
|
1193
1194
|
}
|
|
1194
1195
|
}
|
|
1195
|
-
function getColor(
|
|
1196
|
-
|
|
1197
|
-
|
|
1196
|
+
function getColor(color) {
|
|
1197
|
+
return color >= 0
|
|
1198
|
+
? _currentPalette[
|
|
1199
|
+
~~(_colorPaletteState[color] ?? color) % _currentPalette.length
|
|
1200
|
+
]
|
|
1201
|
+
: color;
|
|
1198
1202
|
}
|
|
1199
1203
|
if (settings.global) {
|
|
1200
1204
|
if (root.ENGINE) {
|
package/dist/dist.js
CHANGED
|
@@ -564,77 +564,56 @@
|
|
|
564
564
|
},
|
|
565
565
|
_checkTapped = (tap) => tap && perf.now() - tap.t <= 300;
|
|
566
566
|
let _pressingMouse = false;
|
|
567
|
-
on(_canvas, "
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
instance.emit("tap", x, y, 0);
|
|
572
|
-
_registerTap(0, x, y);
|
|
567
|
+
on(_canvas, "pointerdown", (ev) => {
|
|
568
|
+
const [x, y] = _getXY(ev);
|
|
569
|
+
if ("mouse" === ev.pointerType) {
|
|
570
|
+
if (ev.button) return;
|
|
573
571
|
_pressingMouse = true;
|
|
574
572
|
}
|
|
573
|
+
const id = ev.pointerId;
|
|
574
|
+
_registerTap(id, x, y);
|
|
575
|
+
instance.emit("tap", x, y, id);
|
|
576
|
+
_canvas.setPointerCapture(id);
|
|
575
577
|
});
|
|
576
|
-
on(_canvas, "
|
|
577
|
-
if (!ev.button) {
|
|
578
|
-
preventDefault(ev);
|
|
579
|
-
const tap = _taps.get(0);
|
|
580
|
-
const [x, y] = _getXY(ev);
|
|
581
|
-
if (_checkTapped(tap)) {
|
|
582
|
-
instance.emit("tapped", tap.xi, tap.yi, 0);
|
|
583
|
-
}
|
|
584
|
-
instance.emit("untap", x, y, 0);
|
|
585
|
-
_taps.delete(0);
|
|
586
|
-
_pressingMouse = false;
|
|
587
|
-
}
|
|
588
|
-
});
|
|
589
|
-
on(root, "mousemove", (ev) => {
|
|
590
|
-
preventDefault(ev);
|
|
578
|
+
on(_canvas, "pointermove", (ev) => {
|
|
591
579
|
const [x, y] = _getXY(ev);
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
_updateTap(0, x, y);
|
|
597
|
-
});
|
|
598
|
-
on(_canvas, "touchstart", (ev) => {
|
|
599
|
-
preventDefault(ev);
|
|
600
|
-
const touches = ev.changedTouches;
|
|
601
|
-
for (const touch of touches) {
|
|
602
|
-
const [x, y] = _getXY(touch);
|
|
603
|
-
instance.emit("tap", x, y, touch.identifier + 1);
|
|
604
|
-
_registerTap(touch.identifier + 1, x, y);
|
|
580
|
+
if ("mouse" === ev.pointerType) {
|
|
581
|
+
instance.def("MX", x);
|
|
582
|
+
instance.def("MY", y);
|
|
583
|
+
if (!_pressingMouse) return;
|
|
605
584
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
for (const touch of touches) {
|
|
611
|
-
const [x, y] = _getXY(touch);
|
|
612
|
-
instance.emit("tapping", x, y, touch.identifier + 1);
|
|
613
|
-
_updateTap(touch.identifier + 1, x, y);
|
|
585
|
+
const id = ev.pointerId;
|
|
586
|
+
if (_taps.get(id)) {
|
|
587
|
+
instance.emit("tapping", x, y, id);
|
|
588
|
+
_updateTap(id, x, y);
|
|
614
589
|
}
|
|
615
590
|
});
|
|
616
|
-
const
|
|
617
|
-
|
|
618
|
-
const
|
|
619
|
-
|
|
620
|
-
|
|
591
|
+
const _pointerUpHandler = (ev) => {
|
|
592
|
+
const id = ev.pointerId;
|
|
593
|
+
const tap = _taps.get(id);
|
|
594
|
+
const [x, y] = _getXY(ev);
|
|
595
|
+
if (!tap) return;
|
|
596
|
+
if ("mouse" === ev.pointerType) {
|
|
597
|
+
if (ev.button) return;
|
|
598
|
+
_pressingMouse = false;
|
|
621
599
|
}
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
if (_checkTapped(tap)) {
|
|
625
|
-
instance.emit("tapped", tap.xi, tap.yi, id);
|
|
626
|
-
}
|
|
627
|
-
instance.emit("untap", tap.x, tap.y, id);
|
|
628
|
-
_taps.delete(id);
|
|
600
|
+
if (_checkTapped(tap)) {
|
|
601
|
+
instance.emit("tapped", tap.xi, tap.yi, id);
|
|
629
602
|
}
|
|
603
|
+
instance.emit("untap", x, y, id);
|
|
604
|
+
_taps.delete(id);
|
|
605
|
+
_canvas.releasePointerCapture(id);
|
|
630
606
|
};
|
|
631
|
-
on(_canvas, "
|
|
632
|
-
on(_canvas, "
|
|
607
|
+
on(_canvas, "pointerup", _pointerUpHandler);
|
|
608
|
+
on(_canvas, "pointercancel", _pointerUpHandler);
|
|
633
609
|
on(root, "blur", () => {
|
|
634
610
|
_pressingMouse = false;
|
|
635
611
|
for (const [id, tap] of _taps) {
|
|
636
612
|
instance.emit("untap", tap.x, tap.y, id);
|
|
637
613
|
_taps.delete(id);
|
|
614
|
+
try {
|
|
615
|
+
_canvas.releasePointerCapture(id);
|
|
616
|
+
} catch (_) {}
|
|
638
617
|
}
|
|
639
618
|
});
|
|
640
619
|
}
|
|
@@ -719,7 +698,7 @@
|
|
|
719
698
|
instance.def("H", height);
|
|
720
699
|
_canvas.width = width;
|
|
721
700
|
_canvas.height = height;
|
|
722
|
-
_canvas.style = "image-rendering:pixelated";
|
|
701
|
+
_canvas.style = "image-rendering:pixelated;touch-action:none";
|
|
723
702
|
if (settings.autoscale) {
|
|
724
703
|
let maxScale = +settings.autoscale;
|
|
725
704
|
if (!_canvas.style.display) {
|
|
@@ -749,9 +728,12 @@
|
|
|
749
728
|
instance.def(key, pluginData[key]);
|
|
750
729
|
}
|
|
751
730
|
}
|
|
752
|
-
function getColor(
|
|
753
|
-
|
|
754
|
-
|
|
731
|
+
function getColor(color) {
|
|
732
|
+
return color >= 0
|
|
733
|
+
? _currentPalette[
|
|
734
|
+
~~(_colorPaletteState[color] ?? color) % _currentPalette.length
|
|
735
|
+
]
|
|
736
|
+
: color;
|
|
755
737
|
}
|
|
756
738
|
if (settings.global) {
|
|
757
739
|
if (root.ENGINE) {
|
package/dist/dist.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(()=>{var e=["#211e20","#555568","#a0a08b","#e9efec"];window.litecanvas=function(t={}){let a,
|
|
1
|
+
(()=>{var e=["#211e20","#555568","#a0a08b","#e9efec"];window.litecanvas=function(t={}){let a,n=window,l=Math,i=performance,o=2*l.PI,r=requestAnimationFrame,s=[],f=(e,t,a)=>{e.addEventListener(t,a,!1),s.push(()=>e.removeEventListener(t,a,!1))},c=(n.zzfxX=new AudioContext,n.zzfxV=1,(e=1,t=.05,a=220,n=0,l=0,i=.1,o=0,r=1,s=0,f=0,c=0,d=0,p=0,u=0,h=0,g=0,m=0,w=1,x=0,y=0,v=0)=>{let b=Math,z=2*b.PI,k=s*=500*z/44100/44100,C=a*=(1-t+2*t*b.random(t=[]))*z/44100,E=0,P=0,I=0,T=1,L=0,S=0,A=0,M=v<0?-1:1,N=z*M*v*2/44100,W=b.cos(N),X=b.sin,H=X(N)/4,q=1+H,B=-2*W/q,V=(1-H)/q,D=(1+M*W)/2/q,O=-(M+W)/q,G=0,R=0,Y=0,$=0;for(n=44100*n+9,x*=44100,l*=44100,i*=44100,m*=44100,f*=500*z/85766121e6,h*=z/44100,c*=z/44100,d*=44100,p=44100*p|0,e*=.3*zzfxV,M=n+x+l+i+m|0;I<M;t[I++]=A*e)++S%(100*g|0)||(A=o?1<o?2<o?3<o?X(E*E):b.max(b.min(b.tan(E),1),-1):1-(2*E/z%2+2)%2:1-4*b.abs(b.round(E/z)-E/z):X(E),A=(p?1-y+y*X(z*I/p):1)*(A<0?-1:1)*b.abs(A)**r*(I<n?I/n:I<n+x?1-(I-n)/x*(1-w):I<n+x+l?w:I<M-m?(M-I-m)/i*w:0),A=m?A/2+(m>I?0:(I<M-m?1:(M-I)/m)*t[I-m|0]/2/e):A,v&&(A=$=D*G+O*(G=R)+D*(R=A)-V*Y-B*(Y=$))),E+=(N=(a+=s+=f)*b.cos(h*P++))+N*u*X(I**5),T&&++T>d&&(a+=c,C+=c,T=0),!p||++L%p||(a=C,s=k,T=T||1);(e=zzfxX.createBuffer(1,M,44100)).getChannelData(0).set(t),(a=zzfxX.createBufferSource()).buffer=e,a.connect(zzfxX.destination),a.start()}),d=(t=Object.assign({width:null,height:null,autoscale:!0,canvas:null,global:!0,loop:null,tapEvents:!0,keyboardEvents:!0},t)).loop,p,u,h,g=1,m,w=.5,x=1,y,v=1e3/60,b,z=0,k=3,C="sans-serif",E=20,P=1.2,I=Date.now(),T=e,L=e,S=[],A=[.5,0,1750,,,.3,1,,,,600,.1],M={},N={W:0,H:0,T:0,MX:-1,MY:-1,TAU:o,lerp:(e,t,a)=>e+a*(t-e),deg2rad:e=>l.PI/180*e,rad2deg:e=>180/l.PI*e,mod:(e,t)=>(e%t+t)%t||0,round:(e,t=0)=>{if(!t)return l.round(e);let a=10**t;return l.round(e*a)/a},clamp:(e,t,a)=>e<t?t:e>a?a:e,dist:(e,t,a,n)=>l.hypot(a-e,n-t),wrap:(e,t,a)=>e-(a-t)*l.floor((e-t)/(a-t)),map(e,t,a,n,l,i){let o=(e-t)/(a-t)*(l-n)+n;return i?N.clamp(o,n,l):o},norm:(e,t,a)=>N.map(e,t,a,0,1),rand:(e=0,t=1)=>(I=(1664525*I+0x3c6ef35f)%0x100000000)/0x100000000*(t-e)+e,randi:(e=0,t=1)=>~~N.rand(e,t+1),rseed(e){I=~~e},cls(e){null==e?m.clearRect(0,0,N.W,N.H):N.rectfill(0,0,N.W,N.H,e)},rect(e,t,a,n,l,i){m.beginPath(),m[i?"roundRect":"rect"](~~e-w,~~t-w,~~a+2*w,~~n+2*w,i),N.stroke(l)},rectfill(e,t,a,n,l,i){m.beginPath(),m[i?"roundRect":"rect"](~~e,~~t,~~a,~~n,i),N.fill(l)},oval(e,t,a,n,l){m.beginPath(),m.ellipse(~~e,~~t,~~a,~~n,0,0,o),N.stroke(l)},ovalfill(e,t,a,n,l){m.beginPath(),m.ellipse(~~e,~~t,~~a,~~n,0,0,o),N.fill(l)},circ(e,t,a,n){N.oval(e,t,a,a,n)},circfill(e,t,a,n){N.ovalfill(e,t,a,a,n)},shape(e){m.beginPath();for(let t=0;t<e.length;t+=2)t?m.lineTo(~~e[t],~~e[t+1]):m.moveTo(~~e[t],~~e[t+1]);m.lineTo(~~e[0],~~e[1])},line(e,t,a,n,l){m.beginPath();let i=w&&~~e==~~a?.5:0,o=w&&~~t==~~n?.5:0;m.moveTo(~~e+i,~~t+o),m.lineTo(~~a+i,~~n+o),N.stroke(l)},linewidth(e){m.lineWidth=~~e,w=~~e%2?.5:0},linedash(e,t=0){m.setLineDash(e),m.lineDashOffset=t},text(e,t,a,n=k,l="normal"){m.font=`${l} ${E}px ${C}`,m.fillStyle=B(n);let i=(""+a).split("\n");for(let a=0;a<i.length;a++)m.fillText(i[a],~~e,~~t+E*P*a)},textgap(e){P=e},textfont(e){C=e},textsize(e){E=e},textalign(e,t){e&&(m.textAlign=e),t&&(m.textBaseline=t)},image(e,t,a){m.drawImage(a,~~e,~~t)},spr(e,t,a){let n=a.replace(/[^\w.\n]/g,"").split("\n").filter(e=>e);for(let a=0;a<n.length;a++)for(let l=0;l<n[a].length;l++)"."!==n[a][l]&&N.rectfill(e+l,t+a,1,1,parseInt(n[a][l],36)||0)},paint(e,t,a,n={}){let l=n.canvas||new OffscreenCanvas(1,1),i=n.scale||1,o=m;return l.width=e*i,l.height=t*i,(m=l.getContext("2d")).scale(i,i),a(m),m=o,l.transferToImageBitmap()},ctx:e=>(e&&(m=e),m),push(e=0,t=e,a=0,n=1,l=n){m.save(),N.translate(e,t),N.rotate(a),N.scale(n,l)},pop(){m.restore()},translate(e,t){m.translate(~~e,~~t)},scale(e,t=e){m.scale(e,t)},rotate(e){m.rotate(e)},alpha(e){m.globalAlpha=N.clamp(e,0,1)},fill(e){m.fillStyle=B(e),m.fill()},stroke(e){m.strokeStyle=B(e),m.stroke()},clip(e){m.beginPath(),e(m),m.clip()},sfx:(e,t,a)=>!!n.zzfxV&&(!navigator.userActivation||!!navigator.userActivation.hasBeenActive)&&(e||=A,(t||a>=0)&&((e=e.slice())[0]=a*(e[0]||1),e[10]=~~e[10]+t),c.apply(0,e),e),volume(e){n.zzfxV=e},canvas:()=>h,use(e,t={}){var a=e,n=t;let l=a(N,n);for(let e in l)N.def(e,l[e])},resize(e,a=e,n){t.width=e,t.height=a,t.autoscale=null==n?t.autoscale:n,H()},listen:(e,t)=>{M[e=e.toLowerCase()]=M[e]||new Set,M[e].add(t)},unlisten:(e,t)=>{M[e=e.toLowerCase()]&&M[e].delete(t)},emit:(e,t,a,l,i)=>(p&&(q("before:"+(e=e.toLowerCase()),t,a,l,i),d||n[e]===N[e]||"function"!=typeof n[e]||n[e](t,a,l,i),q(e,t,a,l,i),q("after:"+e,t,a,l,i)),t),pal(e,t=3){null==e?T=L:(L=T,T=e),S=[],k=t,N.emit("pal",T,k)},palc(e,t){null==e?S=[]:S[e]=t},def(e,a){N[e]=a,t.global&&(n[e]=a)},timescale(e){x=e},framerate(e){v=1e3/~~e},stat:e=>[t,p,v/1e3,g,M,T,A,x,n.zzfxV,I,E,C,S,P][e],ispaused:()=>u,pause(){u||(u=!0,z=~~cancelAnimationFrame(z),N.emit("paused"))},resume(){p&&u&&(W(),u=!1,N.emit("resumed"))},quit(){for(let e of(N.emit("quit"),N.pause(),p=!1,M={},s))e();if(t.global){for(let e in N)delete n[e];delete n.ENGINE}}};for(let e of"PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,min,max,pow,sqrt,sign".split(","))N[e]=l[e];function W(){z||(b=0,y=i.now(),z=r(X))}function X(){z=r(X);let e=i.now(),t=0,a=e-y;for(y=e,b+=a<100?a:v;b>=v;){t++,b-=v;let e=v/1e3*x;N.emit("update",e,t),N.def("T",N.T+e)}t&&(N.emit("draw",m),t>1&&(b=0))}function H(){let e=t.width>0?t.width:innerWidth,a=t.width>0?t.height||t.width:innerHeight;if(N.def("W",e),N.def("H",a),h.width=e,h.height=a,h.style="image-rendering:pixelated;touch-action:none",t.autoscale){let n=+t.autoscale;h.style.display||(h.style.display="block",h.style.margin="auto"),g=l.min(innerWidth/e,innerHeight/a),g=n>1&&g>n?n:g,h.style.width=e*g+"px",h.style.height=a*g+"px"}m.imageSmoothingEnabled=!1,N.textalign("start","top"),N.emit("resized",g)}function q(e,t,a,n,l){if(M[e])for(let i of M[e])i(t,a,n,l)}function B(e){return e>=0?T[~~(S[e]??e)%T.length]:e}if(t.global){if(n.ENGINE)throw Error("only one global litecanvas is allowed");Object.assign(n,N),n.ENGINE=N}if(a=document,m=(h=(h="string"==typeof t.canvas?a.querySelector(t.canvas):t.canvas)||a.createElement("canvas")).getContext("2d"),f(h,"click",()=>focus()),h.parentNode||a.body.appendChild(h),h.oncontextmenu=()=>!1,H(),d)for(let e in d)d[e]&&N.listen(e,d[e]);return r(function(){if(t.autoscale&&f(n,"resize",H),t.tapEvents){let e=e=>[(e.pageX-h.offsetLeft)/g,(e.pageY-h.offsetTop)/g],t=new Map,a=(e,a,n)=>{let l={x:a,y:n,xi:a,yi:n,t:i.now()};return t.set(e,l),l},l=!1;f(h,"pointerdown",t=>{let[n,i]=e(t);if("mouse"===t.pointerType){if(t.button)return;l=!0}let o=t.pointerId;a(o,n,i),N.emit("tap",n,i,o),h.setPointerCapture(o)}),f(h,"pointermove",n=>{let[i,o]=e(n);if("mouse"===n.pointerType&&(N.def("MX",i),N.def("MY",o),!l))return;let r=n.pointerId;if(t.get(r)){let e;N.emit("tapping",i,o,r),(e=t.get(r)||a(r)).x=i,e.y=o}});let o=a=>{let n=a.pointerId,o=t.get(n),[r,s]=e(a);if(o){if("mouse"===a.pointerType){if(a.button)return;l=!1}o&&i.now()-o.t<=300&&N.emit("tapped",o.xi,o.yi,n),N.emit("untap",r,s,n),t.delete(n),h.releasePointerCapture(n)}};f(h,"pointerup",o),f(h,"pointercancel",o),f(n,"blur",()=>{for(let[e,a]of(l=!1,t)){N.emit("untap",a.x,a.y,e),t.delete(e);try{h.releasePointerCapture(e)}catch(e){}}})}if(t.keyboardEvents){let e=new Set,t=new Set,a=(e,t="")=>(t=t.toLowerCase())?e.has("space"===t?" ":t):e.size>0,l="";f(n,"keydown",a=>{let n=a.key.toLowerCase();e.has(n)||(e.add(n),t.add(n),l=" "===n?"space":n)}),f(n,"keyup",t=>{e.delete(t.key.toLowerCase())}),f(n,"blur",()=>e.clear()),N.listen("after:update",()=>t.clear()),N.def("iskeydown",t=>a(e,t)),N.def("iskeypressed",e=>a(t,e)),N.def("lastkey",()=>l)}p=!0,N.emit("init",N),u||W()}),N}})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "litecanvas",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.401.0",
|
|
4
4
|
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Luiz Bills <luizbills@pm.me>",
|
package/src/index.js
CHANGED
|
@@ -364,12 +364,12 @@ export default function litecanvas(settings = {}) {
|
|
|
364
364
|
/**
|
|
365
365
|
* Clear the game screen with an optional color.
|
|
366
366
|
*
|
|
367
|
-
* @param {number} [color] The background color (index) or null/undefined (for transparent)
|
|
367
|
+
* @param {number|string} [color] The background color (index) or null/undefined (for transparent)
|
|
368
368
|
*/
|
|
369
369
|
cls(color) {
|
|
370
370
|
DEV: assert(
|
|
371
|
-
null == color || (isNumber(color) && color >= 0),
|
|
372
|
-
'cls() 1st argument must be a non-negative number'
|
|
371
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
372
|
+
'cls() 1st argument must be a non-negative number or string'
|
|
373
373
|
)
|
|
374
374
|
|
|
375
375
|
if (null == color) {
|
|
@@ -386,7 +386,7 @@ export default function litecanvas(settings = {}) {
|
|
|
386
386
|
* @param {number} y
|
|
387
387
|
* @param {number} width
|
|
388
388
|
* @param {number} height
|
|
389
|
-
* @param {number} [color=0] the color index
|
|
389
|
+
* @param {number|string} [color=0] the color index
|
|
390
390
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
391
391
|
*
|
|
392
392
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
|
|
@@ -403,8 +403,8 @@ export default function litecanvas(settings = {}) {
|
|
|
403
403
|
'rect() 4th argument must be a non-negative number'
|
|
404
404
|
)
|
|
405
405
|
DEV: assert(
|
|
406
|
-
null == color || (isNumber(color) && color >= 0),
|
|
407
|
-
'rect() 5th argument must be a non-negative number'
|
|
406
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
407
|
+
'rect() 5th argument must be a non-negative number or string'
|
|
408
408
|
)
|
|
409
409
|
DEV: assert(
|
|
410
410
|
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
@@ -430,7 +430,7 @@ export default function litecanvas(settings = {}) {
|
|
|
430
430
|
* @param {number} y
|
|
431
431
|
* @param {number} width
|
|
432
432
|
* @param {number} height
|
|
433
|
-
* @param {number} [color=0] the color index
|
|
433
|
+
* @param {number|string} [color=0] the color index
|
|
434
434
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
435
435
|
*/
|
|
436
436
|
rectfill(x, y, width, height, color, radii) {
|
|
@@ -445,8 +445,8 @@ export default function litecanvas(settings = {}) {
|
|
|
445
445
|
'rectfill() 4th argument must be a non-negative number'
|
|
446
446
|
)
|
|
447
447
|
DEV: assert(
|
|
448
|
-
null == color || (isNumber(color) && color >= 0),
|
|
449
|
-
'rectfill() 5th argument must be a non-negative number'
|
|
448
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
449
|
+
'rectfill() 5th argument must be a non-negative number or string'
|
|
450
450
|
)
|
|
451
451
|
DEV: assert(
|
|
452
452
|
null == radii || isNumber(radii) || (Array.isArray(radii) && radii.length >= 1),
|
|
@@ -467,7 +467,7 @@ export default function litecanvas(settings = {}) {
|
|
|
467
467
|
* @param {number} y
|
|
468
468
|
* @param {number} radiusX
|
|
469
469
|
* @param {number} radiusY
|
|
470
|
-
* @param {number} [color=0] the color index
|
|
470
|
+
* @param {number|string} [color=0] the color index
|
|
471
471
|
*/
|
|
472
472
|
oval(x, y, radiusX, radiusY, color) {
|
|
473
473
|
DEV: assert(isNumber(x), 'oval() 1st argument must be a number')
|
|
@@ -481,8 +481,8 @@ export default function litecanvas(settings = {}) {
|
|
|
481
481
|
'oval() 4th argument must be a non-negative number'
|
|
482
482
|
)
|
|
483
483
|
DEV: assert(
|
|
484
|
-
null == color || (isNumber(color) && color >= 0),
|
|
485
|
-
'oval() 5th argument must be a non-negative number'
|
|
484
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
485
|
+
'oval() 5th argument must be a non-negative number or string'
|
|
486
486
|
)
|
|
487
487
|
|
|
488
488
|
beginPath(_ctx)
|
|
@@ -498,7 +498,7 @@ export default function litecanvas(settings = {}) {
|
|
|
498
498
|
* @param {number} y
|
|
499
499
|
* @param {number} radiusX
|
|
500
500
|
* @param {number} radiusY
|
|
501
|
-
* @param {number} [color=0] the color index
|
|
501
|
+
* @param {number|string} [color=0] the color index
|
|
502
502
|
*/
|
|
503
503
|
ovalfill(x, y, radiusX, radiusY, color) {
|
|
504
504
|
DEV: assert(isNumber(x), 'ovalfill() 1st argument must be a number')
|
|
@@ -512,8 +512,8 @@ export default function litecanvas(settings = {}) {
|
|
|
512
512
|
'ovalfill() 4th argument must be a non-negative number'
|
|
513
513
|
)
|
|
514
514
|
DEV: assert(
|
|
515
|
-
null == color || (isNumber(color) && color >= 0),
|
|
516
|
-
'ovalfill() 5th argument must be a non-negative number'
|
|
515
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
516
|
+
'ovalfill() 5th argument must be a non-negative number or string'
|
|
517
517
|
)
|
|
518
518
|
|
|
519
519
|
beginPath(_ctx)
|
|
@@ -528,7 +528,7 @@ export default function litecanvas(settings = {}) {
|
|
|
528
528
|
* @param {number} x
|
|
529
529
|
* @param {number} y
|
|
530
530
|
* @param {number} radius
|
|
531
|
-
* @param {number} [color=0] the color index
|
|
531
|
+
* @param {number|string} [color=0] the color index
|
|
532
532
|
*/
|
|
533
533
|
circ(x, y, radius, color) {
|
|
534
534
|
DEV: assert(isNumber(x), 'circ() 1st argument must be a number')
|
|
@@ -538,8 +538,8 @@ export default function litecanvas(settings = {}) {
|
|
|
538
538
|
'circ() 3rd argument must be a non-negative number'
|
|
539
539
|
)
|
|
540
540
|
DEV: assert(
|
|
541
|
-
null == color || (isNumber(color) && color >= 0),
|
|
542
|
-
'circ() 4th argument must be a non-negative number'
|
|
541
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
542
|
+
'circ() 4th argument must be a non-negative number or string'
|
|
543
543
|
)
|
|
544
544
|
|
|
545
545
|
instance.oval(x, y, radius, radius, color)
|
|
@@ -551,7 +551,7 @@ export default function litecanvas(settings = {}) {
|
|
|
551
551
|
* @param {number} x
|
|
552
552
|
* @param {number} y
|
|
553
553
|
* @param {number} radius
|
|
554
|
-
* @param {number} [color=0] the color index
|
|
554
|
+
* @param {number|string} [color=0] the color index
|
|
555
555
|
*/
|
|
556
556
|
circfill(x, y, radius, color) {
|
|
557
557
|
DEV: assert(isNumber(x), 'circfill() 1st argument must be a number')
|
|
@@ -561,8 +561,8 @@ export default function litecanvas(settings = {}) {
|
|
|
561
561
|
'circfill() 3rd argument must be a non-negative number'
|
|
562
562
|
)
|
|
563
563
|
DEV: assert(
|
|
564
|
-
null == color || (isNumber(color) && color >= 0),
|
|
565
|
-
'circfill() 4th argument must be a non-negative number'
|
|
564
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
565
|
+
'circfill() 4th argument must be a non-negative number or string'
|
|
566
566
|
)
|
|
567
567
|
|
|
568
568
|
instance.ovalfill(x, y, radius, radius, color)
|
|
@@ -600,7 +600,7 @@ export default function litecanvas(settings = {}) {
|
|
|
600
600
|
* @param {number} y1
|
|
601
601
|
* @param {number} x2
|
|
602
602
|
* @param {number} y2
|
|
603
|
-
* @param {number} [color=0] the color index
|
|
603
|
+
* @param {number|string} [color=0] the color index
|
|
604
604
|
*/
|
|
605
605
|
line(x1, y1, x2, y2, color) {
|
|
606
606
|
DEV: assert(isNumber(x1), 'line() 1st argument must be a number')
|
|
@@ -608,8 +608,8 @@ export default function litecanvas(settings = {}) {
|
|
|
608
608
|
DEV: assert(isNumber(x2), 'line() 3rd argument must be a non-negative number')
|
|
609
609
|
DEV: assert(isNumber(y2), 'line() 4th argument must be a non-negative number')
|
|
610
610
|
DEV: assert(
|
|
611
|
-
null == color || (isNumber(color) && color >= 0),
|
|
612
|
-
'line() 5th argument must be a non-negative number'
|
|
611
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
612
|
+
'line() 5th argument must be a non-negative number or string'
|
|
613
613
|
)
|
|
614
614
|
|
|
615
615
|
beginPath(_ctx)
|
|
@@ -665,15 +665,15 @@ export default function litecanvas(settings = {}) {
|
|
|
665
665
|
* @param {number} x
|
|
666
666
|
* @param {number} y
|
|
667
667
|
* @param {string} message the text message
|
|
668
|
-
* @param {number} [color] the color index
|
|
668
|
+
* @param {number|string} [color] the color index
|
|
669
669
|
* @param {string} [fontStyle] can be "normal" (default), "italic" and/or "bold".
|
|
670
670
|
*/
|
|
671
671
|
text(x, y, message, color = _defaultTextColor, fontStyle = 'normal') {
|
|
672
672
|
DEV: assert(isNumber(x), 'text() 1st argument must be a number')
|
|
673
673
|
DEV: assert(isNumber(y), 'text() 2nd argument must be a number')
|
|
674
674
|
DEV: assert(
|
|
675
|
-
null == color || (isNumber(color) && color >= 0),
|
|
676
|
-
'text() 4th argument must be a non-negative number'
|
|
675
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
676
|
+
'text() 4th argument must be a non-negative number or string'
|
|
677
677
|
)
|
|
678
678
|
DEV: assert('string' === typeof fontStyle, 'text() 5th argument must be a string')
|
|
679
679
|
|
|
@@ -947,12 +947,12 @@ export default function litecanvas(settings = {}) {
|
|
|
947
947
|
/**
|
|
948
948
|
* Fills the current path with a given color.
|
|
949
949
|
*
|
|
950
|
-
* @param {number} [color=0]
|
|
950
|
+
* @param {number|string} [color=0]
|
|
951
951
|
*/
|
|
952
952
|
fill(color) {
|
|
953
953
|
DEV: assert(
|
|
954
|
-
null == color || (isNumber(color) && color >= 0),
|
|
955
|
-
'fill() 1st argument must be a non-negative number'
|
|
954
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
955
|
+
'fill() 1st argument must be a non-negative number or string'
|
|
956
956
|
)
|
|
957
957
|
|
|
958
958
|
_ctx.fillStyle = getColor(color)
|
|
@@ -962,12 +962,12 @@ export default function litecanvas(settings = {}) {
|
|
|
962
962
|
/**
|
|
963
963
|
* Outlines the current path with a given color.
|
|
964
964
|
*
|
|
965
|
-
* @param {number} [color=0]
|
|
965
|
+
* @param {number|string} [color=0]
|
|
966
966
|
*/
|
|
967
967
|
stroke(color) {
|
|
968
968
|
DEV: assert(
|
|
969
|
-
null == color || (isNumber(color) && color >= 0),
|
|
970
|
-
'stroke() 1st argument must be a non-negative number'
|
|
969
|
+
null == color || (isNumber(color) && color >= 0) || 'string' === typeof color,
|
|
970
|
+
'stroke() 1st argument must be a non-negative number or string'
|
|
971
971
|
)
|
|
972
972
|
|
|
973
973
|
_ctx.strokeStyle = getColor(color)
|
|
@@ -1502,128 +1502,70 @@ export default function litecanvas(settings = {}) {
|
|
|
1502
1502
|
|
|
1503
1503
|
let _pressingMouse = false
|
|
1504
1504
|
|
|
1505
|
-
on(
|
|
1506
|
-
|
|
1507
|
-
'mousedown',
|
|
1508
|
-
/**
|
|
1509
|
-
* @param {MouseEvent} ev
|
|
1510
|
-
*/
|
|
1511
|
-
(ev) => {
|
|
1512
|
-
if (!ev.button) {
|
|
1513
|
-
preventDefault(ev)
|
|
1514
|
-
const [x, y] = _getXY(ev)
|
|
1515
|
-
instance.emit('tap', x, y, 0)
|
|
1516
|
-
_registerTap(0, x, y)
|
|
1517
|
-
_pressingMouse = true
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
)
|
|
1505
|
+
on(_canvas, 'pointerdown', (ev) => {
|
|
1506
|
+
const [x, y] = _getXY(ev)
|
|
1521
1507
|
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
/**
|
|
1526
|
-
* @param {MouseEvent} ev
|
|
1527
|
-
*/
|
|
1528
|
-
(ev) => {
|
|
1529
|
-
if (!ev.button) {
|
|
1530
|
-
preventDefault(ev)
|
|
1531
|
-
const tap = _taps.get(0)
|
|
1532
|
-
const [x, y] = _getXY(ev)
|
|
1533
|
-
if (_checkTapped(tap)) {
|
|
1534
|
-
instance.emit('tapped', tap.xi, tap.yi, 0)
|
|
1535
|
-
}
|
|
1536
|
-
instance.emit('untap', x, y, 0)
|
|
1537
|
-
_taps.delete(0)
|
|
1538
|
-
_pressingMouse = false
|
|
1539
|
-
}
|
|
1508
|
+
if ('mouse' === ev.pointerType) {
|
|
1509
|
+
if (ev.button) return
|
|
1510
|
+
_pressingMouse = true
|
|
1540
1511
|
}
|
|
1541
|
-
)
|
|
1542
1512
|
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
'
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
(ev) => {
|
|
1550
|
-
preventDefault(ev)
|
|
1513
|
+
const id = ev.pointerId
|
|
1514
|
+
_registerTap(id, x, y)
|
|
1515
|
+
instance.emit('tap', x, y, id)
|
|
1516
|
+
|
|
1517
|
+
_canvas.setPointerCapture(id)
|
|
1518
|
+
})
|
|
1551
1519
|
|
|
1552
|
-
|
|
1520
|
+
on(_canvas, 'pointermove', (ev) => {
|
|
1521
|
+
const [x, y] = _getXY(ev)
|
|
1522
|
+
|
|
1523
|
+
if ('mouse' === ev.pointerType) {
|
|
1553
1524
|
instance.def('MX', x)
|
|
1554
1525
|
instance.def('MY', y)
|
|
1555
|
-
|
|
1556
1526
|
if (!_pressingMouse) return
|
|
1557
|
-
|
|
1558
|
-
instance.emit('tapping', x, y, 0)
|
|
1559
|
-
_updateTap(0, x, y)
|
|
1560
1527
|
}
|
|
1561
|
-
)
|
|
1562
1528
|
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
* @param {TouchEvent} ev
|
|
1568
|
-
*/
|
|
1569
|
-
(ev) => {
|
|
1570
|
-
preventDefault(ev)
|
|
1571
|
-
/** @type {TouchList} touches */
|
|
1572
|
-
const touches = ev.changedTouches
|
|
1573
|
-
for (const touch of touches) {
|
|
1574
|
-
const [x, y] = _getXY(touch)
|
|
1575
|
-
instance.emit('tap', x, y, touch.identifier + 1)
|
|
1576
|
-
_registerTap(touch.identifier + 1, x, y)
|
|
1577
|
-
}
|
|
1529
|
+
const id = ev.pointerId
|
|
1530
|
+
if (_taps.get(id)) {
|
|
1531
|
+
instance.emit('tapping', x, y, id)
|
|
1532
|
+
_updateTap(id, x, y)
|
|
1578
1533
|
}
|
|
1579
|
-
)
|
|
1534
|
+
})
|
|
1580
1535
|
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
* @param {TouchEvent} ev
|
|
1586
|
-
*/
|
|
1587
|
-
(ev) => {
|
|
1588
|
-
preventDefault(ev)
|
|
1589
|
-
const touches = ev.changedTouches
|
|
1590
|
-
for (const touch of touches) {
|
|
1591
|
-
const [x, y] = _getXY(touch)
|
|
1592
|
-
instance.emit('tapping', x, y, touch.identifier + 1)
|
|
1593
|
-
_updateTap(touch.identifier + 1, x, y)
|
|
1594
|
-
}
|
|
1595
|
-
}
|
|
1596
|
-
)
|
|
1536
|
+
const _pointerUpHandler = (ev) => {
|
|
1537
|
+
const id = ev.pointerId
|
|
1538
|
+
const tap = _taps.get(id)
|
|
1539
|
+
const [x, y] = _getXY(ev)
|
|
1597
1540
|
|
|
1598
|
-
|
|
1599
|
-
* @param {TouchEvent} ev
|
|
1600
|
-
*/
|
|
1601
|
-
const _touchEndHandler = (ev) => {
|
|
1602
|
-
preventDefault(ev)
|
|
1603
|
-
const existing = []
|
|
1541
|
+
if (!tap) return
|
|
1604
1542
|
|
|
1605
|
-
|
|
1606
|
-
|
|
1543
|
+
if ('mouse' === ev.pointerType) {
|
|
1544
|
+
if (ev.button) return
|
|
1545
|
+
_pressingMouse = false
|
|
1607
1546
|
}
|
|
1608
1547
|
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
if (_checkTapped(tap)) {
|
|
1612
|
-
instance.emit('tapped', tap.xi, tap.yi, id)
|
|
1613
|
-
}
|
|
1614
|
-
instance.emit('untap', tap.x, tap.y, id)
|
|
1615
|
-
_taps.delete(id)
|
|
1548
|
+
if (_checkTapped(tap)) {
|
|
1549
|
+
instance.emit('tapped', tap.xi, tap.yi, id)
|
|
1616
1550
|
}
|
|
1551
|
+
|
|
1552
|
+
instance.emit('untap', x, y, id)
|
|
1553
|
+
|
|
1554
|
+
_taps.delete(id)
|
|
1555
|
+
_canvas.releasePointerCapture(id)
|
|
1617
1556
|
}
|
|
1618
1557
|
|
|
1619
|
-
on(_canvas, '
|
|
1620
|
-
on(_canvas, '
|
|
1558
|
+
on(_canvas, 'pointerup', _pointerUpHandler)
|
|
1559
|
+
on(_canvas, 'pointercancel', _pointerUpHandler)
|
|
1621
1560
|
|
|
1622
1561
|
on(root, 'blur', () => {
|
|
1623
1562
|
_pressingMouse = false
|
|
1624
1563
|
for (const [id, tap] of _taps) {
|
|
1625
1564
|
instance.emit('untap', tap.x, tap.y, id)
|
|
1626
1565
|
_taps.delete(id)
|
|
1566
|
+
try {
|
|
1567
|
+
_canvas.releasePointerCapture(id)
|
|
1568
|
+
} catch (_) {}
|
|
1627
1569
|
}
|
|
1628
1570
|
})
|
|
1629
1571
|
}
|
|
@@ -1806,8 +1748,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1806
1748
|
_canvas.width = width
|
|
1807
1749
|
_canvas.height = height
|
|
1808
1750
|
|
|
1809
|
-
|
|
1810
|
-
_canvas.style = 'image-rendering:pixelated'
|
|
1751
|
+
_canvas.style = 'image-rendering:pixelated;touch-action:none'
|
|
1811
1752
|
|
|
1812
1753
|
if (settings.autoscale) {
|
|
1813
1754
|
let maxScale = +settings.autoscale
|
|
@@ -1867,12 +1808,13 @@ export default function litecanvas(settings = {}) {
|
|
|
1867
1808
|
}
|
|
1868
1809
|
|
|
1869
1810
|
/**
|
|
1870
|
-
* @param {number}
|
|
1811
|
+
* @param {number|string} color
|
|
1871
1812
|
* @returns {string}
|
|
1872
1813
|
*/
|
|
1873
|
-
function getColor(
|
|
1874
|
-
|
|
1875
|
-
|
|
1814
|
+
function getColor(color) {
|
|
1815
|
+
return color >= 0
|
|
1816
|
+
? _currentPalette[~~(_colorPaletteState[color] ?? color) % _currentPalette.length]
|
|
1817
|
+
: color
|
|
1876
1818
|
}
|
|
1877
1819
|
|
|
1878
1820
|
if (settings.global) {
|
package/src/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// Generated by genversion.
|
|
2
|
-
export const version = '0.
|
|
2
|
+
export const version = '0.401.0'
|
package/types/global.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import './types'
|
|
1
|
+
import './types.d.ts'
|
|
2
2
|
|
|
3
3
|
export default function litecanvas(settings?: LitecanvasOptions): LitecanvasInstance
|
|
4
4
|
|
|
@@ -216,7 +216,7 @@ declare global {
|
|
|
216
216
|
*
|
|
217
217
|
* @param color The background color index or `null`
|
|
218
218
|
*/
|
|
219
|
-
function cls(color
|
|
219
|
+
function cls(color?: number | string): void
|
|
220
220
|
|
|
221
221
|
/**
|
|
222
222
|
* Draw a rectangle outline
|
|
@@ -235,7 +235,7 @@ declare global {
|
|
|
235
235
|
y: number,
|
|
236
236
|
width: number,
|
|
237
237
|
height: number,
|
|
238
|
-
color?: number,
|
|
238
|
+
color?: number | string,
|
|
239
239
|
radii?: number | number[]
|
|
240
240
|
): void
|
|
241
241
|
/**
|
|
@@ -253,7 +253,7 @@ declare global {
|
|
|
253
253
|
y: number,
|
|
254
254
|
width: number,
|
|
255
255
|
height: number,
|
|
256
|
-
color?: number,
|
|
256
|
+
color?: number | string,
|
|
257
257
|
radii?: number | number[]
|
|
258
258
|
): void
|
|
259
259
|
/**
|
|
@@ -264,7 +264,7 @@ declare global {
|
|
|
264
264
|
* @param radius
|
|
265
265
|
* @param [color=0] the color index
|
|
266
266
|
*/
|
|
267
|
-
function circ(x: number, y: number, radius: number, color?: number): void
|
|
267
|
+
function circ(x: number, y: number, radius: number, color?: number | string): void
|
|
268
268
|
/**
|
|
269
269
|
* Draw a color-filled circle
|
|
270
270
|
*
|
|
@@ -273,7 +273,7 @@ declare global {
|
|
|
273
273
|
* @param radius
|
|
274
274
|
* @param [color=0] the color index
|
|
275
275
|
*/
|
|
276
|
-
function circfill(x: number, y: number, radius: number, color?: number): void
|
|
276
|
+
function circfill(x: number, y: number, radius: number, color?: number | string): void
|
|
277
277
|
/**
|
|
278
278
|
* Draw a ellipse outline
|
|
279
279
|
*
|
|
@@ -283,7 +283,13 @@ declare global {
|
|
|
283
283
|
* @param radiusY
|
|
284
284
|
* @param [color=0] the color index
|
|
285
285
|
*/
|
|
286
|
-
function oval(
|
|
286
|
+
function oval(
|
|
287
|
+
x: number,
|
|
288
|
+
y: number,
|
|
289
|
+
radiusX: number,
|
|
290
|
+
radiusY: number,
|
|
291
|
+
color?: number | string
|
|
292
|
+
): void
|
|
287
293
|
/**
|
|
288
294
|
* Draw a color-filled ellipse
|
|
289
295
|
*
|
|
@@ -293,7 +299,13 @@ declare global {
|
|
|
293
299
|
* @param radiusY
|
|
294
300
|
* @param [color=0] the color index
|
|
295
301
|
*/
|
|
296
|
-
function ovalfill(
|
|
302
|
+
function ovalfill(
|
|
303
|
+
x: number,
|
|
304
|
+
y: number,
|
|
305
|
+
radiusX: number,
|
|
306
|
+
radiusY: number,
|
|
307
|
+
color?: number | string
|
|
308
|
+
): void
|
|
297
309
|
/**
|
|
298
310
|
* Make a custom shape in the canvas context.
|
|
299
311
|
* Then, just use `fill` or `stroke` to draw the shape.
|
|
@@ -316,7 +328,7 @@ declare global {
|
|
|
316
328
|
* @param y2
|
|
317
329
|
* @param [color=0] the color index
|
|
318
330
|
*/
|
|
319
|
-
function line(x1: number, y1: number, x2: number, y2: number, color?: number): void
|
|
331
|
+
function line(x1: number, y1: number, x2: number, y2: number, color?: number | string): void
|
|
320
332
|
/**
|
|
321
333
|
* Sets the thickness of lines
|
|
322
334
|
*
|
|
@@ -344,7 +356,13 @@ declare global {
|
|
|
344
356
|
* @param [color=3] the color index
|
|
345
357
|
* @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
|
|
346
358
|
*/
|
|
347
|
-
function text(
|
|
359
|
+
function text(
|
|
360
|
+
x: number,
|
|
361
|
+
y: number,
|
|
362
|
+
message: string,
|
|
363
|
+
color?: number | string,
|
|
364
|
+
fontStyle?: string
|
|
365
|
+
): void
|
|
348
366
|
/**
|
|
349
367
|
* Set the font family
|
|
350
368
|
*
|
|
@@ -498,13 +516,13 @@ declare global {
|
|
|
498
516
|
*
|
|
499
517
|
* @param color
|
|
500
518
|
*/
|
|
501
|
-
function fill(color
|
|
519
|
+
function fill(color?: number | string): void
|
|
502
520
|
/**
|
|
503
521
|
* Outlines the current path with a given color.
|
|
504
522
|
*
|
|
505
523
|
* @param color
|
|
506
524
|
*/
|
|
507
|
-
function stroke(color
|
|
525
|
+
function stroke(color?: number | string): void
|
|
508
526
|
/**
|
|
509
527
|
* Turns a path (in the callback) into the current clipping region.
|
|
510
528
|
*
|
package/types/types.d.ts
CHANGED
|
@@ -210,7 +210,7 @@ type LitecanvasInstance = {
|
|
|
210
210
|
*
|
|
211
211
|
* @param color The background color index or `null`
|
|
212
212
|
*/
|
|
213
|
-
cls(color
|
|
213
|
+
cls(color?: number | string): void
|
|
214
214
|
|
|
215
215
|
/**
|
|
216
216
|
* Draw a rectangle outline
|
|
@@ -229,7 +229,7 @@ type LitecanvasInstance = {
|
|
|
229
229
|
y: number,
|
|
230
230
|
width: number,
|
|
231
231
|
height: number,
|
|
232
|
-
color?: number,
|
|
232
|
+
color?: number | string,
|
|
233
233
|
radii?: number | number[]
|
|
234
234
|
): void
|
|
235
235
|
/**
|
|
@@ -247,7 +247,7 @@ type LitecanvasInstance = {
|
|
|
247
247
|
y: number,
|
|
248
248
|
width: number,
|
|
249
249
|
height: number,
|
|
250
|
-
color?: number,
|
|
250
|
+
color?: number | string,
|
|
251
251
|
radii?: number | number[]
|
|
252
252
|
): void
|
|
253
253
|
/**
|
|
@@ -258,7 +258,7 @@ type LitecanvasInstance = {
|
|
|
258
258
|
* @param radius
|
|
259
259
|
* @param [color=0] the color index
|
|
260
260
|
*/
|
|
261
|
-
circ(x: number, y: number, radius: number, color?: number): void
|
|
261
|
+
circ(x: number, y: number, radius: number, color?: number | string): void
|
|
262
262
|
/**
|
|
263
263
|
* Draw a color-filled circle
|
|
264
264
|
*
|
|
@@ -267,7 +267,7 @@ type LitecanvasInstance = {
|
|
|
267
267
|
* @param radius
|
|
268
268
|
* @param [color=0] the color index
|
|
269
269
|
*/
|
|
270
|
-
circfill(x: number, y: number, radius: number, color?: number): void
|
|
270
|
+
circfill(x: number, y: number, radius: number, color?: number | string): void
|
|
271
271
|
/**
|
|
272
272
|
* Draw a ellipse outline
|
|
273
273
|
*
|
|
@@ -277,7 +277,7 @@ type LitecanvasInstance = {
|
|
|
277
277
|
* @param radiusY
|
|
278
278
|
* @param [color=0] the color index
|
|
279
279
|
*/
|
|
280
|
-
oval(x: number, y: number, radiusX: number, radiusY: number, color?: number): void
|
|
280
|
+
oval(x: number, y: number, radiusX: number, radiusY: number, color?: number | string): void
|
|
281
281
|
/**
|
|
282
282
|
* Draw a color-filled ellipse
|
|
283
283
|
*
|
|
@@ -287,7 +287,7 @@ type LitecanvasInstance = {
|
|
|
287
287
|
* @param radiusY
|
|
288
288
|
* @param [color=0] the color index
|
|
289
289
|
*/
|
|
290
|
-
ovalfill(x: number, y: number, radiusX: number, radiusY: number, color?: number): void
|
|
290
|
+
ovalfill(x: number, y: number, radiusX: number, radiusY: number, color?: number | string): void
|
|
291
291
|
/**
|
|
292
292
|
* Make a custom shape in the canvas context.
|
|
293
293
|
* Then, just use `fill` or `stroke` to draw the shape.
|
|
@@ -304,7 +304,7 @@ type LitecanvasInstance = {
|
|
|
304
304
|
* @param y2
|
|
305
305
|
* @param [color=0] the color index
|
|
306
306
|
*/
|
|
307
|
-
line(x1: number, y1: number, x2: number, y2: number, color?: number): void
|
|
307
|
+
line(x1: number, y1: number, x2: number, y2: number, color?: number | string): void
|
|
308
308
|
/**
|
|
309
309
|
* Sets the thickness of lines
|
|
310
310
|
*
|
|
@@ -332,7 +332,7 @@ type LitecanvasInstance = {
|
|
|
332
332
|
* @param [color=3] the color index
|
|
333
333
|
* @param [fontStyle="normal"] can be "normal" (default), "italic" and/or "bold"
|
|
334
334
|
*/
|
|
335
|
-
text(x: number, y: number, message: string, color?: number, fontStyle?: string): void
|
|
335
|
+
text(x: number, y: number, message: string, color?: number | string, fontStyle?: string): void
|
|
336
336
|
/**
|
|
337
337
|
* Set the font family
|
|
338
338
|
*
|
|
@@ -486,13 +486,13 @@ type LitecanvasInstance = {
|
|
|
486
486
|
*
|
|
487
487
|
* @param color
|
|
488
488
|
*/
|
|
489
|
-
fill(color
|
|
489
|
+
fill(color?: number | string): void
|
|
490
490
|
/**
|
|
491
491
|
* Outlines the current path with a given color.
|
|
492
492
|
*
|
|
493
493
|
* @param color
|
|
494
494
|
*/
|
|
495
|
-
stroke(color
|
|
495
|
+
stroke(color?: number | string): void
|
|
496
496
|
/**
|
|
497
497
|
* Turns a path (in the callback) into the current clipping region.
|
|
498
498
|
*
|