|
39 | 39 | canvas: null,
|
40 | 40 | global: true,
|
41 | 41 | loop: null,
|
42 |
| - pauseOnBlur: true, |
43 | 42 | tapEvents: true,
|
44 | 43 | keyboardEvents: true,
|
45 | 44 | animate: true
|
|
977 | 976 | */
|
978 | 977 | quit() {
|
979 | 978 | cancelAnimationFrame(_rafid);
|
| 979 | + _rafid = 0; |
980 | 980 | instance.emit("quit");
|
981 | 981 | for (const removeListener of _browserEventListeners) {
|
982 | 982 | removeListener();
|
|
1191 | 1191 | }
|
1192 | 1192 | );
|
1193 | 1193 | }
|
1194 |
| - if (settings.pauseOnBlur) { |
1195 |
| - on(root, "blur", () => { |
1196 |
| - _rafid = cancelAnimationFrame(_rafid); |
1197 |
| - }); |
1198 |
| - on(root, "focus", () => { |
1199 |
| - if (!_rafid) { |
1200 |
| - _accumulated = 0; |
1201 |
| - _rafid = raf(drawFrame); |
1202 |
| - } |
1203 |
| - }); |
1204 |
| - } |
1205 | 1194 | _initialized = true;
|
1206 | 1195 | instance.emit("init", instance);
|
1207 | 1196 | _lastFrameTime = performance.now();
|
|
1211 | 1200 | let updated = 0, frameTime = (now - _lastFrameTime) / 1e3;
|
1212 | 1201 | _lastFrameTime = now;
|
1213 | 1202 | if (settings.animate) {
|
1214 |
| - _rafid = raf(drawFrame); |
1215 | 1203 | if (frameTime > 0.3) {
|
1216 |
| - return console.warn("skipping too long frame"); |
1217 |
| - } |
1218 |
| - _accumulated += frameTime; |
1219 |
| - while (_accumulated >= _deltaTime) { |
1220 |
| - instance.emit("update", _deltaTime * _timeScale); |
1221 |
| - instance.def("T", instance.T + _deltaTime * _timeScale); |
1222 |
| - updated++; |
1223 |
| - _accumulated -= _deltaTime; |
| 1204 | + console.warn("skipping too long frame"); |
| 1205 | + } else { |
| 1206 | + _accumulated += frameTime; |
| 1207 | + while (_accumulated >= _deltaTime) { |
| 1208 | + updated++; |
| 1209 | + instance.emit("update", _deltaTime * _timeScale, updated); |
| 1210 | + instance.def("T", instance.T + _deltaTime * _timeScale); |
| 1211 | + _accumulated -= _deltaTime; |
| 1212 | + } |
1224 | 1213 | }
|
| 1214 | + if (_rafid) _rafid = raf(drawFrame); |
1225 | 1215 | } else {
|
1226 | 1216 | updated = 1;
|
1227 | 1217 | }
|
|
1231 | 1221 | }
|
1232 | 1222 | }
|
1233 | 1223 | function setupCanvas() {
|
1234 |
| - if ("string" === typeof settings.canvas) { |
| 1224 | + if (settings.canvas) { |
| 1225 | + DEV: assert( |
| 1226 | + "string" === typeof settings.canvas, |
| 1227 | + `Litecanvas' option "canvas" should be a string (a selector)` |
| 1228 | + ); |
1235 | 1229 | _canvas = document.querySelector(settings.canvas);
|
1236 |
| - } else { |
1237 |
| - _canvas = settings.canvas || document.createElement("canvas"); |
1238 | 1230 | }
|
| 1231 | + _canvas = _canvas || document.createElement("canvas"); |
1239 | 1232 | DEV: assert(
|
1240 | 1233 | _canvas && _canvas.tagName === "CANVAS",
|
1241 | 1234 | "Invalid canvas element"
|
|
1773 | 1766 | }
|
1774 | 1767 | return false;
|
1775 | 1768 | }
|
1776 |
| - var h = { crossOrigin: "anonymous", baseURL: null, allowSoundInterruptions: true, ignoreErrors: false }, f = (t, e) => { |
| 1769 | + var h = { crossOrigin: "anonymous", baseURL: null, allowSoundInterruptions: true, ignoreErrors: false }, l = (t, e) => { |
1777 | 1770 | let i = "LOADING";
|
1778 | 1771 | t.def(i, ~~t[i] + ~~e);
|
1779 | 1772 | };
|
1780 | 1773 | function y2(t, e = {}) {
|
1781 |
| - return e = Object.assign({}, h, e), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.font = {}, { loadFont: async (r, a, l) => { |
1782 |
| - let { baseURL: m, ignoreErrors: p } = e, o = c(a); |
| 1774 | + return e = Object.assign({}, h, e), l(t, 0), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.font = {}, { loadFont: async (r, a, p) => { |
| 1775 | + let { baseURL: m, ignoreErrors: f } = e, o = c(a); |
1783 | 1776 | a = b(a, m);
|
1784 | 1777 | let n = new FontFace(r, `url(${a})`), u = { asset: n, type: "font", fontName: r, src: a, id: o };
|
1785 |
| - t.emit("filter-asset", n, u), document.fonts.add(n), f(t, 1); |
| 1778 | + t.emit("filter-asset", n, u), document.fonts.add(n), l(t, 1); |
1786 | 1779 | let s = n.load();
|
1787 | 1780 | return s.then((d) => {
|
1788 |
| - t.ASSETS.font[o] = d, l && l(d), t.emit("asset-load", u), f(t, -1); |
| 1781 | + t.ASSETS.font[o] = d, p && p(d), t.emit("asset-load", u), l(t, -1); |
1789 | 1782 | }).catch((d) => {
|
1790 |
| - if (console.error(d), !p) throw new Error("Failed to load font from " + a); |
1791 |
| - l && l(), t.emit("asset-error", u); |
| 1783 | + if (console.error(d), !f) throw new Error("Failed to load font from " + a); |
| 1784 | + p && p(), t.emit("asset-error", u); |
1792 | 1785 | }), s;
|
1793 | 1786 | } };
|
1794 | 1787 | }
|
1795 | 1788 | function A(t, e = {}) {
|
1796 |
| - return e = Object.assign({}, h, e), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.image = {}, { loadImage: async (r, a) => { |
1797 |
| - let { baseURL: l, ignoreErrors: m, crossOrigin: p } = e, o = t.stat(5), n = { splitFrames: F, convertColors: j(o) }, u = c(r); |
1798 |
| - r = b(r, l); |
| 1789 | + return e = Object.assign({}, h, e), l(t, 0), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.image = {}, { loadImage: async (r, a) => { |
| 1790 | + let { baseURL: p, ignoreErrors: m, crossOrigin: f } = e, o = t.stat(5), n = { splitFrames: F, convertColors: j(o) }, u = c(r); |
| 1791 | + r = b(r, p); |
1799 | 1792 | let s = new Image(), d = { asset: s, type: "image", src: r, id: u };
|
1800 | 1793 | return new Promise((w) => {
|
1801 |
| - f(t, 1), s.crossOrigin = p, s.onerror = (g) => { |
| 1794 | + l(t, 1), s.crossOrigin = f, s.onerror = (g) => { |
1802 | 1795 | console.error(g);
|
1803 | 1796 | let S = "Failed to load image from " + r;
|
1804 | 1797 | if (!m) throw new Error(S);
|
1805 | 1798 | a && a(), t.emit("asset-error", d);
|
1806 | 1799 | }, s.onload = () => {
|
1807 |
| - t.ASSETS.image[u] = s, a && a(s, n), t.emit("asset-load", d), f(t, -1), w(s); |
| 1800 | + t.ASSETS.image[u] = s, a && a(s, n), t.emit("asset-load", d), l(t, -1), w(s); |
1808 | 1801 | }, t.emit("filter-asset", s, d), s.src = r;
|
1809 | 1802 | });
|
1810 | 1803 | } };
|
1811 | 1804 | }
|
1812 | 1805 | function F(t, e, i, r = 0, a = 0) {
|
1813 |
| - let l = [], m = Math.floor((t.width + a) / (e + a)), p = Math.floor((t.height + a) / (i + a)); |
1814 |
| - for (let o = 0; o < p; o++) for (let n = 0; n < m; n++) { |
| 1806 | + let p = [], m = Math.floor((t.width + a) / (e + a)), f = Math.floor((t.height + a) / (i + a)); |
| 1807 | + for (let o = 0; o < f; o++) for (let n = 0; n < m; n++) { |
1815 | 1808 | let u = new OffscreenCanvas(e, i);
|
1816 |
| - u.getContext("2d").drawImage(t, r + n * e + n * a, r + o * i + o * a, e, i, 0, 0, e, i), l.push(u); |
| 1809 | + u.getContext("2d").drawImage(t, r + n * e + n * a, r + o * i + o * a, e, i, 0, 0, e, i), p.push(u); |
1817 | 1810 | }
|
1818 |
| - return l; |
| 1811 | + return p; |
1819 | 1812 | }
|
1820 | 1813 | function j(t) {
|
1821 | 1814 | return (e, i = false) => {
|
1822 | 1815 | let r = new OffscreenCanvas(e.width, e.height), a = r.getContext("2d");
|
1823 | 1816 | a.drawImage(e, 0, 0);
|
1824 |
| - let l = a.getImageData(0, 0, e.width, e.height), m = l.data, p = /* @__PURE__ */ new Map(); |
| 1817 | + let p = a.getImageData(0, 0, e.width, e.height), m = p.data, f = /* @__PURE__ */ new Map(); |
1825 | 1818 | for (let o = 0, n = m.length; o < n; o += 4) {
|
1826 |
| - let u = m[o], s = m[o + 1], d = m[o + 2], w = [u, s, d], g = w.join(","), S = p.get(g); |
1827 |
| - S || (S = P(w, t), p.set(g, S)); |
| 1819 | + let u = m[o], s = m[o + 1], d = m[o + 2], w = [u, s, d], g = w.join(","), S = f.get(g); |
| 1820 | + S || (S = P(w, t), f.set(g, S)); |
1828 | 1821 | let _ = S.startsWith("#") ? O(S) : D(S);
|
1829 | 1822 | m[o] = _[0], m[o + 1] = _[1], m[o + 2] = _[2], m[o + 3] = i ? m[o + 3] : 255;
|
1830 | 1823 | }
|
1831 |
| - return a.putImageData(l, 0, 0), r; |
| 1824 | + return a.putImageData(p, 0, 0), r; |
1832 | 1825 | };
|
1833 | 1826 | }
|
1834 | 1827 | function O(t) {
|
|
1842 | 1835 | return i.length === 1 && (i = "0" + i), r.length === 1 && (r = "0" + r), a.length === 1 && (a = "0" + a), [i | 0, r | 0, a | 0];
|
1843 | 1836 | }
|
1844 | 1837 | function P(t, e) {
|
1845 |
| - let i = 1 / 0, r = null, [a, l, m] = t; |
1846 |
| - return e.forEach((p) => { |
1847 |
| - let [o, n, u] = p.startsWith("#") ? O(p) : D(p), s = Math.sqrt((a - o) ** 2 + (l - n) ** 2 + (m - u) ** 2); |
1848 |
| - s < i && (i = s, r = p); |
| 1838 | + let i = 1 / 0, r = null, [a, p, m] = t; |
| 1839 | + return e.forEach((f) => { |
| 1840 | + let [o, n, u] = f.startsWith("#") ? O(f) : D(f), s = Math.sqrt((a - o) ** 2 + (p - n) ** 2 + (m - u) ** 2); |
| 1841 | + s < i && (i = s, r = f); |
1849 | 1842 | }), r;
|
1850 | 1843 | }
|
1851 | 1844 | function E(t, e = {}) {
|
1852 |
| - return e = Object.assign({}, h, e), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.script = {}, { loadScript: async (r, a) => { |
1853 |
| - let { baseURL: l, ignoreErrors: m, crossOrigin: p } = e, o = c(r); |
1854 |
| - r = b(r, l); |
| 1845 | + return e = Object.assign({}, h, e), l(t, 0), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.script = {}, { loadScript: async (r, a) => { |
| 1846 | + let { baseURL: p, ignoreErrors: m, crossOrigin: f } = e, o = c(r); |
| 1847 | + r = b(r, p); |
1855 | 1848 | let n = document.createElement("script"), u = { asset: n, type: "script", src: r, id: o };
|
1856 | 1849 | return new Promise((s) => {
|
1857 |
| - f(t, 1), n.crossOrigin = p, n.onerror = (d) => { |
| 1850 | + l(t, 1), n.crossOrigin = f, n.onerror = (d) => { |
1858 | 1851 | if (console.error(d), !m) throw new Error("Failed to load " + r);
|
1859 | 1852 | a && a(), t.emit("asset-error", u);
|
1860 | 1853 | }, n.onload = () => {
|
1861 |
| - t.ASSETS.script[o] = n, a && a(n), t.emit("asset-load", u), f(t, -1), s(n); |
| 1854 | + t.ASSETS.script[o] = n, a && a(n), t.emit("asset-load", u), l(t, -1), s(n); |
1862 | 1855 | }, t.emit("filter-asset", n, u), n.src = r, document.head.appendChild(n);
|
1863 | 1856 | });
|
1864 | 1857 | } };
|
1865 | 1858 | }
|
1866 | 1859 | function x2(t, e = {}) {
|
1867 |
| - return e = Object.assign({}, h, e), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.sound = {}, { loadSound: async (r, a) => { |
1868 |
| - let { crossOrigin: l, ignoreErrors: m, allowSoundInterruptions: p, baseURL: o } = e, n = c(r); |
| 1860 | + return e = Object.assign({}, h, e), l(t, 0), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.sound = {}, { loadSound: async (r, a) => { |
| 1861 | + let { crossOrigin: p, ignoreErrors: m, allowSoundInterruptions: f, baseURL: o } = e, n = c(r); |
1869 | 1862 | r = b(r, o);
|
1870 | 1863 | let u = new Audio(), s = { asset: u, type: "sound", src: r, id: n };
|
1871 | 1864 | return new Promise((d) => {
|
1872 |
| - f(t, 1), u.crossOrigin = l, u.onerror = (w) => { |
| 1865 | + l(t, 1), u.crossOrigin = p, u.onerror = (w) => { |
1873 | 1866 | if (console.error(w), !m) throw new Error("Failed to load " + r);
|
1874 | 1867 | a && a(null), t.emit("asset-error", s);
|
1875 |
| - }, u[p ? "oncanplay" : "oncanplaythrough"] = () => { |
1876 |
| - t.ASSETS.sound[n] = u, a && a(u), t.emit("asset-load", s), f(t, -1), d(u); |
| 1868 | + }, u[f ? "oncanplay" : "oncanplaythrough"] = () => { |
| 1869 | + t.ASSETS.sound[n] = u, a && a(u), t.emit("asset-load", s), l(t, -1), d(u); |
1877 | 1870 | }, t.emit("filter-asset", u, s), u.src = r;
|
1878 | 1871 | });
|
1879 | 1872 | } };
|
|
1884 | 1877 | this.pause(), this.currentTime = 0, this.play();
|
1885 | 1878 | } });
|
1886 | 1879 | function L(t, e = {}) {
|
1887 |
| - return e = Object.assign({}, h, e), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.json = {}, { loadJSON: async (r, a, l) => { |
1888 |
| - let { baseURL: m, ignoreErrors: p } = e, o = c(r); |
| 1880 | + return e = Object.assign({}, h, e), l(t, 0), t.def("ASSETS", t.ASSETS || {}), t.ASSETS.json = {}, { loadJSON: async (r, a, p) => { |
| 1881 | + let { baseURL: m, ignoreErrors: f } = e, o = c(r); |
1889 | 1882 | r = b(r, m);
|
1890 | 1883 | let n = { type: "json", src: r, id: o };
|
1891 |
| - t.emit("filter-asset", null, n), f(t, 1), t.ASSETS.json = {}; |
1892 |
| - let u = fetch(r, l); |
| 1884 | + t.emit("filter-asset", null, n), l(t, 1), t.ASSETS.json = {}; |
| 1885 | + let u = fetch(r, p); |
1893 | 1886 | return u.then((s) => s.json()).then((s) => {
|
1894 |
| - ASSETS.json[o] = s, n.json = s, a && a(s), t.emit("asset-load", n), f(t, -1); |
| 1887 | + ASSETS.json[o] = s, n.json = s, a && a(s), t.emit("asset-load", n), l(t, -1); |
1895 | 1888 | }).catch((s) => {
|
1896 |
| - if (console.error(s), !p) throw new Error("Failed to load JSON from " + r); |
| 1889 | + if (console.error(s), !f) throw new Error("Failed to load JSON from " + r); |
1897 | 1890 | a && a(), t.emit("asset-error", n);
|
1898 | 1891 | }), u;
|
1899 | 1892 | } };
|
1900 | 1893 | }
|
1901 | 1894 | function T(t) {
|
1902 |
| - return { load: (i) => new Promise((r, a) => { |
1903 |
| - f(t, 1), i((m) => (f(t, -1), r(m)), a); |
| 1895 | + return l(t, 0), t.def("ASSETS", t.ASSETS || {}), { load: (i) => new Promise((r, a) => { |
| 1896 | + l(t, 1), i((m) => (l(t, -1), r(m)), a); |
1904 | 1897 | }) };
|
1905 | 1898 | }
|
1906 | 1899 | function v(t, e = {}) {
|
|
0 commit comments