litecanvas 0.95.1 → 0.97.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 +10 -14
- package/dist/dist.dev.js +94 -62
- package/dist/dist.js +72 -56
- package/dist/dist.min.js +1 -1
- package/package.json +4 -4
- package/src/index.js +101 -72
- package/src/version.js +1 -1
- package/types/global.d.ts +29 -4
- package/types/types.d.ts +29 -17
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
# Litecanvas
|
|
6
6
|
|
|
7
|
-
[](https://www.npmjs.com/package/litecanvas/)
|
|
8
8
|
[](LICENSE)
|
|
9
9
|
|
|
10
10
|
Litecanvas is a lightweight HTML5 canvas 2D engine suitable for small web games, prototypes, game jams, animations, creative coding, learning game programming and game design, etc.
|
|
@@ -32,14 +32,10 @@ Litecanvas is a lightweight HTML5 canvas 2D engine suitable for small web games,
|
|
|
32
32
|
|
|
33
33
|
## Getting Started
|
|
34
34
|
|
|
35
|
-
You can try our [online playground](https://litecanvas.github.io) or install the
|
|
35
|
+
You can try our [online playground](https://litecanvas.github.io) or install the NPM package:
|
|
36
36
|
|
|
37
37
|
```sh
|
|
38
|
-
|
|
39
|
-
npx tiged litecanvas/template my-game
|
|
40
|
-
cd my-game
|
|
41
|
-
npm install
|
|
42
|
-
npm run dev
|
|
38
|
+
npm i litecanvas
|
|
43
39
|
```
|
|
44
40
|
|
|
45
41
|
or just use add a `<script>` tag with our CDN link:
|
|
@@ -91,7 +87,7 @@ function draw() {
|
|
|
91
87
|
}
|
|
92
88
|
```
|
|
93
89
|
|
|
94
|
-
[Play with this code in the playground](https://litecanvas.js.org?c=
|
|
90
|
+
[Play with this code in the playground](https://litecanvas.js.org?c=eJx9U8tu2zAQvOsrBsghcitYtnMz4HvvLdD2SJFriTVNEuSqthH430tSduQEQQUI0GNmdnZ22bb4ziIwhFWIxKMHDwSyvbZUtS0MiWBxdIG2GJh93Lat0UxS2L8iLv%2FEpQt9Kzo38nLgo3lKIqxtH6sZVb9WSJdxzm%2FxCm01Nxi9EkwNVBCnBiy8J4VrU10XVa7Lg47Yj1aydhZhtBHOSoLgYrCjZNCmOtUbJsvWC0y1uh47rFCECNIZF%2FC0Qt0ZIQ%2BLApk%2B7vDyAfWC%2BjQk7xMqCKXHmGA%2F0WI9K5bGcNKKh3a9KlDv4vkG3GScJMsU8At1kn1H2CzuhEsifPtA%2BP0p4fpJLIpSxAxptDzElt0oB4pzIlOo9bnB5Z5LrjII21Oc2tBBGspGdGbcIZ2IaRhZYZh%2Fwu3Le1J9bPf82MrlZtOP05h6caQ0917LNJ735meb0ybUih9MHsWBHh3uhTEx5b%2BC12dKjz4FFUk6q%2Bb6X3fYJMQXKP7MRyCrKKSd%2BY%2BXvI1vSyRNrLt%2BUULL56BIRRmIpqSyt702ps5RNMVDc1uYZtqmws2aELdOCpHpzPV61SDfzz%2BET8fvchoo0PMjI8Oq6z8yIyTy)
|
|
95
91
|
|
|
96
92
|
https://github.com/user-attachments/assets/854ac6bd-724f-4da8-bb3c-bc04dba5d8c8
|
|
97
93
|
|
|
@@ -99,13 +95,13 @@ https://github.com/user-attachments/assets/854ac6bd-724f-4da8-bb3c-bc04dba5d8c8
|
|
|
99
95
|
|
|
100
96
|
Try some demos in the playground:
|
|
101
97
|
|
|
102
|
-
- [Pong](https://litecanvas.js.org?c=
|
|
103
|
-
- [Bouncing Ball](https://litecanvas.js.org?c=
|
|
104
|
-
- [Scroller](https://litecanvas.js.org?c=
|
|
105
|
-
- [3D projection](https://litecanvas.js.org?c=
|
|
106
|
-
- [Rendering Benchmark](https://litecanvas.js.org?c=
|
|
98
|
+
- [Pong](https://litecanvas.js.org?c=eJy1VV1y00gQftcpmidJWJYl2U5IIKFMyiEPgKk4YMLW1paQxvYUiqSSxiT8hCtwgn3bQ3AeLsAV6J7RzyjEKV42rozdPT39%2B803CROQh%2FECDsAPPMcA%2FEP5hORxI74h0Wu3z1EeDSs5ZqUg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KaUIDvTAH4AEIGEgw%3D%3D)
|
|
101
|
+
- [3D projection](https://litecanvas.js.org?c=eJyNVcuS2jAQvPsrJofUykE8wykJySlVyRckuy4fZGwWgZEpWQSWFP%2BeGUlgizVLqgyyprul1mgkl9IUc6H%2BiJrFUbTYqbmRlQKppGEx%2FI0AanksYAa%2F4Cv8gG%2F4G8IUPmEfW4S3uloVTjSDBAMAyZjDCJ%2BUuy6%2Bj8MuRWw3pREqqUzdqPs0I4d2k3IYDiE5cHjhcEwdr4vWRjqA%2Fj2kRQgHuwm8jvc7gbTJVZHTYikg1HNJyR1Fp1byd9tcmILlxm2AI%2FWQNRiNIwwsKg2sLAxIkn7G5otP4qAs1LNZYqjXc2KAeaVqgzhl2LImG2G0PLDBYOBUiUxjS6UxdWWEc4i0za5kNoC2fjNrJOY0luPf5D5euJ5yh%2F%2FUzb%2FkCx02oqbiAvopyGGuxd4X8Lys2SimxG139ZIRt5Sq2MvcLNmEukYLVZeUc6rqCbdFbpGuVE%2Bv06vQDZMcmIQejGN4D1PeeI9DGlIQZS1u7EK3BBxkB%2BFkD84WV9Ne9VkkOGTc14QzSitmfrtFmozoOTMoMA4C2TUjuzA%2BhjP6PaEJfbZtuYl5VVLBCdT4omwwXe0tdg1kXpR1iDIvys4AInIBzE30bjZzjBjMElvA7YXvWleaPfzEy62UObiqL%2BoHWwpuVF3U56PYtdPWabDbKHC1mLhL6KJaOdUKVXYZ%2BNqo3Mmqdxt71F3kolw75Zrmc8p1WwlW16NUyjRZp%2FAB87NOk1XqCSffOmsYx%2FFQEp2xEy1XF2anFXGC3WvOtha5xEPg5vXsm7f5vKovAns1qla3TQyRUDby12KXn8e3%2FFwNE85y45tz7TE084aTp%2F93cisP95Pw6qPYdhLc2f4LGJpJDngukxf6O6Yk%2FgdWEko3)
|
|
102
|
+
- [Rendering Benchmark](https://litecanvas.js.org?c=eJylVVtP2zAUfs%2BvsLpJSSCkSS9QurYTQkx7AAkB0x6qariJ21q4See40Az633dsp8G9UNDm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BKMVLRFhGCkf5bkf5Lkf5pqNtVofm8PfMGdpN9t0h%2B1gMO5%2FVY7g7tvcH9QMxT%2FGYyKg96dRb23Ru2aN%2FASaYns8%3D)
|
|
107
103
|
|
|
108
|
-
_See other demos in [samples](samples) folder_
|
|
104
|
+
> _See other demos in [samples](/samples) folder_
|
|
109
105
|
|
|
110
106
|
## Inspirations
|
|
111
107
|
|
package/dist/dist.dev.js
CHANGED
|
@@ -32,27 +32,25 @@
|
|
|
32
32
|
};
|
|
33
33
|
|
|
34
34
|
// src/version.js
|
|
35
|
-
var version = "0.
|
|
35
|
+
var version = "0.97.0";
|
|
36
36
|
|
|
37
37
|
// src/index.js
|
|
38
38
|
function litecanvas(settings = {}) {
|
|
39
39
|
const root = window, math = Math, TWO_PI = math.PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => {
|
|
40
40
|
elem.addEventListener(evt, callback, false);
|
|
41
41
|
_browserEventListeners.push(() => elem.removeEventListener(evt, callback, false));
|
|
42
|
-
}, preventDefault = (ev) => ev.preventDefault(), beginPath = (c) => c.beginPath(), isNumber = Number.isFinite, zzfx = setupZzFX(root), defaults = {
|
|
42
|
+
}, lowerCase = (str) => str.toLowerCase(), preventDefault = (ev) => ev.preventDefault(), beginPath = (c) => c.beginPath(), isNumber = Number.isFinite, zzfx = setupZzFX(root), defaults = {
|
|
43
43
|
width: null,
|
|
44
44
|
height: null,
|
|
45
45
|
autoscale: true,
|
|
46
|
-
pixelart: true,
|
|
47
46
|
canvas: null,
|
|
48
47
|
global: true,
|
|
49
48
|
loop: null,
|
|
50
49
|
tapEvents: true,
|
|
51
|
-
keyboardEvents: true
|
|
52
|
-
animate: true
|
|
50
|
+
keyboardEvents: true
|
|
53
51
|
};
|
|
54
52
|
settings = Object.assign(defaults, settings);
|
|
55
|
-
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _fpsInterval = 1e3 / 60, _accumulated, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors
|
|
53
|
+
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _fpsInterval = 1e3 / 60, _accumulated, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _currentPalette, _colors, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _coreEvents = "init,update,draw,tap,untap,tapping,tapped,resized", _mathFunctions = "PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp", _eventListeners = {};
|
|
56
54
|
const instance = {
|
|
57
55
|
/** @type {number} */
|
|
58
56
|
W: 0,
|
|
@@ -129,7 +127,7 @@
|
|
|
129
127
|
round: (n, precision = 0) => {
|
|
130
128
|
DEV: assert(isNumber(n), "[litecanvas] round() 1st param must be a number");
|
|
131
129
|
DEV: assert(
|
|
132
|
-
|
|
130
|
+
isNumber(precision) && precision >= 0,
|
|
133
131
|
"[litecanvas] round() 2nd param must be a positive number or zero"
|
|
134
132
|
);
|
|
135
133
|
if (!precision) {
|
|
@@ -515,15 +513,15 @@
|
|
|
515
513
|
instance.stroke(color);
|
|
516
514
|
},
|
|
517
515
|
/**
|
|
518
|
-
* Sets the thickness of lines
|
|
516
|
+
* Sets the thickness of the lines
|
|
519
517
|
*
|
|
520
518
|
* @param {number} value
|
|
521
519
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineWidth
|
|
522
520
|
*/
|
|
523
521
|
linewidth(value) {
|
|
524
522
|
DEV: assert(
|
|
525
|
-
isNumber(value) &&
|
|
526
|
-
"[litecanvas] linewidth() 1st param must be a positive number"
|
|
523
|
+
isNumber(value) && value >= 0,
|
|
524
|
+
"[litecanvas] linewidth() 1st param must be a positive number or zero"
|
|
527
525
|
);
|
|
528
526
|
_ctx.lineWidth = ~~value;
|
|
529
527
|
_outline_fix = 0 === ~~value % 2 ? 0 : 0.5;
|
|
@@ -626,19 +624,44 @@
|
|
|
626
624
|
DEV: assert(isNumber(y), "[litecanvas] image() 2nd param must be a number");
|
|
627
625
|
_ctx.drawImage(source, ~~x, ~~y);
|
|
628
626
|
},
|
|
627
|
+
/**
|
|
628
|
+
* Draw a sprite pxiel by pixel represented by a string. Each pixel must be a base 36 number (0-9 or a-z) or a dot.
|
|
629
|
+
*
|
|
630
|
+
* @param {number} x
|
|
631
|
+
* @param {number} y
|
|
632
|
+
* @param {number} width
|
|
633
|
+
* @param {number} height
|
|
634
|
+
* @param {string} pixels
|
|
635
|
+
*/
|
|
636
|
+
spr(x, y, width, height, pixels) {
|
|
637
|
+
DEV: assert(isNumber(x), "[litecanvas] spr() 1st param must be a number");
|
|
638
|
+
DEV: assert(isNumber(y), "[litecanvas] spr() 2nd param must be a number");
|
|
639
|
+
DEV: assert(isNumber(width), "[litecanvas] spr() 3rd param must be a number");
|
|
640
|
+
DEV: assert(isNumber(height), "[litecanvas] spr() 4th param must be a number");
|
|
641
|
+
DEV: assert("string" === typeof pixels, "[litecanvas] spr() 5th param must be a string");
|
|
642
|
+
const chars = pixels.replace(/\s/g, "");
|
|
643
|
+
for (let gridx = 0; gridx < width; gridx++) {
|
|
644
|
+
for (let gridy = 0; gridy < height; gridy++) {
|
|
645
|
+
const char = chars[height * gridy + gridx] || ".";
|
|
646
|
+
if (char !== ".") {
|
|
647
|
+
instance.rectfill(x + gridx, y + gridy, 1, 1, parseInt(char, 16) || 0);
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
},
|
|
629
652
|
/**
|
|
630
653
|
* Draw in an OffscreenCanvas and returns its image.
|
|
631
654
|
*
|
|
632
655
|
* @param {number} width
|
|
633
656
|
* @param {number} height
|
|
634
|
-
* @param {
|
|
657
|
+
* @param {drawCallback} callback
|
|
635
658
|
* @param {object} [options]
|
|
636
659
|
* @param {number} [options.scale=1]
|
|
637
660
|
* @param {OffscreenCanvas} [options.canvas]
|
|
638
661
|
* @returns {ImageBitmap}
|
|
639
662
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas
|
|
640
663
|
*/
|
|
641
|
-
paint(width, height,
|
|
664
|
+
paint(width, height, callback, options = {}) {
|
|
642
665
|
DEV: assert(
|
|
643
666
|
isNumber(width) && width >= 1,
|
|
644
667
|
"[litecanvas] paint() 1st param must be a positive number"
|
|
@@ -648,7 +671,7 @@
|
|
|
648
671
|
"[litecanvas] paint() 2nd param must be a positive number"
|
|
649
672
|
);
|
|
650
673
|
DEV: assert(
|
|
651
|
-
"function" === typeof
|
|
674
|
+
"function" === typeof callback,
|
|
652
675
|
"[litecanvas] paint() 3rd param must be a function or array"
|
|
653
676
|
);
|
|
654
677
|
DEV: assert(
|
|
@@ -659,28 +682,13 @@
|
|
|
659
682
|
options && null == options.canvas || options.canvas instanceof OffscreenCanvas,
|
|
660
683
|
"[litecanvas] paint() 4th param (options.canvas) must be an OffscreenCanvas"
|
|
661
684
|
);
|
|
662
|
-
const canvas = options.canvas || new OffscreenCanvas(1, 1), scale = options.scale || 1,
|
|
685
|
+
const canvas = options.canvas || new OffscreenCanvas(1, 1), scale = options.scale || 1, currentContext = _ctx;
|
|
663
686
|
canvas.width = width * scale;
|
|
664
687
|
canvas.height = height * scale;
|
|
665
688
|
_ctx = canvas.getContext("2d");
|
|
666
689
|
_ctx.scale(scale, scale);
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
_ctx.imageSmoothingEnabled = false;
|
|
670
|
-
for (const str of drawing) {
|
|
671
|
-
for (const color of str) {
|
|
672
|
-
if (" " !== color && "." !== color) {
|
|
673
|
-
instance.rectfill(x, y, 1, 1, parseInt(color, 16));
|
|
674
|
-
}
|
|
675
|
-
x++;
|
|
676
|
-
}
|
|
677
|
-
y++;
|
|
678
|
-
x = 0;
|
|
679
|
-
}
|
|
680
|
-
} else {
|
|
681
|
-
drawing(_ctx);
|
|
682
|
-
}
|
|
683
|
-
_ctx = contextOriginal;
|
|
690
|
+
callback(_ctx);
|
|
691
|
+
_ctx = currentContext;
|
|
684
692
|
return canvas.transferToImageBitmap();
|
|
685
693
|
},
|
|
686
694
|
/** ADVANCED GRAPHICS API */
|
|
@@ -702,23 +710,27 @@
|
|
|
702
710
|
*
|
|
703
711
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/save
|
|
704
712
|
*/
|
|
705
|
-
push
|
|
713
|
+
push() {
|
|
714
|
+
_ctx.save();
|
|
715
|
+
},
|
|
706
716
|
/**
|
|
707
717
|
* restores the drawing style settings and transformations
|
|
708
718
|
*
|
|
709
719
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/restore
|
|
710
720
|
*/
|
|
711
|
-
pop
|
|
721
|
+
pop() {
|
|
722
|
+
_ctx.restore();
|
|
723
|
+
},
|
|
712
724
|
/**
|
|
713
725
|
* Adds a translation to the transformation matrix.
|
|
714
726
|
*
|
|
715
727
|
* @param {number} x
|
|
716
728
|
* @param {number} y
|
|
717
729
|
*/
|
|
718
|
-
translate
|
|
730
|
+
translate(x, y) {
|
|
719
731
|
DEV: assert(isNumber(x), "[litecanvas] translate() 1st param must be a number");
|
|
720
732
|
DEV: assert(isNumber(y), "[litecanvas] translate() 2nd param must be a number");
|
|
721
|
-
|
|
733
|
+
_ctx.translate(~~x, ~~y);
|
|
722
734
|
},
|
|
723
735
|
/**
|
|
724
736
|
* Adds a scaling transformation to the canvas units horizontally and/or vertically.
|
|
@@ -726,19 +738,19 @@
|
|
|
726
738
|
* @param {number} x
|
|
727
739
|
* @param {number} [y]
|
|
728
740
|
*/
|
|
729
|
-
scale
|
|
741
|
+
scale(x, y) {
|
|
730
742
|
DEV: assert(isNumber(x), "[litecanvas] scale() 1st param must be a number");
|
|
731
743
|
DEV: assert(null == y || isNumber(y), "[litecanvas] scale() 2nd param must be a number");
|
|
732
|
-
|
|
744
|
+
_ctx.scale(x, y || x);
|
|
733
745
|
},
|
|
734
746
|
/**
|
|
735
747
|
* Adds a rotation to the transformation matrix.
|
|
736
748
|
*
|
|
737
749
|
* @param {number} radians
|
|
738
750
|
*/
|
|
739
|
-
rotate
|
|
751
|
+
rotate(radians) {
|
|
740
752
|
DEV: assert(isNumber(radians), "[litecanvas] rotate() 1st param must be a number");
|
|
741
|
-
|
|
753
|
+
_ctx.rotate(radians);
|
|
742
754
|
},
|
|
743
755
|
/**
|
|
744
756
|
* Sets the alpha (opacity) value to apply when drawing new shapes and images
|
|
@@ -878,7 +890,7 @@
|
|
|
878
890
|
"function" === typeof callback,
|
|
879
891
|
"[litecanvas] listen() 2nd param must be a function"
|
|
880
892
|
);
|
|
881
|
-
eventName = eventName
|
|
893
|
+
eventName = lowerCase(eventName);
|
|
882
894
|
_eventListeners[eventName] = _eventListeners[eventName] || /* @__PURE__ */ new Set();
|
|
883
895
|
_eventListeners[eventName].add(callback);
|
|
884
896
|
return () => _eventListeners && _eventListeners[eventName].delete(callback);
|
|
@@ -898,14 +910,14 @@
|
|
|
898
910
|
"[litecanvas] emit() 1st param must be a string"
|
|
899
911
|
);
|
|
900
912
|
if (_initialized) {
|
|
901
|
-
eventName = eventName
|
|
913
|
+
eventName = lowerCase(eventName);
|
|
902
914
|
triggerEvent("before:" + eventName, arg1, arg2, arg3, arg4);
|
|
903
915
|
triggerEvent(eventName, arg1, arg2, arg3, arg4);
|
|
904
916
|
triggerEvent("after:" + eventName, arg1, arg2, arg3, arg4);
|
|
905
917
|
}
|
|
906
918
|
},
|
|
907
919
|
/**
|
|
908
|
-
* Set or reset the color palette
|
|
920
|
+
* Set or reset the color palette.
|
|
909
921
|
*
|
|
910
922
|
* @param {string[]} [colors]
|
|
911
923
|
*/
|
|
@@ -915,6 +927,31 @@
|
|
|
915
927
|
"[litecanvas] pal() 1st param must be a array of strings"
|
|
916
928
|
);
|
|
917
929
|
_colors = colors;
|
|
930
|
+
_currentPalette = [...colors];
|
|
931
|
+
},
|
|
932
|
+
/**
|
|
933
|
+
* Swap two colors of the current palette.
|
|
934
|
+
*
|
|
935
|
+
* If called without arguments, reset the current palette.
|
|
936
|
+
*
|
|
937
|
+
* @param {number?} a
|
|
938
|
+
* @param {number?} b
|
|
939
|
+
*/
|
|
940
|
+
palc(a, b) {
|
|
941
|
+
DEV: assert(
|
|
942
|
+
null == a || isNumber(a) && a >= 0,
|
|
943
|
+
"[litecanvas] palc() 1st param must be a positive number"
|
|
944
|
+
);
|
|
945
|
+
DEV: assert(
|
|
946
|
+
isNumber(a) ? isNumber(b) && b >= 0 : null == b,
|
|
947
|
+
"[litecanvas] palc() 2nd param must be a positive number"
|
|
948
|
+
);
|
|
949
|
+
if (a == null) {
|
|
950
|
+
_colors = [..._currentPalette];
|
|
951
|
+
} else {
|
|
952
|
+
;
|
|
953
|
+
[_colors[a], _colors[b]] = [_colors[b], _colors[a]];
|
|
954
|
+
}
|
|
918
955
|
},
|
|
919
956
|
/**
|
|
920
957
|
* Define or update a instance property.
|
|
@@ -963,12 +1000,15 @@
|
|
|
963
1000
|
/**
|
|
964
1001
|
* Returns information about that engine instance.
|
|
965
1002
|
*
|
|
966
|
-
* @param {number}
|
|
1003
|
+
* @param {number|string} index
|
|
967
1004
|
* @returns {any}
|
|
968
1005
|
*/
|
|
969
|
-
stat(
|
|
970
|
-
DEV: assert(
|
|
971
|
-
|
|
1006
|
+
stat(index) {
|
|
1007
|
+
DEV: assert(
|
|
1008
|
+
isNumber(index) || "string" === typeof index,
|
|
1009
|
+
"[litecanvas] stat() 1st param must be a number or string"
|
|
1010
|
+
);
|
|
1011
|
+
const internals = [
|
|
972
1012
|
// 0
|
|
973
1013
|
settings,
|
|
974
1014
|
// 1
|
|
@@ -994,7 +1034,7 @@
|
|
|
994
1034
|
// 11
|
|
995
1035
|
_fontFamily
|
|
996
1036
|
];
|
|
997
|
-
const data = { index
|
|
1037
|
+
const data = { index, value: internals[index] };
|
|
998
1038
|
instance.emit("stat", data);
|
|
999
1039
|
return data.value;
|
|
1000
1040
|
},
|
|
@@ -1223,12 +1263,12 @@
|
|
|
1223
1263
|
const _keysDown = /* @__PURE__ */ new Set();
|
|
1224
1264
|
const _keysPress = /* @__PURE__ */ new Set();
|
|
1225
1265
|
const keyCheck = (keySet, key = "") => {
|
|
1226
|
-
key = key
|
|
1266
|
+
key = lowerCase(key);
|
|
1227
1267
|
return !key ? keySet.size > 0 : keySet.has("space" === key ? " " : key);
|
|
1228
1268
|
};
|
|
1229
1269
|
let _lastKey = "";
|
|
1230
1270
|
on(root, "keydown", (event) => {
|
|
1231
|
-
const key = event.key
|
|
1271
|
+
const key = lowerCase(event.key);
|
|
1232
1272
|
if (!_keysDown.has(key)) {
|
|
1233
1273
|
_keysDown.add(key);
|
|
1234
1274
|
_keysPress.add(key);
|
|
@@ -1236,7 +1276,7 @@
|
|
|
1236
1276
|
}
|
|
1237
1277
|
});
|
|
1238
1278
|
on(root, "keyup", (event) => {
|
|
1239
|
-
_keysDown.delete(event.key
|
|
1279
|
+
_keysDown.delete(lowerCase(event.key));
|
|
1240
1280
|
});
|
|
1241
1281
|
on(root, "blur", () => _keysDown.clear());
|
|
1242
1282
|
instance.listen("after:update", () => _keysPress.clear());
|
|
@@ -1281,9 +1321,7 @@
|
|
|
1281
1321
|
instance.resume();
|
|
1282
1322
|
}
|
|
1283
1323
|
function drawFrame() {
|
|
1284
|
-
|
|
1285
|
-
return instance.emit("draw", _ctx);
|
|
1286
|
-
}
|
|
1324
|
+
_rafid = raf(drawFrame);
|
|
1287
1325
|
let now = Date.now();
|
|
1288
1326
|
let updated = 0;
|
|
1289
1327
|
let frameTime = now - _lastFrameTime;
|
|
@@ -1305,7 +1343,6 @@
|
|
|
1305
1343
|
);
|
|
1306
1344
|
}
|
|
1307
1345
|
}
|
|
1308
|
-
_rafid = raf(drawFrame);
|
|
1309
1346
|
}
|
|
1310
1347
|
function setupCanvas() {
|
|
1311
1348
|
if ("string" === typeof settings.canvas) {
|
|
@@ -1324,11 +1361,11 @@
|
|
|
1324
1361
|
);
|
|
1325
1362
|
_ctx = _canvas.getContext("2d");
|
|
1326
1363
|
on(_canvas, "click", () => focus());
|
|
1327
|
-
_canvas.style = "";
|
|
1328
1364
|
resizeCanvas();
|
|
1329
1365
|
if (!_canvas.parentNode) {
|
|
1330
1366
|
document.body.appendChild(_canvas);
|
|
1331
1367
|
}
|
|
1368
|
+
_canvas.style.imageRendering = "pixelated";
|
|
1332
1369
|
_canvas.oncontextmenu = () => false;
|
|
1333
1370
|
}
|
|
1334
1371
|
function resizeCanvas() {
|
|
@@ -1360,15 +1397,9 @@
|
|
|
1360
1397
|
_canvas.style.width = width * _scale + "px";
|
|
1361
1398
|
_canvas.style.height = height * _scale + "px";
|
|
1362
1399
|
}
|
|
1363
|
-
|
|
1364
|
-
_ctx.imageSmoothingEnabled = false;
|
|
1365
|
-
_canvas.style.imageRendering = "pixelated";
|
|
1366
|
-
}
|
|
1400
|
+
_ctx.imageSmoothingEnabled = false;
|
|
1367
1401
|
instance.textalign("start", "top");
|
|
1368
1402
|
instance.emit("resized", _scale);
|
|
1369
|
-
if (!settings.animate) {
|
|
1370
|
-
raf(drawFrame);
|
|
1371
|
-
}
|
|
1372
1403
|
}
|
|
1373
1404
|
function triggerEvent(eventName, arg1, arg2, arg3, arg4) {
|
|
1374
1405
|
if (!_eventListeners[eventName]) return;
|
|
@@ -1396,6 +1427,7 @@
|
|
|
1396
1427
|
DEV: console.info(`[litecanvas] version ${version} started`);
|
|
1397
1428
|
DEV: console.debug(`[litecanvas] litecanvas() options =`, settings);
|
|
1398
1429
|
setupCanvas();
|
|
1430
|
+
instance.pal();
|
|
1399
1431
|
if ("loading" === document.readyState) {
|
|
1400
1432
|
on(root, "DOMContentLoaded", () => raf(init));
|
|
1401
1433
|
} else {
|