litecanvas 0.40.0 → 0.41.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 +20 -16
- package/dist/dist.js +168 -160
- package/dist/dist.min.js +1 -1
- package/dist/dist.min.js.map +3 -3
- package/package.json +2 -2
- package/src/index.js +197 -218
- package/src/palette.js +1 -1
- package/src/sounds.js +1 -1
- package/src/types.js +4 -11
- package/src/zzfx.js +7 -7
- package/types/index.d.ts +29 -71
- package/types/types.d.ts +25 -90
package/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/* litecanvas v0.
|
|
1
|
+
/* litecanvas v0.41.0 | https://github.com/litecanvas/game-engine */
|
|
2
2
|
import './zzfx'
|
|
3
|
-
import colors from './palette'
|
|
4
|
-
import sounds from './sounds'
|
|
3
|
+
import { colors } from './palette'
|
|
4
|
+
import { sounds } from './sounds'
|
|
5
5
|
|
|
6
6
|
const root = globalThis
|
|
7
7
|
|
|
@@ -13,32 +13,23 @@ const root = globalThis
|
|
|
13
13
|
*/
|
|
14
14
|
export default function litecanvas(settings = {}) {
|
|
15
15
|
// helpers
|
|
16
|
-
const
|
|
17
|
-
math = Math,
|
|
18
|
-
PI = math.PI,
|
|
16
|
+
const PI = Math.PI,
|
|
19
17
|
TWO_PI = PI * 2,
|
|
20
18
|
/** @type {(elem:HTMLElement, evt:string, callback:Function)=>void} */
|
|
21
19
|
on = (elem, evt, callback) => elem.addEventListener(evt, callback),
|
|
22
|
-
/** @type {(elem:HTMLElement, evt:string, callback:Function)=>void} */
|
|
23
|
-
off = (elem, evt, callback) => elem.removeEventListener(evt, callback),
|
|
24
|
-
time = () => performance.now(),
|
|
25
|
-
NULL = null,
|
|
26
|
-
UNDEF = undefined,
|
|
27
20
|
/** @type {LitecanvasOptions} */
|
|
28
21
|
defaults = {
|
|
29
22
|
fps: 60,
|
|
30
23
|
fullscreen: true,
|
|
31
|
-
width:
|
|
32
|
-
height:
|
|
24
|
+
width: null,
|
|
25
|
+
height: null,
|
|
33
26
|
autoscale: true,
|
|
34
27
|
pixelart: false,
|
|
35
28
|
antialias: true,
|
|
36
|
-
|
|
37
|
-
canvas: NULL,
|
|
29
|
+
canvas: null,
|
|
38
30
|
global: true,
|
|
39
31
|
tapEvents: true,
|
|
40
|
-
|
|
41
|
-
loop: NULL,
|
|
32
|
+
loop: null,
|
|
42
33
|
}
|
|
43
34
|
|
|
44
35
|
// setup the settings default values
|
|
@@ -54,18 +45,12 @@ export default function litecanvas(settings = {}) {
|
|
|
54
45
|
_fullscreen = settings.fullscreen,
|
|
55
46
|
/** @type {boolean} */
|
|
56
47
|
_autoscale = settings.autoscale,
|
|
57
|
-
/** @type {number|null} */
|
|
58
|
-
_bg = settings.background,
|
|
59
|
-
/** @type {boolean} */
|
|
60
|
-
_hasMouse = settings.useMouse || matchMedia('(pointer:fine)').matches,
|
|
61
|
-
/** @type {function} */
|
|
62
|
-
_tappingHandler,
|
|
63
48
|
/** @type {number} */
|
|
64
49
|
_scale = 1,
|
|
65
|
-
/** @type {number} */
|
|
66
|
-
|
|
67
|
-
/** @type {number} */
|
|
68
|
-
|
|
50
|
+
/** @type {number?} */
|
|
51
|
+
_mouseX,
|
|
52
|
+
/** @type {number?} */
|
|
53
|
+
_mouseY,
|
|
69
54
|
/** @type {CanvasRenderingContext2D} */
|
|
70
55
|
_ctx,
|
|
71
56
|
/** @type {number} */
|
|
@@ -93,14 +78,17 @@ export default function litecanvas(settings = {}) {
|
|
|
93
78
|
/** @type {string} */
|
|
94
79
|
_textBaseline = 'top',
|
|
95
80
|
/**
|
|
96
|
-
*
|
|
97
|
-
* @type {LitecanvasGameLoopListeners}
|
|
81
|
+
* default game events
|
|
98
82
|
*/
|
|
99
|
-
|
|
83
|
+
_events = {
|
|
100
84
|
init: [],
|
|
101
85
|
update: [],
|
|
102
86
|
draw: [],
|
|
103
87
|
resized: [],
|
|
88
|
+
tap: [],
|
|
89
|
+
untap: [],
|
|
90
|
+
tapping: [],
|
|
91
|
+
tapped: [],
|
|
104
92
|
},
|
|
105
93
|
/**
|
|
106
94
|
* Helpers to be used by plugins
|
|
@@ -120,25 +108,15 @@ export default function litecanvas(settings = {}) {
|
|
|
120
108
|
/** @type {number} */
|
|
121
109
|
HEIGHT: settings.height || settings.width,
|
|
122
110
|
/** @type {HTMLCanvasElement} */
|
|
123
|
-
CANVAS:
|
|
124
|
-
/** @type {boolean} */
|
|
125
|
-
TAPPED: NULL,
|
|
126
|
-
/** @type {boolean} */
|
|
127
|
-
TAPPING: NULL,
|
|
128
|
-
/** @type {number} */
|
|
129
|
-
TAPX: NULL,
|
|
130
|
-
/** @type {number} */
|
|
131
|
-
TAPY: NULL,
|
|
111
|
+
CANVAS: null,
|
|
132
112
|
/** @type {number} */
|
|
133
113
|
ELAPSED: 0,
|
|
134
114
|
/** @type {number} */
|
|
135
115
|
FPS: settings.fps,
|
|
136
116
|
/** @type {number} */
|
|
137
|
-
|
|
117
|
+
CENTERX: null,
|
|
138
118
|
/** @type {number} */
|
|
139
|
-
|
|
140
|
-
/** @type {number} */
|
|
141
|
-
CENTERY: NULL,
|
|
119
|
+
CENTERY: null,
|
|
142
120
|
|
|
143
121
|
/** MATH API */
|
|
144
122
|
/**
|
|
@@ -203,7 +181,7 @@ export default function litecanvas(settings = {}) {
|
|
|
203
181
|
* @param {number} max
|
|
204
182
|
* @returns {number}
|
|
205
183
|
*/
|
|
206
|
-
clamp: (value, min, max) =>
|
|
184
|
+
clamp: (value, min, max) => Math.min(Math.max(value, min), max),
|
|
207
185
|
|
|
208
186
|
/**
|
|
209
187
|
* Wraps a number between `min` (inclusive) and `max` (exclusive).
|
|
@@ -214,7 +192,7 @@ export default function litecanvas(settings = {}) {
|
|
|
214
192
|
* @returns {number}
|
|
215
193
|
*/
|
|
216
194
|
wrap: (value, min, max) =>
|
|
217
|
-
value - (max - min) *
|
|
195
|
+
value - (max - min) * Math.floor((value - min) / (max - min)),
|
|
218
196
|
|
|
219
197
|
/**
|
|
220
198
|
* Re-maps a number from one range to another.
|
|
@@ -243,14 +221,6 @@ export default function litecanvas(settings = {}) {
|
|
|
243
221
|
*/
|
|
244
222
|
norm: (value, min, max) => instance.map(value, min, max, 0, 1),
|
|
245
223
|
|
|
246
|
-
/**
|
|
247
|
-
* Returns the fractional part of a number
|
|
248
|
-
*
|
|
249
|
-
* @param {number} value The number
|
|
250
|
-
* @returns {number}
|
|
251
|
-
*/
|
|
252
|
-
fract: (value) => value % 1,
|
|
253
|
-
|
|
254
224
|
/** RNG API */
|
|
255
225
|
/**
|
|
256
226
|
* Generates a pseudorandom float between min (inclusive) and max (exclusive)
|
|
@@ -259,7 +229,7 @@ export default function litecanvas(settings = {}) {
|
|
|
259
229
|
* @param {number} [max=1.0]
|
|
260
230
|
* @returns {number} the random number
|
|
261
231
|
*/
|
|
262
|
-
rand: (min = 0.0, max = 1.0) =>
|
|
232
|
+
rand: (min = 0.0, max = 1.0) => Math.random() * (max - min) + min,
|
|
263
233
|
|
|
264
234
|
/**
|
|
265
235
|
* Generates a pseudorandom integer between min (inclusive) and max (inclusive)
|
|
@@ -277,8 +247,8 @@ export default function litecanvas(settings = {}) {
|
|
|
277
247
|
*
|
|
278
248
|
* @param {number|null} color The background color (from 0 to 7) or null
|
|
279
249
|
*/
|
|
280
|
-
|
|
281
|
-
if (
|
|
250
|
+
cls(color) {
|
|
251
|
+
if (null == color) {
|
|
282
252
|
_ctx.clearRect(0, 0, instance.WIDTH, instance.HEIGHT)
|
|
283
253
|
} else {
|
|
284
254
|
instance.rectfill(0, 0, instance.WIDTH, instance.HEIGHT, color)
|
|
@@ -295,14 +265,14 @@ export default function litecanvas(settings = {}) {
|
|
|
295
265
|
* @param {number} [color=0] the color index (generally from 0 to 7)
|
|
296
266
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
297
267
|
*/
|
|
298
|
-
rect(x, y, width, height, color = 0, radii =
|
|
268
|
+
rect(x, y, width, height, color = 0, radii = null) {
|
|
299
269
|
_ctx.beginPath()
|
|
300
270
|
_ctx[radii ? 'roundRect' : 'rect'](
|
|
301
271
|
~~x,
|
|
302
272
|
~~y,
|
|
303
273
|
~~width,
|
|
304
274
|
~~height,
|
|
305
|
-
radii
|
|
275
|
+
radii
|
|
306
276
|
)
|
|
307
277
|
instance.stroke(color)
|
|
308
278
|
},
|
|
@@ -317,14 +287,14 @@ export default function litecanvas(settings = {}) {
|
|
|
317
287
|
* @param {number} [color=0] the color index (generally from 0 to 7)
|
|
318
288
|
* @param {number|number[]} [radii] A number or list specifying the radii used to draw a rounded-borders rectangle
|
|
319
289
|
*/
|
|
320
|
-
rectfill(x, y, width, height, color = 0, radii =
|
|
290
|
+
rectfill(x, y, width, height, color = 0, radii = null) {
|
|
321
291
|
_ctx.beginPath()
|
|
322
292
|
_ctx[radii ? 'roundRect' : 'rect'](
|
|
323
293
|
~~x,
|
|
324
294
|
~~y,
|
|
325
295
|
~~width,
|
|
326
296
|
~~height,
|
|
327
|
-
radii
|
|
297
|
+
radii
|
|
328
298
|
)
|
|
329
299
|
instance.fill(color)
|
|
330
300
|
},
|
|
@@ -398,28 +368,6 @@ export default function litecanvas(settings = {}) {
|
|
|
398
368
|
_ctx.lineDashOffset = offset
|
|
399
369
|
},
|
|
400
370
|
|
|
401
|
-
/**
|
|
402
|
-
* Determines the shape used to draw the end points of lines
|
|
403
|
-
* Possible values are: "butt", "round" or "square"
|
|
404
|
-
*
|
|
405
|
-
* @param {string} value
|
|
406
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineCap
|
|
407
|
-
*/
|
|
408
|
-
linecap(value) {
|
|
409
|
-
_ctx.lineCap = value
|
|
410
|
-
},
|
|
411
|
-
|
|
412
|
-
/**
|
|
413
|
-
* Determines the shape used to join two line segments where they meet
|
|
414
|
-
* Possible values are: "round", "bevel", and "miter"
|
|
415
|
-
*
|
|
416
|
-
* @param {string} value
|
|
417
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/lineJoin
|
|
418
|
-
*/
|
|
419
|
-
linejoin(value) {
|
|
420
|
-
_ctx.lineJoin = value
|
|
421
|
-
},
|
|
422
|
-
|
|
423
371
|
/** TEXT RENDERING API */
|
|
424
372
|
/**
|
|
425
373
|
* Draw text
|
|
@@ -702,17 +650,6 @@ export default function litecanvas(settings = {}) {
|
|
|
702
650
|
_ctx.globalCompositeOperation = value
|
|
703
651
|
},
|
|
704
652
|
|
|
705
|
-
/**
|
|
706
|
-
* Provides filter effects such as blurring and grayscaling.
|
|
707
|
-
* It is similar to the CSS filter property and accepts the same values.
|
|
708
|
-
*
|
|
709
|
-
* @param {string} effect
|
|
710
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/filter
|
|
711
|
-
*/
|
|
712
|
-
filter(effect) {
|
|
713
|
-
_ctx.filter = effect
|
|
714
|
-
},
|
|
715
|
-
|
|
716
653
|
/** SOUND API */
|
|
717
654
|
/**
|
|
718
655
|
* Play a defined sound or a ZzFX array of params
|
|
@@ -776,29 +713,53 @@ export default function litecanvas(settings = {}) {
|
|
|
776
713
|
colcirc: (x1, y1, r1, x2, y2, r2) =>
|
|
777
714
|
(x2 - x1) ** 2 + (y2 - y1) ** 2 <= (r1 + r2) ** 2,
|
|
778
715
|
|
|
716
|
+
/**
|
|
717
|
+
* Get the mouse position
|
|
718
|
+
* @returns number[]
|
|
719
|
+
*/
|
|
720
|
+
mousepos: () => [_mouseX, _mouseY],
|
|
721
|
+
|
|
779
722
|
/** PLUGINS API */
|
|
780
723
|
/**
|
|
781
724
|
* Prepares a plugin to be loaded
|
|
782
725
|
*
|
|
783
726
|
* @param {pluginCallback} callback
|
|
784
727
|
*/
|
|
785
|
-
use: (callback) => {
|
|
728
|
+
use: (callback, config = {}) => {
|
|
729
|
+
callback.__config = config
|
|
786
730
|
_initialized ? loadPlugin(callback) : _plugins.push(callback)
|
|
787
731
|
},
|
|
788
732
|
|
|
789
733
|
/**
|
|
790
|
-
* Add a game
|
|
734
|
+
* Add a game event listener
|
|
791
735
|
*
|
|
792
|
-
* @param {string} event
|
|
736
|
+
* @param {string} event the event type name
|
|
793
737
|
* @param {function} callback the function that is called when the event occurs
|
|
794
738
|
* @param {boolean} [highPriority=false] determines whether the callback will be called before or after the others
|
|
795
|
-
* @returns {function
|
|
739
|
+
* @returns {function} a function to remove the listener
|
|
796
740
|
*/
|
|
797
741
|
listen(event, callback, highPriority = false) {
|
|
798
|
-
|
|
799
|
-
|
|
742
|
+
_events[event] = _events[event] || []
|
|
743
|
+
_events[event][highPriority ? 'unshift' : 'push'](callback)
|
|
744
|
+
|
|
745
|
+
// return a function to remove this event listener
|
|
800
746
|
return () => {
|
|
801
|
-
|
|
747
|
+
_events[event] = _events[event].filter(
|
|
748
|
+
(item) => item !== callback
|
|
749
|
+
)
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Call all listeners attached to a game event
|
|
755
|
+
*
|
|
756
|
+
* @param {string} event The game event type
|
|
757
|
+
* @param {...any} args Arguments passed to all listeners
|
|
758
|
+
*/
|
|
759
|
+
emit(event, ...args) {
|
|
760
|
+
if (!_events[event]) return
|
|
761
|
+
for (let i = 0; i < _events[event].length; ++i) {
|
|
762
|
+
_events[event][i](...args)
|
|
802
763
|
}
|
|
803
764
|
},
|
|
804
765
|
|
|
@@ -836,10 +797,6 @@ export default function litecanvas(settings = {}) {
|
|
|
836
797
|
},
|
|
837
798
|
}
|
|
838
799
|
|
|
839
|
-
// alias
|
|
840
|
-
instance.cls = instance.clear
|
|
841
|
-
instance.print = instance.text
|
|
842
|
-
|
|
843
800
|
/** Copy some functions from native `Math` object */
|
|
844
801
|
for (const k of [
|
|
845
802
|
'sin',
|
|
@@ -860,74 +817,30 @@ export default function litecanvas(settings = {}) {
|
|
|
860
817
|
'exp',
|
|
861
818
|
]) {
|
|
862
819
|
// import some native Math functions
|
|
863
|
-
instance[k] =
|
|
820
|
+
instance[k] = Math[k]
|
|
864
821
|
}
|
|
865
822
|
|
|
866
823
|
function init() {
|
|
867
824
|
_initialized = true
|
|
868
825
|
setupCanvas()
|
|
869
826
|
|
|
870
|
-
|
|
871
|
-
const _tappedLimit = 200
|
|
872
|
-
const _getXY = (event) => {
|
|
873
|
-
return _hasMouse
|
|
874
|
-
? [event.pageX, event.pageY]
|
|
875
|
-
: [event.touches[0].pageX, event.touches[0].pageY]
|
|
876
|
-
},
|
|
877
|
-
_eventTapStart = _hasMouse ? 'mousedown' : 'touchstart',
|
|
878
|
-
_eventTapEnd = _hasMouse ? 'mouseup' : 'touchend',
|
|
879
|
-
_eventTapMove = _hasMouse ? 'mousemove' : 'touchmove'
|
|
880
|
-
|
|
881
|
-
let _tapStartX, _tapStartY, _last, _start
|
|
882
|
-
|
|
883
|
-
_tappingHandler = (ev) => {
|
|
884
|
-
updateTapping(true, ..._getXY(ev))
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
on(instance.CANVAS, _eventTapStart, function (ev) {
|
|
888
|
-
ev.preventDefault()
|
|
889
|
-
if (!_rafid) _resume()
|
|
890
|
-
|
|
891
|
-
on(body, _eventTapMove, _tappingHandler)
|
|
892
|
-
const [x, y] = ([_tapStartX, _tapStartY] = _getXY(ev))
|
|
893
|
-
updateTapping(true, x, y)
|
|
894
|
-
_last = _start = time()
|
|
895
|
-
})
|
|
896
|
-
|
|
897
|
-
on(instance.CANVAS, _eventTapEnd, function (ev) {
|
|
898
|
-
off(body, _eventTapMove, _tappingHandler)
|
|
899
|
-
updateTapping(false)
|
|
900
|
-
|
|
901
|
-
if (time() - _start <= _tappedLimit) {
|
|
902
|
-
updateTapped(true, _tapStartX, _tapStartY)
|
|
903
|
-
}
|
|
904
|
-
})
|
|
905
|
-
}
|
|
906
|
-
|
|
907
|
-
on(root, 'focus', () => {
|
|
908
|
-
if (!_rafid) _resume()
|
|
909
|
-
})
|
|
910
|
-
|
|
911
|
-
function _resume() {
|
|
912
|
-
_lastFrame = time()
|
|
913
|
-
_rafid = requestAnimationFrame(frame)
|
|
914
|
-
}
|
|
915
|
-
|
|
827
|
+
// pause on page blur
|
|
916
828
|
on(root, 'blur', () => {
|
|
917
829
|
cancelAnimationFrame(_rafid)
|
|
918
830
|
_rafid = 0
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
)
|
|
925
|
-
|
|
831
|
+
})
|
|
832
|
+
|
|
833
|
+
// resume on page focus if paused
|
|
834
|
+
on(root, 'focus', () => {
|
|
835
|
+
if (!_rafid) {
|
|
836
|
+
_lastFrame = performance.now()
|
|
837
|
+
_rafid = requestAnimationFrame(drawFrame)
|
|
926
838
|
}
|
|
927
839
|
})
|
|
928
840
|
|
|
841
|
+
// listen the default events
|
|
929
842
|
const source = settings.loop ? settings.loop : root
|
|
930
|
-
for (const event in
|
|
843
|
+
for (const event in _events) {
|
|
931
844
|
if (source[event]) instance.listen(event, source[event])
|
|
932
845
|
}
|
|
933
846
|
|
|
@@ -936,26 +849,132 @@ export default function litecanvas(settings = {}) {
|
|
|
936
849
|
loadPlugin(_plugins[i])
|
|
937
850
|
}
|
|
938
851
|
|
|
939
|
-
// set canvas background color
|
|
940
|
-
if (NULL != _bg) {
|
|
941
|
-
// prettier-ignore
|
|
942
|
-
instance.CANVAS.style.background = instance.getcolor(_bg)
|
|
943
|
-
}
|
|
944
|
-
|
|
945
852
|
// listen window resize event
|
|
946
853
|
on(root, 'resize', pageResized)
|
|
947
854
|
pageResized()
|
|
948
855
|
|
|
949
856
|
// start the game loop
|
|
950
|
-
emit('init')
|
|
951
|
-
_lastFrame =
|
|
952
|
-
_rafid = requestAnimationFrame(
|
|
857
|
+
instance.emit('init')
|
|
858
|
+
_lastFrame = performance.now()
|
|
859
|
+
_rafid = requestAnimationFrame(drawFrame)
|
|
860
|
+
|
|
861
|
+
// default mouse/touch handlers
|
|
862
|
+
if (settings.tapEvents) {
|
|
863
|
+
const _getXY = (pageX, pageY) => [
|
|
864
|
+
(pageX - _canvas.offsetLeft) / _scale,
|
|
865
|
+
(pageY - _canvas.offsetTop) / _scale,
|
|
866
|
+
],
|
|
867
|
+
_taps = new Map(),
|
|
868
|
+
_registerTap = (id, x, y) => {
|
|
869
|
+
_taps.set(id, {
|
|
870
|
+
x,
|
|
871
|
+
y,
|
|
872
|
+
startX: x,
|
|
873
|
+
startY: y,
|
|
874
|
+
timestamp: performance.now(),
|
|
875
|
+
})
|
|
876
|
+
},
|
|
877
|
+
_updateTap = (id, x, y) => {
|
|
878
|
+
const tap = _taps.get(id)
|
|
879
|
+
tap.x = x
|
|
880
|
+
tap.y = y
|
|
881
|
+
},
|
|
882
|
+
_checkTapped = (tap) => performance.now() - tap.timestamp <= 200
|
|
883
|
+
|
|
884
|
+
let _pressingMouse = false
|
|
885
|
+
|
|
886
|
+
on(_canvas, 'mousedown', (ev) => {
|
|
887
|
+
ev.preventDefault()
|
|
888
|
+
const [x, y] = _getXY(ev.pageX, ev.pageY)
|
|
889
|
+
instance.emit('tap', x, y, 0)
|
|
890
|
+
_registerTap(0, x, y)
|
|
891
|
+
_pressingMouse = true
|
|
892
|
+
})
|
|
893
|
+
|
|
894
|
+
on(_canvas, 'mousemove', (ev) => {
|
|
895
|
+
ev.preventDefault()
|
|
896
|
+
const [x, y] = ([_mouseX, _mouseY] = _getXY(ev.pageX, ev.pageY))
|
|
897
|
+
if (!_pressingMouse) return
|
|
898
|
+
instance.emit('tapping', x, y, 0)
|
|
899
|
+
_updateTap(0, x, y)
|
|
900
|
+
})
|
|
901
|
+
|
|
902
|
+
on(_canvas, 'mouseup', (ev) => {
|
|
903
|
+
ev.preventDefault()
|
|
904
|
+
const tap = _taps.get(0)
|
|
905
|
+
const [x, y] = _getXY(ev.pageX, ev.pageY)
|
|
906
|
+
if (_checkTapped(tap)) {
|
|
907
|
+
instance.emit('tapped', tap.startX, tap.startY, 0)
|
|
908
|
+
}
|
|
909
|
+
instance.emit('untap', x, y, 0)
|
|
910
|
+
_taps.delete(0)
|
|
911
|
+
_pressingMouse = false
|
|
912
|
+
})
|
|
913
|
+
|
|
914
|
+
on(_canvas, 'touchstart', (ev) => {
|
|
915
|
+
ev.preventDefault()
|
|
916
|
+
/** @type {TouchList} touches */
|
|
917
|
+
const touches = ev.changedTouches
|
|
918
|
+
for (let i = 0; i < touches.length; i++) {
|
|
919
|
+
const touch = touches[i]
|
|
920
|
+
const [x, y] = _getXY(touch.pageX, touch.pageY)
|
|
921
|
+
instance.emit('tap', x, y, touch.identifier + 1)
|
|
922
|
+
_registerTap(touch.identifier + 1, x, y)
|
|
923
|
+
}
|
|
924
|
+
})
|
|
925
|
+
|
|
926
|
+
on(_canvas, 'touchmove', (ev) => {
|
|
927
|
+
ev.preventDefault()
|
|
928
|
+
/** @type {TouchList} touches */
|
|
929
|
+
const touches = ev.changedTouches
|
|
930
|
+
for (let i = 0; i < touches.length; i++) {
|
|
931
|
+
const touch = touches[i]
|
|
932
|
+
const [x, y] = _getXY(touch.pageX, touch.pageY)
|
|
933
|
+
instance.emit('tapping', x, y, touch.identifier + 1)
|
|
934
|
+
_updateTap(touch.identifier + 1, x, y)
|
|
935
|
+
}
|
|
936
|
+
})
|
|
937
|
+
|
|
938
|
+
const _touchEndHandler = (ev) => {
|
|
939
|
+
ev.preventDefault()
|
|
940
|
+
const existing = []
|
|
941
|
+
|
|
942
|
+
if (ev.targetTouches.length > 0) {
|
|
943
|
+
for (const touch of ev.targetTouches) {
|
|
944
|
+
existing.push(touch.identifier + 1)
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
for (const [id, tap] of _taps) {
|
|
949
|
+
if (existing.includes(id)) continue
|
|
950
|
+
if (_checkTapped(tap)) {
|
|
951
|
+
instance.emit('tapped', tap.startX, tap.startY, id)
|
|
952
|
+
}
|
|
953
|
+
instance.emit('untap', tap.x, tap.y, id)
|
|
954
|
+
_taps.delete(id)
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
on(_canvas, 'touchend', _touchEndHandler)
|
|
959
|
+
on(_canvas, 'touchcancel', _touchEndHandler)
|
|
960
|
+
|
|
961
|
+
on(root, 'blur', () => {
|
|
962
|
+
_pressingMouse = false
|
|
963
|
+
|
|
964
|
+
if (_taps.size === 0) return
|
|
965
|
+
|
|
966
|
+
for (const [id, tap] of _taps) {
|
|
967
|
+
instance.emit('untap', tap.x, tap.y, id)
|
|
968
|
+
_taps.delete(id)
|
|
969
|
+
}
|
|
970
|
+
})
|
|
971
|
+
}
|
|
953
972
|
}
|
|
954
973
|
|
|
955
974
|
/**
|
|
956
975
|
* @param {number} now
|
|
957
976
|
*/
|
|
958
|
-
function
|
|
977
|
+
function drawFrame(now) {
|
|
959
978
|
let ticks = 0,
|
|
960
979
|
t = now - _lastFrame
|
|
961
980
|
|
|
@@ -963,17 +982,15 @@ export default function litecanvas(settings = {}) {
|
|
|
963
982
|
_accumulated += t
|
|
964
983
|
|
|
965
984
|
while (_accumulated >= _stepMs) {
|
|
966
|
-
|
|
967
|
-
emit('update', _step)
|
|
985
|
+
instance.emit('update', _step)
|
|
968
986
|
instance.setvar('ELAPSED', instance.ELAPSED + _step)
|
|
969
987
|
_accumulated -= _stepMs
|
|
970
988
|
ticks++
|
|
971
|
-
instance.setvar('TAPPED', false)
|
|
972
989
|
}
|
|
973
990
|
|
|
974
991
|
if (ticks) {
|
|
975
992
|
_drawCount++
|
|
976
|
-
emit('draw')
|
|
993
|
+
instance.emit('draw')
|
|
977
994
|
_drawTime += _stepMs * ticks
|
|
978
995
|
if (_drawTime + _accumulated >= 1000) {
|
|
979
996
|
instance.setvar('FPS', _drawCount)
|
|
@@ -982,7 +999,7 @@ export default function litecanvas(settings = {}) {
|
|
|
982
999
|
}
|
|
983
1000
|
}
|
|
984
1001
|
|
|
985
|
-
if (_rafid) _rafid = requestAnimationFrame(
|
|
1002
|
+
if (_rafid) _rafid = requestAnimationFrame(drawFrame)
|
|
986
1003
|
}
|
|
987
1004
|
|
|
988
1005
|
function setupCanvas() {
|
|
@@ -1000,7 +1017,7 @@ export default function litecanvas(settings = {}) {
|
|
|
1000
1017
|
_canvas.width = instance.WIDTH
|
|
1001
1018
|
_canvas.height = instance.HEIGHT || instance.WIDTH
|
|
1002
1019
|
|
|
1003
|
-
if (!_canvas.parentNode) body.appendChild(_canvas)
|
|
1020
|
+
if (!_canvas.parentNode) document.body.appendChild(_canvas)
|
|
1004
1021
|
|
|
1005
1022
|
if (!settings.antialias || settings.pixelart) {
|
|
1006
1023
|
_ctx.imageSmoothingEnabled = false
|
|
@@ -1024,11 +1041,11 @@ export default function litecanvas(settings = {}) {
|
|
|
1024
1041
|
instance.setvar('WIDTH', innerWidth)
|
|
1025
1042
|
instance.setvar('HEIGHT', innerHeight)
|
|
1026
1043
|
} else if (_autoscale) {
|
|
1027
|
-
_scale =
|
|
1044
|
+
_scale = Math.min(
|
|
1028
1045
|
innerWidth / instance.WIDTH,
|
|
1029
|
-
innerHeight / instance.HEIGHT
|
|
1046
|
+
innerHeight / instance.HEIGHT
|
|
1030
1047
|
)
|
|
1031
|
-
_scale = settings.pixelart ?
|
|
1048
|
+
_scale = settings.pixelart ? Math.floor(_scale) : _scale
|
|
1032
1049
|
_canvas.style.width = instance.WIDTH * _scale + 'px'
|
|
1033
1050
|
_canvas.style.height = instance.HEIGHT * _scale + 'px'
|
|
1034
1051
|
}
|
|
@@ -1036,42 +1053,17 @@ export default function litecanvas(settings = {}) {
|
|
|
1036
1053
|
instance.setvar('CENTERX', instance.WIDTH / 2)
|
|
1037
1054
|
instance.setvar('CENTERY', instance.HEIGHT / 2)
|
|
1038
1055
|
|
|
1039
|
-
_offsetTop = _canvas.offsetTop
|
|
1040
|
-
_offsetLeft = _canvas.offsetLeft
|
|
1041
|
-
|
|
1042
1056
|
// fix the font align and baseline
|
|
1043
1057
|
instance.textalign(_textAlign, _textBaseline)
|
|
1044
1058
|
|
|
1045
|
-
emit('resized')
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
/**
|
|
1049
|
-
* @param {boolean} tapped
|
|
1050
|
-
* @param {number} x
|
|
1051
|
-
* @param {number} y
|
|
1052
|
-
*/
|
|
1053
|
-
function updateTapped(tapped, x, y) {
|
|
1054
|
-
instance.setvar('TAPPED', tapped)
|
|
1055
|
-
instance.setvar('TAPX', (x - _offsetLeft) / _scale)
|
|
1056
|
-
instance.setvar('TAPY', (y - _offsetTop) / _scale)
|
|
1057
|
-
}
|
|
1058
|
-
|
|
1059
|
-
/**
|
|
1060
|
-
* @param {boolean} tapped
|
|
1061
|
-
* @param {number} x
|
|
1062
|
-
* @param {number} y
|
|
1063
|
-
*/
|
|
1064
|
-
function updateTapping(tapped, x, y) {
|
|
1065
|
-
instance.setvar('TAPPING', tapped)
|
|
1066
|
-
instance.setvar('TAPX', (x - _offsetLeft) / _scale)
|
|
1067
|
-
instance.setvar('TAPY', (y - _offsetTop) / _scale)
|
|
1059
|
+
instance.emit('resized', _scale)
|
|
1068
1060
|
}
|
|
1069
1061
|
|
|
1070
1062
|
/**
|
|
1071
1063
|
* @param {pluginCallback} callback
|
|
1072
1064
|
*/
|
|
1073
1065
|
function loadPlugin(callback) {
|
|
1074
|
-
const pluginData = callback(instance, _helpers)
|
|
1066
|
+
const pluginData = callback(instance, _helpers, callback.__config)
|
|
1075
1067
|
if ('object' === typeof pluginData) {
|
|
1076
1068
|
for (const key in pluginData) {
|
|
1077
1069
|
instance.setvar(key, pluginData[key])
|
|
@@ -1079,19 +1071,6 @@ export default function litecanvas(settings = {}) {
|
|
|
1079
1071
|
}
|
|
1080
1072
|
}
|
|
1081
1073
|
|
|
1082
|
-
/**
|
|
1083
|
-
* Call all listeners attached to a game loop
|
|
1084
|
-
*
|
|
1085
|
-
* @param {string} event The game loop event
|
|
1086
|
-
* @param {...any} args Arguments passed to all functions
|
|
1087
|
-
*/
|
|
1088
|
-
function emit(event, ...args) {
|
|
1089
|
-
if (!_loop[event]) return
|
|
1090
|
-
for (let i = 0; i < _loop[event].length; ++i) {
|
|
1091
|
-
_loop[event][i](...args)
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
|
|
1095
1074
|
if (settings.global) {
|
|
1096
1075
|
if (root.__litecanvas) {
|
|
1097
1076
|
throw new Error('Cannot instantiate litecanvas globally twice')
|
package/src/palette.js
CHANGED
package/src/sounds.js
CHANGED