/* Watch Encyclopedia stage runtime. Each entry registers WE.mount(slug, fn) and draws on a canvas. */ (function () { var C = { bg: '#0d0f13', panel: '#161a21', ink: '#f1ece2', mut: '#9a927f', dim: '#4d525d', grid: '#232831', acc: '#d5a35a', blue: '#6fb1e6', red: '#e8735c', green: '#7ecb93', violet: '#b79cf0', yellow: '#f0d264', teal: '#5fd0c5', pink: '#ee8fb5' }; var reg = {}, audio = null; function el(tag, cls, txt) { var e = document.createElement(tag); if (cls) e.className = cls; if (txt != null) e.textContent = txt; return e; } function start(root) { var fn = reg[root.dataset.exp]; if (!fn || root._we) return; root._we = 1; var cv = root.querySelector('canvas'), ctx = cv.getContext('2d'), ui = root.querySelector('.ctl'), cap = root.querySelector('.cap'); var S = { el: root, cv: cv, ctx: ctx, C: C, W: 0, H: 0, t: 0, dirty: true, TAU: Math.PI * 2 }; function size() { var r = cv.getBoundingClientRect(), d = Math.min(2, window.devicePixelRatio || 1); if (!r.width) return; S.W = r.width; S.H = r.height; S.dpr = d; S.u = Math.min(S.W, S.H); cv.width = Math.round(r.width * d); cv.height = Math.round(r.height * d); ctx.setTransform(d, 0, 0, d, 0, 0); S.dirty = true; if (S.onresize) S.onresize(); } S.clear = function (c) { ctx.fillStyle = c || C.bg; ctx.fillRect(0, 0, S.W, S.H); }; S.txt = function (s, x, y, col, px, align, wt) { ctx.fillStyle = col || C.ink; ctx.font = (wt || 500) + ' ' + (px || 13) + 'px system-ui,-apple-system,"Segoe UI",sans-serif'; ctx.textAlign = align || 'left'; ctx.textBaseline = 'middle'; ctx.fillText(s, x, y); }; S.ln = function (x1, y1, x2, y2, col, w) { ctx.strokeStyle = col || C.ink; ctx.lineWidth = w || 1; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y2); ctx.stroke(); }; S.dot = function (x, y, r, col) { ctx.fillStyle = col || C.ink; ctx.beginPath(); ctx.arc(x, y, Math.max(0, r), 0, 6.2832); ctx.fill(); }; S.ring = function (x, y, r, col, w) { ctx.strokeStyle = col || C.ink; ctx.lineWidth = w || 1; ctx.beginPath(); ctx.arc(x, y, Math.max(0, r), 0, 6.2832); ctx.stroke(); }; S.arrow = function (x1, y1, x2, y2, col, w) { var a = Math.atan2(y2 - y1, x2 - x1), h = 7 + (w || 1.5) * 1.5; S.ln(x1, y1, x2, y2, col, w || 1.5); ctx.fillStyle = col || C.ink; ctx.beginPath(); ctx.moveTo(x2, y2); ctx.lineTo(x2 - h * Math.cos(a - .45), y2 - h * Math.sin(a - .45)); ctx.lineTo(x2 - h * Math.cos(a + .45), y2 - h * Math.sin(a + .45)); ctx.fill(); }; S.rr = function (x, y, w, h, r, fill, stroke) { ctx.beginPath(); if (ctx.roundRect) ctx.roundRect(x, y, w, h, r); else ctx.rect(x, y, w, h); if (fill) { ctx.fillStyle = fill; ctx.fill(); } if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = 1; ctx.stroke(); } }; S.say = function (h) { if (cap && cap._h !== h) { cap._h = h; cap.innerHTML = h; } }; S.small = function () { return S.W < 560; }; // ---- controls S.slider = function (label, min, max, val, step, fmt, cb) { var w = el('label', 'c-sl'), top = el('span', 'c-lab'), nm = el('span', '', label), out = el('b'), inp = el('input'); inp.type = 'range'; inp.min = min; inp.max = max; inp.step = step || 1; inp.value = val; top.appendChild(nm); top.appendChild(out); w.appendChild(top); w.appendChild(inp); ui.appendChild(w); var o = { v: +val, el: w, set: function (v) { o.v = +v; inp.value = v; out.textContent = fmt ? fmt(o.v) : o.v; S.dirty = true; } }; inp.addEventListener('input', function () { o.v = +inp.value; out.textContent = fmt ? fmt(o.v) : o.v; S.dirty = true; if (cb) cb(o.v); }); out.textContent = fmt ? fmt(o.v) : o.v; return o; }; S.toggle = function (label, on, cb) { var b = el('button', 'c-tg', label); b.type = 'button'; b.setAttribute('aria-pressed', !!on); ui.appendChild(b); var o = { v: !!on, el: b, set: function (v) { o.v = !!v; b.setAttribute('aria-pressed', o.v); S.dirty = true; } }; b.addEventListener('click', function () { o.set(!o.v); if (cb) cb(o.v); }); return o; }; S.button = function (label, cb) { var b = el('button', 'c-bt', label); b.type = 'button'; ui.appendChild(b); b.addEventListener('click', function () { cb(); S.dirty = true; }); return b; }; S.select = function (label, opts, val, cb) { var w = el('div', 'c-seg'); w.setAttribute('role', 'group'); w.setAttribute('aria-label', label); if (label) w.appendChild(el('span', 'c-lab', label)); var row = el('div', 'c-row'); w.appendChild(row); ui.appendChild(w); var o = { v: val, el: w, set: function (v) { o.v = v; [].forEach.call(row.children, function (b) { b.setAttribute('aria-pressed', b.dataset.v === String(v)); }); S.dirty = true; } }; opts.forEach(function (op) { var v = op[0], b = el('button', '', op[1]); b.type = 'button'; b.dataset.v = v; row.appendChild(b); b.addEventListener('click', function () { o.set(v); if (cb) cb(v); }); }); o.set(val); return o; }; S.input = function (label, val, cb, max) { var w = el('label', 'c-sl c-in'), top = el('span', 'c-lab', label), inp = el('input'); inp.type = 'text'; inp.value = val; inp.maxLength = max || 60; inp.autocomplete = 'off'; inp.spellcheck = false; w.appendChild(top); w.appendChild(inp); ui.appendChild(w); var o = { v: val, el: inp }; inp.addEventListener('input', function () { o.v = inp.value; S.dirty = true; if (cb) cb(o.v); }); return o; }; S.note = function (txt) { var p = el('span', 'c-note', txt); ui.appendChild(p); return p; }; // ---- pointer (canvas coords in CSS px) S.ptr = { x: -1, y: -1, down: false }; S.pointer = function (h) { cv.style.touchAction = h.scroll ? 'pan-y' : 'none'; cv.style.cursor = h.cursor || 'pointer'; function pos(e) { var r = cv.getBoundingClientRect(); S.ptr.x = e.clientX - r.left; S.ptr.y = e.clientY - r.top; } cv.addEventListener('pointerdown', function (e) { pos(e); S.ptr.down = true; try { cv.setPointerCapture(e.pointerId); } catch (x) { } if (h.down) h.down(S.ptr.x, S.ptr.y, e); S.dirty = true; }); cv.addEventListener('pointermove', function (e) { pos(e); if (h.move) h.move(S.ptr.x, S.ptr.y, S.ptr.down, e); S.dirty = true; }); function up(e) { if (!S.ptr.down) return; S.ptr.down = false; if (h.up) h.up(S.ptr.x, S.ptr.y, e); S.dirty = true; } cv.addEventListener('pointerup', up); cv.addEventListener('pointercancel', up); cv.addEventListener('pointerleave', function () { if (!S.ptr.down) { S.ptr.x = -1; S.ptr.y = -1; S.dirty = true; } }); if (h.wheel) cv.addEventListener('wheel', function (e) { e.preventDefault(); pos(e); h.wheel(e.deltaY, S.ptr.x, S.ptr.y); S.dirty = true; }, { passive: false }); }; // ---- sound (only after a tap) S.tone = function (f, dur, type, vol, when) { try { audio = audio || new (window.AudioContext || window.webkitAudioContext)(); if (audio.state === 'suspended') audio.resume(); var o = audio.createOscillator(), g = audio.createGain(), t0 = audio.currentTime + (when || 0); o.type = type || 'sine'; o.frequency.value = f; g.gain.setValueAtTime(0, t0); g.gain.linearRampToValueAtTime(vol || .12, t0 + .01); g.gain.exponentialRampToValueAtTime(.0001, t0 + (dur || .3)); o.connect(g); g.connect(audio.destination); o.start(t0); o.stop(t0 + (dur || .3) + .05); return o; } catch (x) { } }; // ---- loop var vis = true, raf = 0, last = 0, frame = null; function tick(now) { raf = 0; if (!vis || document.hidden) return; var dt = Math.min(.05, (now - last) / 1000 || 0); last = now; S.t += dt; if (frame) frame(dt, S.t); raf = requestAnimationFrame(tick); } function kick() { if (!raf && frame) { last = performance.now(); raf = requestAnimationFrame(tick); } } S.loop = function (f) { frame = f; kick(); }; if ('IntersectionObserver' in window) new IntersectionObserver(function (en) { vis = en[0].isIntersecting; if (vis) kick(); }, { rootMargin: '80px' }).observe(root); document.addEventListener('visibilitychange', kick); if ('ResizeObserver' in window) new ResizeObserver(size).observe(cv); else window.addEventListener('resize', size); var fs = root.querySelector('.fs'); if (fs) { if (!root.requestFullscreen) fs.hidden = true; fs.addEventListener('click', function () { if (document.fullscreenElement) document.exitFullscreen(); else root.requestFullscreen(); }); } size(); fn(S); size(); } function boot() { [].forEach.call(document.querySelectorAll('.stage[data-exp]'), start); } window.WE = { mount: function (slug, fn) { reg[slug] = fn; if (document.readyState !== 'loading') boot(); }, C: C }; if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot); else boot(); })();