Skip to content

Commit 5ec3578

Browse files
committed
v0.201
* emit() refactored
1 parent c4fdfdd commit 5ec3578

File tree

5 files changed

+74
-51
lines changed

5 files changed

+74
-51
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "litecanvas",
3-
"version": "0.200.0",
3+
"version": "0.201.0",
44
"description": "Lightweight HTML5 canvas 2D game engine suitable for small projects and creative coding. Inspired by PICO-8 and p5.js/Processing.",
55
"license": "MIT",
66
"author": "Luiz Bills <luizbills@pm.me>",

src/index.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ export default function litecanvas(settings = {}) {
8686
_colorPaletteState = [],
8787
/** @type {number[]} */
8888
_defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1],
89-
/** @type {string} */
90-
_coreEvents = 'init,update,draw,tap,untap,tapping,tapped,resized',
9189
/** @type {string} list of functions copied from `Math` module*/
9290
_mathFunctions =
9391
'PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp',
@@ -1075,7 +1073,7 @@ export default function litecanvas(settings = {}) {
10751073
},
10761074

10771075
/**
1078-
* Add a game event listener
1076+
* Add a game event listener.
10791077
*
10801078
* @param {string} eventName the event type name
10811079
* @param {Function} callback the function that is called when the event occurs
@@ -1097,7 +1095,7 @@ export default function litecanvas(settings = {}) {
10971095
},
10981096

10991097
/**
1100-
* Remove a game event listener
1098+
* Remove a game event listener.
11011099
*
11021100
* @param {string} eventName the event type name
11031101
* @param {Function} callback the function that is called when the event occurs
@@ -1120,13 +1118,17 @@ export default function litecanvas(settings = {}) {
11201118
},
11211119

11221120
/**
1123-
* Call all listeners attached to a game event
1121+
* Call all listeners attached to a game event.
1122+
*
1123+
* Note: when the `litecanvas()` "loop" option is `null` (default),
1124+
* `emit()` will first call a global function matching the event name (if it exists).
1125+
* E.g: `emit("boom")` calls `window.boom()`.
11241126
*
11251127
* @param {string} eventName The event type name
1126-
* @param {*} [arg1] any data to be passed over the listeners
1127-
* @param {*} [arg2] any data to be passed over the listeners
1128-
* @param {*} [arg3] any data to be passed over the listeners
1129-
* @param {*} [arg4] any data to be passed over the listeners
1128+
* @param {any} [arg1] any data to be passed over the listeners
1129+
* @param {any} [arg2] any data to be passed over the listeners
1130+
* @param {any} [arg3] any data to be passed over the listeners
1131+
* @param {any} [arg4] any data to be passed over the listeners
11301132
*/
11311133
emit(eventName, arg1, arg2, arg3, arg4) {
11321134
DEV: assert(
@@ -1138,7 +1140,12 @@ export default function litecanvas(settings = {}) {
11381140
eventName = lowerCase(eventName)
11391141

11401142
triggerEvent('before:' + eventName, arg1, arg2, arg3, arg4)
1143+
1144+
if (!settings.loop && 'function' === typeof root[eventName]) {
1145+
root[eventName](arg1, arg2, arg3, arg4)
1146+
}
11411147
triggerEvent(eventName, arg1, arg2, arg3, arg4)
1148+
11421149
triggerEvent('after:' + eventName, arg1, arg2, arg3, arg4)
11431150
}
11441151
},
@@ -1194,8 +1201,13 @@ export default function litecanvas(settings = {}) {
11941201
/**
11951202
* Define or update a instance property.
11961203
*
1197-
* @param {string} key
1198-
* @param {*} value
1204+
* Note: when the `litecanvas()` option "global" is `true` (default),
1205+
* `def()` with set/update a global property.
1206+
*
1207+
* E.g: `def('ONE', 1)` do `window.ONE = 1`.
1208+
*
1209+
* @param {string} key the property name
1210+
* @param {any} value the property value
11991211
*/
12001212
def(key, value) {
12011213
DEV: assert('string' === typeof key, loggerPrefix + 'def() 1st param must be a string')
@@ -1682,9 +1694,9 @@ export default function litecanvas(settings = {}) {
16821694
_canvas = _canvas || document.createElement('canvas')
16831695

16841696
DEV: assert(
1685-
'CANVAS' === _canvas.tagName,
1697+
_canvas instanceof HTMLElement && 'CANVAS' === _canvas.tagName,
16861698
loggerPrefix +
1687-
'litecanvas() option "canvas" should be a canvas element or string (CSS selector)'
1699+
'litecanvas() option "canvas" should be a canvas element or string (CSS selector of a canvas)'
16881700
)
16891701

16901702
_ctx = _canvas.getContext('2d')
@@ -1698,6 +1710,8 @@ export default function litecanvas(settings = {}) {
16981710
}
16991711

17001712
_canvas.style.imageRendering = 'pixelated'
1713+
1714+
// disable default browser's right click in canvas
17011715
_canvas.oncontextmenu = () => false
17021716
}
17031717

@@ -1752,21 +1766,22 @@ export default function litecanvas(settings = {}) {
17521766

17531767
/**
17541768
* @param {string} eventName
1755-
* @param {*} arg1
1756-
* @param {*} arg2
1757-
* @param {*} arg3
1758-
* @param {*} arg4
1769+
* @param {any} [arg1]
1770+
* @param {any} [arg2]
1771+
* @param {any} [arg3]
1772+
* @param {any} [arg4]
17591773
*/
17601774
function triggerEvent(eventName, arg1, arg2, arg3, arg4) {
1761-
if (!_eventListeners[eventName]) return
1762-
for (const callback of _eventListeners[eventName]) {
1763-
callback(arg1, arg2, arg3, arg4)
1775+
if (_eventListeners[eventName]) {
1776+
for (const callback of _eventListeners[eventName]) {
1777+
callback(arg1, arg2, arg3, arg4)
1778+
}
17641779
}
17651780
}
17661781

17671782
/**
17681783
* @param {pluginCallback} callback
1769-
* @param {*} config
1784+
* @param {any} config
17701785
*/
17711786
function loadPlugin(callback, config) {
17721787
const pluginData = callback(instance, config)
@@ -1805,12 +1820,10 @@ export default function litecanvas(settings = {}) {
18051820
setupCanvas()
18061821

18071822
// setup default event listeners
1808-
const source = settings.loop ? settings.loop : root
1809-
for (const event of _coreEvents.split(',')) {
1810-
DEV: if (root === source && source[event]) {
1811-
console.info(loggerPrefix + `using window.${event}()`)
1823+
if (settings.loop) {
1824+
for (const eventName in settings.loop) {
1825+
if (settings.loop[eventName]) instance.listen(eventName, settings.loop[eventName])
18121826
}
1813-
if (source[event]) instance.listen(event, source[event])
18141827
}
18151828

18161829
// init the engine (async)

src/version.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/global.d.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -547,21 +547,25 @@ declare global {
547547
*/
548548
function use(callback: pluginCallback): void
549549
/**
550-
* Add a game loop event listener
550+
* Add a game loop event listener.
551551
*
552552
* @param event The game event type
553553
* @param callback the function that is called when the event occurs
554554
*/
555555
function listen(event: string, callback: Function): void
556556
/**
557-
* Remove a game loop event listener
557+
* Remove a game loop event listener.
558558
*
559559
* @param event The game event type
560560
* @param callback the function that is called when the event occurs
561561
*/
562562
function unlisten(event: string, callback: Function): void
563563
/**
564-
* Call all listeners attached to a game event
564+
* Call all listeners attached to a game event.
565+
*
566+
* Note: when the `litecanvas()` "loop" option is `null` (default),
567+
* `emit()` will first call a global function matching the event name (if it exists).
568+
* E.g: `emit("boom", 10)` calls `window.boom(10)`.
565569
*
566570
* @param event The game event type
567571
* @param [arg1] any data to be passed over the listeners
@@ -589,10 +593,14 @@ declare global {
589593
*/
590594
function palc(a?: number, b?: number): void
591595
/**
592-
* Define or update a instance property
596+
* Define or update a instance property.
593597
*
594-
* @param key
595-
* @param value
598+
* Note: when the `litecanvas()` option "global" is `true` (default),
599+
* `def()` with set/update a global property.
600+
* E.g: `def('ONE', 1)` also do `window.ONE = 1`.
601+
*
602+
* @param key the property name
603+
* @param value the property value
596604
*/
597605
function def(key: string, value: any): void
598606
/**

types/types.d.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -531,21 +531,25 @@ type LitecanvasInstance = {
531531
*/
532532
use(callback: pluginCallback): void
533533
/**
534-
* Add a game loop event listener
534+
* Add a game loop event listener.
535535
*
536536
* @param event The game event type
537537
* @param callback the function that is called when the event occurs
538538
*/
539539
listen(event: string, callback: Function): void
540540
/**
541-
* Remove a game loop event listener
541+
* Remove a game loop event listener.
542542
*
543543
* @param event The game event type
544544
* @param callback the function that is called when the event occurs
545545
*/
546546
unlisten(event: string, callback: Function): void
547547
/**
548-
* Call all listeners attached to a game event
548+
* Call all listeners attached to a game event.
549+
*
550+
* Note: when the `litecanvas()` "loop" option is `null` (default),
551+
* `emit()` will first call a global function matching the event name (if it exists).
552+
* E.g: `emit("boom", 10)` calls `window.boom(10)`.
549553
*
550554
* @param event The game event type
551555
* @param [arg1] any data to be passed over the listeners
@@ -555,10 +559,14 @@ type LitecanvasInstance = {
555559
*/
556560
emit(event: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void
557561
/**
558-
* Define or update a instance property
562+
* Define or update a instance property.
559563
*
560-
* @param key
561-
* @param value
564+
* Note: when the `litecanvas()` option "global" is `true` (default),
565+
* `def()` with set/update a global property.
566+
* E.g: `def('ONE', 1)` also do `window.ONE = 1`.
567+
*
568+
* @param key the property name
569+
* @param value the property value
562570
*/
563571
def(key: string, value: any): void
564572
/**
@@ -658,19 +666,13 @@ type LitecanvasOptions = {
658666
*/
659667
global?: boolean
660668
/**
661-
* Specify your game loop callbacks.
662-
* By default use that global functions (if they exist):
663-
* - `window.init(instance: LitecanvasInstance): void`
664-
* - `window.update(dt: number): void`
665-
* - `window.draw(ctx: CanvasRenderingContext2D): void`
666-
* - `window.resized(scale: number): void`
667-
* - `window.tap(tapX: number, tapY: number, touchId: number): void`
668-
* - `window.untap(tapX: number, tapY: number, touchId: number): void`
669-
* - `window.tapped(tapX: number, tapY: number, touchId: number): void`
670-
* - `window.tapping(tapX: number, tapY: number, touchId: number): void`
669+
* Specify your game loop listener callbacks.
670+
*
671+
* By default, it uses global functions with the same name as the events (if they exist).
672+
*
673+
* Example: `window.init`, `window.update`, `window.draw`, etc
671674
*/
672675
loop?: LitecanvasGameLoop
673-
674676
/**
675677
* default: `true`
676678
*

0 commit comments

Comments
 (0)