Skip to content

Commit 470c0e0

Browse files
committed
update engine and FPS meter
1 parent f217bbd commit 470c0e0

File tree

2 files changed

+51
-60
lines changed

2 files changed

+51
-60
lines changed

public/litecanvas.js

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,7 @@
4545
animate: true
4646
};
4747
settings = Object.assign(defaults, settings);
48-
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _defaultSound = [0.5, 0, 1750, , , 0.3, 1, , , , 600, 0.1], _events = {
49-
init: null,
50-
update: null,
51-
draw: null,
52-
resized: null,
53-
tap: null,
54-
untap: null,
55-
tapping: null,
56-
tapped: null
57-
};
48+
let _initialized = false, _plugins = [], _canvas, _scale = 1, _ctx, _outline_fix = 0.5, _timeScale = 1, _lastFrameTime, _deltaTime = 1 / 60, _accumulated = 0, _rafid, _fontFamily = "sans-serif", _fontSize = 20, _rngSeed = Date.now(), _colors = defaultPalette, _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 = {};
5849
const instance = {
5950
/** @type {number} */
6051
W: 0,
@@ -807,9 +798,9 @@
807798
DEV: assert("string" === typeof eventName, "listen: 1st param must be a string");
808799
DEV: assert("function" === typeof callback, "listen: 2nd param must be a function");
809800
eventName = eventName.toLowerCase();
810-
_events[eventName] = _events[eventName] || /* @__PURE__ */ new Set();
811-
_events[eventName].add(callback);
812-
return () => _events[eventName].delete(callback);
801+
_eventListeners[eventName] = _eventListeners[eventName] || /* @__PURE__ */ new Set();
802+
_eventListeners[eventName].add(callback);
803+
return () => _eventListeners && _eventListeners[eventName].delete(callback);
813804
},
814805
/**
815806
* Call all listeners attached to a game event
@@ -901,7 +892,7 @@
901892
// 3
902893
_scale,
903894
// 4
904-
_events,
895+
_eventListeners,
905896
// 5
906897
_colors,
907898
// 6
@@ -929,10 +920,10 @@
929920
cancelAnimationFrame(_rafid);
930921
_rafid = 0;
931922
instance.emit("quit");
923+
_eventListeners = {};
932924
for (const removeListener of _browserEventListeners) {
933925
removeListener();
934926
}
935-
_events = {};
936927
if (settings.global) {
937928
for (const key in instance) {
938929
delete root[key];
@@ -942,12 +933,12 @@
942933
_initialized = false;
943934
}
944935
};
945-
for (const k of "PI,sin,cos,atan2,hypot,tan,abs,ceil,floor,trunc,min,max,pow,sqrt,sign,exp".split(",")) {
936+
for (const k of _mathFunctions.split(",")) {
946937
instance[k] = math[k];
947938
}
948939
function init() {
949940
const source = settings.loop ? settings.loop : root;
950-
for (const event in _events) {
941+
for (const event of _coreEvents.split(",")) {
951942
if (source[event]) instance.listen(event, source[event]);
952943
}
953944
for (const [callback, config] of _plugins) {
@@ -1251,8 +1242,8 @@
12511242
}
12521243
}
12531244
function triggerEvent(eventName, arg1, arg2, arg3, arg4) {
1254-
if (!_events[eventName]) return;
1255-
for (const callback of _events[eventName]) {
1245+
if (!_eventListeners[eventName]) return;
1246+
for (const callback of _eventListeners[eventName]) {
12561247
callback(arg1, arg2, arg3, arg4);
12571248
}
12581249
}
@@ -1999,79 +1990,79 @@
19991990
})();
20001991
(() => {
20011992
function _() {
2002-
let u = 0, a = true, i = document.createElement("div"), n = [], p = () => (performance || Date).now();
1993+
let u = 0, a = true, i = document.createElement("div"), s = [], l = () => (performance || Date).now();
20031994
i.style.cssText = "position:absolute;top:0;right:0;cursor:pointer;opacity:0.8;z-index:10000", i.addEventListener("click", function(e) {
2004-
e.preventDefault(), o(++u % i.children.length);
1995+
e.preventDefault(), m(++u % i.children.length);
20051996
}, false);
2006-
function l(e, r, x, t) {
1997+
function o(e, r, x, t) {
20071998
let g = new T(e, r, x, i, t);
2008-
return n.push(g), g;
1999+
return s.push(g), g;
20092000
}
2010-
function o(e) {
2001+
function m(e) {
20112002
for (let r = 0; r < i.children.length; r++) i.children[r].style.display = r === e ? "block" : "none";
20122003
u = e;
20132004
}
20142005
function f() {
2015-
u++, u >= i.children.length && (u = 0), o(u);
2006+
u++, u >= i.children.length && (u = 0), m(u);
20162007
}
2017-
function s(e = "all") {
2018-
if (e === "all") for (let r = 0; r < n.length; r++) n[r].reset();
2019-
else n[e] && n[e].reset();
2020-
h = p(), y = 0;
2008+
function n(e = "all") {
2009+
if (e === "all") for (let r = 0; r < s.length; r++) s[r].reset();
2010+
else s[e] && s[e].reset();
2011+
h = l(), y = 0;
20212012
}
20222013
function w(e = true) {
20232014
a = !!e, i.style.display = a ? "" : "none";
20242015
}
2025-
let b = p(), h = b, y = 0, c = l("FPS", "#0ff", "#002"), d = l("MS", "#0f0", "#020"), m;
2026-
return self.performance && self.performance.memory && (m = l("MB", "#f08", "#201")), o(0), { dom: i, addPanel: l, showPanel: o, nextPanel: f, resetPanel: s, display: w, get hidden() {
2016+
let b = l(), h = b, y = 0, d = o("FPS", "#0ff", "#002"), c = o("MS", "#0f0", "#020"), p;
2017+
return self.performance && self.performance.memory && (p = o("MB", "#f08", "#201")), m(0), { dom: i, addPanel: o, showPanel: m, nextPanel: f, resetPanel: n, display: w, get hidden() {
20272018
return !a;
20282019
}, begin: function() {
2029-
b = p();
2020+
b = l();
20302021
}, end: function() {
20312022
y++;
2032-
let e = p();
2033-
if (d.update(e - b, 200), e >= h + 1e3 && (c.update(y * 1e3 / (e - h), 100), h = e, y = 0, m)) {
2023+
let e = l();
2024+
if (c.update(e - b, 200), e >= h + 1e3 && (d.update(y * 1e3 / (e - h), 100), h = e, y = 0, p)) {
20342025
let r = performance.memory;
2035-
m.update(r.usedJSHeapSize / 1048576, r.jsHeapSizeLimit / 1048576);
2026+
p.update(r.usedJSHeapSize / 1048576, r.jsHeapSizeLimit / 1048576);
20362027
}
20372028
return e;
20382029
}, update: function() {
20392030
b = this.end();
20402031
} };
20412032
}
2042-
function T(u, a, i, n, p = {}) {
2043-
let l = Math.round, o = 1 / 0, f = 0, s = l(window.devicePixelRatio || 1), w = (p.width || 80) * s, b = 48 * s, h = 3 * s, y = 2 * s, c = 3 * s, d = 15 * s, m = (w - 6) * s, e = 30 * s, r = document.createElement("canvas");
2044-
r.width = w, r.height = b;
2045-
let x = n.children.length;
2046-
n.appendChild(r);
2033+
function T(u, a, i, s, l = {}) {
2034+
let o = Math.round, m = 1 / 0, f = 0, n = o(window.devicePixelRatio || 1), w = l.width || 80, b = 48, h = 3 * n, y = 2 * n, d = 3 * n, c = 15 * n, p = (w - 6) * n, e = 30 * n, r = document.createElement("canvas");
2035+
r.width = w * n, r.height = b * n, r.style.cssText = `width:${l.width};height:48px`;
2036+
let x = s.children.length;
2037+
s.appendChild(r);
20472038
let t = r.getContext("2d");
2048-
t.font = "bold " + 9 * s + "px Helvetica,Arial,sans-serif", t.textBaseline = "top";
2039+
t.font = `bold ${9 * n}px Helvetica,Arial,sans-serif`, t.textBaseline = "top";
20492040
function g() {
2050-
t.fillStyle = i, t.fillRect(0, 0, w, b), t.fillStyle = a, t.fillText(u, h, y), t.fillRect(c, d, m, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(c, d, m, e);
2041+
t.fillStyle = i, t.fillRect(0, 0, w * n, b * n), t.fillStyle = a, t.fillText(u, h, y), t.fillRect(d, c, p, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(d, c, p, e);
20512042
}
2052-
return g(), { id: x, dom: r, reset: g, update: function(v, P) {
2053-
o = Math.min(o, v), f = Math.max(f, v), t.fillStyle = i, t.globalAlpha = 1, t.fillRect(0, 0, w, d), t.fillStyle = a;
2054-
let E = [l(v), u];
2055-
p.labelBefore && E.reverse(), t.fillText(E.join(" ") + " (" + l(o) + "-" + l(f) + ")", h, y), t.drawImage(r, c + s, d, m - s, e, c, d, m - s, e), t.fillRect(c + m - s, d, s, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(c + m - s, d, s, l((1 - v / P) * e));
2043+
return g(), { id: x, dom: r, reset: g, update: function(v, S) {
2044+
m = Math.min(m, v), f = Math.max(f, v), t.fillStyle = i, t.globalAlpha = 1, t.fillRect(0, 0, w * n, c), t.fillStyle = a;
2045+
let E = [o(v), u];
2046+
l.labelBefore && E.reverse(), t.fillText(E.join(" ") + " (" + o(m) + "-" + o(f) + ")", h, y), t.drawImage(r, d + n, c, p - n, e, d, c, p - n, e), t.fillRect(d + p - n, c, n, e), t.fillStyle = i, t.globalAlpha = 0.9, t.fillRect(d + p - n, c, n, o((1 - v / S) * e));
20562047
} };
20572048
}
2058-
var k = { hotkeyShow: "F1", hotkeyNext: "F2", css: {}, hidden: false, id: "" };
2049+
var P = { hotkeyShow: "F1", hotkeyNext: "F2", css: {}, hidden: false, id: "" };
20592050
function A(u, a = {}) {
2060-
a = Object.assign({}, k, a);
2061-
let i = u.stat(0), n = new _(), p = n.display, l = (o = true) => {
2062-
a.hidden = !o, p(o), n.resetPanel();
2051+
a = Object.assign({}, P, a);
2052+
let i = u.stat(0), s = new _(), l = s.display, o = (m = true) => {
2053+
a.hidden = !m, l(m), s.resetPanel();
20632054
};
2064-
a.id && (n.dom.id = a.id);
2065-
for (let [o, f] of Object.entries(a.css || {})) n.dom.style[o] = f;
2066-
return u.canvas().parentElement.appendChild(n.dom), l(!a.hidden), i.keyboardEvents && listen("update", () => {
2067-
a.hotkeyShow && u.iskeypressed(a.hotkeyShow) && l(a.hidden), a.hotkeyNext && u.iskeypressed(a.hotkeyNext) && n.nextPanel();
2068-
}), listen("before:update", (o, f = 1) => {
2069-
a.hidden || f === 1 && n.begin();
2055+
a.id && (s.dom.id = a.id);
2056+
for (let [m, f] of Object.entries(a.css || {})) s.dom.style[m] = f;
2057+
return u.canvas().parentElement.appendChild(s.dom), o(!a.hidden), i.keyboardEvents && listen("update", () => {
2058+
a.hotkeyShow && u.iskeypressed(a.hotkeyShow) && o(a.hidden), a.hotkeyNext && u.iskeypressed(a.hotkeyNext) && s.nextPanel();
2059+
}), listen("before:update", (m, f = 1) => {
2060+
a.hidden || f === 1 && s.begin();
20702061
}), listen("after:draw", () => {
2071-
a.hidden || n.end();
2062+
a.hidden || s.end();
20722063
}), listen("quit", () => {
2073-
n.dom.remove();
2074-
}), n.display = l, { FPS_METER: n };
2064+
s.dom.remove();
2065+
}), s.display = o, { FPS_METER: s };
20752066
}
20762067
window.pluginFrameRateMeter = A;
20772068
})();

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.06.26.0";
2+
const version = "2025.06.28.0";
33

44
const precacheResources = [
55
"/",

0 commit comments

Comments
 (0)