Skip to content

Commit 249f5f9

Browse files
committed
add palc() and remove "pixelart" and "animate" options
1 parent 6cab6f0 commit 249f5f9

File tree

21 files changed

+95
-80
lines changed

21 files changed

+95
-80
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"test:watch": "ava --watch",
5656
"dev": "esbuild src/web.js --bundle --watch --outfile=samples/dist.js --servedir=samples",
5757
"build": "npm run genversion && node script/build.js && size-limit",
58-
"format": "prettier -w src/* samples/* types/* script/* types/*",
59-
"check-types": "npx ts types/*",
58+
"format": "prettier -w src/* samples/* types/* script/* tests/*",
59+
"check-types": "npx tsc ./types/*",
6060
"genversion": "genversion --es6 src/version.js"
6161
},
6262
"files": [

samples/colors/colors.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ litecanvas({
88
width: tile * cols,
99
height: tile * rows,
1010
autoscale: false,
11-
animate: false,
1211
canvas: 'canvas',
1312
})
1413

@@ -25,10 +24,11 @@ function draw() {
2524

2625
if (showLabel) {
2726
const label = c
28-
// text(i * tile + tile / 2, 2 + j * tile + tile / 2, label, fg)
27+
text(i * tile + tile / 2, 2 + j * tile + tile / 2, label, fg)
2928
}
3029
}
3130
}
31+
pause() // draw only once
3232
}
3333

3434
function tapped() {

samples/custom-canvas/custom-canvas.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
litecanvas({
22
canvas: '#game-canvas',
3-
animate: false,
43
})
54

65
function draw() {

samples/custom-palette/custom-palette.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
litecanvas({
22
width: 5,
3-
animate: false,
43
})
54

65
function init() {
@@ -40,4 +39,5 @@ function draw() {
4039
.b7d.
4140
..c..`
4241
)
42+
pause() // draw only once
4343
}

samples/logo/logo.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const url = new URL(location),
55
litecanvas({
66
width: size,
77
autoscale: false,
8-
animate: false,
98
})
109

1110
let scale = floor(size / minsize)
@@ -43,6 +42,13 @@ logo = paint(
4342
}
4443
)
4544

46-
// just draw once
47-
// function draw() is not necessary with `animate=false`
45+
// just draw once outside of the `draw()` function
4846
image(0, 0, logo)
47+
48+
// we don't need the game loop here, so lets pause it.
49+
// but we can immediately pause it, because the game loop has not started yet
50+
// so we pause the game loop in the first "update" event
51+
listen('before:update', pause)
52+
53+
// another alternative would be to leave the game with only 1 FPS
54+
// framerate(1)

samples/palette-dither-test/palette-dither-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ const size = 12
55
const cols = 12
66

77
litecanvas({
8-
animate: false,
98
width: size * cols,
109
height: size * (cols - 1),
1110
autoscale: zoomed,
1211
canvas: 'canvas',
1312
})
1413

1514
function init() {
15+
framerate(1)
1616
combinations = []
1717
for (let y = 0; y < cols; y++) {
1818
for (let x = 0; x < cols; x++) {

samples/pixelart/pixelart.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,22 @@ const palKulepu = [
3434
'#306645',
3535
]
3636

37-
let usingAltPal = false
37+
let swapColors = false
3838

3939
function tapped() {
40-
if (usingAltPal) {
41-
pal()
42-
} else {
43-
pal(palKulepu)
44-
}
45-
usingAltPal = !usingAltPal
40+
swapColors = !swapColors
4641
}
4742

4843
function draw() {
49-
cls(3)
44+
cls(1)
5045
push()
5146
scale(2)
47+
if (swapColors) {
48+
palc(0, 3) // swap the black with white
49+
}
5250
spr(0, 0, 8, 8, smile)
51+
if (swapColors) {
52+
palc(3, 0) // undo the swap
53+
}
5354
pop()
5455
}

samples/text/text.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
litecanvas({
22
width: innerWidth,
33
height: innerWidth * 0.75,
4-
animate: false,
54
})
65

76
function init() {

src/index.js

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export default function litecanvas(settings = {}) {
2323
elem.addEventListener(evt, callback, false)
2424
_browserEventListeners.push(() => elem.removeEventListener(evt, callback, false))
2525
},
26+
/** @type {(str: string) => string} */
27+
lowerCase = (str) => str.toLowerCase(),
2628
/** @type {(ev: Event) => void} */
2729
preventDefault = (ev) => ev.preventDefault(),
2830
/** @type {(c: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D) => void} */
@@ -34,13 +36,11 @@ export default function litecanvas(settings = {}) {
3436
width: null,
3537
height: null,
3638
autoscale: true,
37-
pixelart: true,
3839
canvas: null,
3940
global: true,
4041
loop: null,
4142
tapEvents: true,
4243
keyboardEvents: true,
43-
animate: true,
4444
}
4545

4646
// setup the settings default values
@@ -75,7 +75,9 @@ export default function litecanvas(settings = {}) {
7575
/** @type {number} */
7676
_rngSeed = Date.now(),
7777
/** @type {string[]} */
78-
_colors = defaultPalette,
78+
_currentPalette,
79+
/** @type {string[]} */
80+
_colors,
7981
/** @type {number[]} */
8082
_defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1],
8183
/** @type {string} */
@@ -1040,7 +1042,7 @@ export default function litecanvas(settings = {}) {
10401042
'[litecanvas] listen() 2nd param must be a function'
10411043
)
10421044

1043-
eventName = eventName.toLowerCase()
1045+
eventName = lowerCase(eventName)
10441046

10451047
_eventListeners[eventName] = _eventListeners[eventName] || new Set()
10461048
_eventListeners[eventName].add(callback)
@@ -1064,7 +1066,7 @@ export default function litecanvas(settings = {}) {
10641066
'[litecanvas] emit() 1st param must be a string'
10651067
)
10661068
if (_initialized) {
1067-
eventName = eventName.toLowerCase()
1069+
eventName = lowerCase(eventName)
10681070

10691071
triggerEvent('before:' + eventName, arg1, arg2, arg3, arg4)
10701072
triggerEvent(eventName, arg1, arg2, arg3, arg4)
@@ -1073,7 +1075,7 @@ export default function litecanvas(settings = {}) {
10731075
},
10741076

10751077
/**
1076-
* Set or reset the color palette
1078+
* Set or reset the color palette.
10771079
*
10781080
* @param {string[]} [colors]
10791081
*/
@@ -1083,6 +1085,31 @@ export default function litecanvas(settings = {}) {
10831085
'[litecanvas] pal() 1st param must be a array of strings'
10841086
)
10851087
_colors = colors
1088+
_currentPalette = [...colors]
1089+
},
1090+
1091+
/**
1092+
* Swap two colors of the current palette.
1093+
*
1094+
* If called without arguments, reset the current palette.
1095+
*
1096+
* @param {number?} a
1097+
* @param {number?} b
1098+
*/
1099+
palc(a, b) {
1100+
DEV: assert(
1101+
null == a || (isNumber(a) && a >= 0),
1102+
'[litecanvas] palc() 1st param must be a positive number'
1103+
)
1104+
DEV: assert(
1105+
isNumber(a) ? isNumber(b) && b >= 0 : null == b,
1106+
'[litecanvas] palc() 2nd param must be a positive number'
1107+
)
1108+
if (a == null) {
1109+
_colors = [..._currentPalette]
1110+
} else {
1111+
;[_colors[a], _colors[b]] = [_colors[b], _colors[a]]
1112+
}
10861113
},
10871114

10881115
/**
@@ -1460,15 +1487,15 @@ export default function litecanvas(settings = {}) {
14601487
* @returns {boolean}
14611488
*/
14621489
const keyCheck = (keySet, key = '') => {
1463-
key = key.toLowerCase()
1490+
key = lowerCase(key)
14641491
return !key ? keySet.size > 0 : keySet.has('space' === key ? ' ' : key)
14651492
}
14661493

14671494
/** @type {string} */
14681495
let _lastKey = ''
14691496

14701497
on(root, 'keydown', (/** @type {KeyboardEvent} */ event) => {
1471-
const key = event.key.toLowerCase()
1498+
const key = lowerCase(event.key)
14721499
if (!_keysDown.has(key)) {
14731500
_keysDown.add(key)
14741501
_keysPress.add(key)
@@ -1477,7 +1504,7 @@ export default function litecanvas(settings = {}) {
14771504
})
14781505

14791506
on(root, 'keyup', (/** @type {KeyboardEvent} */ event) => {
1480-
_keysDown.delete(event.key.toLowerCase())
1507+
_keysDown.delete(lowerCase(event.key))
14811508
})
14821509

14831510
on(root, 'blur', () => _keysDown.clear())
@@ -1529,9 +1556,8 @@ export default function litecanvas(settings = {}) {
15291556
}
15301557

15311558
function drawFrame() {
1532-
if (!settings.animate) {
1533-
return instance.emit('draw', _ctx)
1534-
}
1559+
// request the next frame
1560+
_rafid = raf(drawFrame)
15351561

15361562
let now = Date.now()
15371563
let updated = 0
@@ -1561,9 +1587,6 @@ export default function litecanvas(settings = {}) {
15611587
)
15621588
}
15631589
}
1564-
1565-
// request the next frame
1566-
_rafid = raf(drawFrame)
15671590
}
15681591

15691592
function setupCanvas() {
@@ -1638,22 +1661,15 @@ export default function litecanvas(settings = {}) {
16381661
}
16391662

16401663
// set canvas image rendering properties
1641-
if (settings.pixelart) {
1642-
_ctx.imageSmoothingEnabled = false
1643-
_canvas.style.imageRendering = 'pixelated'
1644-
}
1664+
_ctx.imageSmoothingEnabled = false
1665+
_canvas.style.imageRendering = 'pixelated'
16451666

16461667
// set the default text align and baseline
16471668
instance.textalign('start', 'top')
16481669

16491670
// trigger "resized" event
16501671
// note: not triggered before the "init" event
16511672
instance.emit('resized', _scale)
1652-
1653-
// force redraw when the canvas is not animated
1654-
if (!settings.animate) {
1655-
raf(drawFrame)
1656-
}
16571673
}
16581674

16591675
/**
@@ -1700,6 +1716,9 @@ export default function litecanvas(settings = {}) {
17001716

17011717
setupCanvas()
17021718

1719+
// init the color palette
1720+
instance.pal()
1721+
17031722
if ('loading' === document.readyState) {
17041723
on(root, 'DOMContentLoaded', () => raf(init))
17051724
} else {

tests/math.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ test.before(() => {
1111
sinon.stub(console) // silent console
1212

1313
local = litecanvas({
14-
animate: false,
1514
global: false,
1615
})
1716
})

0 commit comments

Comments
 (0)