Skip to content

Commit d506c70

Browse files
committed
update engine
1 parent b1fce04 commit d506c70

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

public/about.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,9 @@ <h2><a id="engine-api">Engine API</a></h2>
684684
// example: pal() resets the default color palette
685685
pal(colors: string[]): void
686686

687-
// Swaps two colors of the current palette
688-
// example: pal(0, 3) // now the color #0 is white and the color #3 is black
687+
// Replace the color "a" with the color "b"
688+
// example: pal(0, 3) // now the color black (#0) is white (#3)
689+
// note: reset the current palette if called without arguments
689690
palc(a: number, b: number): void
690691

691692
// Loads a plugin.

public/js/app.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.

public/js/litecanvas.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
var assert = (condition, message = "Assertion failed") => {
2828
if (!condition) throw new Error(message);
2929
};
30-
var version = "0.98.0";
30+
var version = "0.98.1";
3131
function litecanvas(settings = {}) {
3232
const root = window, math = Math, TWO_PI = math.PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => {
3333
elem.addEventListener(evt, callback, false);
@@ -43,7 +43,7 @@
4343
keyboardEvents: true
4444
};
4545
settings = Object.assign(defaults, settings);
46-
let _initialized = false, _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 = {};
46+
let _initialized = false, _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _fpsInterval = 1e3 / 60, _accumulated, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colorPalette = defaultPalette, _colorPaletteState = [], _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 = {};
4747
const instance = {
4848
/** @type {number} */
4949
W: 0,
@@ -558,7 +558,7 @@
558558
"[litecanvas] text() 5th param must be a string"
559559
);
560560
_ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`;
561-
_ctx.fillStyle = _colors[~~color % _colors.length];
561+
_ctx.fillStyle = getColor(color);
562562
_ctx.fillText(message, ~~x2, ~~y2);
563563
},
564564
/**
@@ -765,7 +765,7 @@
765765
null == color || isNumber(color) && color >= 0,
766766
"[litecanvas] fill() 1st param must be a positive number or zero"
767767
);
768-
_ctx.fillStyle = _colors[~~color % _colors.length];
768+
_ctx.fillStyle = getColor(color);
769769
_ctx.fill();
770770
},
771771
/**
@@ -778,7 +778,7 @@
778778
null == color || isNumber(color) && color >= 0,
779779
"[litecanvas] stroke() 1st param must be a positive number or zero"
780780
);
781-
_ctx.strokeStyle = _colors[~~color % _colors.length];
781+
_ctx.strokeStyle = getColor(color);
782782
_ctx.stroke();
783783
},
784784
/**
@@ -906,7 +906,7 @@
906906
}
907907
},
908908
/**
909-
* Set or reset the color palette.
909+
* Set new palette colors or restore the default palette.
910910
*
911911
* @param {string[]} [colors]
912912
*/
@@ -915,14 +915,16 @@
915915
Array.isArray(colors) && colors.length > 0,
916916
"[litecanvas] pal() 1st param must be a array of strings"
917917
);
918-
_colors = colors;
919-
_currentPalette = [...colors];
918+
_colorPalette = colors;
919+
_colorPaletteState = [];
920920
},
921921
/**
922-
* Swap two colors of the current palette.
922+
* Replace the color "a" with color "b".
923923
*
924924
* If called without arguments, reset the current palette.
925925
*
926+
* Note: `palc()` don't affect drawings made with `image()`.
927+
*
926928
* @param {number?} a
927929
* @param {number?} b
928930
*/
@@ -936,10 +938,9 @@
936938
"[litecanvas] palc() 2nd param must be a positive number"
937939
);
938940
if (a == null) {
939-
_colors = [..._currentPalette];
941+
_colorPaletteState = [];
940942
} else {
941-
;
942-
[_colors[a], _colors[b]] = [_colors[b], _colors[a]];
943+
_colorPaletteState[a] = b;
943944
}
944945
},
945946
/**
@@ -1009,7 +1010,7 @@
10091010
// 4
10101011
_eventListeners,
10111012
// 5
1012-
_colors,
1013+
_colorPalette,
10131014
// 6
10141015
_defaultSound,
10151016
// 7
@@ -1403,6 +1404,10 @@
14031404
instance.def(key, pluginData[key]);
14041405
}
14051406
}
1407+
function getColor(index) {
1408+
const i = _colorPaletteState[index] ?? index;
1409+
return _colorPalette[~~i % _colorPalette.length];
1410+
}
14061411
if (settings.global) {
14071412
if (root.ENGINE) {
14081413
throw new Error("only one global litecanvas is allowed");
@@ -1413,7 +1418,6 @@
14131418
DEV: console.info(`[litecanvas] version ${version} started`);
14141419
DEV: console.debug(`[litecanvas] litecanvas() options =`, settings);
14151420
setupCanvas();
1416-
instance.pal();
14171421
if ("loading" === document.readyState) {
14181422
on(root, "DOMContentLoaded", () => raf(init));
14191423
} else {

public/sw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const cacheName = "luizbills.litecanvas-editor-v1";
2-
const version = "2025.8.7.0";
2+
const version = "2025.8.7.1";
33

44
const precacheResources = [
55
"/",

src/completions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default function customCompletions(context) {
201201
type: "function",
202202
apply: "palc(",
203203
detail: "(a, b)",
204-
info: "swap two colors of the current palette",
204+
info: 'replace the color "a" with color "b"',
205205
},
206206
{
207207
label: "ctx",

0 commit comments

Comments
 (0)