Skip to content

Commit fe61431

Browse files
committed
v0.99
* add `stat(12)` * internal code improvements
1 parent a55dfab commit fe61431

File tree

9 files changed

+103
-67
lines changed

9 files changed

+103
-67
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.98.4",
3+
"version": "0.99.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>",

samples/fullscreen/fullscreen.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ litecanvas({
66
let fullscreen
77

88
function resized() {
9-
// fix: HTML canvas reset the text align and baseline
10-
// when the canvas resizes
9+
// the canvas resets the text align
10+
// and baseline when the canvas resizes
1111
textalign('start', 'top')
12-
console.log('resized')
1312
}
1413

1514
function update() {

src/index.js

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ export default function litecanvas(settings = {}) {
4848

4949
let /** @type {boolean} */
5050
_initialized = false,
51+
/** @type {boolean} */
52+
_paused = true,
5153
/** @type {HTMLCanvasElement} _canvas */
5254
_canvas,
5355
/** @type {number} */
@@ -1056,6 +1058,7 @@ export default function litecanvas(settings = {}) {
10561058
'string' === typeof eventName,
10571059
'[litecanvas] emit() 1st param must be a string'
10581060
)
1061+
10591062
if (_initialized) {
10601063
eventName = lowerCase(eventName)
10611064

@@ -1156,7 +1159,7 @@ export default function litecanvas(settings = {}) {
11561159
},
11571160

11581161
/**
1159-
* Returns information about that engine instance.
1162+
* Returns information about the engine instance.
11601163
*
11611164
* @param {number|string} index
11621165
* @returns {any}
@@ -1190,8 +1193,10 @@ export default function litecanvas(settings = {}) {
11901193
_rngSeed,
11911194
// 10
11921195
_fontSize,
1193-
// 11
1196+
// 11
11941197
_fontFamily,
1198+
// 12
1199+
_colorPaletteState,
11951200
]
11961201

11971202
const data = { index, value: internals[index] }
@@ -1202,49 +1207,24 @@ export default function litecanvas(settings = {}) {
12021207
return data.value
12031208
},
12041209

1205-
/**
1206-
* Stops the litecanvas instance and remove all event listeners.
1207-
*/
1208-
quit() {
1209-
// stop the game loop (update & draw)
1210-
instance.pause()
1211-
1212-
// emit "quit" event to manual clean ups
1213-
instance.emit('quit')
1214-
1215-
// clear all engine event listeners
1216-
_eventListeners = {}
1217-
1218-
// clear all browser event listeners
1219-
for (const removeListener of _browserEventListeners) {
1220-
removeListener()
1221-
}
1222-
1223-
// maybe clear global context
1224-
if (settings.global) {
1225-
for (const key in instance) {
1226-
delete root[key]
1227-
}
1228-
delete root.ENGINE
1229-
}
1230-
1231-
// unset that flag
1232-
_initialized = false
1233-
},
1234-
12351210
/**
12361211
* Pauses the engine loop (update & draw).
12371212
*/
12381213
pause() {
1214+
_paused = true
12391215
cancelAnimationFrame(_rafid)
1240-
_rafid = 0
12411216
},
12421217

12431218
/**
12441219
* Resumes (if paused) the engine loop.
12451220
*/
12461221
resume() {
1247-
if (_initialized && !_rafid) {
1222+
DEV: assert(
1223+
_initialized,
1224+
'[litecanvas] resume() cannot be called before the "init" event and neither after the quit() function'
1225+
)
1226+
if (_initialized && _paused) {
1227+
_paused = false
12481228
_accumulated = _fpsInterval
12491229
_lastFrameTime = Date.now()
12501230
_rafid = raf(drawFrame)
@@ -1257,7 +1237,37 @@ export default function litecanvas(settings = {}) {
12571237
* @returns {boolean}
12581238
*/
12591239
paused() {
1260-
return !_rafid
1240+
return _paused
1241+
},
1242+
1243+
/**
1244+
* Shutdown the litecanvas instance and remove all event listeners.
1245+
*/
1246+
quit() {
1247+
// emit "quit" event to manual clean ups
1248+
instance.emit('quit')
1249+
1250+
// stop the game loop (update & draw)
1251+
instance.pause()
1252+
1253+
// deinitialize the engine
1254+
_initialized = false
1255+
1256+
// clear all engine event listeners
1257+
_eventListeners = {}
1258+
1259+
// clear all browser event listeners
1260+
for (const removeListener of _browserEventListeners) {
1261+
removeListener()
1262+
}
1263+
1264+
// maybe clear global context
1265+
if (settings.global) {
1266+
for (const key in instance) {
1267+
delete root[key]
1268+
}
1269+
delete root.ENGINE
1270+
}
12611271
},
12621272
}
12631273

@@ -1655,6 +1665,8 @@ export default function litecanvas(settings = {}) {
16551665
// trigger "resized" event
16561666
// note: not triggered before the "init" event
16571667
instance.emit('resized', _scale)
1668+
1669+
DEV: console.warn('[litecanvas] quit() terminated a Litecanvas instance.')
16581670
}
16591671

16601672
/**
@@ -1711,9 +1723,11 @@ export default function litecanvas(settings = {}) {
17111723
setupCanvas()
17121724

17131725
if ('loading' === document.readyState) {
1714-
on(root, 'DOMContentLoaded', () => raf(init))
1726+
on(root, 'DOMContentLoaded', () => {
1727+
raf(init)
1728+
})
17151729
} else {
1716-
raf(init)
1730+
_rafid = raf(init)
17171731
}
17181732

17191733
return instance

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.

tests/cls.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import * as sinon from 'sinon'
77
let /** @type {LitecanvasInstance} */
88
local,
99
/** @type {sinon.SinonSpiedInstance<CanvasRenderingContext2D>} */
10-
contextSpy,
11-
/** @type {sinon.SinonSpiedInstance<Console>} */
12-
consoleSpy
10+
contextSpy
1311

1412
test.before(() => {
1513
setupDOM()

tests/quit.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,17 @@ test('remove all engine event listeners', async (t) => {
3636
global: false,
3737
})
3838

39-
local.listen('draw', () => {
40-
t.fail()
41-
})
42-
4339
local.listen('update', () => {
40+
resolve()
41+
4442
// "update" runs before the "draw" event
45-
// so destroy lets destroy that engine instance
43+
// so lets destroy that engine instance
4644
// and the "draw" event will never happens
4745
local.quit()
48-
resolve()
46+
})
47+
48+
local.listen('draw', () => {
49+
t.fail()
4950
})
5051
})
5152

tests/stat.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,28 @@ test('stat(11) returns the current font family', async (t) => {
211211
}
212212
})
213213

214+
test('stat(12) returns the current color palette state modified by palc()', (t) => {
215+
{
216+
const expected = [] // initial value (default)
217+
const actual = local.stat(12)
218+
t.deepEqual(actual, expected)
219+
}
220+
221+
{
222+
// prettier-ignore
223+
const expected = [3, , , 5,]
224+
225+
local.palc(0, 3)
226+
local.palc(3, 5)
227+
228+
const actual = local.stat(12)
229+
230+
t.deepEqual(actual, expected)
231+
232+
local.palc() // reset
233+
}
234+
})
235+
214236
test('stat modified via event', async (t) => {
215237
const expected = 'BAR'
216238

types/global.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,9 @@ declare global {
583583
*/
584584
function framerate(fps: number): void
585585
/**
586-
* Returns information about that engine instance.
586+
* Returns information about the engine instance.
587587
*
588-
* - n = 0: the settings passed to that instance
588+
* - n = 0: the settings passed to this instance
589589
* - n = 1: returns true if the "init" event has already been emitted
590590
* - n = 2: the current delta time (dt)
591591
* - n = 3: the current canvas element scale (not the context 2D scale)
@@ -597,15 +597,12 @@ declare global {
597597
* - n = 9: the current RNG state
598598
* - n = 10: the current font size
599599
* - n = 11: the current font family
600+
* - n = 12: the current state of the color palette
600601
* - n = *any other value*: probably returns undefined
601602
*
602-
* @param n
603+
* @param index
603604
*/
604-
function stat(n: number): any
605-
/**
606-
* Shutdown the litecanvas instance and remove all event listeners.
607-
*/
608-
function quit(): void
605+
function stat(index: number | string): any
609606
/**
610607
* Pauses the engine loop (update & draw).
611608
*/
@@ -618,4 +615,8 @@ declare global {
618615
* Returns `true` if the engine loop is paused.
619616
*/
620617
function paused(): boolean
618+
/**
619+
* Shutdown the litecanvas instance and remove all event listeners.
620+
*/
621+
function quit(): void
621622
}

types/types.d.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,9 +573,9 @@ type LitecanvasInstance = {
573573
*/
574574
framerate(fps: number): void
575575
/**
576-
* Returns information about that engine instance.
576+
* Returns information about the engine instance.
577577
*
578-
* - n = 0: the settings passed to that instance
578+
* - n = 0: the settings passed to this instance
579579
* - n = 1: returns true if the "init" event has already been emitted
580580
* - n = 2: the current delta time (dt)
581581
* - n = 3: the current canvas element scale (not the context 2D scale)
@@ -587,15 +587,12 @@ type LitecanvasInstance = {
587587
* - n = 9: the current RNG state
588588
* - n = 10: the current font size
589589
* - n = 11: the current font family
590+
* - n = 12: the current state of the color palette
590591
* - n = *any other value*: probably returns undefined
591592
*
592-
* @param n
593+
* @param index
593594
*/
594-
stat(n: number): any
595-
/**
596-
* Stops the litecanvas instance and remove all event listeners.
597-
*/
598-
quit(): void
595+
stat(index: number | string): any
599596
/**
600597
* Pauses the engine loop (update & draw).
601598
*/
@@ -608,6 +605,10 @@ type LitecanvasInstance = {
608605
* Returns `true` if the engine loop is paused.
609606
*/
610607
paused(): boolean
608+
/**
609+
* Shutdown the litecanvas instance and remove all event listeners.
610+
*/
611+
quit(): void
611612
}
612613

613614
type LitecanvasOptions = {

0 commit comments

Comments
 (0)