GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Kuiper, cosmos and drawing engines / kuiper-belt / learn-the-kuiper/202610050609

Lesson and 1 more file

A JavaScript file in the learn the kuiper folder of Kuiper Belt that wraps deg (`wrapDeg`), parses typed (`parseTyped`), finds target (`findTarget`) and calculates planar distances and angles (units: kV).

619 lines of JavaScript and HTML from kuiper-belt, in 2 files. Topic: Kuiper, cosmos and drawing engines. Section 1528 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Lesson render · kuiper-belt / learn-the-kuiper/202610050609/lesson.js lines 701–1285
  2. Modes render · kuiper-belt / learn-the-kuiper/202610050609/modes.html lines 1–34

Lesson

A JavaScript file in the learn the kuiper folder of Kuiper Belt that wraps deg (`wrapDeg`), parses typed (`parseTyped`), finds target (`findTarget`) and calculates planar distances and angles (units: kV).

renderkuiper-belt / learn-the-kuiper/202610050609/lesson.js · lines 701–1285 of 1285 · version 2026-10-05 06:09 (continued from section 1527) · Entry point · pattern exportState (shared by 2 files)

What it does
It draws its content. Defines 124 functions or classes, including LAW, SCALE, G32, newRule, lawPath. Side effects: changes the web page (DOM); reads or writes files; uses storage (browser storage, DuckDB or Parquet).
Why it exists in the grid workflow
Domain stage agent_infra (path word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief).
Inputs & outputs
In: browser storage (getItem). Out: browser storage (setItem); canvas drawing; SVG drawing.
Downstream impact
Used directly by 1 file: Lesson (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/lesson.html). Reached from 3 other entry points, including Menu (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050354/menu.html); Play (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/play.html); Solid (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/solid.html).
What it achieves
It places one (`placeOne`), solves key (`solveKey`) and turns deg (`turnDeg`). `lesson.js` LEARN THE KUIPER, rung KUIPER: the four formulas, opened up and played with. It responds to click, pointerdown and pointermove events.
Why it matters
Loaded by the page of the same name at https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/lesson.html.
Application
kuiper-belt · learn-the-kuiper/202610050609 · script
Uses
none found
Used by
loaded by Lesson (script src)
Produces
LAW, SCALE, G32, newRule, lawPath, isLive, isExercise, power, offset, params, prep, placeOne
Defines here
layout, applyPanelPos, placeCircle, fitScale, hold, releaseHold, refit, recompute, restD, keepPrev, snapRest, setRule, preset, ensureKey, startStep, land, stepKey, jumpTo, typeKey, scrubKey, setCount, tryFind, reward, gesture, note, starAt, bind, pos, up, resized
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 learn-the-kuiper/202610050609/lesson.js:701-1285 · blob 1c14cd3923030bb98dba60e43ca70bbf4f02103d · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom, filesystem, storage · domain stage: agent_infra · users: ai_agent · canonical: unknown
      if (widest > avail) { var fs = Math.max(24, Math.floor(s.fs * avail / widest)); L.style.fontSize = fs + 'px'; if (s.fs * avail / widest < 24) L.classList.add('kgl-wrap'); }
    }
    var ph = s.panel.offsetHeight; if (ph !== s.panelH) { s.panelH = ph; if (!s.first) placeCircle(); }
  }

  // ------------------------------------------------------------------ layout: a true circle, as large as the screen allows
  function layout() {
    var s = S, host = s.host, W = host.clientWidth || root.innerWidth || 390, H = host.clientHeight || root.innerHeight || 844;
    var dpr = Math.min(3, root.devicePixelRatio || 1);
    s.W = W; s.H = H; s.dpr = dpr;
    var cw = Math.round(W * dpr), chh = Math.round(H * dpr);
    if (s.canvas.width !== cw || s.canvas.height !== chh) { s.canvas.width = cw; s.canvas.height = chh; }
    var side = s.side = W > H * 1.4 && H < 560, small = Math.min(W, H) < 560;
    s.root.classList.toggle('kgl-side', side);
    s.root.classList.remove('kgl-more'); s.F.moreTag.setAttribute('aria-expanded', 'false');
    s.fs = small ? 28 : (W >= 1100 ? 32 : 30);
    s.panel.style.fontSize = s.fs + 'px';
    s.colW = side ? Math.max(300, W - H) : W;
    var icons = s.iconsEl.children.length * 44 + 8, room = (side ? s.colW : W) - icons - 5 * 26 - 16;
    var names = W >= 1000 && !side, kids = s.pipsEl.children, i;
    for (i = 0; i < kids.length && i < 5; i++) { kids[i]._name.style.display = (names || (i === 4 && room >= 100)) ? '' : 'none'; kids[i].className = 'kgl-pip' + (i === 4 ? ' on' : (root.KGPlay && typeof root.KGPlay.open === 'function' ? '' : ' dim')); }
    s.pipsEl.style.left = (side ? W - s.colW + 6 : 6) + 'px';
    var ps = s.pad.style; if (side) { ps.left = (W - s.colW) + 'px'; ps.margin = '0'; } else { ps.left = '0'; ps.margin = '0 auto'; }
    if (s.edit) s.padH = s.pad.offsetHeight;
    applyLive(); applyPanelPos();
    s.panelH = -1; fitLines(); placeCircle();
    s.first = false;
  }
  function applyPanelPos() {
    var s = S, ps = s.panel.style;
    s.F.extra.style.width = s.side ? Math.max(120, s.W - s.colW - 16) + 'px' : '';
    s.F.extra.style.maxHeight = s.side ? Math.max(80, s.H - 60) + 'px' : '';
    s.F.tags.style.left = (s.side ? s.W - s.colW : 0) + 'px';
    s.tagsH = s.edit ? 0 : s.F.tags.offsetHeight;
    if (s.side) { ps.left = (s.W - s.colW) + 'px'; ps.right = '0'; ps.top = '50px'; ps.bottom = Math.max(s.padH, s.tagsH, 44) + 'px'; }
    else { ps.left = '0'; ps.right = '0'; ps.top = 'auto'; ps.bottom = (s.padH + s.tagsH) + 'px'; }
  }
  function placeCircle() {
    var s = S, W = s.W, H = s.H, x1 = W, y0, y1, R;
    if (s.side) { x1 = W - s.colW; y0 = 0; y1 = H; R = Math.min(x1, y1 - y0) / 2 - 14; s.stackX = 30; s.stackY = H - 22; s.marksX = W - 20; }
    else {
      y0 = 50; y1 = H - s.panel.offsetHeight - s.padH - (s.tagsH || 0); R = Math.min(W, y1 - y0) / 2 - 14;
      if (W / 2 - R >= 190) { s.stackX = 30; s.stackY = y1 - 24; }
      else { y1 -= 40; R = Math.min(W, y1 - y0) / 2 - 14; s.stackX = 30; s.stackY = (y0 + y1) / 2 + R + 14 + 22; }
      s.marksX = W - 20;
    }
    s.cxT = x1 / 2; s.cyT = (y0 + y1) / 2; s.RpT = Math.max(40, R);
  }
  function fitScale() { return 0.86 * S.RpT / (S.rFit > 0 ? S.rFit : 1) * S.zoomK; }
  function hold(ms) { S.holdUntil = performance.now() + ms; }
  function releaseHold() { if (S) { hold(300); need(); } }
  function refit() { // what must fit: the last key, the selected dot, and the thing to find
    var s = S, R = s.rule, base = Math.max(s.rLast, s.selR);
    if (s.piece === 'r' && s.find) base = Math.max(base, s.tgt.r + s.tgt.w);
    if (R.rad === 'sqrt' && base < 5000) base = Math.floor(base + 1e-9) + 1; // room for the whole ring being filled
    s.rFit = base > 0 && isFinite(base) ? base : 1;
  }

  // ------------------------------------------------------------------ which stars, and where the formulas put them
  function recompute(chTop) {
    var s = S, R = s.rule, N = s.N, K = s.K, KS = s.KS, n = 0, i, k, T = s.T, st = R.start, last = st + N - 1;
    if (chTop == null) chTop = K; s.chTop = chTop;
    if (N <= CAP) { for (i = 0; i < N && i < CAP; i++) KS[n++] = st + i; }
    else {
      var lo = Math.max(st, chTop - CH), hi = Math.min(chTop, last);
      var smp = SCALE().sample({ first: st, count: N }, CAP), L = Math.min(smp.length, CAP + 2);
      for (i = 0; i < L; i++) { k = smp[i]; if (k >= lo && k <= hi) continue; KS[n++] = k; }
      for (i = 0; i <= CH; i++) { k = lo + i; if (k > hi) break; KS[n++] = k; }
    }
    s.cnt = n; s.selIdx = -1; s.sig = st + ':' + N;
    fill(R, KS, n, s.SX, s.SY);
    for (i = 0; i < n; i++) if (KS[i] === K) { s.selIdx = i; break; }
    s.chN = 0;
    for (i = 0; i <= CH + 1; i++) { k = chTop - i; if (k < 0) break; placeOne(R, k, T); s.CT[i] = T.turns * 360; s.CR[i] = T.r; s.chN = i + 1; }
    s.rLast = 0; if (N > 0) { placeOne(R, last, T); s.rLast = T.r; }
    if (K < TWO53) { placeOne(R, K + 1, T); s.nextX = T.x; s.nextY = T.y; }
    s.selTh = s.CT[chTop - K] || 0; s.selR = s.kx !== K ? radiusAt(R, s.kx) : (s.CR[chTop - K] || 0);
    s.Tdeg = turnDeg(R);
    refit();
  }
  function restD() { var s = S, D = s.D; D.has = s.K >= 1 && s.chN >= 2 && s.chTop === s.K; D.e = 1; D.cross = false; }
  function keepPrev() { var s = S, i, n = s.cnt; for (i = 0; i < n; i++) { s.PX[i] = s.SX[i]; s.PY[i] = s.SY[i]; } s.prevSig = s.sig; }
  function snapRest() { var s = S; if (s.A.on) { s.A.on = false; s.queue = 0; recompute(); restD(); s.J = 1; s.wedgeA = 1; updateFormula(); } }
  // a number of the formulas changed: every dot re-flows from the same four lines
  function setRule(change, morph) {
    var s = S, R = s.rule; snapRest(); setPlaying(false);
    if (morph && s.cnt > 0) keepPrev(); else s.prevSig = '';
    change(R); R._p = null;
    if (R.start + s.N - 1 > TWO53) s.N = Math.max(0, TWO53 - R.start + 1);
    if (s.N > 0 && s.K > R.start + s.N - 1) { s.K = R.start + s.N - 1; s.kx = s.K; } // the selected key stays among the keys shown
    recompute(); restD();
    if (morph && s.prevSig === s.sig) { s.morph = 0; s.morphT0 = performance.now(); hold(700); } else s.morph = 1;
    s.zoomK = 1; updateFormula(); need();
  }
  function preset(start, off) { setRule(function (R) { S.N = Math.max(S.N, S.K - start + 1); R.start = start; R.off = off; R.G = G32(); R.W = TWO32; R.rad = 'sqrt'; }, S.rule.start === start); }
  function ensureKey(k) { var s = S, st = s.rule.start; if (k >= st && k - st + 1 > s.N) s.N = Math.min(TWO53 - st, k - st + 1); }

  // ------------------------------------------------------------------ keys: one more equal turn each
  function startStep(dir, dur, auto) {
    var s = S, A = s.A, R = s.rule, lo, hi;
    if (dir > 0) {
      if (s.K >= TWO53 - 1) { showMsg(LIMIT_LINE); s.queue = 0; setPlaying(false); return; }
      lo = s.K; hi = lo + 1; A.newStar = hi >= R.start && hi > lastKey();
      s.K = hi; ensureKey(hi); if (A.newStar) s.userN = s.N;
    } else {
      if (s.K <= R.start) { s.queue = 0; return; }
      hi = s.K; lo = hi - 1; A.newStar = false; s.K = lo;
    }
    s.kx = s.K;
    recompute(hi);
    A.FL = angleForm(R, lo); A.FH = angleForm(R, hi);
    A.rLo = s.CR[1]; A.rHi = s.CR[0];
    var more = A.FH.nNum > A.FL.nNum;
    A.ec = !more ? Infinity : (A.FL.remDeg + s.Tdeg >= 360 - 1e-9 && s.Tdeg > 0 ? (360 - A.FL.remDeg) / s.Tdeg : 0.5);
    A.on = true; A.type = 'step'; A.dir = dir; A.t0 = performance.now(); A.dur = dur; A.auto = !!auto; A.lastText = 0;
    s.D.has = true; s.D.e = dir > 0 ? 0 : 1; s.D.cross = dir < 0 && more; s.J = 1; s.wedgeA = 1;
    updateFormula(); // exact values for the key now named; the running numbers take over on the next frame
    need();
  }
  function land(now) {
    var s = S, A = s.A;
    A.on = false;
    if (A.type === 'step') {
      if (A.dir < 0) recompute();
      restD(); updateFormula();
      s.landT0 = now; note(220 * Math.pow(2, s.selTh / 180));
      if (A.dir > 0 && A.newStar && s.rule.rad === 'sqrt' && s.K >= 1 && isSquare(s.K)) { // a new ring starts at a square number
        s.ringEvT0 = now; s.ringEvN = ringOf(s.K); note(196 * Math.pow(2, (s.ringEvN % 15) / 5), true);
      }
      if (!A.auto) { s.userLands++; if (s.userLands === 2 && !seen('h2')) { markSeen('h2'); [s.F.keyT.el, s.F.G.el, s.F.W1.el].forEach(function (e) { e.classList.add('kgl-pulse'); }); } }
    } else { s.J = 1; s.wedgeT0 = now; s.wedgeA = 0; s.landT0 = now; if (s.gest) note(220 * Math.pow(2, s.selTh / 180)); }
    if (s.queue > 0) {
      if (s.queue > 5) { var skip = s.queue - 3; s.queue = 3; s.K = Math.min(TWO53 - 2, s.K + skip); s.kx = s.K; ensureKey(s.K); s.userN = s.N; recompute(); restD(); }
      s.queue--; startStep(1, Math.max(90, 520 / (1 + s.queue)));
    } else if (s.playing) startStep(1, 125);
  }
  function stepKey(dir) {
    var s = S;
    if (s.A.on) { if (dir > 0 && s.A.type === 'step' && s.A.dir > 0) { if (s.queue < 400) s.queue++; return; } snapRest(); }
    startStep(dir, dir > 0 ? 620 : 420);
  }
  function jumpTo(k) { // a far key: no billions of wedges. The arm swings what is left after the whole circles.
    var s = S, A = s.A; k = clamp(k, 0, TWO53);
    var K = Math.floor(k), oldN = s.N, was = s.K;
    if (s.A.on) { s.A.on = false; s.queue = 0; }
    s.K = K; s.kx = k; ensureKey(K); recompute(); restD(); updateFormula();
    if (s.N > oldN) { s.pourOn = true; s.pourFrom = s.rule.start + oldN; s.pourT0 = performance.now(); }
    if (K !== was) { A.on = true; A.type = 'jump'; A.t0 = performance.now(); A.dur = 500; s.J = 0; s.wedgeA = 0; } else { s.J = 1; s.wedgeA = 1; }
    need();
  }
  function typeKey(k) { // a number was given for the key: the count goes back to what was asked for, or grows to hold it
    var s = S; setPlaying(false); s.zoomK = 1; s.N = Math.min(s.N, s.userN);
    jumpTo(k);
    if (s.piece === 'r' && s.find) tryFind(false);
  }
  function scrubKey(k) { // dragging the key number: stars pour in as it rises
    var s = S; k = clamp(Math.floor(k), s.rule.start, TWO53 - 1); if (k === s.K) return;
    if (s.A.on) { s.A.on = false; s.queue = 0; }
    var oldN = s.N; s.K = k; s.kx = k; ensureKey(k); s.userN = s.N;
    recompute(); restD(); s.J = 1; s.wedgeA = 1; updateFormula();
    if (s.N > oldN) { s.pourOn = true; s.pourFrom = s.rule.start + oldN; s.pourT0 = performance.now() - 250; }
    if (s.piece === 'r' && s.find) tryFind(false);
    note(220 * Math.pow(2, s.selTh / 180)); need();
  }
  function setCount(n) { // how many keys: the same four lines place them all
    var s = S, st = s.rule.start; n = clamp(Math.floor(n), 0, TWO53 - st); snapRest(); setPlaying(false);
    var oldN = s.N; s.N = n; s.zoomK = 1;
    s.userN = n; if (n > 0) { s.K = st + n - 1; s.kx = s.K; }
    recompute(); restD(); updateFormula();
    if (n > oldN) { s.pourOn = true; s.pourFrom = st + oldN; s.pourT0 = performance.now(); } else s.pourOn = false;
    s.J = 1; s.wedgeA = 1;
    need();
  }

  // ------------------------------------------------------------------ things to find: answered with light and a short rising phrase
  function tryFind(typed) {
    var s = S, t = s.tgt, r = s.selR, now = performance.now();
    if (!t || t.found) return;
    if (Math.abs(r - t.r) <= t.w + 1e-12) {
      if (!typed) { s.glowT0 = now; need(); return; }   // exploring is welcome; only a typed, committed number is answered
      t.found = true; // Consume before the delayed target transition.
      reward(s.cx + Math.cos(s.selTh * D2R) * r * s.vsT, s.cy - Math.sin(s.selTh * D2R) * r * s.vsT, Math.max(1, Math.round(10 * (s.findIdx + 1) / (s.nTries + 1))));
      s.findIdx++; s.nTries = 0;
      var s0 = s; s.timers.push(setTimeout(function () { if (S !== s0 || s0.tgt !== t) return; s0.tgt = findTarget(s0.findIdx); refit(); need(); }, 1100));
    } else if (typed && s.nTries < 8) { s.tries[s.nTries * 2] = r; s.tries[s.nTries * 2 + 1] = s.selTh; s.nTries++; }
  }
  function reward(x, y, points) {
    var s = S, now = performance.now(); s.burstT0 = now; s.burstX = x; s.burstY = y; s.marks++; s.score += points || 10; s.scoreStr = commas(String(s.score));
    note(392, true, 0); note(494, true, 0.11); note(587, true, 0.22); need();
  }

  // ------------------------------------------------------------------ sound: one soft note per landing, pitch from the angle
  function gesture() { if (S) S.gest = true; }
  function note(freq, extra, delay) {
    var s = S; if (!s || !s.gest || muted()) return;
    var now = performance.now(); if (!extra) { if (now - s.lastNote < 45) return; s.lastNote = now; }
    try {
      if (!s.ac) { var AC = root.AudioContext || root.webkitAudioContext; if (!AC) return; s.ac = new AC(); }
      var ac = s.ac; if (ac.state === 'suspended') ac.resume();
      var t = ac.currentTime + (delay || 0), o = ac.createOscillator(), g = ac.createGain();
      o.type = 'sine'; o.frequency.value = freq;
      g.gain.setValueAtTime(0.0001, t); g.gain.exponentialRampToValueAtTime(0.07, t + 0.012); g.gain.exponentialRampToValueAtTime(0.0001, t + 0.38);
      o.connect(g); g.connect(ac.destination); o.start(t); o.stop(t + 0.4);
    } catch (e) { }
  }

  // ------------------------------------------------------------------ touch and mouse on the drawing
  function starAt(px, py) {
    var s = S, i, n = s.cnt, best = -1, bd = Infinity, vs = s.vs, cx = s.cx, cy = s.cy;
    var hit = clamp(s.Rp * 0.62 / Math.sqrt(Math.max(1, n)), 9, 26);
    for (i = 0; i < n; i++) { var dx = cx + s.SX[i] * vs - px, dy = cy - s.SY[i] * vs - py, d = dx * dx + dy * dy; if (d < bd) { bd = d; best = i; } }
    return bd <= hit * hit ? best : -1;
  }
  function bind() {
    var s = S, cv = s.canvas;
    function pos(e) { var r = cv.getBoundingClientRect(); return [e.clientX - r.left, e.clientY - r.top]; }
    on(cv, 'pointerdown', function (e) {
      gesture();
      var p = pos(e); try { cv.setPointerCapture(e.pointerId); } catch (x) { }
      if (s.probe.on) { s.probe.on = false; s.probe.idx = -1; updateFormula(); }
      if (!s.ptr[e.pointerId]) s.nptr++; s.ptr[e.pointerId] = { x: p[0], y: p[1] };
      if (s.nptr === 2) {
        var ids = Object.keys(s.ptr), a = s.ptr[ids[0]], b = s.ptr[ids[1]];
        s.pinch = { d0: Math.max(20, Math.hypot(a.x - b.x, a.y - b.y)), z0: s.zoomK }; s.drag = null; return;
      }
      if (s.nptr > 2) { s.drag = null; return; }
      var i = starAt(p[0], p[1]);
      s.drag = { id: e.pointerId, x0: p[0], y0: p[1], star: i, key: i >= 0 ? s.KS[i] : -1, mode: '', moved: false, wasEdit: !!s.edit };
    });
    on(cv, 'pointermove', function (e) {
      var pt = s.ptr[e.pointerId]; if (!pt) return; var p = pos(e); pt.x = p[0]; pt.y = p[1];
      if (s.pinch && s.nptr === 2) { // stretch or shrink the space by hand
        var ids = Object.keys(s.ptr), a = s.ptr[ids[0]], b = s.ptr[ids[1]], d = Math.max(20, Math.hypot(a.x - b.x, a.y - b.y));
        s.zoomK = clamp(s.pinch.z0 * d / s.pinch.d0, 0.02, 200); need(); return;
      }
      var g = s.drag; if (!g || g.id !== e.pointerId) return;
      var dx = p[0] - g.x0, dy = p[1] - g.y0;
      if (!g.moved) {
        if (dx * dx + dy * dy < 100) return; g.moved = true;
        if (s.dragRule && s.piece === 't' && g.star >= 0 && g.key >= 1) { g.mode = 'turn'; endEdit(true); snapRest(); setPlaying(false); }
      }
      if (g.mode === 'turn') { // drag a dot round the circle: the multiplier follows (exploring)
        var a2 = Math.atan2(-(p[1] - s.cy), p[0] - s.cx) / TAU; if (a2 < 0) a2 += 1;
        var j = g.key, R = s.rule, W = R.W, cur = (R.G % W) / W, m = Math.round(j * cur - a2), t = (a2 + m) / j;
        if (t < 0) t += 1 / j; if (t > 1) t -= 1 / j;
        var q = j <= 1 ? 360 : j <= 2 ? 720 : j <= 10 ? 3600 : j <= 100 ? 36000 : 0;
        var Gn = q && W >= q ? Math.round(Math.round(t * q) / q * W) : Math.round(t * W);
        Gn = clamp(Gn, 0, W);
        if (Gn !== R.G) { s.F.G.el.classList.add('kgl-pulse'); R.G = Gn; R._p = null; recompute(); restD(); updateFormula(); need(); }
        hold(500);
      }
    });
    function up(e) {
      var pt = s.ptr[e.pointerId]; if (!pt) return; delete s.ptr[e.pointerId]; s.nptr = Math.max(0, s.nptr - 1);
      if (s.pinch) { if (s.nptr < 2) s.pinch = null; s.drag = null; return; }
      var g = s.drag; s.drag = null; if (!g || g.id !== e.pointerId) return;
      if (g.moved) { if (g.mode) releaseHold(); return; }
      if (e.type === 'pointercancel') return;
      // a tap
      if (g.wasEdit) { endEdit(true); return; }
      if (s.playing) { setPlaying(false); return; }
      if (g.star >= 0 && g.key !== s.K) { if (g.key === s.K + 1) stepKey(1); else jumpTo(g.key); return; }
      stepKey(1);
    }
    on(cv, 'pointerup', up); on(cv, 'pointercancel', up);
    on(cv, 'mousemove', function (e) { if (e.buttons || s.drag || s.nptr) return; var p = pos(e), P = s.probe; P.on = true; P.x = p[0]; P.y = p[1]; P.dirty = true; need(); });
    on(cv, 'mouseleave', function () { var P = s.probe; if (!P.on) return; P.on = false; P.idx = -1; P.ring = -1; if (!s.edit) updateFormula(); need(); });
    on(cv, 'wheel', function (e) { e.preventDefault(); gesture(); s.zoomK = clamp(s.zoomK * (e.deltaY < 0 ? 1.15 : 1 / 1.15), 0.02, 200); need(); }, { passive: false });
    on(document, 'keydown', function (e) {
      if (S !== s || e.ctrlKey || e.metaKey || e.altKey) return; var t = e.target; if (t && (t.tagName === 'INPUT' || t.tagName === 'TEXTAREA')) return;
      var k = e.key;
      if (s.edit) {
        if (k >= '0' && k <= '9') { e.preventDefault(); gesture(); typeCh(k, e.isTrusted === true); }
        else if (k === '.' || k === ',') { e.preventDefault(); typeCh('.', e.isTrusted === true); }
        else if (k === '-') { e.preventDefault(); typeCh('-', e.isTrusted === true); }
        else if (k === 'Backspace') { e.preventDefault(); typeCh('back', e.isTrusted === true); }
        else if (k === 'Delete' || k === 'c' || k === 'C') { e.preventDefault(); typeCh('clear', e.isTrusted === true); }
        else if (k === 'Enter' || k === 'Tab') { e.preventDefault(); endEdit(true, e.isTrusted === true); }
        else if (k === 'Escape') { e.preventDefault(); endEdit(false); }
        return;
      }
      if (k >= '0' && k <= '9') { e.preventDefault(); gesture(); var F = s.F; startEdit(s.piece === 'r' ? F.keyR : s.piece === 't' ? F.keyT : F.key); typeCh(k, e.isTrusted === true); if (s.piece === 'x') setPiece('k'); }
      else if (k === ' ' || k === 'ArrowRight' || k === 'ArrowUp') { e.preventDefault(); gesture(); setPlaying(false); stepKey(1); }
      else if (k === 'ArrowLeft' || k === 'ArrowDown') { e.preventDefault(); gesture(); setPlaying(false); stepKey(-1); }
      else if (k === 'Escape' && s.closeBtn) close();
    });
    function resized() { if (S !== s) return; layout(); need(); }
    on(s.root, 'scroll', function () { if (s.root.scrollLeft || s.root.scrollTop) { s.root.scrollLeft = 0; s.root.scrollTop = 0; } });
    on(root, 'resize', resized);
    if (typeof ResizeObserver === 'function') { s.ro = new ResizeObserver(resized); s.ro.observe(s.host); }
  }

  // ------------------------------------------------------------------ the frame
  function tick(now) {
    var s = S; if (!s) return; s.raf = 0;
    var dt = Math.min(64, Math.max(0, now - s.last)); s.last = now;
    var busy = false, A = s.A, D = s.D, i;
    if (s.fade < 1) { s.fade = clamp((now - s.t0) / 350, 0, 1); busy = true; }
    if (A.on) {
      var u = (now - A.t0) / A.dur;
      if (u >= 1) land(now);
      else if (u < 0) { /* waiting to start */ }
      else if (A.type === 'step') {
        var e = A.dur > 1000 ? easeIO(u) : easeO(u); D.e = A.dir > 0 ? e : 1 - e;
        var cross = D.e >= A.ec;
        if (cross && !D.cross && A.dir > 0) spawnFly(now);
        D.cross = cross; liveFormula(now);
      } else s.J = easeO(u);
      busy = true;
    }
    if (s.wedgeA < 1 && !(A.on && A.type === 'jump')) { s.wedgeA = clamp((now - s.wedgeT0) / 350, 0, 1); busy = true; }
    if (s.morph < 1) { s.morph = clamp((now - s.morphT0) / 550, 0, 1); busy = true; }
    if (s.pourOn) { if (now - s.pourT0 > 700) s.pourOn = false; busy = true; }
    if (now >= s.holdUntil) s.vsT = fitScale(); else busy = true;
    var kf = 1 - Math.exp(-dt / 110);
    if (Math.abs(s.vs / s.vsT - 1) > 0.002) { s.vs *= Math.pow(s.vsT / s.vs, kf); busy = true; } else s.vs = s.vsT;
    if (Math.abs(s.cx - s.cxT) + Math.abs(s.cy - s.cyT) + Math.abs(s.Rp - s.RpT) > 0.5) { s.cx += (s.cxT - s.cx) * kf; s.cy += (s.cyT - s.cy) * kf; s.Rp += (s.RpT - s.Rp) * kf; busy = true; }
    else { s.cx = s.cxT; s.cy = s.cyT; s.Rp = s.RpT; }
    for (i = 0; i < s.fly.length; i++) if (s.fly[i].on) { if (now - s.fly[i].t0 > 650) s.fly[i].on = false; else busy = true; }
    if (now - s.landT0 < 300 || now - s.burstT0 < 900 || now - s.glowT0 < 700 || now - s.ringEvT0 < 1000) busy = true;
    if (s.piece === 'r' && s.find) busy = true; // the thing to find breathes
    if (s.probe.dirty) { s.probe.dirty = false; if (s.probe.on && !s.edit && !A.on) probeFormula(); }
    draw(now);
    if (busy) need();
  }
  function spawnFly(now) { var s = S, i; if (s.piece !== 't') return; for (i = 0; i < s.fly.length; i++) if (!s.fly[i].on) { s.fly[i].on = true; s.fly[i].t0 = now; return; } }

  function sector(ctx, cx, cy, r, a0, span) { // from a0, anticlockwise by span (degrees, world: y up)
    ctx.beginPath(); ctx.moveTo(cx, cy);
    if (span >= 360) { ctx.arc(cx, cy, r, 0, TAU); }
    else { ctx.arc(cx, cy, r, -a0 * D2R, -(a0 + span) * D2R, true); ctx.closePath(); }
  }
  function arcOnly(ctx, cx, cy, r, a0, span) { ctx.beginPath(); if (span >= 360) ctx.arc(cx, cy, r, 0, TAU); else ctx.arc(cx, cy, r, -a0 * D2R, -(a0 + span) * D2R, true); }
  function label(ctx, text, x, y, color, W, H) { // 24 px, on a dark plate so it reads over stars
    var w = ctx.measureText(text).width + 12; x = clamp(x, w / 2 + 2, W - w / 2 - 2); y = clamp(y, 16, H - 16);
    ctx.fillStyle = 'rgba(0,0,0,0.72)'; ctx.fillRect(x - w / 2, y - 15, w, 30);
    ctx.fillStyle = '#fff'; ctx.fillText(text, x, y + 1);
  }
  var RSTEP = [1, 2, 5]; var RLBL = {};
  function ringLabel(v) { var t = RLBL[v]; if (!t) { t = RLBL[v] = commas(String(v)); } return t; }

  function draw(now) {
    var s = S, ctx = s.ctx, W = s.W, H = s.H, cx = s.cx, cy = s.cy, Rp = s.Rp, vs = s.vs, R = s.rule, D = s.D, A = s.A, piece = s.piece;
    var i, a, r, n = s.cnt, K = s.K, Td = s.Tdeg, started = A.on && now >= A.t0, jumping = started && A.type === 'jump', stepping = started && A.type === 'step';
    if (A.on && !started && A.type === 'jump') jumping = true;
    ctx.setTransform(s.dpr, 0, 0, s.dpr, 0, 0); ctx.globalAlpha = 1; ctx.fillStyle = '#000'; ctx.fillRect(0, 0, W, H);
    var ga = s.fade; ctx.globalAlpha = ga; ctx.lineCap = 'butt';
    ctx.font = '400 24px ' + FONT; ctx.textAlign = 'center'; ctx.textBaseline = 'middle';

    // rim, ticks every 30 degrees, the zero line at 3 o'clock
    ctx.lineWidth = 1.5; ctx.strokeStyle = 'rgba(0,255,255,0.30)'; ctx.beginPath(); ctx.arc(cx, cy, Rp, 0, TAU); ctx.stroke();
    ctx.strokeStyle = 'rgba(0,255,255,0.55)'; ctx.beginPath();
    for (i = 0; i < 12; i++) { a = i * 30 * D2R; var len = i % 3 === 0 ? 14 : 8, co = Math.cos(a), si = Math.sin(a); ctx.moveTo(cx + co * (Rp - len), cy - si * (Rp - len)); ctx.lineTo(cx + co * Rp, cy - si * Rp); }
    ctx.stroke();
    ctx.strokeStyle = 'rgba(0,255,255,0.42)'; ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(cx + Rp + 8, cy); ctx.stroke();

    // where the selected dot is right now (riding the arm while it swings)
    var selTh = s.selTh, selR = s.selR, armDeg = selTh, remDeg = selTh, travel = false;
    if (stepping) { armDeg = s.CT[1] + D.e * Td; remDeg = armDeg % 360; if (A.dir > 0) { travel = true; selTh = armDeg; selR = A.rLo + (A.rHi - A.rLo) * D.e; } }
    else if (jumping) { armDeg = remDeg = selTh * s.J; travel = true; selR = selR * (0.35 + 0.65 * s.J); selTh = armDeg; }
    var selX = cx + Math.cos(selTh * D2R) * selR * vs, selY = cy - Math.sin(selTh * D2R) * selR * vs;
    var m = s.morph < 1 ? easeIO(s.morph) : 1;
    if (m < 1 && s.selIdx >= 0) { // the selected dot mid re-flow
      var px0 = s.PX[s.selIdx], py0 = s.PY[s.selIdx], x1 = s.SX[s.selIdx], y1 = s.SY[s.selIdx];
      var a0 = Math.atan2(py0, px0), a1 = Math.atan2(y1, x1), da = a1 - a0; if (da > Math.PI) da -= TAU; if (da < -Math.PI) da += TAU;
      var rr = Math.hypot(px0, py0) + (Math.hypot(x1, y1) - Math.hypot(px0, py0)) * m, aa = a0 + da * m;
      selX = cx + Math.cos(aa) * rr * vs; selY = cy - Math.sin(aa) * rr * vs; selR = rr; armDeg = remDeg = aa / D2R; if (remDeg < 0) remDeg += 360;
    }
    var showDot = K >= R.start || travel || s.kx !== K;

    // rings of whole-number distance. Their numbers re-label as the space stretches: 1, 2, 3 ... 10, 20, 30 ... 100, 200 ...
    var distPiece = piece === 'r' || piece === 'k';
    if (!s.band || piece !== 'r') {
      var stp = 1, g2 = 0, want = 44 / vs; while (stp < want && g2 < 60) { g2++; stp = RSTEP[g2 % 3] * Math.pow(10, Math.floor(g2 / 3)); }
      var unit = vs >= 7 ? 1 : stp;
      ctx.strokeStyle = distPiece ? 'rgba(0,255,255,0.22)' : 'rgba(0,255,255,0.13)'; ctx.lineWidth = 1; ctx.beginPath();
      for (i = 1; i <= 200; i++) { r = i * unit * vs; if (r >= Rp - 2) break; ctx.moveTo(cx + r, cy); ctx.arc(cx, cy, r, 0, TAU); }
      ctx.stroke();
      if (distPiece) {
        // each ring fills as its dots arrive: ring n begins at key n x n and holds 2n + 1 dots
        if (R.rad === 'sqrt' && s.N > 0 && vs >= 7 && !(s.find && piece === 'r')) {
          var last = R.start + s.N - 1, nOut = ringOf(last), nn, lo2, hi2, fr;
          ctx.lineWidth = 2;
          for (i = 0; i < 40; i++) {
            nn = nOut - i; if (nn < 1) break; r = nn * vs; if (r >= Rp + 40) continue;
            lo2 = Math.max(nn * nn, R.start); hi2 = Math.min(nn * nn + 2 * nn, last); fr = Math.max(0, hi2 - lo2 + 1) / (2 * nn + 1);
            ctx.strokeStyle = fr >= 1 ? 'rgba(0,255,255,0.32)' : 'rgba(0,255,255,0.75)';
            arcOnly(ctx, cx, cy, r, 0, fr * 360); ctx.stroke();
          }
        }
        ctx.fillStyle = 'rgba(0,255,255,0.85)';
        for (i = 1; i <= 12; i++) { r = i * stp * vs; if (r >= Rp - 14) break; label(ctx, ringLabel(i * stp), cx + r, cy + 19, 'rgba(0,255,255,0.9)', W, H); }
      }
    } else { // the band each key owns
      var kk = stepping ? (s.chTop - 1 + D.e) : K, r0 = bandEdge(R, kk - 0.5) * vs, r1 = bandEdge(R, kk + 0.5) * vs, prev;
      ctx.strokeStyle = 'rgba(0,255,255,0.20)'; ctx.lineWidth = 1; ctx.beginPath(); prev = r0;
      for (i = 1; i <= 48; i++) { if (K - 0.5 - i < -1) break; r = bandEdge(R, K - 0.5 - i) * vs; if (prev - r < 3 || r <= 0) break; ctx.moveTo(cx + r, cy); ctx.arc(cx, cy, r, 0, TAU); prev = r; }
      prev = r1;
      for (i = 1; i <= 48; i++) { r = bandEdge(R, K + 0.5 + i) * vs; if (r - prev < 3 || r > Rp) break; ctx.moveTo(cx + r, cy); ctx.arc(cx, cy, r, 0, TAU); prev = r; }
      ctx.stroke();
      if (r1 - r0 < 1.6) { ctx.strokeStyle = 'rgba(0,255,255,0.75)'; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(cx, cy, (r0 + r1) / 2, 0, TAU); ctx.stroke(); }
      else { ctx.fillStyle = 'rgba(0,255,255,0.34)'; ctx.beginPath(); ctx.arc(cx, cy, r1, 0, TAU, false); ctx.arc(cx, cy, r0, TAU, 0, true); ctx.fill(); }
    }
    // a new ring starting
    var rv = now - s.ringEvT0;
    if (rv < 1000) { ctx.globalAlpha = ga * (1 - rv / 1000); ctx.strokeStyle = '#ffffff'; ctx.lineWidth = 2 + 4 * (1 - rv / 1000); ctx.beginPath(); ctx.arc(cx, cy, s.ringEvN * vs, 0, TAU); ctx.stroke(); ctx.globalAlpha = ga; }

    // the thing to find: a glowing band at a distance; type the number whose square root reaches it
    if (piece === 'r' && s.find && !s.band) {
      var tr = s.tgt.r * vs, tw = s.tgt.w * vs, pul = 0.5 + 0.5 * Math.sin(now / 420), gl = now - s.glowT0 < 700 ? 0.4 : 0;
      ctx.strokeStyle = '#00ffff'; ctx.globalAlpha = ga * (0.10 + 0.07 * pul + gl * 0.5); ctx.lineWidth = tw * 2 + 14; ctx.beginPath(); ctx.arc(cx, cy, tr, 0, TAU); ctx.stroke(); // a soft halo so a thin band can be seen
      ctx.globalAlpha = ga * (0.38 + 0.2 * pul + gl); ctx.lineWidth = Math.max(2, tw * 2); ctx.beginPath(); ctx.arc(cx, cy, tr, 0, TAU); ctx.stroke(); // the band itself, at its true width
      ctx.globalAlpha = ga * 0.5; ctx.strokeStyle = '#ffffff'; ctx.lineWidth = 1.5;
      for (i = 0; i < s.nTries && i < 8; i++) { ctx.beginPath(); ctx.arc(cx + Math.cos(s.tries[i * 2 + 1] * D2R) * s.tries[i * 2] * vs, cy - Math.sin(s.tries[i * 2 + 1] * D2R) * s.tries[i * 2] * vs, 6, 0, TAU); ctx.stroke(); }
      ctx.globalAlpha = ga;
    }

    // the wedges: every key is one more equal turn. Ghosts behind, the newest in amber.
    if (piece === 't' && D.has && m >= 1) {
      var wa = s.wedgeA * ga, gapPx = Math.abs(s.CR[0] - s.CR[1]) * vs, G = gapPx >= 1.2 ? 9 : 0;
      ctx.lineWidth = 2;
      for (i = Math.min(G, s.chN - 2); i >= 1; i--) { // key chTop - i: from the dot before it, round by one turn
        r = s.CR[i] * vs; if (r < 1) continue; a = s.CT[i + 1];
        if (i <= 3) { ctx.globalAlpha = wa; ctx.fillStyle = i === 1 ? 'rgba(255,176,0,0.10)' : i === 2 ? 'rgba(255,176,0,0.06)' : 'rgba(255,176,0,0.035)'; sector(ctx, cx, cy, r, a, Td); ctx.fill(); }
        ctx.globalAlpha = wa * Math.max(0.12, 0.62 - i * 0.06); ctx.strokeStyle = '#ffb000'; arcOnly(ctx, cx, cy, r, a, Td); ctx.stroke();
      }
      ctx.globalAlpha = wa;
      r = (stepping ? A.rLo + (A.rHi - A.rLo) * D.e : s.CR[0]) * vs; a = s.CT[1];
      var span = (stepping ? D.e : 1) * Td;
      if (r >= 1 && span > 0) {
        ctx.fillStyle = 'rgba(255,176,0,0.26)'; sector(ctx, cx, cy, r, a, span); ctx.fill();
        ctx.strokeStyle = '#ffb000'; ctx.lineWidth = 3; arcOnly(ctx, cx, cy, r, a, span); ctx.stroke();
        ctx.lineWidth = 1.5; ctx.globalAlpha = wa * 0.7; ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(cx + Math.cos(a * D2R) * r, cy - Math.sin(a * D2R) * r); ctx.stroke();
      }
      ctx.globalAlpha = ga;
    }

    // what is left after the whole circles: the green arc along the rim, and the arm
    var ax = Math.cos(armDeg * D2R), ay = Math.sin(armDeg * D2R);
    if (piece === 't') {
      if (remDeg > 0.01) { ctx.strokeStyle = '#00ffff'; ctx.lineWidth = 4; arcOnly(ctx, cx, cy, Rp, 0, remDeg); ctx.stroke(); }
      ctx.strokeStyle = 'rgba(255,176,0,0.85)'; ctx.lineWidth = 2; ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(cx + ax * Rp, cy - ay * Rp); ctx.stroke();
      ctx.fillStyle = '#00ffff'; ctx.beginPath(); ctx.arc(cx + ax * Rp, cy - ay * Rp, 5, 0, TAU); ctx.fill();
    }

    // the square on the radius: its side is r, its area is the number under the root
    if (piece === 'r' && showDot && !s.band) {
      var ux = selX - cx, uy = selY - cy, qx = -uy, qy = ux; if (qy < 0) { qx = -qx; qy = -qy; } // keep the square on the lower side, clear of the top edge
      ctx.fillStyle = 'rgba(0,255,255,0.10)'; ctx.strokeStyle = 'rgba(0,255,255,0.7)'; ctx.lineWidth = 2;
      ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(selX, selY); ctx.lineTo(selX + qx, selY + qy); ctx.lineTo(cx + qx, cy + qy); ctx.closePath(); ctx.fill(); ctx.stroke();
      if (Math.hypot(ux, uy) > 54) label(ctx, s.lblArea, cx + (ux + qx) / 2, cy + (uy + qy) / 2, '#00ffff', W, H);
    }
    // across and up: the triangle under the dot
    if (piece === 'x' && showDot) {
      ctx.lineWidth = 3; ctx.strokeStyle = '#ffb000'; ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(selX, cy); ctx.lineTo(selX, selY); ctx.stroke();
      ctx.fillStyle = 'rgba(255,176,0,0.10)'; ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(selX, cy); ctx.lineTo(selX, selY); ctx.closePath(); ctx.fill();
    }

    // the dots
    var rDot = clamp(Rp * 0.32 / Math.sqrt(Math.max(1, n)), 1.6, 9), hr = rDot * 2.6, spr = s.sprC;
    var tiny = rDot < 2.2 && n > 600, pour = s.pourOn, pu = pour ? (now - s.pourT0) / 450 : 1, pspan = Math.max(1, R.start + s.N - s.pourFrom);
    var selKeyIdx = s.selIdx, x, y, k, solo = piece === 'r' && s.find && !s.band, ga0 = ga;
    if (solo) ga = ga * 0.16;
    ctx.globalAlpha = ga; ctx.fillStyle = '#00ffff';
    for (i = 0; i < n; i++) {
      if (i === selKeyIdx) continue;
      if (m < 1) {
        var p0x = s.PX[i], p0y = s.PY[i], q1x = s.SX[i], q1y = s.SY[i], b0 = Math.atan2(p0y, p0x), b1 = Math.atan2(q1y, q1x), db = b1 - b0;
        if (db > Math.PI) db -= TAU; if (db < -Math.PI) db += TAU;
        var h0 = Math.hypot(p0x, p0y), hh = h0 + (Math.hypot(q1x, q1y) - h0) * m, bb = b0 + db * m;
        x = cx + Math.cos(bb) * hh * vs; y = cy - Math.sin(bb) * hh * vs;
      } else { x = cx + s.SX[i] * vs; y = cy - s.SY[i] * vs; }
      if (x < -20 || y < -20 || x > W + 20 || y > H + 20) continue;
      if (pour) { k = s.KS[i]; if (k >= s.pourFrom) { var pa = clamp((pu * 1.4 - (k - s.pourFrom) / pspan) * 4, 0, 1); if (pa <= 0) continue; ctx.globalAlpha = ga * pa; } else ctx.globalAlpha = ga; }
      if (tiny) ctx.fillRect(x - rDot, y - rDot, rDot * 2, rDot * 2); else ctx.drawImage(spr, x - hr, y - hr, hr * 2, hr * 2);
    }
    ga = ga0; ctx.globalAlpha = ga;

    // the distance: one line from the centre to the selected dot
    if (showDot) {
      ctx.strokeStyle = '#00ffff'; ctx.lineWidth = piece === 'r' ? 4 : 2; ctx.globalAlpha = ga * (piece === 'r' || piece === 'x' ? 1 : 0.7); ctx.beginPath(); ctx.moveTo(cx, cy); ctx.lineTo(selX, selY); ctx.stroke(); ctx.globalAlpha = ga;
    }
    ctx.fillStyle = 'rgba(0,255,255,0.9)'; ctx.beginPath(); ctx.arc(cx, cy, 3, 0, TAU); ctx.fill();
    // the selected dot, amber
    var lf = now - s.landT0, pop = lf < 300 ? 1 + 0.9 * (1 - lf / 300) : 1, sr = Math.max(rDot, 5) * 2.6 * pop;
    if (showDot && piece === 'r' && s.find) {
      var cl = 1 / (1 + Math.abs(s.selR - s.tgt.r) / Math.max(s.tgt.w * 4, 0.05)), wr = sr * (1 + 1.1 * cl);
      ctx.globalAlpha = ga * (0.1 + 0.7 * cl); ctx.drawImage(s.sprA, selX - wr, selY - wr, wr * 2, wr * 2); ctx.globalAlpha = ga;
    }
    if (showDot) ctx.drawImage(s.sprA, selX - sr, selY - sr, sr * 2, sr * 2);

    // key numbers on the dots while they are few, so the order of placement can be read
    var few = n <= 64 && Rp * 1.4 / Math.sqrt(Math.max(1, n)) >= 50 && m >= 1;
    if (few) {
      for (i = 0; i < n; i++) {
        k = s.KS[i]; if (k > 99) continue; var isSel = i === selKeyIdx; if (solo && !isSel) continue; if (isSel) { if (s.kx !== K) continue; x = selX; y = selY; } else { x = cx + s.SX[i] * vs; y = cy - s.SY[i] * vs; }
        var ox = x - cx, oy = y - cy, ol = Math.hypot(ox, oy) || 1, off = (isSel ? Math.max(rDot, 5) : rDot) + 17;
        ctx.fillStyle = isSel ? '#ffffff' : 'rgba(255,255,255,0.66)';
        ctx.fillText(LABELS[k], x + ox / ol * off, y + oy / ol * off + 1);
      }
    }
    // the selected dot's numbers, on the drawing
    if (piece === 't' && (K > 0 || travel)) {
      var lr = Rp - 26, tw3 = ctx.measureText(s.lblRem).width / 2 + 12, back = Math.min(remDeg, (tw3 / lr) / D2R), la = (armDeg - back) * D2R;
      label(ctx, s.lblRem, cx + Math.cos(la) * lr, cy - Math.sin(la) * lr, '#00ffff', W, H);
    }
    if (piece === 'r' && showDot) {
      var mx = (cx + selX) / 2, my = (cy + selY) / 2, nx = -(selY - cy), ny = selX - cx, nl = Math.hypot(nx, ny);
      if (piece === 'r' && ny > 0) { nx = -nx; ny = -ny; } // the square is below; the length reads above
      if (nl > 44) label(ctx, s.lblDist, mx + nx / nl * 22, my + ny / nl * 22, '#00ffff', W, H);
    }
    if (piece === 'x' && showDot) {
      if (Math.abs(selX - cx) > 40) label(ctx, s.lblX, (cx + selX) / 2, cy + (selY < cy ? 20 : -20), '#ffb000', W, H);
      if (Math.abs(selY - cy) > 24) label(ctx, s.lblY, selX + (selX < cx ? -1 : 1) * (ctx.measureText(s.lblY).width / 2 + 14), (cy + selY) / 2, '#ffb000', W, H);
    }

    // whole circles: each time the arm passes the zero line a ring leaves the rim and drops onto the stack
    var sx = s.stackX, sy = s.stackY;
    if (piece === 't') {
      var cnt = (stepping ? (D.cross ? A.FH.nNum : A.FL.nNum) : s.stackNum), str = stepping ? (D.cross ? A.FH.nStr : A.FL.nStr) : s.stackStr;
      var inflight = 0; for (i = 0; i < s.fly.length; i++) if (s.fly[i].on) inflight++;
      ctx.strokeStyle = '#00ffff'; ctx.lineWidth = 3; ctx.textAlign = 'left'; ctx.fillStyle = '#00ffff';
      var shown = cnt <= 6 ? LABELS[Math.max(0, cnt - inflight)] : str, tw2 = ctx.measureText(shown).width, ringsN = cnt <= 6 ? Math.max(1, cnt - inflight) : 1, rx0 = sx - 12 + tw2 + 20;
      ctx.fillStyle = '#fff'; ctx.fillText(shown, sx - 12, sy + 1); ctx.textAlign = 'center';
      if (cnt - inflight <= 0 && cnt <= 6) ctx.globalAlpha = ga * 0.35;
      for (i = 0; i < ringsN; i++) { ctx.beginPath(); ctx.arc(rx0 + i * 15, sy, 12, 0, TAU); ctx.stroke(); }
      ctx.globalAlpha = ga;
      for (i = 0; i < s.fly.length; i++) if (s.fly[i].on) {
        var fu = easeIO(clamp((now - s.fly[i].t0) / 650, 0, 1)), slot = cnt > 6 ? 0 : Math.max(0, cnt - 1);
        var tx = rx0 + slot * 15, fx = cx + (tx - cx) * fu, fy = cy + (sy - cy) * fu, fr2 = Rp + (12 - Rp) * fu;
        ctx.globalAlpha = ga * (0.55 + 0.45 * fu); ctx.lineWidth = 3 + 2 * (1 - fu); ctx.beginPath(); ctx.arc(fx, fy, fr2, 0, TAU); ctx.stroke(); ctx.globalAlpha = ga;
      }
    }
    // a small lit mark for each thing found
    if (s.marks > 0) {
      ctx.fillStyle = '#ffb000'; ctx.textAlign = 'right'; ctx.fillStyle = '#fff'; ctx.fillText(s.scoreStr, s.marksX - Math.min(s.marks, 8) * 18 - 2, sy + 1); ctx.textAlign = 'center';
      for (i = 0; i < s.marks && i < 8; i++) { var qx2 = s.marksX - i * 18, qy2 = sy; ctx.beginPath(); ctx.moveTo(qx2, qy2 - 7); ctx.lineTo(qx2 + 2.4, qy2 - 2.4); ctx.lineTo(qx2 + 7, qy2); ctx.lineTo(qx2 + 2.4, qy2 + 2.4); ctx.lineTo(qx2, qy2 + 7); ctx.lineTo(qx2 - 2.4, qy2 + 2.4); ctx.lineTo(qx2 - 7, qy2); ctx.lineTo(qx2 - 2.4, qy2 - 2.4); ctx.closePath(); ctx.fill(); }
    }
    // found: light
    var bt = now - s.burstT0;
    if (bt < 900) {
      var bu = bt / 900, br = 20 + 150 * easeO(bu);
      ctx.globalAlpha = ga * (1 - bu); ctx.strokeStyle = '#ffffff'; ctx.lineWidth = 4 * (1 - bu) + 1; ctx.beginPath(); ctx.arc(s.burstX, s.burstY, br, 0, TAU); ctx.stroke();
      ctx.strokeStyle = '#ffb000'; ctx.lineWidth = 3; ctx.beginPath();
      for (i = 0; i < 16; i++) { a = i * TAU / 16 + 0.2; ctx.moveTo(s.burstX + Math.cos(a) * br * 0.55, s.burstY + Math.sin(a) * br * 0.55); ctx.lineTo(s.burstX + Math.cos(a) * br * 0.92, s.burstY + Math.sin(a) * br * 0.92); }
      ctx.stroke(); ctx.globalAlpha = ga;
    }

    // roaming: the nearest drawn key lights, with its number and how far away it is
    var P = s.probe;
    if (P.on && P.idx >= 0 && P.idx < n && !A.on) {
      var qx3 = cx + s.SX[P.idx] * vs, qy3 = cy - s.SY[P.idx] * vs;
      ctx.strokeStyle = '#ffffff'; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(qx3, qy3, hr + 3, 0, TAU); ctx.stroke();
      ctx.globalAlpha = ga * 0.6; ctx.lineWidth = 1.5; ctx.beginPath(); ctx.moveTo(P.x, P.y); ctx.lineTo(qx3, qy3); ctx.stroke(); ctx.globalAlpha = ga;
      if (!few) label(ctx, P.keyStr, qx3, qy3 - hr - 20, '#ffffff', W, H);
      if (Math.hypot(P.x - qx3, P.y - qy3) > 70) label(ctx, P.gap, (P.x + qx3) / 2, (P.y + qy3) / 2, 'rgba(255,255,255,0.8)', W, H);
    }
    ctx.globalAlpha = 1;
  }

  // ------------------------------------------------------------------ what a test may ask the screen
  function debug() {
    var s = S; if (!s) return null;
    var stars = [], i, lim = Math.min(s.cnt, 40), R = s.rule;
    for (i = 0; i < lim; i++) stars.push({ key: s.KS[i], x: s.cx + s.SX[i] * s.vs, y: s.cy - s.SY[i] * s.vs });
    return {
      dragRule: s.dragRule, stripKey: s.F.sk.textContent, rootWorking: s.F.rSq.textContent, K: s.K, kx: s.kx, N: s.N, cnt: s.cnt, piece: s.piece, start: R.start, G: R.G, circle: R.W, rad: R.rad, off: R.off, live: isLive(R), exercise: isExercise(R),
      circles: s.form ? s.form.nStr : '', product: s.form ? s.form.prodStr : '', remainder: s.form ? s.form.mStr : '', m: s.form ? s.form.m : 0, deg: s.form ? s.form.degStr : '', diff: s.diff,
      selR: s.selR, selTh: s.selTh, rFit: s.rFit, animating: s.A.on, queue: s.queue, playing: s.playing, band: s.band, editing: !!s.edit,
      find: s.find, findIdx: s.findIdx, target: { r: s.tgt.r, w: s.tgt.w }, marks: s.marks, score: s.score, copied: s.copied || '', sheet: sheetText(), hash: hashText(),
      cx: s.cx, cy: s.cy, Rp: s.Rp, vs: s.vs, vsT: s.vsT, W: s.W, H: s.H, side: s.side, stackX: s.stackX, stackY: s.stackY, raf: !!s.raf, stars: stars,
      sel: { x: s.cx + Math.cos(s.selTh * D2R) * s.selR * s.vs, y: s.cy - Math.sin(s.selTh * D2R) * s.selR * s.vs }
    };
  }

  var api = { open: open, close: function () { close(); }, math: M, debug: debug, state: function () { return S ? exportState() : null; } };
  root.KGLesson = api;
  if (typeof module !== 'undefined' && module.exports) module.exports = api;
  if (typeof window !== 'undefined') {
    (root.KGModules = root.KGModules || []).push({
      id: 'lesson', label: 'KUIPER', sentence: 'The four formulas of the Kuiper',
      open: function (host, opts) { return open(host, opts); }, close: function () { close(); }
    });
  }
})(typeof window !== 'undefined' ? window : globalThis);

Modes

A web page in the learn the kuiper folder of Kuiper Belt. Published at https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/modes.html.

renderkuiper-belt / learn-the-kuiper/202610050609/modes.html · lines 1–34 of 34 · version 2026-10-05 06:09 · Entry point

What it does
It draws its content. Uses libraries modules/realgame-data.js, modules/grid-data.js. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage agent_infra (path word 'kuiper'): AI-agent memory, coordination and code audit (the multi-agent journey named in Ventus Ltd's brief).
Inputs & outputs
Out: web page.
Downstream impact
Used directly by 1 file: Menu (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050354/menu.html). Reached from 3 other entry points, including Lesson (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/lesson.html); Play (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/play.html); Solid (kuiper-belt, https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/solid.html). It is itself an entry point, published at https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050609/modes.html.
What it achieves
Fully offline: every file is in this folder, system fonts only, no network needed.
Why it matters
Loaded by the Menu page at https://ventusltd.github.io/kuiper-belt/learn-the-kuiper/202610050354/menu.html.
Application
kuiper-belt · learn-the-kuiper/202610050609 · page
Uses
Audio Unlock (script src), Kuiper Law (script src), Scale Logic (script src), Start Here (script src), Radar (script src), Realgame (script src), Strings (script src), Zoom (script src), and 2 more, modules/realgame-data.js, modules/grid-data.js
Used by
linked from Menu (link)
Produces
none declared
Defines here
none
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
kuiper-belt@26fba12ab1b5bab8e71934ab86c58273e5f32682 learn-the-kuiper/202610050609/modes.html:1-34 · blob 96e555dcdc091b63c39c32ee061c8b9e140c023f · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: agent_infra · users: ai_agent · canonical: unknown
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
<meta name="color-scheme" content="dark">
<title>Learn the Kuiper</title>
<style>html,body{margin:0;height:100%;background:#000;color:#fff;overflow:hidden;overscroll-behavior:none}body{position:fixed;left:0;top:0;right:0;height:100%;height:100dvh;touch-action:none}</style>
</head>
<body>
<!-- Fully offline: every file is in this folder, system fonts only, no network needed. Double-click index.html. -->
<script src="audio-unlock.js"></script>
<script src="kuiper-law.js"></script>
<script src="scale-logic.js"></script>
<script src="start-here.js"></script>
<!-- the modules (each registers itself in window.KGModules; a missing file is skipped) -->
<script src="modules/radar.js" onerror="this.remove()"></script>
<script src="modules/realgame-data.js" onerror="this.remove()"></script>
<script src="modules/realgame.js" onerror="this.remove()"></script>
<script src="modules/strings.js" onerror="this.remove()"></script>
<script src="modules/zoom.js" onerror="this.remove()"></script>
<script src="modules/grid-data.js" onerror="this.remove()"></script>
<script src="modules/grid.js" onerror="this.remove()"></script>
<script src="modules/descent.js" onerror="this.remove()"></script>
<script>
  // one sheet, full screen. ?quiet skips the opening moment; ?run=SENTENCE runs one sentence after opening (for looking at it).
  // A shared zoom link (#key=N&zoom=Z, written by the ZOOM module) opens the sheet quietly and goes straight to ZOOM.
  var q = new URLSearchParams(location.search), zoomLink = /^#key=/.test(location.hash);
  window.KGStartHere.open({ quiet: q.has('quiet') || q.has('run') || zoomLink });
  if (q.get('run')) setTimeout(function () { window.KGStartHere.run(q.get('run')); }, 200);
  else if (zoomLink) setTimeout(function () { window.KGStartHere.openModule('zoom'); }, 50);
</script>
</body>
</html>