Skip to content

Commit 5b183d4

Browse files
committed
update engine and fix asset loader
1 parent 7a6b528 commit 5b183d4

File tree

3 files changed

+60
-71
lines changed

3 files changed

+60
-71
lines changed

public/about.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,6 @@ <h2><a id="settings">Game Configuration</a></h2>
338338
settings.width = null
339339
settings.height = settings.width || null
340340

341-
// Determines whether the game loop should
342-
// be paused when the "blur" event happens.
343-
settings.pauseOnBlur = true
344-
345341
// Determines whether the canvas should
346342
// scale to fill the canvas
347343
settings.autoscale = true

public/litecanvas.js

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
canvas: null,
4040
global: true,
4141
loop: null,
42-
pauseOnBlur: true,
4342
tapEvents: true,
4443
keyboardEvents: true,
4544
animate: true
@@ -977,6 +976,7 @@
977976
*/
978977
quit() {
979978
cancelAnimationFrame(_rafid);
979+
_rafid = 0;
980980
instance.emit("quit");
981981
for (const removeListener of _browserEventListeners) {
982982
removeListener();
@@ -1191,17 +1191,6 @@
11911191
}
11921192
);
11931193
}
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-
}
12051194
_initialized = true;
12061195
instance.emit("init", instance);
12071196
_lastFrameTime = performance.now();
@@ -1211,17 +1200,18 @@
12111200
let updated = 0, frameTime = (now - _lastFrameTime) / 1e3;
12121201
_lastFrameTime = now;
12131202
if (settings.animate) {
1214-
_rafid = raf(drawFrame);
12151203
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+
}
12241213
}
1214+
if (_rafid) _rafid = raf(drawFrame);
12251215
} else {
12261216
updated = 1;
12271217
}
@@ -1231,11 +1221,14 @@
12311221
}
12321222
}
12331223
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+
);
12351229
_canvas = document.querySelector(settings.canvas);
1236-
} else {
1237-
_canvas = settings.canvas || document.createElement("canvas");
12381230
}
1231+
_canvas = _canvas || document.createElement("canvas");
12391232
DEV: assert(
12401233
_canvas && _canvas.tagName === "CANVAS",
12411234
"Invalid canvas element"
@@ -1773,62 +1766,62 @@
17731766
}
17741767
return false;
17751768
}
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) => {
17771770
let i = "LOADING";
17781771
t.def(i, ~~t[i] + ~~e);
17791772
};
17801773
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);
17831776
a = b(a, m);
17841777
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);
17861779
let s = n.load();
17871780
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);
17891782
}).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);
17921785
}), s;
17931786
} };
17941787
}
17951788
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);
17991792
let s = new Image(), d = { asset: s, type: "image", src: r, id: u };
18001793
return new Promise((w) => {
1801-
f(t, 1), s.crossOrigin = p, s.onerror = (g) => {
1794+
l(t, 1), s.crossOrigin = f, s.onerror = (g) => {
18021795
console.error(g);
18031796
let S = "Failed to load image from " + r;
18041797
if (!m) throw new Error(S);
18051798
a && a(), t.emit("asset-error", d);
18061799
}, 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);
18081801
}, t.emit("filter-asset", s, d), s.src = r;
18091802
});
18101803
} };
18111804
}
18121805
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++) {
18151808
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);
18171810
}
1818-
return l;
1811+
return p;
18191812
}
18201813
function j(t) {
18211814
return (e, i = false) => {
18221815
let r = new OffscreenCanvas(e.width, e.height), a = r.getContext("2d");
18231816
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();
18251818
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));
18281821
let _ = S.startsWith("#") ? O(S) : D(S);
18291822
m[o] = _[0], m[o + 1] = _[1], m[o + 2] = _[2], m[o + 3] = i ? m[o + 3] : 255;
18301823
}
1831-
return a.putImageData(l, 0, 0), r;
1824+
return a.putImageData(p, 0, 0), r;
18321825
};
18331826
}
18341827
function O(t) {
@@ -1842,38 +1835,38 @@
18421835
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];
18431836
}
18441837
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);
18491842
}), r;
18501843
}
18511844
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);
18551848
let n = document.createElement("script"), u = { asset: n, type: "script", src: r, id: o };
18561849
return new Promise((s) => {
1857-
f(t, 1), n.crossOrigin = p, n.onerror = (d) => {
1850+
l(t, 1), n.crossOrigin = f, n.onerror = (d) => {
18581851
if (console.error(d), !m) throw new Error("Failed to load " + r);
18591852
a && a(), t.emit("asset-error", u);
18601853
}, 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);
18621855
}, t.emit("filter-asset", n, u), n.src = r, document.head.appendChild(n);
18631856
});
18641857
} };
18651858
}
18661859
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);
18691862
r = b(r, o);
18701863
let u = new Audio(), s = { asset: u, type: "sound", src: r, id: n };
18711864
return new Promise((d) => {
1872-
f(t, 1), u.crossOrigin = l, u.onerror = (w) => {
1865+
l(t, 1), u.crossOrigin = p, u.onerror = (w) => {
18731866
if (console.error(w), !m) throw new Error("Failed to load " + r);
18741867
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);
18771870
}, t.emit("filter-asset", u, s), u.src = r;
18781871
});
18791872
} };
@@ -1884,23 +1877,23 @@
18841877
this.pause(), this.currentTime = 0, this.play();
18851878
} });
18861879
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);
18891882
r = b(r, m);
18901883
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);
18931886
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);
18951888
}).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);
18971890
a && a(), t.emit("asset-error", n);
18981891
}), u;
18991892
} };
19001893
}
19011894
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);
19041897
}) };
19051898
}
19061899
function v(t, e = {}) {

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 = "2.83.0";
2+
const version = "2.84.0";
33

44
const precacheResources = [
55
"/",

0 commit comments

Comments
 (0)