Watch Encyclopedia / website · watchencyclopedia.org / assets / we.js
| 1 | /* Watch Encyclopedia stage runtime. Each entry registers WE.mount(slug, fn) and draws on a canvas. */ |
| 2 | (function () { |
| 3 | 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' }; |
| 4 | var reg = {}, audio = null; |
| 5 | function el(tag, cls, txt) { var e = document.createElement(tag); if (cls) e.className = cls; if (txt != null) e.textContent = txt; return e; } |
| 6 | function start(root) { |
| 7 | var fn = reg[root.dataset.exp]; if (!fn || root._we) return; root._we = 1; |
| 8 | var cv = root.querySelector('canvas'), ctx = cv.getContext('2d'), ui = root.querySelector('.ctl'), cap = root.querySelector('.cap'); |
| 9 | var S = { el: root, cv: cv, ctx: ctx, C: C, W: 0, H: 0, t: 0, dirty: true, TAU: Math.PI * 2 }; |
| 10 | function size() { |
| 11 | var r = cv.getBoundingClientRect(), d = Math.min(2, window.devicePixelRatio || 1); |
| 12 | if (!r.width) return; |
| 13 | S.W = r.width; S.H = r.height; S.dpr = d; S.u = Math.min(S.W, S.H); |
| 14 | cv.width = Math.round(r.width * d); cv.height = Math.round(r.height * d); |
| 15 | ctx.setTransform(d, 0, 0, d, 0, 0); S.dirty = true; if (S.onresize) S.onresize(); |
| 16 | } |
| 17 | S.clear = function (c) { ctx.fillStyle = c || C.bg; ctx.fillRect(0, 0, S.W, S.H); }; |
| 18 | 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); }; |
| 19 | 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(); }; |
| 20 | 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(); }; |
| 21 | 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(); }; |
| 22 | 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(); }; |
| 23 | 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(); } }; |
| 24 | S.say = function (h) { if (cap && cap._h !== h) { cap._h = h; cap.innerHTML = h; } }; |
| 25 | S.small = function () { return S.W < 560; }; |
| 26 | // ---- controls |
| 27 | S.slider = function (label, min, max, val, step, fmt, cb) { |
| 28 | var w = el('label', 'c-sl'), top = el('span', 'c-lab'), nm = el('span', '', label), out = el('b'), inp = el('input'); |
| 29 | inp.type = 'range'; inp.min = min; inp.max = max; inp.step = step || 1; inp.value = val; |
| 30 | top.appendChild(nm); top.appendChild(out); w.appendChild(top); w.appendChild(inp); ui.appendChild(w); |
| 31 | 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; } }; |
| 32 | inp.addEventListener('input', function () { o.v = +inp.value; out.textContent = fmt ? fmt(o.v) : o.v; S.dirty = true; if (cb) cb(o.v); }); |
| 33 | out.textContent = fmt ? fmt(o.v) : o.v; return o; |
| 34 | }; |
| 35 | S.toggle = function (label, on, cb) { |
| 36 | var b = el('button', 'c-tg', label); b.type = 'button'; b.setAttribute('aria-pressed', !!on); ui.appendChild(b); |
| 37 | var o = { v: !!on, el: b, set: function (v) { o.v = !!v; b.setAttribute('aria-pressed', o.v); S.dirty = true; } }; |
| 38 | b.addEventListener('click', function () { o.set(!o.v); if (cb) cb(o.v); }); return o; |
| 39 | }; |
| 40 | 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; }; |
| 41 | S.select = function (label, opts, val, cb) { |
| 42 | var w = el('div', 'c-seg'); w.setAttribute('role', 'group'); w.setAttribute('aria-label', label); |
| 43 | if (label) w.appendChild(el('span', 'c-lab', label)); |
| 44 | var row = el('div', 'c-row'); w.appendChild(row); ui.appendChild(w); |
| 45 | 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; } }; |
| 46 | 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); }); }); |
| 47 | o.set(val); return o; |
| 48 | }; |
| 49 | S.input = function (label, val, cb, max) { |
| 50 | 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; |
| 51 | w.appendChild(top); w.appendChild(inp); ui.appendChild(w); var o = { v: val, el: inp }; |
| 52 | inp.addEventListener('input', function () { o.v = inp.value; S.dirty = true; if (cb) cb(o.v); }); return o; |
| 53 | }; |
| 54 | S.note = function (txt) { var p = el('span', 'c-note', txt); ui.appendChild(p); return p; }; |
| 55 | // ---- pointer (canvas coords in CSS px) |
| 56 | S.ptr = { x: -1, y: -1, down: false }; |
| 57 | S.pointer = function (h) { |
| 58 | cv.style.touchAction = h.scroll ? 'pan-y' : 'none'; cv.style.cursor = h.cursor || 'pointer'; |
| 59 | function pos(e) { var r = cv.getBoundingClientRect(); S.ptr.x = e.clientX - r.left; S.ptr.y = e.clientY - r.top; } |
| 60 | 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; }); |
| 61 | 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; }); |
| 62 | 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; } |
| 63 | cv.addEventListener('pointerup', up); cv.addEventListener('pointercancel', up); |
| 64 | cv.addEventListener('pointerleave', function () { if (!S.ptr.down) { S.ptr.x = -1; S.ptr.y = -1; S.dirty = true; } }); |
| 65 | 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 }); |
| 66 | }; |
| 67 | // ---- sound (only after a tap) |
| 68 | S.tone = function (f, dur, type, vol, when) { |
| 69 | try { |
| 70 | audio = audio || new (window.AudioContext || window.webkitAudioContext)(); if (audio.state === 'suspended') audio.resume(); |
| 71 | var o = audio.createOscillator(), g = audio.createGain(), t0 = audio.currentTime + (when || 0); |
| 72 | 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)); |
| 73 | o.connect(g); g.connect(audio.destination); o.start(t0); o.stop(t0 + (dur || .3) + .05); return o; |
| 74 | } catch (x) { } |
| 75 | }; |
| 76 | // ---- loop |
| 77 | var vis = true, raf = 0, last = 0, frame = null; |
| 78 | function tick(now) { |
| 79 | raf = 0; if (!vis || document.hidden) return; |
| 80 | var dt = Math.min(.05, (now - last) / 1000 || 0); last = now; S.t += dt; |
| 81 | if (frame) frame(dt, S.t); raf = requestAnimationFrame(tick); |
| 82 | } |
| 83 | function kick() { if (!raf && frame) { last = performance.now(); raf = requestAnimationFrame(tick); } } |
| 84 | S.loop = function (f) { frame = f; kick(); }; |
| 85 | if ('IntersectionObserver' in window) new IntersectionObserver(function (en) { vis = en[0].isIntersecting; if (vis) kick(); }, { rootMargin: '80px' }).observe(root); |
| 86 | document.addEventListener('visibilitychange', kick); |
| 87 | if ('ResizeObserver' in window) new ResizeObserver(size).observe(cv); else window.addEventListener('resize', size); |
| 88 | var fs = root.querySelector('.fs'); |
| 89 | if (fs) { if (!root.requestFullscreen) fs.hidden = true; fs.addEventListener('click', function () { if (document.fullscreenElement) document.exitFullscreen(); else root.requestFullscreen(); }); } |
| 90 | size(); fn(S); size(); |
| 91 | } |
| 92 | function boot() { [].forEach.call(document.querySelectorAll('.stage[data-exp]'), start); } |
| 93 | window.WE = { mount: function (slug, fn) { reg[slug] = fn; if (document.readyState !== 'loading') boot(); }, C: C }; |
| 94 | if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', boot); else boot(); |
| 95 | })(); |