In this section
- Layers Panel render · galaxies-wafers / iterations/09-deeplink-receiver/layers-panel.mjs lines 1–537
Layers Panel
A JavaScript module in the iterations folder of Galaxies Wafers using data from the ventus-grid-engine repository. Served with the page at https://ventusltd.github.io/galaxies-wafers/iterations/09-deeplink-receiver/.
rendergalaxies-wafers / iterations/09-deeplink-receiver/layers-panel.mjs · lines 1–537 of 537 · Entry point · pattern placeOverlay (shared by 3 files)
- What it does
- It draws its content. Defines 25 functions or classes, including $, esc, row, renderPanel, renderElements. Side effects: changes the web page (DOM); makes network requests.
- Why it exists in the grid workflow
- Domain stage sub33kv_design (path word 'panel'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary.
- Inputs & outputs
- In: data source: ventus-grid-engine data; form fields (user input). Out: canvas drawing.
- Downstream impact
- Used directly by 1 file: Iterations — 09 Deeplink Receiver Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/09-deeplink-receiver/). Reached from 1 other entry point, including Iterations — 45 Deeplink Round Trip Page (galaxies-wafers, https://ventusltd.github.io/galaxies-wafers/iterations/45-deeplink-round-trip/). It is itself an entry point, published at https://ventusltd.github.io/galaxies-wafers/iterations/09-deeplink-receiver/.
- What it achieves
- It renders panel (`renderPanel`), renders elements (`renderElements`) and writes layers URL (`writeLayersToURL`). Layers over the inherited wafer, in Grid Atlas's grammar. It responds to click, change and pointerdown events.
- Why it matters
- Loaded by the 09 Deeplink Receiver page at https://ventusltd.github.io/galaxies-wafers/iterations/09-deeplink-receiver/.
- Application
- galaxies-wafers · iterations/09-deeplink-receiver · JavaScript module
- Uses
- Galaxies Wafers — Lib (import), Receiver (import), Code Galaxy Engine (import)
- Used by
- loaded by Iterations — 09 Deeplink Receiver Page (script src)
- Produces
- $, esc, row, renderPanel, renderElements, el, link, kv, toggle, hydrateTiles, writeLayersToURL, requestedLayers
- Defines here
- $, esc, row, renderPanel, renderElements, el, link, kv, toggle, hydrateTiles, writeLayersToURL, requestedLayers, readLayersFromURL, liveView, buildCache, visibleLayers, placeOverlay, carry, drawOverlay, segDist2, pickAt, node, inspect, onStage, end
- 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/09-deeplink-receiver/layers-panel.mjs:1-537 · blob 984c838b68a34daa67fe92f5ed81555462cdd53e · GitHub at these lines
- State
- achieves: comment · context: readme_derived · stage: output · effects: dom, network · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: unknown
/* layers-panel.mjs — layers over the inherited wafer, in Grid Atlas's grammar.
*
* The wafer page (app.mjs, lib.mjs) is inherited unchanged but for one hook.
* This file adds a transparent canvas above it and a panel of layers, and
* touches nothing the wafer draws. Remove this script tag and the wafer is
* exactly what it was.
*
* Layers are files in layers/, listed by layers/manifest.json. Each is a
* CodeFeatureCollection whose geometry is permanent keys, never coordinates, so
* every mark is placed by the wafer's own frozen law and cannot disagree with
* the ground. States follow the Atlas: WAIT until ticked, LOAD while fetching,
* OK when drawn, EMPTY when loaded with nothing in it, FAIL with its reason.
*
* FOLLOWING THE CAMERA. app.mjs exposes exactly one read-only hook,
* window.__wafer: a getter for its live `view` and a set of `onDraw` listeners
* it calls at the end of every render(). The overlay registers there, so it
* repaints in the same frame as the wafer and maps world to screen with the
* wafer's own formula. An earlier version re-derived only the initial frame and
* drifted off the points the moment anyone panned; that is what this replaces.
*
* TAP TO INSPECT. A tap on the wafer that lands within 14 px of a drawn layer
* feature shows that feature's properties and its layer's evidence. The wafer
* still receives the same tap and does whatever it does with it; nothing here
* captures, prevents or stops the event.
*/
import { placeAll } from '../../lib.mjs';
/* Iteration 09: layers come from the engine's parse, and dropped ids are reported, not swallowed. */
import { LINK, ROOT, reportLayers } from './receiver.mjs';
import { parseDeepLink } from '../../engine/code-galaxy-engine.mjs';
const $ = id => document.getElementById(id);
const esc = s => String(s ?? '').replace(/[&<>"']/g, c =>
({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
const state = new Map(); /* id -> {status, doc, why} */
let manifest = null;
let picked = null; /* {layer, feature} currently inspected */
const TAP_SLOP = 7; /* px of movement under which a pointer-up is a tap */
const PICK_REACH = 14; /* px within which a tap picks a layer feature */
/* ── the panel ───────────────────────────────────────────────────────────── */
const panel = document.createElement('section');
panel.id = 'layers';
panel.innerHTML = `<button id="layersToggle" type="button">LAYERS</button><div id="layersBody" hidden><div id="layersInspect" hidden></div><div id="layersList"></div></div>`;
document.body.appendChild(panel);
const style = document.createElement('style');
style.textContent = `
#layers{position:fixed;z-index:5;right:.5rem;top:calc(env(safe-area-inset-top,0px) + 2.2rem);
max-width:min(22rem,calc(100% - 1rem));font:12px/1.4 ui-monospace,Menlo,Consolas,monospace}
#layersToggle{float:right;background:#11151f;color:#5ec8f2;border:1px solid #5ec8f2;border-radius:6px;
padding:.35rem .7rem;font:inherit;letter-spacing:.1em;cursor:pointer}
#layersBody{clear:both;margin-top:.4rem;max-height:60vh;overflow:auto;background:#0e121bf4;
border:1px solid #1b2030;border-radius:8px;padding:.5rem .6rem}
#layersBody[hidden],#layersInspect[hidden]{display:none!important}
#layersInspect{border-bottom:1px solid #1b2030;padding-bottom:.45rem;margin-bottom:.3rem;position:relative}
#layersInspect h3{margin:.1rem 1.6rem .25rem 0;font-size:12.5px;font-weight:600}
#layersInspect dl{display:grid;grid-template-columns:auto 1fr;gap:.1rem .5rem;margin:.3rem 0}
#layersInspect dt{color:#8b93a7}
#layersInspect dd{margin:0;overflow-wrap:anywhere}
#layersInspect .lnote{margin-left:0}
#layersInspectClose{position:absolute;top:0;right:0;background:none;border:0;color:#8b93a7;font:inherit;cursor:pointer}
.lgroup{color:#8b93a7;font-size:10.5px;letter-spacing:.08em;margin:.55rem 0 .15rem}
.lrow{display:flex;gap:.45rem;align-items:flex-start;margin:.2rem 0}
.lrow input{margin-top:.15rem}
.lname{flex:1}
.ltag{font-size:10.5px}
.ltag.WAIT{color:#8b93a7}.ltag.LOAD{color:#ffd54a}.ltag.OK{color:#7fd6a2}
.ltag.EMPTY{color:#b39ddb}.ltag.FAIL{color:#ff8a80}
.lnote{color:#8b93a7;font-size:10.5px;margin:.1rem 0 .35rem 1.5rem}
.ecard{border:1px solid #1b2030;border-radius:6px;padding:.35rem .45rem;margin:.3rem 0;background:#11151f}
.ehead .esym{font-weight:700}.ename{color:#e7ebf3}
.efns{color:#8b93a7;font-size:10.5px;margin:.1rem 0 .2rem}
.emod{border-top:1px dashed #1b2030;padding-top:.2rem;margin-top:.2rem}
.ekv{font-size:10.5px;word-break:break-word}.ek{color:#8b93a7}.ev{color:#e7ebf3}
.eschema{color:#7fd6a2}.enone{color:#b39ddb}
.elinks{font-size:10.5px;margin-top:.15rem}.elink{color:#5ec8f2}
#overlay{position:fixed;left:0;top:0;pointer-events:none;z-index:1;transform-origin:50% 50%;will-change:transform}
`;
document.head.appendChild(style);
const overlay = document.createElement('canvas');
overlay.id = 'overlay';
document.body.appendChild(overlay);
const ctx = overlay.getContext('2d');
$('layersToggle').addEventListener('click', () => { $('layersBody').hidden = !$('layersBody').hidden; });
function row(l) {
const s = state.get(l.id) || { status: 'WAIT' };
const why = s.why ? ` · ${esc(s.why)}` : '';
const stat = s.doc?.stats ? ` · ${esc(JSON.stringify(s.doc.stats).slice(0, 80))}` : '';
return `<div class="lrow"><input type="checkbox" id="L-${esc(l.id)}" ${s.on ? 'checked' : ''}>
<label class="lname" for="L-${esc(l.id)}" style="color:${esc(l.colour)}">${esc(l.label)}
<span class="ltag ${s.status}">[${s.status}]</span></label></div>
<div class="lnote">${esc(l.evidence)}${why}${s.status === 'OK' ? stat : ''}</div>`;
}
function renderPanel() {
if (!manifest) return;
const groups = [...new Set(manifest.layers.map(l => l.group))];
$('layersList').innerHTML = groups.map(g =>
`<div class="lgroup">${esc(g)}</div>` + manifest.layers.filter(l => l.group === g).map(row).join('')
).join('') + `<div class="lnote">substrate ${esc(manifest.substrate)} · frozen · layers built ${esc(manifest.built_utc)}</div>`;
for (const l of manifest.layers) {
$('L-' + l.id).addEventListener('change', e => toggle(l, e.target.checked));
}
renderElements(manifest, state);
}
/* ── ELEMENT: what a consumer of a module meets ─────────────────────────────
Shown when a layer that carries properties.schema or properties.export_subpath
is OK. One card per block symbol, one entry per module that block's functions
resolve to: its export subpath (or why there is none), the schema it stamps,
what its NOT_COMPUTED refuses, and where it lives. Built from DOM nodes with
textContent only; nothing from a dataset is ever parsed as HTML. */
const ENGINE_LIVE = 'https://ventusltd.github.io/ventus-grid-engine/';
function renderElements(manifest, state) {
const body = $('layersBody');
if (!body || !manifest) return;
$('layersElements')?.remove();
const el = (tag, cls, text) => {
const n = document.createElement(tag);
if (cls) n.className = cls;
if (text != null) n.textContent = String(text);
return n;
};
const link = (href, text) => {
const a = el('a', 'elink', text);
a.href = href; a.target = '_blank'; a.rel = 'noopener noreferrer';
return a;
};
const cards = new Map(); /* symbol -> {title, colour, modules: Map(path -> props), functions: Set} */
for (const l of manifest.layers) {
const s = state.get(l.id);
if (!s?.on || s.status !== 'OK') continue;
const feats = s.doc.features.filter(f => f.properties && ('schema' in f.properties || 'export_subpath' in f.properties));
for (const f of feats) {
const p = f.properties;
if (!p.module_path || !p.block) continue;
if (!cards.has(p.block)) cards.set(p.block, { title: p.title, colour: l.colour, modules: new Map(), functions: new Set() });
const c = cards.get(p.block);
c.functions.add(p.function);
if (!c.modules.has(p.module_path)) c.modules.set(p.module_path, p);
}
}
if (!cards.size) return;
const sec = el('div'); sec.id = 'layersElements';
sec.appendChild(el('div', 'lgroup', 'ELEMENT'));
for (const [sym, c] of [...cards].sort((a, b) => a[0].localeCompare(b[0]))) {
const card = el('div', 'ecard');
const head = el('div', 'ehead');
const symEl = el('span', 'esym', sym); symEl.style.color = c.colour;
head.append(symEl, el('span', 'ename', ' ' + (c.title || '')));
card.appendChild(head);
card.appendChild(el('div', 'efns', [...c.functions].join(', ')));
for (const [path, p] of c.modules) {
const m = el('div', 'emod');
const kv = (k, v, cls) => { const r = el('div', 'ekv'); r.append(el('span', 'ek', k + ' '), el('span', cls || 'ev', v)); m.appendChild(r); };
kv('module', path);
kv('export', p.export_subpath ?? ('none — ' + (p.export_reason || 'no subpath')), p.export_subpath ? 'ev' : 'ev enone');
kv('schema', p.schema ?? ('none — ' + (p.schema_reason || 'no schema')), p.schema ? 'ev eschema' : 'ev enone');
kv('refuses', (p.refuses && p.refuses.length) ? p.refuses.join(', ')
: (p.not_computed_form ? `NOT_COMPUTED is a ${p.not_computed_form}, no keys` : 'nothing declared'));
const links = el('div', 'elinks');
links.append(link(ENGINE_LIVE, 'live engine'), el('span', null, ' · '),
link(p.github || `https://github.com/Ventusltd/ventus-grid-engine/blob/main/${path}`, 'GitHub ' + path));
m.appendChild(links);
card.appendChild(m);
}
sec.appendChild(card);
}
body.appendChild(sec);
}
async function toggle(l, on) {
const s = state.get(l.id) || { status: 'WAIT' };
s.on = on; state.set(l.id, s);
writeLayersToURL();
if (on && !s.doc && !s.loading) {
s.loading = true; s.why = '';
s.status = 'LOAD'; renderPanel();
try {
if (l.tiles) s.doc = await hydrateTiles(l, s);
else {
const r = await fetch(ROOT + l.file, { cache: 'default' });
if (!r.ok) throw new Error('HTTP ' + r.status);
s.doc = await r.json();
}
s.cache = s.doc.features.length ? buildCache(s.doc) : null; /* 09b: world positions placed once per layer */
s.status = s.doc.features.length ? 'OK' : 'EMPTY';
if (!s.doc.features.length) s.why = 'loaded; the layer holds no features';
} catch (e) { s.status = 'FAIL'; s.why = e.message; }
s.loading = false;
}
if (!on && picked?.layer.id === l.id) inspect(null);
renderPanel(); drawOverlay();
}
/* A tiled layer is split by radius band (build/tile_layer.py): because r = sqrt(key),
band b is the key range [(b*S)^2, ((b+1)*S)^2). For now every band is fetched up
front, but as separate requests, so the panel can count them in (LOAD n/N) and a
later version can fetch only the bands in view. The assembled doc is identical in
shape to the whole-file layer, so drawing does not know the difference. */
async function hydrateTiles(l, s) {
const r = await fetch(ROOT + l.tiles, { cache: 'default' });
if (!r.ok) throw new Error('tile index HTTP ' + r.status);
const index = await r.json();
const bands = index.bands || [];
const N = bands.length;
let n = 0;
s.status = `LOAD ${n}/${N}`; renderPanel();
const parts = await Promise.all(bands.map(async b => {
const t = await fetch(ROOT + b.file, { cache: 'default' });
if (!t.ok) throw new Error(`band ${b.band} HTTP ${t.status}`);
const doc = await t.json();
if (doc.features.length !== b.features)
throw new Error(`band ${b.band} holds ${doc.features.length} features, index says ${b.features}`);
n++; s.status = `LOAD ${n}/${N}`; renderPanel();
return doc.features;
}));
const features = parts.flat();
if (index.source && features.length !== index.source.features)
throw new Error(`tiles hold ${features.length} features, source had ${index.source.features}`);
return {
type: 'CodeFeatureCollection', substrate: index.substrate, layer: index.layer,
provenance: index.provenance, stats: { features: features.length, bands: N }, features,
};
}
/* ── the URL: ?layers=engine,declared ─────────────────────────────────────── */
/* The wafer's URL grammar: only permanent identifiers travel. A layer id is used
only if the manifest names it; anything else in the URL is dropped, and the
next write puts the URL back into canonical, manifest-ordered form. */
function writeLayersToURL() {
if (!manifest) return;
const on = manifest.layers.filter(l => state.get(l.id)?.on).map(l => l.id);
const u = new URL(location.href);
if (on.length) u.searchParams.set('layers', on.join(','));
else u.searchParams.delete('layers');
const next = u.pathname + u.search.replace(/%2C/gi, ',') + u.hash;
if (next !== location.pathname + location.search + location.hash)
history.replaceState(history.state, '', next);
}
/* A link that carries layers but no line is not a deep link to a line, so the
receiver refuses it as one, but its layers are still a plain request the root
page honours. Earlier this page left them silently in WAIT; now they load and
the card says the line was missing. Any other refusal (malformed line, bad
zoom) still loads nothing, as before. */
function requestedLayers() {
if (LINK.ok) return LINK.layers ?? null;
if (!/^missing /.test(LINK.why || '')) return null;
/* parseDeepLink stops at the missing line before it checks to and zoom (Codex's
review, 19:10Z), so check them here with the engine's own rules: a layers-only
link with a malformed to or an out-of-range zoom loads nothing. */
const withLine = new URL(location.href);
withLine.searchParams.set('line', '1');
if (!parseDeepLink(withLine.href).ok) return null;
const raw = new URL(location.href).searchParams.get('layers');
return raw ? [...new Set(raw.split(',').map(x => x.trim()).filter(Boolean))] : null;
}
function readLayersFromURL() {
const asked = manifest && requestedLayers();
if (!asked) return;
const known = new Map(manifest.layers.map(l => [l.id, l]));
const wanted = asked.filter(id => known.has(id));
const dropped = asked.filter(id => !known.has(id));
reportLayers({ ticked: wanted, dropped });
for (const id of wanted) state.get(id).on = true; /* mark all first, so each write keeps the rest */
writeLayersToURL();
for (const id of wanted) toggle(known.get(id), true);
}
/* ── the camera: the wafer's own, read live ──────────────────────────────── */
const liveView = () => window.__wafer?.view ?? null;
/* SMOOTH ZOOM (09b), ported from the root layers-panel.mjs ("Merge 38") and
iterations/22-fast-zoom/layers-panel.mjs, keeping this iteration's receiver
behaviour untouched.
POSITIONS ONCE PER LAYER. buildCache places every key of a layer into
Float32Arrays when the layer loads, with a bounding box per route, so a frame
never calls place() and routes wholly off screen are skipped.
MOVING FRAMES WITHOUT RE-STROKING. The overlay canvas is OVERLAY_SPAN times the
screen each way, centred on it. A settled frame rasterises every layer into it
around the current camera (the anchor). While the wafer reports a gesture, a
frame only sets a CSS transform that carries the anchor raster to where the
camera now puts it; only when that raster no longer covers the screen, or is
magnified past OVERLAY_MAGNIFY, is it drawn again, thinned to every
THIN_STEP-th vertex and point. Full detail returns when the wafer settles. */
const THIN_STEP = 4;
const OVERLAY_SPAN = 1.5;
const OVERLAY_MAGNIFY = 2;
let overlayDirty = false, anchor = null;
function buildCache(doc) {
const ptKeys = [], ptFeat = [], rtKeys = [], rtStart = [0], rtFeat = [];
doc.features.forEach((f, i) => {
const g = f.geometry;
if (g?.type === 'Point') { ptKeys.push(g.key); ptFeat.push(i); }
else if (g?.type === 'LineString' && g.keys?.length) {
for (const k of g.keys) rtKeys.push(k);
rtStart.push(rtKeys.length); rtFeat.push(i);
}
});
const pts = placeAll(ptKeys), verts = placeAll(rtKeys);
const nR = rtFeat.length, box = new Float32Array(nR * 4);
for (let r = 0; r < nR; r++) {
let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
for (let j = rtStart[r]; j < rtStart[r + 1]; j++) {
const x = verts[j * 2], y = verts[j * 2 + 1];
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
box[r * 4] = x0; box[r * 4 + 1] = y0; box[r * 4 + 2] = x1; box[r * 4 + 3] = y1;
}
return { pts, ptFeat: Int32Array.from(ptFeat), verts, rtStart: Uint32Array.from(rtStart), rtFeat: Int32Array.from(rtFeat), box };
}
function* visibleLayers() {
for (const l of (manifest?.layers || [])) {
const s = state.get(l.id);
if (s?.on && s.status === 'OK' && s.cache) yield [l, s.doc, s.cache];
}
}
/* ── drawing ─────────────────────────────────────────────────────────────── */
function placeOverlay(v) {
const dpr = v.dpr || 1;
const cw = Math.round(v.w * OVERLAY_SPAN), ch = Math.round(v.h * OVERLAY_SPAN);
const W = Math.round(cw * dpr), H = Math.round(ch * dpr);
if (overlay.width !== W || overlay.height !== H) {
overlay.width = W; overlay.height = H;
overlay.style.width = cw + 'px'; overlay.style.height = ch + 'px';
overlay.style.left = Math.round((v.w - cw) / 2) + 'px'; overlay.style.top = Math.round((v.h - ch) / 2) + 'px';
overlayDirty = true; anchor = null;
}
return { cw, ch, dpr };
}
/* The CSS transform that carries the anchor raster to the camera v, or null when
the result would leave part of the screen uncovered or be too magnified. */
function carry(v, cw, ch) {
if (!anchor || anchor.w !== v.w || anchor.h !== v.h) return null;
const s = v.zoom / anchor.zoom;
if (s > OVERLAY_MAGNIFY) return null;
const dx = (anchor.x - v.x) * v.zoom, dy = -(anchor.y - v.y) * v.zoom;
if (Math.abs(dx) + v.w / 2 > s * cw / 2 || Math.abs(dy) + v.h / 2 > s * ch / 2) return null;
return `translate(${dx.toFixed(2)}px,${dy.toFixed(2)}px) scale(${s.toFixed(5)})`;
}
function drawOverlay(snap) {
const v = snap && typeof snap.zoom === 'number' ? snap : liveView();
if (!v || !v.w || !v.h) return; /* the wafer has not framed itself yet */
const { cw, ch, dpr } = placeOverlay(v);
if (v.moving && snap) {
const t = carry(v, cw, ch);
if (t) { overlay.style.transform = t; return; }
}
const units = [...visibleLayers()];
overlay.style.transform = '';
if (!units.length && !picked && !overlayDirty) { anchor = null; return; }
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, cw, ch);
overlayDirty = units.length > 0 || !!picked;
anchor = { x: v.x, y: v.y, zoom: v.zoom, w: v.w, h: v.h };
/* The wafer's own mapping (drawMarks() and nearestKeyAt() in app.mjs), shifted
into the larger canvas: X = (x - v.x) * zoom + cw/2, Y = ch/2 - (y - v.y) * zoom. */
const z = v.zoom, ox = cw / 2 - v.x * z, oy = ch / 2 + v.y * z;
const step = v.moving ? THIN_STEP : 1;
const wx0 = (-2 - ox) / z, wx1 = (cw + 2 - ox) / z, wy0 = (oy - ch - 2) / z, wy1 = (oy + 2) / z;
for (const [l, , c] of units) {
ctx.globalAlpha = l.draws === 'lines' ? 0.55 : 0.8;
if (c.ptFeat.length) {
ctx.fillStyle = l.colour;
ctx.beginPath();
const P = c.pts;
for (let i = 0; i < c.ptFeat.length; i += step) {
const X = P[i * 2] * z + ox, Y = oy - P[i * 2 + 1] * z;
if (X < -2 || Y < -2 || X > cw + 2 || Y > ch + 2) continue;
ctx.rect(X - 1, Y - 1, 2, 2);
}
ctx.fill();
}
if (c.rtFeat.length) {
ctx.strokeStyle = l.colour; ctx.lineWidth = 1;
ctx.beginPath();
const V = c.verts, B = c.box, S = c.rtStart;
for (let r = 0; r < c.rtFeat.length; r++) {
if (B[r * 4 + 2] < wx0 || B[r * 4] > wx1 || B[r * 4 + 3] < wy0 || B[r * 4 + 1] > wy1) continue;
const a = S[r], e = S[r + 1] - 1;
ctx.moveTo(V[a * 2] * z + ox, oy - V[a * 2 + 1] * z);
for (let j = a + step; j < e; j += step) ctx.lineTo(V[j * 2] * z + ox, oy - V[j * 2 + 1] * z);
if (e > a) ctx.lineTo(V[e * 2] * z + ox, oy - V[e * 2 + 1] * z);
}
ctx.stroke();
}
ctx.globalAlpha = 1;
}
if (picked) {
const g = picked.feature.geometry;
ctx.strokeStyle = picked.layer.colour; ctx.lineWidth = 1.4;
const pk = g.type === 'Point' ? [g.key] : g.keys;
const P = placeAll(pk);
for (let i = 0; i < pk.length; i++) {
ctx.beginPath(); ctx.arc(P[i * 2] * z + ox, oy - P[i * 2 + 1] * z, 7, 0, 6.2832); ctx.stroke();
}
}
}
window.__wafer?.onDraw.add(drawOverlay);
/* ── tap to inspect ──────────────────────────────────────────────────────── */
function segDist2(px, py, ax, ay, bx, by) {
const dx = bx - ax, dy = by - ay, L = dx * dx + dy * dy;
const t = L ? Math.max(0, Math.min(1, ((px - ax) * dx + (py - ay) * dy) / L)) : 0;
const qx = ax + t * dx - px, qy = ay + t * dy - py;
return qx * qx + qy * qy;
}
/* The nearest drawn feature to a screen point, or null when none is within reach.
Reads the positions cached per layer; never calls place() per feature. */
function pickAt(cx, cy) {
const v = liveView();
if (!v || !v.zoom) return null;
const z = v.zoom, ox = v.w / 2 - v.x * z, oy = v.h / 2 + v.y * z;
let best = null, bestD = PICK_REACH * PICK_REACH;
for (const [l, doc, c] of visibleLayers()) {
for (let i = 0; i < c.ptFeat.length; i++) {
const X = c.pts[i * 2] * z + ox, Y = oy - c.pts[i * 2 + 1] * z;
const d = (X - cx) ** 2 + (Y - cy) ** 2;
if (d < bestD) { bestD = d; best = { layer: l, feature: doc.features[c.ptFeat[i]] }; }
}
const V = c.verts, S = c.rtStart;
for (let r = 0; r < c.rtFeat.length; r++) {
const a = S[r], e = S[r + 1];
let px = V[a * 2] * z + ox, py = oy - V[a * 2 + 1] * z;
let d = (px - cx) ** 2 + (py - cy) ** 2;
for (let j = a + 1; j < e; j++) {
const qx = V[j * 2] * z + ox, qy = oy - V[j * 2 + 1] * z;
d = Math.min(d, segDist2(cx, cy, px, py, qx, qy));
px = qx; py = qy;
}
if (d < bestD) { bestD = d; best = { layer: l, feature: doc.features[c.rtFeat[r]] }; }
}
}
return best;
}
const node = (tag, text, cls) => {
const n = document.createElement(tag);
if (text != null) n.textContent = String(text);
if (cls) n.className = cls;
return n;
};
/* Dataset text only ever reaches the page through textContent. */
function inspect(hit) {
picked = hit;
const box = $('layersInspect');
box.replaceChildren();
if (!hit) { box.hidden = true; drawOverlay(); return; }
const { layer: l, feature: f } = hit;
const close = node('button', '✕');
close.id = 'layersInspectClose'; close.type = 'button'; close.setAttribute('aria-label', 'close');
close.addEventListener('click', () => inspect(null));
const h = node('h3', l.label); h.style.color = l.colour;
const g = f.geometry;
const where = node('div', g.type === 'Point' ? `line ${g.key}` : `lines ${g.keys.join(' → ')}`, 'lnote');
const dl = node('dl');
for (const [k, val] of Object.entries(f.properties || {})) {
dl.append(node('dt', k), node('dd', val !== null && typeof val === 'object' ? JSON.stringify(val) : val));
}
const ev = node('div', 'evidence: ' + (l.evidence ?? 'none recorded'), 'lnote');
box.append(close, h, where, dl, ev);
box.hidden = false;
$('layersBody').hidden = false;
drawOverlay();
}
/* Non-capturing, and never prevents or stops the event: the wafer's own tap
(which opens its line panel) still happens exactly as before. */
{
const pts = new Map();
let moved = 0;
const onStage = e => e.target instanceof Element && e.target.id === 'stage';
document.addEventListener('pointerdown', e => {
if (!onStage(e)) return;
pts.set(e.pointerId, [e.clientX, e.clientY]);
if (pts.size === 1) moved = 0;
});
document.addEventListener('pointermove', e => {
const prev = pts.get(e.pointerId);
if (!prev) return;
moved += Math.abs(e.clientX - prev[0]) + Math.abs(e.clientY - prev[1]);
pts.set(e.pointerId, [e.clientX, e.clientY]);
});
const end = e => {
if (!pts.has(e.pointerId)) return;
const single = pts.size === 1;
pts.delete(e.pointerId);
if (e.type !== 'pointerup' || !single || moved >= TAP_SLOP) return;
const hit = pickAt(e.clientX, e.clientY);
if (hit) inspect(hit);
};
document.addEventListener('pointerup', end);
document.addEventListener('pointercancel', end);
}
(async () => {
try {
const r = await fetch(ROOT + 'layers/manifest.json', { cache: 'no-cache' });
if (!r.ok) throw new Error(ROOT + 'layers/manifest.json returned HTTP ' + r.status);
manifest = await r.json();
for (const l of manifest.layers) state.set(l.id, { status: 'WAIT', on: false });
renderPanel();
readLayersFromURL();
} catch (e) {
if (requestedLayers()) reportLayers({ ticked: [], dropped: [], failed: 'unavailable: ' + e.message });
$('layersList').innerHTML = `<div class="lnote">Layers unavailable: ${esc(e.message)}. The wafer itself is unaffected.</div>`;
}
})();