In this section
- Iterations — 08 Space and Time render · galaxies-wafers / iterations/08-space-and-time/app.mjs lines 1–626
- Clock transform · galaxies-wafers / iterations/08-space-and-time/clock.mjs lines 1–74
Iterations — 08 Space and Time
A JavaScript module in Galaxies Wafers that draws lines meta and families, using SHA-256. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/.
rendergalaxies-wafers / iterations/08-space-and-time/app.mjs · lines 1–626 of 626 · Entry point · pattern nearestKeyAt (shared by 16 files)
- What it does
- It draws lines meta and families. Defines 35 functions or classes, including $, snapshot, indexOfKey, bin, tier1. Side effects: changes the web page (DOM); makes network requests.
- Why it exists in the grid workflow
- README: "The page (`index.html`, `app.mjs`, `lib.mjs`) is inherited unchanged from testcode/202609151339."
- Inputs & outputs
- In: JSON file all-lines.meta.json; JSON file families.json. Out: WebGL/WebGPU drawing.
- Downstream impact
- Used directly by 1 file: Iterations — 08 Space and Time Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/). It is itself an entry point, published at https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/.
- What it achieves
- It indexes key (`indexOfKey`), builds gl (`buildGL`) and draws marks (`drawMarks`). The Line Wafer. It responds to pointerdown, pointermove and pointerup events.
- Why it matters
- Loaded by the page of the same name at https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/.
- Application
- galaxies-wafers · iterations/08-space-and-time · JavaScript module
- Uses
- Galaxies Wafers — Lib (import), Clock (import)
- Used by
- loaded by Iterations — 08 Space and Time Page (script src)
- Produces
- $, snapshot, indexOfKey, bin, tier1, tier2, compile, initGL, fallback2d, buildGL, put, resize
- Defines here
- $, snapshot, indexOfKey, bin, tier1, tier2, compile, initGL, fallback2d, buildGL, put, resize, ensureOverlay, drawMarks, toScreen, render, draw, home, flyTo, nearestKeyAt, famsOf, paintPanel, family, connect, famLine, s, gestures, up, readURL, refuse
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/08-space-and-time/app.mjs:1-626 · blob 78785789a51a73416cecd9c93c82df48106aa35d · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom, network · domain stage: unknown · users: none · canonical: unknown
/* The Line Wafer.
*
* WHAT THIS IS. Every line of code in the estate that has been given a
* permanent number is one point on one surface. There are 250,174 of them and
* they are numbered 1 to 342,795: the number is the line's permanent key, it
* never changes and it is never reused, and numbers missing from the sequence
* were never issued. Those gaps are drawn as gaps.
*
* WHY THE NUMBER IS THE ADDRESS. A point's position here is a pure function of
* its own permanent key and of nothing else:
*
* r = SPACING * sqrt(key) theta = key * GOLDEN
*
* so line 8,285 is in the same place on every device, in every session, for
* ever, and the surface can be reconstituted from the numbers alone. Nothing
* about a position is stored, fetched or remembered. That is what makes the
* surface unbounded rather than large: the estate can issue line 400,000
* tomorrow and the wafer already has somewhere to put it.
*
* THE BEAM. The camera is a beam. It steers over the wafer and focuses, and
* where it focuses the structure resolves — one line, its length, whether any
* function family carries it, and which. Focus is the only instrument; there
* are no levels and nothing is pre-rendered.
*
* THE RULE THIS PAGE EXISTS TO MEET. It must be able to connect any two
* uniquely numbered lines in the estate. Type two numbers and it answers: the
* families that carry both, or a refusal naming which of the two no family
* carries.
*
* TWO RELATIONS, AND THE FIRST VERSION OF THIS PAGE CONFUSED THEM.
* FANOUT is one number appearing in several families: the same text sitting in
* several places, which is duplication. CO-MEMBERSHIP is two different numbers
* appearing in one family: two lines of the same function, which is
* neighbourhood and is NOT the same text. Connecting uses co-membership, so a
* line carried by exactly one family is perfectly connectable — to another line
* of that family. The page originally claimed the opposite and a reviewer was
* right to reject it. See lib.mjs.
*
* WHAT IS NOT CLAIMED. Neither relation is a dependency and neither means one
* line calls the other. Triviality is visible rather than hidden: a line's
* fanout is printed, so line 2, an empty line in 2,281 families, looks exactly
* as meaningless as it is.
*/
import { place, placeAll, parseKey, indexOfKey as findKey, ownersOf, fanoutCount,
connectAnswer, esc, fmt, SPACING } from '../../lib.mjs';
import { clock, setT, onTick, dateIndex, GLOW_SHARE, GLOW_GROWTH } from './clock.mjs';
const DATA = 'https://globalgrid2050.com/testcode/202609142202/data/';
const $ = id => document.getElementById(id);
let stage = $("stage");
const U = {
fanout: null, /* lines carried by MORE THAN ONE family; counted, never typed */
keys: null, /* Uint32Array, sorted: every permanent line number issued */
lens: null, /* Uint16Array: characters in that line */
inFam: null, /* Uint8Array: 1 when at least one family carries it */
pos: null, /* Float32Array 2N: the pure function of the key */
n: 0,
families: null, /* tier 2 */
famLines: null,
ownerOf: null, /* Map<key, number[]> family indexes, built once on demand */
meta: null
};
const view = { x: 0, y: 0, zoom: 1, w: 0, h: 0, dpr: 1, focus: -1, link: null };
/* The one read-only hook for layers: the live camera, and listeners called after every frame. */
/* Listeners get a frozen scalar snapshot, never the live object: a listener that
mutates what it received cannot move the wafer's camera. (Codex review.) */
const snapshot = () => Object.freeze({ x: view.x, y: view.y, zoom: view.zoom, w: view.w, h: view.h, dpr: view.dpr, t: clock.t });
window.__wafer = Object.freeze({ get view() { return snapshot(); }, onDraw: new Set() });
/* ── the surface ─────────────────────────────────────────────────────────── */
const placeOne = place; /* where a number sits, issued or not */
const indexOfKey = key => findKey(U.keys, key);
/* ── loading ─────────────────────────────────────────────────────────────── */
async function bin(name, Kind) {
const r = await fetch(DATA + name, { cache: 'default' });
if (!r.ok) throw new Error(DATA + name + ' returned HTTP ' + r.status);
return new Kind(await r.arrayBuffer());
}
async function tier1() {
const [meta, keys, lens, inFam] = await Promise.all([
fetch(DATA + 'all-lines.meta.json').then(r => r.json()),
bin('all-lines.bin', Uint32Array),
bin('all-lines.len.bin', Uint16Array),
bin('all-lines.family.bin', Uint8Array)
]);
U.meta = meta; U.keys = keys; U.lens = lens; U.inFam = inFam; U.n = keys.length;
U.pos = placeAll(keys);
let carried = 0;
for (let i = 0; i < inFam.length; i++) carried += inFam[i];
$('count').textContent =
`${fmt(U.n)} numbered lines · 1 to ${fmt(meta.max)} · ${fmt(carried)} carried by a family`;
$('prov').textContent =
`numbered database built ${meta.built_utc.slice(0, 16).replace('T', ' ')} UTC · pack ${meta.source.sha256.slice(0, 12)}`;
}
async function tier2() {
const [families, famLines] = await Promise.all([
fetch(DATA + 'families.json').then(r => r.json()),
bin('lines.bin', Uint32Array)
]);
U.families = families; U.famLines = famLines;
const owner = ownersOf(families, famLines);
U.ownerOf = owner;
U.fanout = fanoutCount(owner);
dateOf = dateIndex(families, famLines);
paintClock();
if (view.focus >= 0) paintPanel(view.focus);
if (view.link) connect(view.link[0], view.link[1]);
}
/* ── drawing ─────────────────────────────────────────────────────────────── */
const VS = `#version 300 es
precision highp float;
in vec2 a_pos; in float a_len; in float a_fam; in float a_key;
uniform float u_t; uniform float u_glowShare; uniform float u_glowGrowth;
out float v_glow; out float v_born;
uniform vec2 u_res; uniform vec2 u_cam; uniform float u_zoom; uniform float u_dpr;
uniform float u_focusKey;
out float v_fam; out float v_len; out float v_focus;
void main(){
vec2 p = (a_pos - u_cam) * u_zoom;
gl_Position = vec4(p / (u_res * 0.5), 0.0, 1.0);
float base = 1.0 + min(a_len, 120.0) * 0.012;
v_born = a_key <= u_t ? 1.0 : 0.0;
v_glow = v_born * clamp(1.0 - (u_t - a_key) / max(u_t * u_glowShare, 1.0), 0.0, 1.0);
base *= 1.0 + u_glowGrowth * v_glow;
gl_PointSize = v_born * clamp(base * sqrt(u_zoom) * u_dpr, 1.0, 26.0 * u_dpr);
v_fam = a_fam; v_len = a_len; v_focus = 0.0;
}`;
const FS = `#version 300 es
precision highp float;
in float v_fam; in float v_len; in float v_focus; in float v_glow; in float v_born;
out vec4 o;
void main(){
vec2 d = gl_PointCoord - 0.5;
float r = length(d);
if (r > 0.5 || v_born < 0.5) discard;
float edge = smoothstep(0.5, 0.42, r);
vec3 carried = vec3(0.84, 0.87, 0.94);
vec3 alone = vec3(0.30, 0.34, 0.44);
vec3 c = mix(mix(alone, carried, v_fam), vec3(1.0, 0.93, 0.62), v_glow);
o = vec4(c, edge * mix(0.30 + 0.70 * v_fam, 1.0, v_glow));
}`;
let gl = null, prog = null, loc = {}, vao = null, ctx2d = null;
function compile(g, type, src) {
const s = g.createShader(type); g.shaderSource(s, src); g.compileShader(s);
if (!g.getShaderParameter(s, g.COMPILE_STATUS)) throw new Error(g.getShaderInfoLog(s));
return s;
}
/* If anything after acquiring the context fails — a shader that will not compile
on some driver, a program that will not link — the page must fall back, not
half-run. A canvas cannot hand out a 2D context once it has given out a WebGL
one, so the canvas itself is replaced. The first version left `gl` non-null on
failure and then asked the same canvas for a 2D context, which returns null:
the fallback it advertised did not exist. */
function initGL() {
gl = stage.getContext('webgl2', { antialias: true, alpha: false });
if (!gl) { gl = null; return fallback2d(); }
try { return buildGL(); }
catch (e) { gl = null; console.warn('WebGL setup failed, falling back:', e.message); return fallback2d(); }
}
function fallback2d() {
const fresh = stage.cloneNode(false);
stage.replaceWith(fresh);
stage = fresh;
ctx2d = stage.getContext('2d');
$('hint').textContent = ctx2d
? 'drawn without the GPU: at low zoom one line in seven is plotted'
: 'this browser gave neither a GPU nor a 2D canvas; nothing can be drawn';
return false;
}
function buildGL() {
prog = gl.createProgram();
gl.attachShader(prog, compile(gl, gl.VERTEX_SHADER, VS));
gl.attachShader(prog, compile(gl, gl.FRAGMENT_SHADER, FS));
gl.linkProgram(prog);
if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(prog));
gl.useProgram(prog);
for (const u of ['u_res', 'u_cam', 'u_zoom', 'u_dpr', 'u_focusKey', 'u_t', 'u_glowShare', 'u_glowGrowth']) loc[u] = gl.getUniformLocation(prog, u);
vao = gl.createVertexArray(); gl.bindVertexArray(vao);
const put = (data, name, size, Kind, norm) => {
const b = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);
const l = gl.getAttribLocation(prog, name);
gl.enableVertexAttribArray(l);
gl.vertexAttribPointer(l, size, Kind, !!norm, 0, 0);
};
put(U.pos, 'a_pos', 2, gl.FLOAT, false);
put(U.lens, 'a_len', 1, gl.UNSIGNED_SHORT, false);
put(U.inFam, 'a_fam', 1, gl.UNSIGNED_BYTE, false);
put(U.keys, 'a_key', 1, gl.UNSIGNED_INT, false);
gl.enable(gl.BLEND);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
return true;
}
function resize() {
view.dpr = Math.min(window.devicePixelRatio || 1, 2);
view.w = stage.clientWidth; view.h = stage.clientHeight;
stage.width = Math.round(view.w * view.dpr);
stage.height = Math.round(view.h * view.dpr);
if (gl) gl.viewport(0, 0, stage.width, stage.height);
}
let overlay = null;
function ensureOverlay() {
if (overlay) return overlay;
overlay = document.createElement('canvas');
overlay.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;pointer-events:none;z-index:1';
document.body.appendChild(overlay);
return overlay;
}
function drawMarks() {
const o = ensureOverlay();
o.width = stage.width; o.height = stage.height;
const c = o.getContext('2d');
c.setTransform(view.dpr, 0, 0, view.dpr, 0, 0);
c.clearRect(0, 0, view.w, view.h);
const toScreen = ([x, y]) => [
(x - view.x) * view.zoom + view.w / 2,
view.h / 2 - ((y - view.y) * view.zoom)
];
if (view.link) {
const [ka, kb] = view.link;
const A = toScreen(placeOne(ka)), B = toScreen(placeOne(kb));
c.strokeStyle = '#5ec8f2'; c.lineWidth = 1.2; c.setLineDash([5, 4]);
c.beginPath(); c.moveTo(A[0], A[1]);
c.quadraticCurveTo((A[0] + B[0]) / 2, (A[1] + B[1]) / 2 - 40, B[0], B[1]);
c.stroke(); c.setLineDash([]);
for (const P of [A, B]) {
c.strokeStyle = '#5ec8f2'; c.beginPath(); c.arc(P[0], P[1], 9, 0, 6.2832); c.stroke();
}
}
if (view.focus >= 0) {
const P = toScreen(placeOne(view.focus));
c.strokeStyle = '#ffd54a'; c.lineWidth = 1.4;
c.beginPath(); c.arc(P[0], P[1], 11, 0, 6.2832); c.stroke();
c.beginPath(); c.moveTo(P[0] - 18, P[1]); c.lineTo(P[0] - 13, P[1]);
c.moveTo(P[0] + 13, P[1]); c.lineTo(P[0] + 18, P[1]);
c.moveTo(P[0], P[1] - 18); c.lineTo(P[0], P[1] - 13);
c.moveTo(P[0], P[1] + 13); c.lineTo(P[0], P[1] + 18);
c.stroke();
}
}
function render() {
if (gl) {
gl.clearColor(0.043, 0.051, 0.071, 1); gl.clear(gl.COLOR_BUFFER_BIT);
gl.useProgram(prog); gl.bindVertexArray(vao);
gl.uniform2f(loc.u_res, stage.width, stage.height);
gl.uniform2f(loc.u_cam, view.x, view.y);
gl.uniform1f(loc.u_zoom, view.zoom * view.dpr);
gl.uniform1f(loc.u_dpr, view.dpr);
gl.uniform1f(loc.u_focusKey, view.focus);
gl.uniform1f(loc.u_t, clock.t);
gl.uniform1f(loc.u_glowShare, GLOW_SHARE);
gl.uniform1f(loc.u_glowGrowth, GLOW_GROWTH);
gl.drawArrays(gl.POINTS, 0, U.n);
} else if (ctx2d) {
const c = ctx2d;
c.setTransform(1, 0, 0, 1, 0, 0);
c.fillStyle = '#0b0d12'; c.fillRect(0, 0, stage.width, stage.height);
c.fillStyle = '#7d8598';
const step = view.zoom < 0.5 ? 7 : 1;
for (let i = 0; i < U.n; i += step) {
if (U.keys[i] > clock.t) break; /* keys are sorted: the rest are not yet born */
const x = (U.pos[i * 2] - view.x) * view.zoom * view.dpr + stage.width / 2;
const y = stage.height / 2 - (U.pos[i * 2 + 1] - view.y) * view.zoom * view.dpr;
if (x < 0 || y < 0 || x > stage.width || y > stage.height) continue;
c.fillRect(x, y, view.dpr, view.dpr);
}
}
drawMarks();
{ const snap = snapshot(); for (const f of window.__wafer.onDraw) { try { f(snap); } catch (e) { console.warn('onDraw listener failed:', e); } } }
}
let pending = false;
function draw() { if (!pending) { pending = true; requestAnimationFrame(() => { pending = false; render(); }); } }
/* ── the beam: steering and focusing ─────────────────────────────────────── */
function home() {
const maxR = SPACING * Math.sqrt(U.meta.max);
view.x = 0; view.y = 0;
view.zoom = Math.min(view.w, view.h) / (maxR * 2.15);
draw();
}
function flyTo(key, zoom) {
const [x, y] = placeOne(key);
const z0 = view.zoom, x0 = view.x, y0 = view.y;
const z1 = zoom ?? Math.max(view.zoom, 9);
const t0 = performance.now(), ms = 620;
(function step(t) {
const u = Math.min(1, (t - t0) / ms);
const e = u < .5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
view.x = x0 + (x - x0) * e; view.y = y0 + (y - y0) * e;
view.zoom = Math.exp(Math.log(z0) + (Math.log(z1) - Math.log(z0)) * e);
render();
if (u < 1) requestAnimationFrame(step);
})(t0);
}
function nearestKeyAt(cx, cy) {
const wx = (cx - view.w / 2) / view.zoom + view.x;
const wy = (view.h / 2 - cy) / view.zoom + view.y;
const reach = 22 / view.zoom;
let best = -1, bestD = reach * reach;
for (let i = 0; i < U.n; i++) {
const dx = U.pos[i * 2] - wx, dy = U.pos[i * 2 + 1] - wy;
const d = dx * dx + dy * dy;
if (d < bestD) { bestD = d; best = i; }
}
return best < 0 ? -1 : U.keys[best];
}
/* ── the panel: what the beam found ──────────────────────────────────────── */
function famsOf(key) {
if (!U.ownerOf) return null;
return U.ownerOf.get(key) || [];
}
function paintPanel(key) {
const i = indexOfKey(key);
const p = $('panelbody');
if (i < 0) {
p.innerHTML =
`<h2>Line <span class="n">${fmt(key)}</span></h2>
<p class="refuse">This number was never issued. The numbering runs 1 to ${fmt(U.meta.max)} and
${fmt(U.meta.max - U.n)} of those numbers were skipped, so a gap is a real answer rather than a
missing record. The surface still has a place for it, and the beam is pointing at it.</p>`;
$('panel').hidden = false; return;
}
const chars = U.lens[i], carried = U.inFam[i] === 1;
const fams = famsOf(key);
let body =
`<h2>Line <span class="n">${fmt(key)}</span></h2>
<dl>
<dt>characters</dt><dd>${chars === 65535
? 'at least 65,535 — the pack stores this in sixteen bits and this line reaches the ceiling, so its true length is not known here'
: fmt(chars) + (chars === 0 ? ' (an empty line)' : '')}</dd>
<dt>permanent</dt><dd>this number is never reused, so it means this line for ever</dd>
<dt>carried by</dt><dd>${carried ? 'at least one function family' : 'no function family'}</dd>
</dl>`;
if (!carried) {
body += `<p class="refuse">Nothing can be connected to this line. It is numbered and it exists, but no
function family in the estate carries it, so it has no partner to join it to.</p>`;
} else if (fams === null) {
body += `<p class="dim">The family index is still loading; the families carrying this line will appear here.</p>`;
} else if (fams.length === 0) {
body += `<p class="refuse">The pack marks this line as carried, but no family range in this build
contains it. That disagreement is shown rather than smoothed over.</p>`;
} else {
const show = fams.slice(0, 12);
body += `<p>Carried by <span class="fam">${fmt(fams.length)}</span> ${fams.length === 1 ? 'family' : 'families'}${fams.length > 12 ? ', first twelve' : ''}:</p><ul>` +
show.map(f => {
const fa = U.families[f];
const cat = fa.category ?? 'no category recorded';
return `<li><span class="fam">${esc(fa.name)}</span> <span class="dim">#${fmt(fa.n)} · ${esc(fa.kind)} · ${esc(cat)} · ${fmt(fa.lineCount)} lines</span></li>`;
}).join('') + `</ul>`;
if (fams.length > 60) {
body += `<p class="dim">A line carried by this many families is almost certainly trivial: a brace, a
blank, an import. Its connection count is drawn here rather than hidden, so you can see that for
yourself.</p>`;
}
}
p.innerHTML = body;
$('panel').hidden = false;
}
function connect(ka, kb) {
view.link = [ka, kb];
const p = $('panelbody');
const head = `<h2>Connect <span class="n">${fmt(ka)}</span> to <span class="n">${fmt(kb)}</span></h2>`;
/* The index is the only thing that can be missing; everything else is decided
by lib.mjs so the page cannot disagree with the proofs. */
if (!U.ownerOf) {
p.innerHTML = head + `<p class="dim">The family index is still loading. The answer will appear here
without another tap.</p>`;
$('panel').hidden = false; draw(); return;
}
const a = connectAnswer(ka, kb, { keys: U.keys, owner: U.ownerOf });
const famLine = f => {
const fa = U.families[f];
return `<li><span class="fam">${esc(fa.name)}</span> <span class="dim">#${fmt(fa.n)} · ${esc(fa.category ?? 'no category recorded')}</span></li>`;
};
let body = head;
if (a.verdict === 'absent') {
body += `<p class="refuse">${a.absent.map(fmt).join(' and ')} ${a.absent.length > 1 ? 'were' : 'was'}
never issued in this numbering, so there is nothing at that address to connect.</p>`;
} else if (a.verdict === 'same-line') {
body += `<p class="refuse">Those are the same line. A line is not connected to itself, and reporting
that it is would be the page agreeing with you rather than answering you.</p>` +
(a.both.length
? `<p>Line ${fmt(ka)} is carried by ${fmt(a.both.length)} ${a.both.length === 1 ? 'family' : 'families'}.</p>`
: `<p>No family carries line ${fmt(ka)}.</p>`);
} else if (a.verdict === 'no-family') {
body += `<p class="refuse">${a.without.map(fmt).join(' and ')} ${a.without.length > 1 ? 'are' : 'is'}
carried by no function family at all, so ${a.without.length > 1 ? 'they have' : 'it has'} nothing to be
joined through.</p>`;
} else if (a.verdict === 'joined') {
body += `<p>Joined by <span class="fam">${fmt(a.both.length)}</span>
${a.both.length === 1 ? 'family that carries' : 'families that carry'} both lines${a.both.length > 12 ? ', first twelve' : ''}:</p>
<ul>${a.both.slice(0, 12).map(famLine).join('')}</ul>
<p class="dim">This is co-membership: two different lines sitting in one function. It is not the same
text in two places, and it is not a dependency. Either line may be carried by only that one family and
still connect perfectly well through it.</p>`;
} else {
body += `<p class="refuse">No family carries both lines, so these two are not joined.</p>
<dl><dt>${fmt(ka)}</dt><dd>${a.A.length ? fmt(a.A.length) + ' ' + (a.A.length === 1 ? 'family' : 'families') : 'no family'}</dd>
<dt>${fmt(kb)}</dt><dd>${a.B.length ? fmt(a.B.length) + ' ' + (a.B.length === 1 ? 'family' : 'families') : 'no family'}</dd></dl>
<p class="dim">An unjoined pair is the ordinary case: most lines of the estate sit in unrelated
functions. This is not about fanout. ${fmt(U.fanout)} of the ${fmt(U.n)} numbered lines appear in more
than one family, which is duplication; joining two numbers asks the different question of whether one
family holds them both.</p>`;
}
p.innerHTML = body;
$('panel').hidden = false;
/* Frame both ends. */
const [ax, ay] = placeOne(ka), [bx, by] = placeOne(kb);
view.x = (ax + bx) / 2; view.y = (ay + by) / 2;
const span = Math.max(Math.hypot(bx - ax, by - ay), 4);
view.zoom = Math.min(view.w, view.h) / (span * 1.8);
draw();
}
/* ── gestures ────────────────────────────────────────────────────────────── */
function gestures() {
let down = null, moved = 0, pinch = null;
const pts = new Map();
stage.addEventListener('pointerdown', e => {
stage.setPointerCapture(e.pointerId);
pts.set(e.pointerId, [e.clientX, e.clientY]);
if (pts.size === 1) { down = [e.clientX, e.clientY]; moved = 0; }
if (pts.size === 2) {
const [p, q] = [...pts.values()];
pinch = { d: Math.hypot(p[0] - q[0], p[1] - q[1]), z: view.zoom };
}
});
stage.addEventListener('pointermove', e => {
if (!pts.has(e.pointerId)) return;
const prev = pts.get(e.pointerId);
pts.set(e.pointerId, [e.clientX, e.clientY]);
if (pts.size === 2 && pinch) {
const [p, q] = [...pts.values()];
const d = Math.hypot(p[0] - q[0], p[1] - q[1]);
if (pinch.d > 0) view.zoom = Math.max(0.02, Math.min(4000, pinch.z * (d / pinch.d)));
draw(); return;
}
if (pts.size === 1) {
const dx = e.clientX - prev[0], dy = e.clientY - prev[1];
moved += Math.abs(dx) + Math.abs(dy);
view.x -= dx / view.zoom; view.y += dy / view.zoom;
draw();
}
});
const up = e => {
if (pts.size === 1 && down && moved < 7) {
const key = nearestKeyAt(e.clientX, e.clientY);
if (key >= 0) { view.focus = key; paintPanel(key); view.link = null; draw(); }
}
pts.delete(e.pointerId);
if (pts.size < 2) pinch = null;
if (pts.size === 0) down = null;
};
stage.addEventListener('pointerup', up);
stage.addEventListener('pointercancel', e => { pts.delete(e.pointerId); pinch = null; down = null; });
stage.addEventListener('wheel', e => {
e.preventDefault();
const f = Math.exp(-e.deltaY * 0.0016);
view.zoom = Math.max(0.02, Math.min(4000, view.zoom * f));
draw();
}, { passive: false });
stage.addEventListener('dblclick', () => { view.focus = -1; view.link = null; $('panel').hidden = true; home(); });
}
/* ── URL state: the whole of it, in permanent numbers ────────────────────── */
function readURL() {
const q = new URLSearchParams(location.search);
const pa = parseKey(q.get('line') ?? ''), pb = parseKey(q.get('to') ?? '');
if (!pa.ok) { if (q.get('line')) refuse(`The link carried a line number this page cannot use: ${pa.why}.`); return; }
$('a').value = String(pa.key);
const rawTo = q.get('to');
if (pb.ok) { $('b').value = String(pb.key); view.link = [pa.key, pb.key]; connect(pa.key, pb.key); }
else if (rawTo !== null && rawTo.trim() !== '') {
/* focus stays unset: tier 2 repaints the panel for a focused key when it
lands, which would silently replace this refusal with an ordinary card. */
refuse(`The link asked to connect to "${rawTo}", which is not a line number: ${pb.why}. `
+ `Line ${pa.key} is where the beam is pointing.`);
flyTo(pa.key);
}
else { view.focus = pa.key; paintPanel(pa.key); flyTo(pa.key); }
}
/* One refusal path, so a bad number is always visible rather than ignored. */
function refuse(sentence) {
$('panelbody').replaceChildren();
const h = document.createElement('h2'); h.textContent = 'Refused';
const p2 = document.createElement('p'); p2.className = 'refuse'; p2.textContent = sentence;
$('panelbody').append(h, p2);
$('panel').hidden = false;
}
function writeURL(a, b) {
const q = new URLSearchParams();
if (a) q.set('line', String(a));
if (b) q.set('to', String(b));
history.replaceState(null, '', q.toString() ? '?' + q : location.pathname);
}
/* ── the clock: time as depth ────────────────────────────────────────────── */
const SWEEP_SECONDS = 60; /* with no ?rate=, PLAY crosses the whole key range in a minute */
let dateOf = null; /* built by tier 2 from families.json; null until then */
let playing = false, lastFrame = 0, rate = 0;
function paintClock() {
$('tslider').value = String(clock.t);
$('tkey').textContent = `key ${fmt(clock.t)} of ${fmt(clock.max)}`;
if (!dateOf) { $('tdate').textContent = 'dates loading…'; return; }
const d = dateOf(clock.t);
$('tdate').textContent = d ? d.iso : 'before the first family';
/* The date stops when no family whole at a later key was written later; say so, never hide it. */
$('tsince').textContent = d ? ` · newest recorded family date visible, whole since key ${fmt(d.wholeAt)}` : '';
$('tdate').title = d ? `newest recorded family date among families whole by discovery position ${fmt(clock.t)}: ${d.name}, last line ${fmt(d.wholeAt)}` : '';
}
function writeT() {
const u = new URL(location.href);
u.searchParams.set('t', String(clock.t));
history.replaceState(history.state, '', u.pathname + u.search.replace(/%2C/gi, ',') + u.hash);
}
function tickPlay(now) {
if (!playing) return;
if (lastFrame) setT(clock.t + rate * (now - lastFrame) / 1000);
lastFrame = now;
if (clock.t >= clock.max) { playing = false; $('tplay').textContent = 'PLAY'; writeT(); return; }
requestAnimationFrame(tickPlay);
}
function startClock() {
clock.max = U.meta.max;
const q = new URLSearchParams(location.search);
const pt = parseKey(q.get('t') ?? '');
const pr = parseKey(q.get('rate') ?? '');
rate = pr.ok ? pr.key : Math.max(1, Math.round(clock.max / SWEEP_SECONDS));
const s = $('tslider');
s.min = '1'; s.max = String(clock.max); s.step = '1';
clock.t = pt.ok ? Math.min(pt.key, clock.max) : clock.max;
onTick(() => { paintClock(); draw(); });
s.addEventListener('input', () => setT(Number(s.value)));
s.addEventListener('change', writeT);
$('tplay').addEventListener('click', () => {
playing = !playing;
$('tplay').textContent = playing ? 'PAUSE' : 'PLAY';
if (playing) { if (clock.t >= clock.max) setT(1); lastFrame = 0; requestAnimationFrame(tickPlay); }
else writeT();
});
paintClock();
}
/* ── start ───────────────────────────────────────────────────────────────── */
(async function start() {
try {
await tier1();
} catch (e) {
$('count').textContent = 'Could not load the numbered database: ' + e.message
+ '. Check the internet connection and reload.';
return;
}
startClock();
resize();
try { initGL(); } catch (e) {
ctx2d = stage.getContext('2d');
$('hint').textContent = 'GPU not available: drawn without animation';
}
home();
gestures();
window.addEventListener('resize', () => { resize(); draw(); });
$('beam').addEventListener('submit', e => {
e.preventDefault();
const pa = parseKey($('a').value), pb = parseKey($('b').value);
if (!pa.ok) { refuse(`That is not a line number: ${pa.why}.`); return; }
if ($('b').value.trim() && !pb.ok) { refuse(`Second number: ${pb.why}.`); return; }
if (pb.ok) { view.focus = -1; connect(pa.key, pb.key); writeURL(pa.key, pb.key); }
else { view.focus = pa.key; view.link = null; paintPanel(pa.key); flyTo(pa.key); writeURL(pa.key, null); }
document.activeElement?.blur();
});
$('close').addEventListener('click', () => { $('panel').hidden = true; view.focus = -1; view.link = null; draw(); });
readURL();
tier2().catch(e => { $('prov').textContent = 'family index unavailable: ' + e.message; });
})();Clock
A JavaScript module in the iterations folder of Galaxies Wafers that works with born prefix (`bornPrefix`) and date index (`dateIndex`). Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/.
transformgalaxies-wafers / iterations/08-space-and-time/clock.mjs · lines 1–74 of 74
- What it does
- It calculates its inputs. Defines 6 functions or classes, including born, glow, setT, onTick, bornPrefix.
- Downstream impact
- Used directly by 3 files: Layers Panel (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/); Iterations — 08 Space and Time (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/); Questions (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/). Reached from 1 other entry point, including Iterations — 08 Space and Time Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/08-space-and-time/).
- What it achieves
- It is built from the born step (`born`), the glow step (`glow`) and the set t helper (`setT`). The third axis of the wafer, drawn in two dimensions.
- Why it matters
- Imported by Layers Panel, Iterations — 08 Space and Time and Questions.
- Application
- galaxies-wafers · iterations/08-space-and-time · JavaScript module
- Uses
- none found
- Used by
- imported by Layers Panel (import), imported by Iterations — 08 Space and Time (import), imported by Questions (import)
- Produces
- GLOW_SHARE, GLOW_GROWTH, clock, born, glow, setT, onTick, bornPrefix, dateIndex
- Defines here
- born, glow, setT, onTick, bornPrefix, dateIndex
- Cannot conclude
- Whether this code runs, is used by a live app today, or is correct. This is a static reading.
- Evidence
- galaxies-wafers@5814793812add74605660fa821e6a39b8de80588 iterations/08-space-and-time/clock.mjs:1-74 · blob eae08f983543d4873a7074fd9bb174a25a788f1a · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: unknown · users: none · canonical: unknown
/* clock.mjs — the third axis of the wafer, drawn in two dimensions.
*
* A key is a DISCOVERY POSITION: the order in which the numbered database met
* each line, which is not the time the line was written (a lone brace has a
* tiny key and belongs to families written months later). This module holds
* one number, t, and everything that draws asks it the same two questions:
* born(k) is key k discovered yet? (k <= t)
* glow(k) how recently was it discovered? 1 at the instant of discovery, 0
* once it is older than GLOW_SHARE of t. This is relative discovery
* age, not elapsed historical time. Age is drawn as brightness and size,
* never as a projection: the wafer stays flat and frozen.
* The page and the overlay import this one file, so both read the same clock.
*/
export const GLOW_SHARE = 0.02; /* born within the last 2% of t glows */
export const GLOW_GROWTH = 0.5; /* a newborn is drawn 1.5x its size */
const listeners = new Set();
export const clock = { t: 1, max: 1 };
export const born = k => k <= clock.t;
export function glow(k) {
if (k > clock.t) return 0;
const span = clock.t * GLOW_SHARE;
return span > 0 ? Math.max(0, 1 - (clock.t - k) / span) : 0;
}
export function setT(t) {
const n = Math.max(1, Math.min(clock.max, Math.round(t)));
if (n === clock.t) return;
clock.t = n;
for (const f of listeners) { try { f(n); } catch (e) { console.warn('clock listener failed:', e); } }
}
export const onTick = f => listeners.add(f);
/* The last index of a key list in numbering order whose key is born, or -1. */
export function bornPrefix(keys) {
let i = -1;
while (i + 1 < keys.length && keys[i + 1] <= clock.t) i++;
return i;
}
/* THE DATE SHOWN: the newest recorded family date visible. A family is whole
at t once every line it carries is discovered, that is once its largest key
is at or below t. The clock prints the newest first_written among the
families whole at t: sorted by that largest key with a running maximum of
first_written, it is one binary search away, and it can only move forward
as t does. Being a prefix maximum it plateaus: on the 14 Sep pack it reaches
2026-09-13T23:49:57Z at key 18,543 and stays there to key 342,795. That is
the statistic behaving as defined, not the sweep stopping. A replay in
physical time would need timestamp ordering, which the pack does not carry.
Two readings were tried and dropped, and the data is why. Keyed by a family's
SMALLEST line, the date saturates by key 1,000, because common lines (a lone
brace) carry tiny keys into families written months later. Keyed by the
single latest-born family without the running maximum, the date jumps back
and forth, because keys are issued in database order and first_written is
git history: the two agree in trend, not line by line. */
export function dateIndex(families, famLines) {
const rows = [];
for (const f of families) {
if (!f.first_written || !f.lineCount) continue;
let whole = 0;
for (let i = f.lineOffset; i < f.lineOffset + f.lineCount; i++) whole = Math.max(whole, famLines[i]);
const ms = Date.parse(f.first_written);
if (whole > 0 && Number.isFinite(ms)) rows.push([whole, ms, f.first_written, f.name]);
}
rows.sort((a, b) => a[0] - b[0]);
const keys = new Float64Array(rows.length), best = [];
let top = null;
rows.forEach((r, i) => { keys[i] = r[0]; if (!top || r[1] > top[1]) top = r; best.push(top); });
return t => {
let lo = 0, hi = keys.length - 1, at = -1;
while (lo <= hi) { const m = (lo + hi) >> 1; if (keys[m] <= t) { at = m; lo = m + 1; } else hi = m - 1; }
return at < 0 ? null : { iso: best[at][2], name: best[at][3], wholeAt: best[at][0] };
};
}