The showcase prompt, round 3, Fable 5.2
agent
+43:40reasoning
The WebGL gap in this box's Firefox and WebKit showed the piece only has a text fallback, so I'm adding a real Canvas-2D renderer and hardening the shaders for mobile GPUs.
Read/tmp/tools/template.html
input
{ "file_path": "/tmp/tools/template.html", "offset": 196, "limit": 60 }result, 60 lines
196 let vw = 2, vh = 1; 197 function overview() { 198 const d = Math.max(H / (2 * .7 * TAN), W / (2 * .93 * TAN * vw / vh)); 199 return { x: W / 2, y: H / 2 + d * TAN * .03, d, phi: 0, psi: 0 }; 200 } 201 function blend(a, b, k) { 202 return { x: mix(a.x, b.x, k), y: mix(a.y, b.y, k), d: Math.exp(mix(Math.log(a.d), Math.log(b.d), k)), phi: mix(a.phi, b.phi, k), psi: mix(a.psi, b.psi, k) }; 203 } 204 function cinema(t) { 205 const u = threadAt(t), h = along(u), i = Math.min(LEN - 1, u | 0); 206 const speed = (threadAt(t + .08) - threadAt(t - .08)) / .16, k = ss(14, 150, speed); 207 const chase = { 208 d: mix(9, 40, ss(6, 300, speed) ** .8), 209 phi: mix(54, 47, k) * RAD, psi: (-30 + 9 * Math.sin(t * .21)) * RAD, 210 }; 211 // glide along the smoothed route, but never let the head of the thread leave the middle of the frame 212 let ox = track[2 * i] - h[0], oy = track[2 * i + 1] - h[1]; 213 const reach = Math.min(1, chase.d * TAN * .45 / (Math.hypot(ox, oy) + 1e-6)); 214 chase.x = h[0] + ox * reach * k + 1; chase.y = h[1] + oy * reach * k; 215 // open on the whole thing, seen from the side like a model on a table; end directly overhead 216 const o = overview(), push = clamp(t / T.dive, 0, 1.4); 217 const opening = { x: W * .5, y: H * .53, d: o.d * (.98 - .1 * push), phi: 54 * RAD, psi: (-24 + 5 * push) * RAD }; 218 const dive = ss(T.dive - .4, T.go + .9, t), rise = ss(T.back + 1.2, T.home - 2.4, t); 219 const c = blend(blend(opening, chase, dive), o, rise); 220 c.dof = mix(.75, 1, dive) * (1 - rise); 221 c.gain = mix(2.1, 1, ss(T.dive - .4, T.go, t)); // open the aperture for the dark establishing shot 222 return c; 223 } 224 function basis(c) { 225 const sp = Math.sin(c.phi), cp = Math.cos(c.phi), sy = Math.sin(c.psi), cy = Math.cos(c.psi); 226 return { pos: [c.x + c.d * sp * sy, c.y + c.d * sp * cy, c.d * cp], R: [cy, -sy, 0], U: [-sy * cp, -cy * cp, sp], F: [-sp * sy, -sp * cy, -cp] }; 227 } 228 function pick(c, nx, ny) { // screen (−1..1) → the point on the floor under it 229 const b = basis(c), ax = nx * TAN * vw / vh, ay = ny * TAN; 230 const d = [0, 1, 2].map(i => b.F[i] + b.R[i] * ax + b.U[i] * ay), t = -b.pos[2] / d[2]; 231 return [b.pos[0] + d[0] * t, b.pos[1] + d[1] * t]; 232 } 233 234 // ─────────────────────────────────────────────────────────────────────────── 235 // THE LIGHT (one fragment shader: cast a ray per pixel, walk it through the walls) 236 // ─────────────────────────────────────────────────────────────────────────── 237 const VERT = `#version 300 es 238 void main() { gl_Position = vec4(vec2(gl_VertexID & 1, gl_VertexID >> 1) * 4. - 1., 0, 1); }`; 239 240 const SCENE = `#version 300 es 241 precision highp float; precision highp int; precision highp usampler2D; 242 out vec4 O; 243 uniform vec2 uRes; 244 uniform vec3 uCam, uR, uU, uF; 245 uniform float uTan, uFocus, uDof, uTime; 246 uniform usampler2D uMaze, uSilver; // walls + the gold route · your silver route 247 uniform sampler2D uLight; // per-cell light: r gold, g silver, b the monster's ember 248 uniform ivec2 uSize; 249 uniform float uGold, uSilverLen, uEmber, uPulse, uDone; 250 uniform vec4 uHead; // the clew: x, y, radius, spark 251 uniform vec3 uHover, uLair; 252 253 const float WH = .78; // wall height, in cells; walls are a quarter-cell thick 254 const float LANE = ${LANE}; 255 const vec2 DIR[4] = vec2[4](vec2(1, 0), vec2(0, 1), vec2(-1, 0), vec2(0, -1));Bashcd /tmp/tools && cat > tail.js <<'EOF'
command
cd /tmp/tools && cat > tail.js <<'EOF' const stage = document.getElementById('stage'); let canvas = document.getElementById('gl'); // The maze as bytes, one RGBA texel per cell (plus an extra row and column for the far walls). // r: 1 = wall to the west, 2 = wall to the north, 4 = corner post // g: which sides the gold thread enters and leaves by · b, a: how far along the route this cell is const mazeData = new Uint8Array((W + 1) * (H + 1) * 4); for (let y = 0; y <= H; y++) for (let x = 0; x <= W; x++) { const c = x + y * W; let b = 4; if (y < H && (x === 0 ? c !== L.A && c !== L.B : x === W || !(open[c] >> 2 & 1))) b |= 1; if (x < W && (y === 0 || y === H || !(open[c] >> 3 & 1))) b |= 2; mazeData[(x + y * (W + 1)) * 4] = b; } path.forEach((c, i) => { const o = (c % W + (c / W | 0) * (W + 1)) * 4; mazeData[o + 1] = goldSides[i]; mazeData[o + 2] = i & 255; mazeData[o + 3] = i >> 8; }); const silverData = new Uint8Array(N * 4), lightData = new Uint8Array(N * 4); function makeGL() { const gl = canvas.getContext('webgl2', { antialias: false, alpha: false, powerPreference: 'high-performance' }); if (!gl) return null; gl.getExtension('EXT_color_buffer_float'); gl.getExtension('EXT_color_buffer_half_float'); const texture = (w, h, internal, format, type, filter, data) => { const t = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, t); gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1); gl.texImage2D(gl.TEXTURE_2D, 0, internal, w, h, 0, format, type, data || null); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, filter); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, filter); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); return t; }; let HDR = true; const target = (w, h) => { const tex = HDR ? texture(w, h, gl.RGBA16F, gl.RGBA, gl.HALF_FLOAT, gl.LINEAR) : texture(w, h, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, gl.LINEAR); const fb = gl.createFramebuffer(); gl.bindFramebuffer(gl.FRAMEBUFFER, fb); gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, tex, 0); return { tex, fb, w, h }; }; // Half-float render targets if the GPU allows; otherwise 8 bits, square-root encoded, which is nearly as good. target(4, 4); HDR = gl.checkFramebufferStatus(gl.FRAMEBUFFER) === gl.FRAMEBUFFER_COMPLETE; const CODEC = HDR ? '#define ENC(c) (c)\n#define DEC(c) (c)\n' : '#define ENC(c) sqrt(clamp((c) / 8., 0., 1.))\n#define DEC(c) ((c) * (c) * 8.)\n'; const program = fs => { const p = gl.createProgram(); for (const [type, src] of [[gl.VERTEX_SHADER, VERT], [gl.FRAGMENT_SHADER, fs.replace('out vec4 O;', 'out vec4 O;\n' + CODEC)]]) { const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s); if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s)); gl.attachShader(p, s); } gl.linkProgram(p); if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p)); const u = {}; for (let i = 0; i < gl.getProgramParameter(p, gl.ACTIVE_UNIFORMS); i++) { const name = gl.getActiveUniform(p, i).name; u[name] = gl.getUniformLocation(p, name); } return { p, u }; }; let pScene, pBlur, pFinal; try { pScene = program(SCENE); pBlur = program(BLUR); pFinal = program(FINAL); } catch (e) { console.warn('A Fabled Clue: falling back to the flat renderer.', e); return null; } gl.bindVertexArray(gl.createVertexArray()); const texMaze = texture(W + 1, H + 1, gl.RGBA8UI, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE, gl.NEAREST, mazeData); const texSilver = texture(W, H, gl.RGBA8UI, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE, gl.NEAREST, silverData); const texLight = texture(W, H, gl.RGBA8, gl.RGBA, gl.UNSIGNED_BYTE, gl.LINEAR, lightData); const bind = (unit, tex) => { gl.activeTexture(gl.TEXTURE0 + unit); gl.bindTexture(gl.TEXTURE_2D, tex); }; [redacted: code variable flagged by the scan] (to, w, h) => { gl.bindFramebuffer(gl.FRAMEBUFFER, to); gl.viewport(0, 0, w, h); gl.drawArrays(gl.TRIANGLES, 0, 3); }; canvas.addEventListener('webglcontextlost', e => e.preventDefault()); canvas.addEventListener('webglcontextrestored', () => location.reload()); let scene, down = [], up = []; // render targets: the scene, then a chain of halvings for lens blur and bloom return { flat: false, resize(w, h) { for (const t of [scene, ...down, ...up]) if (t) { gl.deleteTexture(t.tex); gl.deleteFramebuffer(t.fb); } scene = target(w, h); down = []; up = []; for (let i = 0; i < 6; i++) { w = Math.max(1, w >> 1); h = Math.max(1, h >> 1); down.push(target(w, h)); up.push(i > 0 && i < 5 ? target(w, h) : null); } }, silver() { bind(1, texSilver); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, W, H, gl.RGBA_INTEGER, gl.UNSIGNED_BYTE, silverData); }, finish() { gl.finish(); }, draw(f) { // 1 ─ the scene const S = pScene.u, b = basis(f.cam); bind(2, texLight); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, W, H, gl.RGBA, gl.UNSIGNED_BYTE, lightData); bind(0, texMaze); bind(1, texSilver); gl.useProgram(pScene.p); gl.uniform2f(S.uRes, scene.w, scene.h); gl.uniform3fv(S.uCam, b.pos); gl.uniform3fv(S.uR, b.R); gl.uniform3fv(S.uU, b.U); gl.uniform3fv(S.uF, b.F); gl.uniform1f(S.uTan, TAN); gl.uniform1f(S.uFocus, f.cam.d); gl.uniform1f(S.uDof, f.cam.dof); gl.uniform1f(S.uTime, f.t % 1000); gl.uniform1i(S.uMaze, 0); gl.uniform1i(S.uSilver, 1); gl.uniform1i(S.uLight, 2); gl.uniform2i(S.uSize, W, H); gl.uniform1f(S.uGold, f.u); gl.uniform1f(S.uSilverLen, f.silverLen); gl.uniform1f(S.uEmber, f.ember); gl.uniform1f(S.uPulse, f.pulse); gl.uniform1f(S.uDone, f.done); gl.uniform4f(S.uHead, f.head[0], f.head[1], f.ball, f.spark); gl.uniform3f(S.uHover, f.hover[0], f.hover[1], f.hover[2]); gl.uniform2f(S.uLair, f.lair[0], f.lair[1]); pass(scene.fb, scene.w, scene.h); // 2 ─ halve it six times, then climb back up, averaging: soft copies for the lens, a wide one for bloom const B = pBlur.u; gl.useProgram(pBlur.p); gl.uniform1i(B.uA, 0); gl.uniform1i(B.uB, 1); let src = scene; gl.uniform1f(B.uUp, 0); for (const d of down) { gl.uniform2f(B.uInv, 1 / d.w, 1 / d.h); bind(1, src.tex); bind(0, src.tex); pass(d.fb, d.w, d.h); src = d; } gl.uniform1f(B.uUp, 1); for (let i = 4; i >= 1; i--) { const d = up[i]; gl.uniform2f(B.uInv, 1 / d.w, 1 / d.h); bind(0, down[i].tex); bind(1, src.tex); pass(d.fb, d.w, d.h); src = d; } // 3 ─ develop: lens, bloom, vignette, film curve, grain const F = pFinal.u; gl.useProgram(pFinal.p); [scene, down[0], down[1], up[1]].forEach((x, i) => bind(i, x.tex)); gl.uniform1i(F.uScene, 0); gl.uniform1i(F.uSoft, 1); gl.uniform1i(F.uSofter, 2); gl.uniform1i(F.uBloom, 3); gl.uniform2f(F.uRes, scene.w, scene.h); gl.uniform1f(F.uFade, f.fade); gl.uniform1f(F.uGain, f.cam.gain); gl.uniform1f(F.uTime, f.t % 1000); pass(null, scene.w, scene.h); }, }; } // No WebGL 2? The same labyrinth, thread and telling, drawn flat from above with the 2D canvas. function makeFlat() { let ctx = canvas.getContext('2d'); if (!ctx) { const fresh = canvas.cloneNode(); canvas.replaceWith(fresh); canvas = fresh; ctx = canvas.getContext('2d'); } let wallsAt = 0, wallImg = null; const walls = k => { // the walls, drawn once at k pixels per cell const c = document.createElement('canvas'), g = c.getContext('2d'); c.width = W * k + 2; c.height = H * k + 2; g.strokeStyle = '#56679a'; g.lineWidth = Math.max(1, k * .22); g.lineCap = 'square'; g.beginPath(); for (let y = 0; y <= H; y++) for (let x = 0; x <= W; x++) { const b = mazeData[(x + y * (W + 1)) * 4]; if (b & 1) { g.moveTo(x * k + 1, y * k + 1); g.lineTo(x * k + 1, y * k + k + 1); } if (b & 2) { g.moveTo(x * k + 1, y * k + 1); g.lineTo(x * k + k + 1, y * k + 1); } } g.stroke(); return c; }; return { flat: true, resize() { wallsAt = 0; }, silver() {}, finish() {}, draw(f) { const w = canvas.width, h = canvas.height, k = h / (2 * f.cam.d * TAN), ox = w / 2 - f.cam.x * k, oy = h / 2 - f.cam.y * k; const fit = Math.max(2, Math.round(h / (2 * overview().d * TAN))); if (wallsAt !== fit) wallImg = walls(wallsAt = fit); const trace = (route, len, width, colour, glow) => { ctx.beginPath(); ctx.moveTo(ox, oy + (route[0] / W + .5 | 0) * k + k / 2); for (let i = 0; i < len && i < route.length; i++) ctx.lineTo(ox + (route[i] % W + .5) * k, oy + ((route[i] / W | 0) + .5) * k); ctx.lineWidth = width; ctx.strokeStyle = colour; ctx.shadowColor = colour; ctx.shadowBlur = glow; ctx.stroke(); ctx.shadowBlur = 0; }; ctx.globalAlpha = 1; ctx.fillStyle = '#05060a'; ctx.fillRect(0, 0, w, h); ctx.globalAlpha = f.fade; ctx.fillStyle = '#0c1226'; ctx.fillRect(ox, oy, W * k, H * k); ctx.lineJoin = ctx.lineCap = 'round'; if (f.u > 0) trace(path, Math.ceil(f.u), k * .8, '#c9862c', k * 1.5); ctx.drawImage(wallImg, ox - k / wallsAt, oy - k / wallsAt, wallImg.width * k / wallsAt, wallImg.height * k / wallsAt); if (f.u > 0) trace(path, Math.ceil(f.u), Math.max(1, k * .16), '#ffe6a8', 0); if (silver.length) trace(silver, Math.ceil(f.silverLen), Math.max(1, k * .16), '#a9c8ff', k * .6); if (f.ember > .01) { ctx.fillStyle = `rgba(255,40,20,${Math.min(1, f.ember * .8)})`; ctx.beginPath(); ctx.arc(ox + f.lair[0] * k, oy + f.lair[1] * k, k * .6, 0, 7); ctx.fill(); } }, }; } const view = makeGL() || makeFlat(); // ─────────────────────────────────────────────────────────────────────────── // THE TITLE (eleven letters that know two places to stand) // ─────────────────────────────────────────────────────────────────────────── const titleEl = document.getElementById('title'), capEl = document.getElementById('cap'); // glyph · its place in "FIG. 5.2 — A FABLED CLUE" · its place in "CLAUDE FABLE 5.2" · how high it hops on the way const GLYPHS = [ ['FIG.', 0, -1, 0], ['5.2', 1, 12, -1.5], ['—', 2, -1, 0], ['A', 3, 2, 1.25], ['F', 5, 6, -1], ['A', 6, 7, -1], ['B', 7, 8, -1], ['L', 8, 9, -1], ['E', 9, 10, -1], ['D', 10, 4, 1.25], ['C', 12, 0, .55], ['L', 13, 1, .55], ['U', 14, 3, .55], ['E', 15, 5, .55], ].map(([s, a, b, hop]) => ({ s, a, b, hop, el: titleEl.appendChild(Object.assign(document.createElement('span'), { textContent: s })) })); const GAPS_A = { 0: .9, 1: .9, 2: .9, 3: 1.05, 10: 1.05 }, GAPS_B = { 5: 1.05, 10: 1.25 }; // word spaces fall after these places function layoutTitle() { const em = parseFloat(getComputedStyle(titleEl).fontSize), tracking = .4 * em; for (const g of GLYPHS) { g.el.style.letterSpacing = g.s.length > 1 ? '.16em' : '0'; g.w = g.el.getBoundingClientRect().width || g.s.length * em * .7; } for (const [key, gaps] of [['a', GAPS_A], ['b', GAPS_B]]) { const row = GLYPHS.filter(g => g[key] >= 0).sort((p, q) => p[key] - q[key]); let x = 0; for (const g of row) { g['x' + key] = x; x += g.w + tracking + (gaps[g[key]] || 0) * em; } for (const g of row) g['x' + key] -= (x - tracking) / 2; } for (const g of GLYPHS) if (g.b < 0) g.xb = g.xa; titleEl.em = em; } function drawTitle(t) { const k = clamp((t - T.anagram) / 2.3, 0, 1), em = titleEl.em || 12, lit = ss(.55, 1, k), show = ss(.2, 1.2, t); for (const g of GLYPHS) { const d = clamp(k * 1.35 - (g.hop > 1 ? .12 : g.hop > 0 ? 0 : .22), 0, 1), e = d * d * (3 - 2 * d); const x = mix(g.xa, g.xb, e), y = -g.hop * em * Math.sin(Math.PI * e) * .95, quiet = g.s.length > 1; g.el.style.transform = `translate(${x.toFixed(2)}px,${y.toFixed(2)}px)`; g.el.style.opacity = show * (g.b < 0 ? 1 - ss(0, .3, k) : 1) * (quiet ? mix(.5, 1, g.b < 0 ? 0 : lit) : mix(.86, 1, lit)); g.el.style.color = `rgb(${mix(222, 255, lit) | 0},${mix(214, 207, lit) | 0},${mix(198, 122, lit) | 0})`; g.el.style.textShadow = `0 0 ${(.9 * em).toFixed(1)}px rgba(255,170,60,${(lit * .75).toFixed(3)}), 0 0 6px #05060a`; } } let capShown = -2; function drawCaption(t, hint) { let which = -1, alpha = 0; LINES.forEach(([a, b], i) => { if (t >= a && t < b) { which = i; alpha = ss(a, a + .55, t) * (1 - ss(b - .45, b, t)); } }); if (which < 0 && hint > 0) { which = LINES.length; alpha = hint; } if (which !== capShown && which >= 0) capEl.innerHTML = which < LINES.length ? LINES[which][2] : HINT; capShown = which; capEl.style.opacity = alpha.toFixed(3); } // ─────────────────────────────────────────────────────────────────────────── // YOUR THREAD (after the telling: the one route from the door to wherever you point) // ─────────────────────────────────────────────────────────────────────────── let silver = [], silverLen = 0, hoverCell = -1, hoverGlow = 0, hoverXY = [0, 0]; function runThreadTo(cell) { if (cell === hoverCell) return; hoverCell = cell; const r = cell < 0 ? [] : L.route(cell), s = sides(r, true); let shared = 0; while (shared < r.length && shared < silver.length && r[shared] === silver[shared]) shared++; for (const c of silver) silverData[c * 4] = 0; r.forEach((c, i) => { silverData[c * 4] = s[i]; silverData[c * 4 + 1] = i & 255; silverData[c * 4 + 2] = i >> 8; }); view.silver(); silverGoal.fill(0); for (const c of r) flood(c, 4, (m, d) => { const v = falloff(d, 4); if (v > silverGoal[m]) silverGoal[m] = v; }); silver = r; silverLen = Math.min(silverLen, shared); // keep what the old route and the new one have in common if (cell >= 0) hoverXY = [cell % W + .5, (cell / W | 0) + .5]; } // ─────────────────────────────────────────────────────────────────────────── // THE HAND (pointer, wheel, pinch; an embed should never trap the page's scroll) // ─────────────────────────────────────────────────────────────────────────── let cam = overview(), free = null, engaged = false; const pointers = new Map(); let pinch = null, moved = 0; const ndc = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * 2 - 1, 1 - (e.clientY - r.top) / r.height * 2]; }; const takeOver = () => free || (free = { x: cam.x, y: cam.y, d: cam.d }); function tiltFor(d) { // the closer you look, the more the camera leans in if (view.flat) return { phi: 0, psi: 0 }; const o = overview(), z = clamp(Math.log(o.d / d) / Math.log(o.d / 7), 0, 1); return { phi: 57 * RAD * ss(.04, .85, z), psi: -26 * RAD * ss(.1, 1, z) }; } function zoomAt(p, factor) { // zoom, keeping the spot under the pointer where it is const f = takeOver(), before = pick({ ...f, ...tiltFor(f.d) }, p[0], p[1]); f.d = clamp(f.d * factor, 7, overview().d * 1.1); const after = pick({ ...f, ...tiltFor(f.d) }, p[0], p[1]); f.x = clamp(f.x + before[0] - after[0], 0, W); f.y = clamp(f.y + before[1] - after[1], 0, H); } function point(e) { if (clock < T.home) return; const p = ndc(e), g = pick(cam, p[0], p[1]), x = Math.floor(g[0]), y = Math.floor(g[1]); if (x >= 0 && y >= 0 && x < W && y < H) runThreadTo(x + y * W); } canvas.addEventListener('pointerdown', e => { canvas.setPointerCapture(e.pointerId); pointers.set(e.pointerId, ndc(e)); moved = 0; pinch = null; if (e.pointerType === 'mouse') engaged = true; }); canvas.addEventListener('pointermove', e => { const p = ndc(e), was = pointers.get(e.pointerId); if (!was) { if (e.pointerType === 'mouse') point(e); return; } pointers.set(e.pointerId, p); if (pointers.size === 1) { moved += Math.hypot(p[0] - was[0], p[1] - was[1]); if (moved > .02) { const f = takeOver(), c = { ...f, ...tiltFor(f.d) }, a = pick(c, was[0], was[1]), b = pick(c, p[0], p[1]); f.x = clamp(f.x + a[0] - b[0], 0, W); f.y = clamp(f.y + a[1] - b[1], 0, H); stage.classList.add('drag'); } } else if (pointers.size === 2) { const [a, b] = [...pointers.values()], mid = [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2], gap = Math.hypot((a[0] - b[0]) * vw / vh, a[1] - b[1]); if (pinch) { const f = takeOver(), c = { ...f, ...tiltFor(f.d) }, g0 = pick(c, pinch.mid[0], pinch.mid[1]), g1 = pick(c, mid[0], mid[1]); f.x += g0[0] - g1[0]; f.y += g0[1] - g1[1]; zoomAt(mid, pinch.gap / Math.max(gap, 1e-3)); } pinch = { mid, gap }; moved = 1; } }); const release = e => { if (e.type === 'pointerup' && pointers.size === 1 && moved <= .02) point(e); // a tap or a click pointers.delete(e.pointerId); pinch = null; stage.classList.remove('drag'); }; canvas.addEventListener('pointerup', release); canvas.addEventListener('pointercancel', release); canvas.addEventListener('pointerleave', e => { if (e.pointerType === 'mouse') engaged = false; }); canvas.addEventListener('wheel', e => { if (!engaged && !e.ctrlKey && !document.fullscreenElement) return; // not ours yet: let the page scroll past e.preventDefault(); zoomAt(ndc(e), Math.exp(clamp(e.deltaY, -120, 120) * (e.ctrlKey ? .01 : .0022))); }, { passive: false }); canvas.addEventListener('dblclick', e => zoomAt(ndc(e), takeOver().d > overview().d * .3 ? .28 : 3.6)); document.getElementById('replay').onclick = () => { clock = 0; free = null; runThreadTo(-1); silverLen = 0; hintAt = 0; playing = true; }; const fullBtn = document.getElementById('full'); if (!stage.requestFullscreen) fullBtn.style.display = 'none'; fullBtn.onclick = () => document.fullscreenElement ? document.exitFullscreen() : stage.requestFullscreen(); document.addEventListener('fullscreenchange', () => stage.classList.toggle('full', !!document.fullscreenElement)); // ─────────────────────────────────────────────────────────────────────────── // THE LOOP // ─────────────────────────────────────────────────────────────────────────── const still = matchMedia('(prefers-reduced-motion: reduce)').matches; // then skip the flight: start at the ending let clock = query.has('t') ? +query.get('t') : still ? T.free + 1 : 0, playing = !query.has('t'), visible = false, lastFrame = 0, hintAt = 0; const maxRatio = query.has('ratio') ? +query.get('ratio') : Math.min(devicePixelRatio || 1, 2); let scale = 1, pw = 0, ph = 0, slow = 0, quick = 0; function resize() { vw = Math.max(2, stage.clientWidth); vh = Math.max(2, stage.clientHeight); const w = Math.max(2, Math.round(vw * maxRatio * scale)), h = Math.max(2, Math.round(vh * maxRatio * scale)); if (w === pw && h === ph) return; pw = canvas.width = w; ph = canvas.height = h; view.resize(w, h); layoutTitle(); } function draw(t, dt) { resize(); const u = threadAt(t); // the camera is the film's, until you take hold of it if (free) { const goal = { ...free, ...tiltFor(free.d), dof: .8 * ss(0, 1.6, Math.log(overview().d / free.d)), gain: 1 }, k = 1 - Math.exp(-dt * 9); cam = { ...blend(cam, goal, k), dof: mix(cam.dof, goal.dof, k), gain: mix(cam.gain, 1, k) }; } else cam = cinema(t); // light: gold follows the thread; silver eases toward your route goldLight(u); const ease = 1 - Math.exp(-dt * 10), reach = Math.max(0, silver.length - .5); silverLen = Math.min(reach, silverLen + (reach - silverLen) * (1 - Math.exp(-dt * 6)) + dt * 30); hoverGlow += ((hoverCell >= 0 ? 1 : 0) - hoverGlow) * ease; for (let i = 0; i < N; i++) { lightSilver[i] += (silverGoal[i] - lightSilver[i]) * ease; lightData[4 * i] = lightGold[i] * 255; lightData[4 * i + 1] = lightSilver[i] * 255; lightData[4 * i + 2] = lightEmber[i] * 255; } const laying = t >= T.go && t < T.home, dying = ss(T.lair + .25, T.back, t); view.draw({ t, u, cam, silverLen, head: along(u), ball: laying || t < T.go ? .3 * Math.cbrt(1 - u / LEN) : 0, spark: laying ? .6 + .4 * Math.sin(t * 23) : 0, ember: (1 - dying) * (.72 + .28 * Math.sin(t * 3.1) + 2.4 * ss(T.lair - .1, T.lair + .25, t)), // the monster: a flare, then nothing lair: along(LAIR), pulse: t < T.home ? -1e4 : (t - T.home) / 1.5 * LEN, // when the thread comes home, a wave of light runs its length done: ss(T.home, T.home + 1.2, t), hover: [hoverXY[0], hoverXY[1], hoverGlow], fade: ss(0, .9, t), }); drawTitle(t); if (t > T.free && !hintAt) hintAt = t; drawCaption(t, hintAt ? ss(T.free, T.free + .8, t) * (1 - ss(hintAt + 9, hintAt + 11, t)) : 0); } function frame(ms) { requestAnimationFrame(frame); const dt = Math.min(.1, (ms - lastFrame) / 1000 || 0); lastFrame = ms; if (!visible) return; // off-screen: wait. The telling starts when you can see it. if (playing) clock += dt; draw(clock, dt); // keep it smooth: trade resolution for frame rate on a slow machine, and take it back on a fast one if (query.has('ratio')) return; if (dt > .034) { quick = 0; if (++slow > 20 && scale > .45) { scale *= .8; slow = 0; } } else if (dt < .02) { slow = 0; if (++quick > 240 && scale < 1) { scale = Math.min(1, scale * 1.12); quick = 0; } } } new IntersectionObserver(es => { visible = es[0].isIntersecting; }, { threshold: .25 }).observe(stage); addEventListener('resize', () => visible || draw(clock, 0)); draw(clock, 0); requestAnimationFrame(frame); // For driving this from outside, one frame at a time (it is how the video was captured): clue.seek(12.5) window.clue = { T, labyrinth: L, seek(t) { playing = false; draw(clock = t, 1 / 30); view.finish(); }, play() { playing = true; } }; const P = L.proof, sorted = s => [...s.replace(/\W/g, '')].sort().join(''); console.log('%cA FABLED CLUE', 'font: 600 15px Georgia; color: #d9a441', `\n ${P.perfect ? '✓' : '✗'} ${P.cells.toLocaleString()} cells, ${P.passages.toLocaleString()} passages, all reachable: a perfect maze (one route between any two cells) ${P.exact ? '✓' : '✗'} the route from IN to OUT is ${P.pathLength.toLocaleString()} cells long and covers the handwriting exactly (${P.pathInsideWord.toLocaleString()} of ${P.wordCells.toLocaleString()} cells) ${sorted('A FABLED CLUE') === sorted('CLAUDE FABLE') ? '✓' : '✗'} "A FABLED CLUE" and "CLAUDE FABLE" are the same eleven letters built from seed ${SEED} in ${buildMs.toFixed(0)} ms: ${P.regions.toLocaleString()} regions, ${P.doors.toLocaleString()} doors, ${view.flat ? 'drawn flat (no WebGL 2 here)' : 'drawn by one fragment shader'} try ?seed=7\n`); </script> </html> EOF python3 - <<'EOF' s=open('/tmp/tools/template.html').read() top=s[:s.index("const stage = document.getElementById('stage')")] tail=open('/tmp/tools/tail.js').read() rep=[ ("precision highp float; precision highp int; precision highp usampler2D;","precision highp float; precision highp int; precision highp sampler2D; precision highp usampler2D;"), ("uniform vec3 uHover, uLair;","uniform vec3 uHover; uniform vec2 uLair;"), ("vec2 k = g - uLair.xy;","vec2 k = g - uLair;"), ("col += RED * uEmber * uLair.z * 5. * smoothstep(.03, .0, horns);","col += RED * uEmber * 5. * smoothstep(.03, .0, horns);"), (""" float a = uGold * 1.3 / max(uHead.z, .05) * .02 + uTime * .6; n.xz = mat2(cos(a), -sin(a), sin(a), cos(a)) * n.xz; float wind = max(max(sin(dot(n, vec3(.8, .6, 0)) * 34.), sin(dot(n, vec3(-.5, .5, .7)) * 34.)), sin(dot(n, vec3(.1, -.7, .7)) * 34.)); float edge = smoothstep(0., px * t * 4. * uHead.z + 1e-4, sqrt(h)); vec3 ball = GOLD * vec3(1., 1.05, 1.2) * (.55 + .5 * wind) * (.9 + 1.1 * max(dot((uCam + rd * t - ce) / uHead.z, SUN), 0.)) + GOLD * .5; col = mix(col, ball, edge);""", """ vec3 w = n; float a = uGold * 1.1; // it rolls as it goes w.xz = mat2(cos(a), -sin(a), sin(a), cos(a)) * w.xz; vec3 m = abs(w); // three windings; whichever lies on top shows its strands float wind = m.x > m.y && m.x > m.z ? sin(w.y * 26. + w.z * 9.) : m.y > m.z ? sin(w.z * 26. + w.x * 9.) : sin(w.x * 26. + w.y * 9.); vec3 ball = GOLD * vec3(1., 1.06, 1.25) * (.5 + .34 * wind) * (1.25 + 1.3 * max(dot(n, SUN), 0.) + 1.2 * pow(1. - max(dot(n, -rd), 0.), 2.)); col = mix(col, ball, smoothstep(0., px * t * 3. * uHead.z + 1e-4, sqrt(h)));"""), ("precision highp float; out vec4 O;\nuniform sampler2D uA, uB;","precision highp float; precision highp sampler2D; out vec4 O;\nuniform sampler2D uA, uB;"), ("precision highp float; out vec4 O;\nuniform sampler2D uScene,","precision highp float; precision highp sampler2D; out vec4 O;\nuniform sampler2D uScene,"), (""" [T.back - .3, 19.4, 'Then he followed it home.'], [19.9, T.home + .3, 'Which is why whatever leads you out of a maze is still called a <em>clue</em>.'], [T.six, T.seven - .3, 'This one has been writing as it went.'], [T.seven, T.free - .3, 'So has the title.'],""", """ [T.back - .3, 19.4, 'Then he followed it home.'], [19.9, T.home + .3, 'And that is why anything that leads you out of a maze is still called a <em>clue</em>.'], [T.six, T.seven - .3, 'This one was spelling a name the whole way.'], [T.seven, T.free - .3, 'So was the title.'],"""), ("""function cinema(t) { const u = threadAt(t),""","""function cinema(t) { if (view.flat) return { ...overview(), dof: 0, gain: 1 }; const u = threadAt(t),"""), ] for a,b in rep: assert a in top, a top=top.replace(a,b) open('/tmp/tools/template.html','w').write(top+tail) EOF node build.js && (timeout 300 node shot2.js chromium /tmp/probe/c_start.png 800x450 2 4.9 & timeout 300 node shot2.js chromium /tmp/probe/c_mid.png 800x450 1 14.9 & timeout 300 node shot2.js firefox /tmp/probe/ff.png 800x450 1 20 2>&1 | grep -v "^║\|^╔\|^╚\|AllowWebgl2\|FEATURE_FAILURE" | cut -c1-400 & timeout 300 node shot2.js firefox /tmp/probe/ff2.png 800x450 1 40 620,110 2>&1 | grep -v "^║\|^╔\|^╚\|AllowWebgl2\|FEATURE_FAILURE" | cut -c1-400 & wait)result, 8 lines
built 57984 bytes [console] [JavaScript Warning: "Failed to create WebGL context: WebGL creation failed: firefox ok 800x450 chromium ok 800x450 [console] [JavaScript Warning: "Failed to create WebGL context: WebGL creation failed: firefox ok 800x450 chromium ok 1600x900 Shell cwd was reset to ~/run