/* Kit loop + view (v1) - from _Tools\Library\web\kit. Needs kit-core.js. Game loop with a fixed simulation step and smooth rendering: const loop = Kit.loop({ step: 1000 / 60, // ms per update (fixed: deterministic, replayable) update(dt) { ... }, // dt = step in seconds render(alpha, now) { ... }, // alpha 0..1 between the last two updates (interpolate) }); loop.start(); loop.pause(); loop.resume(); loop.paused Pauses itself while the tab is hidden; a long hitch is capped (no spiral of death). Fixed-size layout scaled to the window (sharp canvases on hi-DPI screens): Kit.fit(Kit.$('app'), { canvases: [[cv, 960, 540]], onFit(scale) {} }) #app is laid out at its natural pixel size and scaled with CSS transform to fill the window; every listed canvas gets a backing store of size * scale * devicePixelRatio and a matching ctx transform, so you always draw in design pixels (960 x 540). */ (function (root) { 'use strict'; const Kit = (root.Kit = root.Kit || {}); Kit.loop = opt => { const step = opt.step || 1000 / 60, maxFrame = opt.maxFrame || 250; let acc = 0, last = 0, raf = 0, running = false; const api = { paused: false, ticks: 0 }; function frame(now) { raf = requestAnimationFrame(frame); let d = now - last; last = now; if (api.paused || document.hidden) { acc = 0; opt.render && opt.render(1, now); return; } if (d > maxFrame) d = maxFrame; acc += d; while (acc >= step) { acc -= step; api.ticks++; opt.update && opt.update(step / 1000); if (api.paused) { acc = 0; break; } } opt.render && opt.render(acc / step, now); } api.start = () => { if (running) return api; running = true; last = performance.now(); raf = requestAnimationFrame(frame); return api; }; api.stop = () => { running = false; cancelAnimationFrame(raf); }; api.pause = () => { api.paused = true; }; api.resume = () => { api.paused = false; acc = 0; last = performance.now(); }; return api; }; Kit.setRes = (cv, w, h, k) => { cv.width = Math.round(w * k); cv.height = Math.round(h * k); cv.style.width = w + 'px'; cv.style.height = h + 'px'; const c = cv.getContext('2d'); c.setTransform(k, 0, 0, k, 0, 0); return c; }; Kit.fit = (app, opt = {}) => { const margin = opt.margin == null ? 0.96 : opt.margin, min = opt.min || 0.3, max = opt.max || 4; const canvases = opt.canvases || []; app.style.transformOrigin = 'center center'; function fit() { app.style.transform = 'none'; const w = app.offsetWidth, h = app.offsetHeight; const sc = Kit.clamp(Math.min(innerWidth * margin / w, innerHeight * margin / h), min, max); app.style.transform = `scale(${sc})`; const k = sc * (root.devicePixelRatio || 1); for (const [cv, cw, ch] of canvases) Kit.setRes(cv, cw, ch, k); opt.onFit && opt.onFit(sc); } addEventListener('resize', fit); if (document.fonts) document.fonts.ready.then(fit); fit(); return fit; }; // Screen position (mouse/touch event) -> design pixels of a canvas laid out by Kit.fit. Kit.toCanvas = (cv, e, w, h) => { const r = cv.getBoundingClientRect(); return { x: (e.clientX - r.left) * (w / r.width), y: (e.clientY - r.top) * (h / r.height) }; }; })(typeof window !== 'undefined' ? window : globalThis);