|
27 | 27 | var assert = (condition, message = "Assertion failed") => { |
28 | 28 | if (!condition) throw new Error(message); |
29 | 29 | }; |
30 | | - var version = "0.98.0"; |
| 30 | + var version = "0.98.1"; |
31 | 31 | function litecanvas(settings = {}) { |
32 | 32 | const root = window, math = Math, TWO_PI = math.PI * 2, raf = requestAnimationFrame, _browserEventListeners = [], on = (elem, evt, callback) => { |
33 | 33 | elem.addEventListener(evt, callback, false); |
|
43 | 43 | keyboardEvents: true |
44 | 44 | }; |
45 | 45 | 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 = {}; |
47 | 47 | const instance = { |
48 | 48 | /** @type {number} */ |
49 | 49 | W: 0, |
|
558 | 558 | "[litecanvas] text() 5th param must be a string" |
559 | 559 | ); |
560 | 560 | _ctx.font = `${fontStyle} ${_fontSize}px ${_fontFamily}`; |
561 | | - _ctx.fillStyle = _colors[~~color % _colors.length]; |
| 561 | + _ctx.fillStyle = getColor(color); |
562 | 562 | _ctx.fillText(message, ~~x2, ~~y2); |
563 | 563 | }, |
564 | 564 | /** |
|
765 | 765 | null == color || isNumber(color) && color >= 0, |
766 | 766 | "[litecanvas] fill() 1st param must be a positive number or zero" |
767 | 767 | ); |
768 | | - _ctx.fillStyle = _colors[~~color % _colors.length]; |
| 768 | + _ctx.fillStyle = getColor(color); |
769 | 769 | _ctx.fill(); |
770 | 770 | }, |
771 | 771 | /** |
|
778 | 778 | null == color || isNumber(color) && color >= 0, |
779 | 779 | "[litecanvas] stroke() 1st param must be a positive number or zero" |
780 | 780 | ); |
781 | | - _ctx.strokeStyle = _colors[~~color % _colors.length]; |
| 781 | + _ctx.strokeStyle = getColor(color); |
782 | 782 | _ctx.stroke(); |
783 | 783 | }, |
784 | 784 | /** |
|
906 | 906 | } |
907 | 907 | }, |
908 | 908 | /** |
909 | | - * Set or reset the color palette. |
| 909 | + * Set new palette colors or restore the default palette. |
910 | 910 | * |
911 | 911 | * @param {string[]} [colors] |
912 | 912 | */ |
|
915 | 915 | Array.isArray(colors) && colors.length > 0, |
916 | 916 | "[litecanvas] pal() 1st param must be a array of strings" |
917 | 917 | ); |
918 | | - _colors = colors; |
919 | | - _currentPalette = [...colors]; |
| 918 | + _colorPalette = colors; |
| 919 | + _colorPaletteState = []; |
920 | 920 | }, |
921 | 921 | /** |
922 | | - * Swap two colors of the current palette. |
| 922 | + * Replace the color "a" with color "b". |
923 | 923 | * |
924 | 924 | * If called without arguments, reset the current palette. |
925 | 925 | * |
| 926 | + * Note: `palc()` don't affect drawings made with `image()`. |
| 927 | + * |
926 | 928 | * @param {number?} a |
927 | 929 | * @param {number?} b |
928 | 930 | */ |
|
936 | 938 | "[litecanvas] palc() 2nd param must be a positive number" |
937 | 939 | ); |
938 | 940 | if (a == null) { |
939 | | - _colors = [..._currentPalette]; |
| 941 | + _colorPaletteState = []; |
940 | 942 | } else { |
941 | | - ; |
942 | | - [_colors[a], _colors[b]] = [_colors[b], _colors[a]]; |
| 943 | + _colorPaletteState[a] = b; |
943 | 944 | } |
944 | 945 | }, |
945 | 946 | /** |
|
1009 | 1010 | // 4 |
1010 | 1011 | _eventListeners, |
1011 | 1012 | // 5 |
1012 | | - _colors, |
| 1013 | + _colorPalette, |
1013 | 1014 | // 6 |
1014 | 1015 | _defaultSound, |
1015 | 1016 | // 7 |
|
1403 | 1404 | instance.def(key, pluginData[key]); |
1404 | 1405 | } |
1405 | 1406 | } |
| 1407 | + function getColor(index) { |
| 1408 | + const i = _colorPaletteState[index] ?? index; |
| 1409 | + return _colorPalette[~~i % _colorPalette.length]; |
| 1410 | + } |
1406 | 1411 | if (settings.global) { |
1407 | 1412 | if (root.ENGINE) { |
1408 | 1413 | throw new Error("only one global litecanvas is allowed"); |
|
1413 | 1418 | DEV: console.info(`[litecanvas] version ${version} started`); |
1414 | 1419 | DEV: console.debug(`[litecanvas] litecanvas() options =`, settings); |
1415 | 1420 | setupCanvas(); |
1416 | | - instance.pal(); |
1417 | 1421 | if ("loading" === document.readyState) { |
1418 | 1422 | on(root, "DOMContentLoaded", () => raf(init)); |
1419 | 1423 | } else { |
|
0 commit comments