{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-marched-knurled-aluminium-dial-on-black-glass-with-a-moving-softbox-engraved-stops-and-an-amber-halo","type":"registry:block","title":"Ray-Marched Knurled Aluminium Dial on Black Glass with a Moving Softbox, Engraved Stops and an Amber Halo","description":"Selector rendered as a knurled aluminium dial in a WebGL fragment shader, on black glass, with a real radio group underneath. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/ray-marched-knurled-aluminium-dial-on-black-glass-with-a-moving-softbox-engraved-stops-and-an-amber-halo.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\nimport type { KeyboardEvent as ReactKeyboardEvent } from \"react\";\n\n// The stops, left to right. `level` is how hard the halo under the dial burns (0 to 1).\nconst stops = [\n  { label: \"Off\", note: \"Lamp dark, halo resting\", level: 0 },\n  { label: \"Night\", note: \"Five percent, enough to find the stairs\", level: 0.1 },\n  { label: \"Evening\", note: \"A third of the output, warm and low\", level: 0.34 },\n  { label: \"Reading\", note: \"Seventy percent over the armchair\", level: 0.68 },\n  { label: \"Full\", note: \"Everything the lamp has\", level: 1 },\n];\nconst SWEEP = 148; // degrees of travel between the first stop and the last\nconst START = 3; // the stop it opens on\nconst LAMP: [number, number, number] = [1, 0.5, 0.13]; // halo, pointer paint and lit numeral (linear RGB)\nconst FONT = '\"Inter Tight\", \"Inter\", ui-sans-serif, system-ui, -apple-system, \"Segoe UI\", sans-serif';\nconst SERIF = '\"Iowan Old Style\", \"Palatino Linotype\", Palatino, \"Book Antiqua\", Georgia, serif';\n\nconst rad = (d: number) => (d * Math.PI) / 180;\nconst stopAngle = (i: number) => rad((stops.length === 1 ? 0 : i / (stops.length - 1) - 0.5) * SWEEP);\nconst levelAt = (a: number) => {\n  const f = Math.max(0, Math.min(stops.length - 1, (a / rad(SWEEP) + 0.5) * (stops.length - 1)));\n  const i = Math.min(stops.length - 2, Math.floor(f));\n  return stops[i].level + (stops[i + 1].level - stops[i].level) * (f - i);\n};\n\nconst VERT = `ATTR vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;\n\nconst TONE = `\nvec3 tone(vec3 x){ x *= 0.92; vec3 a = x * (2.51 * x + 0.03); vec3 b = x * (2.43 * x + 0.59) + 0.14; return pow(clamp(a / b, 0.0, 1.0), vec3(1.0 / 2.2)); }\n`;\n\n// The whole photograph: signed-distance geometry, studio softboxes, one traced bounce.\nconst SCENE = `\nprecision highp float;\nuniform vec2 uRes; uniform vec4 uJit; uniform float uSeed; uniform float uAng; uniform float uLvl;\nuniform vec3 uCam; uniform vec3 uTgt; uniform float uFoc; uniform vec2 uShift; uniform float uFocus;\nuniform float uKey; uniform float uFlash; uniform vec3 uLamp; uniform sampler2D uTex; uniform float uDirect;\nconst float HB = 0.14; const float RB = 1.78; const float KB = 0.165; const float KH = 0.95;\nconst float PI = 3.14159265;\nconst vec3 ALU = vec3(0.913, 0.921, 0.925);\n${TONE}\nfloat h11(float p){ p = fract(p * 0.1031); p *= p + 33.33; p *= p + p; return fract(p); }\nfloat h21(vec2 p){ vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }\nfloat n1(float x){ float i = floor(x); float f = fract(x); f = f * f * (3.0 - 2.0 * f); return mix(h11(i), h11(i + 1.0), f); }\nfloat n2(vec2 x){ vec2 i = floor(x); vec2 f = fract(x); f = f * f * (3.0 - 2.0 * f);\n  return mix(mix(h21(i), h21(i + vec2(1.0, 0.0)), f.x), mix(h21(i + vec2(0.0, 1.0)), h21(i + vec2(1.0, 1.0)), f.x), f.y); }\n\n// Pointer frame of the knob: x runs across the pointer slot, y runs along it.\nvec2 knobFrame(vec2 xz){ float s = sin(uAng); float c = cos(uAng); return vec2(xz.x * c + xz.y * s, xz.x * s - xz.y * c); }\n\nfloat sdBase(vec3 p){\n  float r = length(p.xz);\n  float d = max(r - RB, p.y - HB);\n  d = max(d, (r - RB + p.y - HB + 0.075) * 0.7071);\n  return d;\n}\nfloat knurl(vec3 p, float y){\n  vec2 k = knobFrame(p.xz);\n  float u = atan(k.x, k.y) * (54.0 / (2.0 * PI));\n  float v = y * (54.0 / (2.0 * PI)) * 0.62;\n  return min(1.0, (1.0 - 2.0 * max(abs(fract(u + v) - 0.5), abs(fract(u - v) - 0.5))) * 1.45);\n}\nfloat slotCut(vec3 p){\n  vec2 k = knobFrame(p.xz);\n  float ax = abs(k.x) - 0.04;\n  float top = max(max(ax, 0.36 - k.y), KB + KH - 0.05 - p.y);\n  float side = max(max(ax, 0.957 - k.y), KB + 0.80 - p.y);\n  return min(top, side);\n}\nfloat sdKnob(vec3 p){\n  float r = length(p.xz); float y = p.y - KB;\n  float R = 1.0 - max(0.0, 0.034 - abs(y - 0.165)) - max(0.0, 0.034 - abs(y - 0.755)) - 0.014 * step(0.755, y);\n  float band = smoothstep(0.20, 0.235, y) * smoothstep(0.72, 0.685, y);\n  if (band > 0.0 && r > 0.93 && r < 1.06) R -= 0.026 * band * (1.0 - knurl(p, y));\n  float d = max(r - R, max(y - KH, -y));\n  d = max(d, (r + y - (0.986 + KH - 0.085)) * 0.7071);\n  d = max(d, (r - y - 0.975) * 0.7071);\n  d = max(d, 9.2 - length(p - vec3(0.0, KB + KH + 9.2 - 0.034, 0.0)));\n  d = max(d, -slotCut(p));\n  return d;\n}\nfloat map(vec3 p){ return min(sdBase(p), sdKnob(p)); }\n\nvec2 bound(vec3 ro, vec3 rd){\n  float a = dot(rd.xz, rd.xz); float b = dot(ro.xz, rd.xz); float c = dot(ro.xz, ro.xz) - 3.24;\n  float h = b * b - a * c; if (h < 0.0 || a < 1e-6) return vec2(1.0, -1.0);\n  h = sqrt(h); float t0 = (-b - h) / a; float t1 = (-b + h) / a;\n  if (abs(rd.y) > 1e-5) { float ya = (0.0 - ro.y) / rd.y; float yb = (1.13 - ro.y) / rd.y; t0 = max(t0, min(ya, yb)); t1 = min(t1, max(ya, yb)); }\n  else if (ro.y < 0.0 || ro.y > 1.13) return vec2(1.0, -1.0);\n  return vec2(max(t0, 0.0), t1);\n}\nfloat march(vec3 ro, vec3 rd){\n  vec2 tb = bound(ro, rd); if (tb.x > tb.y) return -1.0;\n  float t = tb.x;\n  for (int i = 0; i < 80; i++) { float d = map(ro + rd * t); if (d < 0.0006 * (1.0 + t * 0.3)) return t; t += d * 0.72; if (t > tb.y) break; }\n  return -1.0;\n}\nfloat marchLite(vec3 ro, vec3 rd){\n  vec2 tb = bound(ro, rd); if (tb.x > tb.y) return -1.0;\n  float t = tb.x + 0.004;\n  for (int i = 0; i < 44; i++) { float d = map(ro + rd * t); if (d < 0.0016) return t; t += d * 0.8; if (t > tb.y) break; }\n  return -1.0;\n}\nvec3 calcN(vec3 p){\n  vec2 e = vec2(0.0011, -0.0011);\n  return normalize(e.xyy * map(p + e.xyy) + e.yyx * map(p + e.yyx) + e.yxy * map(p + e.yxy) + e.xxx * map(p + e.xxx));\n}\nfloat calcAO(vec3 p, vec3 n){\n  float o = 0.0; float s = 1.0;\n  for (int i = 1; i <= 5; i++) { float h = 0.012 + 0.055 * float(i); o += (h - map(p + n * h)) * s; s *= 0.72; }\n  return clamp(1.0 - 2.4 * o, 0.0, 1.0);\n}\nfloat softShadow(vec3 ro, vec3 rd){\n  float res = 1.0; float t = 0.03;\n  for (int i = 0; i < 22; i++) { float h = map(ro + rd * t); res = min(res, 3.6 * h / t); t += clamp(h, 0.035, 0.3); if (res < 0.01 || t > 4.5) break; }\n  return clamp(res, 0.0, 1.0);\n}\n\n// A rectangular softbox: where the ray meets its plane, with edges that soften as the surface gets rougher.\nfloat rectL(vec3 ro, vec3 rd, vec3 c, vec3 n, vec3 u, vec3 v, vec2 hs, float soft){\n  float dn = dot(rd, n); if (dn > -1e-4) return 0.0;\n  float t = dot(c - ro, n) / dn; if (t < 0.0) return 0.0;\n  vec3 q = ro + rd * t - c; float s = soft * t;\n  vec2 d = abs(vec2(dot(q, u), dot(q, v))) - hs;\n  // a faint cloth falloff toward the edges of the scrim\n  float cloth = 1.0 - 0.28 * clamp(length(vec2(dot(q, u), dot(q, v)) / hs), 0.0, 1.4);\n  return smoothstep(s, -s, d.x) * smoothstep(s, -s, d.y) * cloth;\n}\nvec3 keyPos(){ return vec3(uKey, 7.3, 9.5); }\nvec3 env(vec3 ro, vec3 rd, float rough){\n  float soft = 0.035 + rough * 0.75;\n  vec3 c = mix(vec3(0.02, 0.021, 0.026), vec3(0.12, 0.125, 0.14), smoothstep(-0.1, 1.0, rd.y));\n  // key: a big scrim high behind the dial, leaning over it\n  vec3 kn = normalize(vec3(0.0, -0.42, -1.0));\n  c += vec3(1.0, 0.965, 0.93) * 7.5 * rectL(ro, rd, keyPos(), kn, vec3(1.0, 0.0, 0.0), normalize(cross(kn, vec3(1.0, 0.0, 0.0))), vec2(1.9, 1.35), soft);\n  // overhead box that the bevels pick up\n  c += vec3(0.93, 0.96, 1.0) * 3.4 * rectL(ro, rd, vec3(uKey * 0.8 - 0.4, 7.5, -4.6), vec3(0.0, -1.0, 0.0), vec3(1.0, 0.0, 0.0), vec3(0.0, 0.0, 1.0), vec2(2.6, 1.7), soft);\n  // cool strip, camera left\n  vec3 ln = normalize(vec3(1.0, -0.05, 0.42));\n  c += vec3(0.82, 0.9, 1.0) * 6.0 * rectL(ro, rd, vec3(-7.6, 2.5, -3.4), ln, normalize(cross(vec3(0.0, 1.0, 0.0), ln)), vec3(0.0, 1.0, 0.0), vec2(1.5, 2.5), soft);\n  // warm strip, camera right\n  vec3 rn = normalize(vec3(-1.0, -0.05, 0.3));\n  c += vec3(1.0, 0.84, 0.66) * 3.4 * rectL(ro, rd, vec3(7.8, 2.8, -2.2), rn, normalize(cross(vec3(0.0, 1.0, 0.0), rn)), vec3(0.0, 1.0, 0.0), vec2(0.9, 2.8), soft);\n  // the cyclorama far behind, lit from the floor: it is what the glass shows at the top of the frame\n  float cyc = smoothstep(0.03, 0.2, rd.y) * smoothstep(0.52, 0.2, rd.y) * smoothstep(-0.2, 0.75, rd.z) * (0.55 + 0.45 * smoothstep(-0.9, 0.5, rd.x));\n  c += vec3(0.62, 0.72, 0.9) * 0.36 * cyc;\n  return c;\n}\n// Below the horizon the room is the glass table, which mirrors the lights dimly.\nvec3 envFloor(vec3 ro, vec3 rd, float rough){\n  if (rd.y >= 0.0) return env(ro, rd, rough);\n  float t = -ro.y / rd.y; vec3 gp = ro + rd * t; vec3 r = vec3(rd.x, -rd.y, rd.z);\n  float f = 0.05 + 0.95 * pow(1.0 - clamp(-rd.y, 0.0, 1.0), 5.0);\n  return vec3(0.004, 0.0045, 0.006) + f * env(gp, r, rough + 0.03);\n}\n\nstruct Mat { vec3 alb; vec3 f0; float rough; vec3 emi; float kind; vec3 bit; float spread; float occ; float wide; };\n\nMat getMat(vec3 p, inout vec3 n){\n  Mat m; m.alb = vec3(0.0); m.f0 = ALU; m.rough = 0.08; m.emi = vec3(0.0); m.kind = 0.0; m.bit = vec3(0.0, 1.0, 0.0); m.spread = 0.0; m.occ = 1.0; m.wide = 0.03;\n  float r = length(p.xz); vec3 rdir = vec3(p.x, 0.0, p.z) / max(r, 1e-4);\n  if (sdBase(p) < sdKnob(p)) {\n    float ch = r - RB + p.y - HB + 0.075;\n    if (p.y > HB - 0.002 && ch < -0.003) {\n      // top of the plate: satin black anodising, paint-filled engraving, the halo ring\n      vec2 uv = vec2(p.x, -p.z) / (2.0 * RB) + 0.5;\n      float e = 1.0 / 1024.0;\n      float ink = texture2D(uTex, uv).r;\n      float ix = texture2D(uTex, uv + vec2(e, 0.0)).r - texture2D(uTex, uv - vec2(e, 0.0)).r;\n      float iy = texture2D(uTex, uv + vec2(0.0, e)).r - texture2D(uTex, uv - vec2(0.0, e)).r;\n      n = normalize(n + vec3(ix, 0.0, -iy) * 0.9);\n      float grainv = n2(p.xz * 260.0);\n      m.alb = vec3(0.011, 0.012, 0.015); m.f0 = vec3(0.05); m.rough = 0.3 + 0.08 * grainv; m.kind = 6.0; m.bit = rdir; m.spread = 0.3;\n      n = normalize(n + rdir * (n1(r * 240.0) - 0.5) * 0.05);\n      n = normalize(n + vec3(grainv - 0.5, 0.0, n2(p.zx * 260.0 + 9.0) - 0.5) * 0.05);\n      float phi = atan(p.x, -p.z);\n      float sel = smoothstep(0.2, 0.07, abs(phi - uAng)) * step(1.17, r);\n      vec3 paint = mix(vec3(0.74, 0.75, 0.74), uLamp * 0.9, sel);\n      m.alb = mix(m.alb, paint, ink * 0.94);\n      m.rough = mix(m.rough, 0.55, ink);\n      m.emi = uLamp * ink * sel * (0.55 + 1.3 * uLvl + 3.0 * uFlash);\n      float ring = smoothstep(1.035, 1.05, r) * smoothstep(1.125, 1.11, r);\n      float spill = exp(-max(r - 1.11, 0.0) * 5.0) * step(1.11, r);\n      m.alb = mix(m.alb, vec3(0.3, 0.25, 0.2), ring);\n      m.rough = mix(m.rough, 0.3, ring);\n      m.emi += uLamp * ring * (0.03 + 5.5 * uLvl) * (1.0 + 0.5 * uFlash) + uLamp * spill * uLvl * 0.26 * (0.75 + 0.5 * grainv);\n    } else if (ch > -0.004 && p.y > HB - 0.074) {\n      // diamond-cut chamfer: bare bright aluminium with the cutter's fine rings\n      m.rough = 0.045; m.kind = 2.0; m.bit = normalize(rdir - vec3(0.0, 1.0, 0.0)); m.spread = 0.035;\n      n = normalize(n + m.bit * (n1(ch * 900.0 + r * 300.0) - 0.5) * 0.03);\n    } else {\n      m.alb = vec3(0.011, 0.012, 0.015); m.f0 = vec3(0.045); m.rough = 0.36; m.kind = 1.0;\n    }\n    return m;\n  }\n  float y = p.y - KB;\n  if (slotCut(p) < 0.004) {\n    // enamel paint flooded into the pointer slot\n    m.alb = uLamp * vec3(0.95, 0.8, 0.7); m.f0 = vec3(0.045); m.rough = 0.22; m.kind = 1.0; m.emi = uLamp * (0.05 + 0.5 * uLvl);\n    return m;\n  }\n  if (n.y > 0.94 && r < 0.9) {\n    // faced on the lathe: thousands of circular scratches, so the highlight opens into two fans\n    float g = n1(r * 310.0) - 0.5; float g2 = n1(r * 71.0 + 4.0) - 0.5;\n    n = normalize(n + rdir * (g * 0.05 + g2 * 0.035));\n    n = normalize(n - rdir * smoothstep(0.045, 0.0, r) * 0.6);\n    m.rough = 0.05 + 0.03 * n1(r * 37.0); m.kind = 3.0; m.bit = rdir; m.spread = 0.42;\n    m.occ = 0.86 + 0.14 * n1(r * 160.0 + 2.0); m.wide = 0.05;\n    return m;\n  }\n  if (abs(n.y) < 0.3) {\n    float band = smoothstep(0.20, 0.235, y) * smoothstep(0.72, 0.685, y);\n    if (band > 0.5) {\n      // the knurl: cut pyramids, bright on the faces and dull in the valleys\n      float k = knurl(p, y);\n      m.rough = 0.16; m.kind = 4.0; m.occ = mix(0.35, 1.0, smoothstep(0.0, 0.55, k)); m.wide = 0.3;\n      return m;\n    }\n    // turned skirt and cap: horizontal tool marks smear the lights vertically\n    n = normalize(n + vec3(0.0, 1.0, 0.0) * ((n1(y * 420.0) - 0.5) * 0.07 + (n1(y * 90.0 + 3.0) - 0.5) * 0.04));\n    m.rough = 0.06; m.kind = 5.0; m.bit = vec3(0.0, 1.0, 0.0); m.spread = 0.3; m.wide = 0.2;\n    return m;\n  }\n  // polished bevels and groove walls\n  m.rough = 0.04; m.kind = 2.0; m.bit = normalize(cross(cross(n, vec3(0.0, 1.0, 0.0)), n) + vec3(0.0, 1e-4, 0.0)); m.spread = 0.03;\n  n = normalize(n + m.bit * (n1(y * 800.0 + r * 500.0) - 0.5) * 0.02);\n  return m;\n}\n\nvec3 diffuseLight(vec3 p, vec3 n, float sh, float ao){\n  vec3 lk = normalize(keyPos() - p);\n  vec3 c = vec3(1.0, 0.965, 0.93) * 1.5 * max(dot(n, lk), 0.0) * sh;\n  c += vec3(0.93, 0.96, 1.0) * 0.5 * max(n.y, 0.0) * ao;\n  c += vec3(0.82, 0.9, 1.0) * 0.55 * max(dot(n, normalize(vec3(-7.6, 2.5, -3.4) - p)), 0.0) * ao;\n  c += vec3(1.0, 0.84, 0.66) * 0.25 * max(dot(n, normalize(vec3(7.8, 2.8, -2.2) - p)), 0.0) * ao;\n  return c + vec3(0.03) * ao;\n}\nvec3 fres(vec3 f0, float c){ return f0 + (1.0 - f0) * pow(1.0 - clamp(c, 0.0, 1.0), 5.0); }\nvec3 envAniso(vec3 p, vec3 rd, vec3 n, vec3 b, float spread, float rough){\n  vec3 c = vec3(0.0); float w = 0.0;\n  for (int i = -6; i <= 6; i++) { float f = float(i) / 6.0; float g = exp(-2.6 * f * f); c += g * envFloor(p, reflect(rd, normalize(n + b * f * spread)), rough); w += g; }\n  return c / w;\n}\n// What a mirror sees: shading without a further bounce.\nvec3 shadeLite(vec3 p, vec3 n, vec3 rd){\n  Mat m = getMat(p, n);\n  vec3 r = reflect(rd, n);\n  vec3 s = m.spread > 0.1 ? envAniso(p, rd, n, m.bit, m.spread, m.rough) : envFloor(p, r, m.rough + 0.04);\n  vec3 dl = diffuseLight(p, n, 0.6, 0.8);\n  return m.emi + m.alb * dl + fres(m.f0, dot(-rd, n)) * (s + m.wide * dl) * m.occ;\n}\nvec3 groundLite(vec3 gp, vec3 rd){\n  vec3 r = vec3(rd.x, -rd.y, rd.z);\n  float f = 0.05 + 0.95 * pow(1.0 - clamp(-rd.y, 0.0, 1.0), 5.0);\n  float occ = smoothstep(0.0, 0.5, map(gp + vec3(0.0, 0.02, 0.0)));\n  return vec3(0.004, 0.0045, 0.006) * occ + f * env(gp, r, 0.05) * (0.25 + 0.75 * occ);\n}\nvec3 trace(vec3 p, vec3 r, float rough){\n  float t = marchLite(p, r);\n  if (t > 0.0) { vec3 hp = p + r * t; vec3 hn = calcN(hp); return shadeLite(hp, hn, r); }\n  if (r.y < 0.0) return groundLite(p + r * (-p.y / r.y), r);\n  return env(p, r, rough);\n}\nvec3 shadeFull(vec3 p, vec3 n, vec3 rd, vec2 rnd){\n  vec3 gn = n;\n  Mat m = getMat(p, n);\n  float ao = calcAO(p, gn);\n  vec3 lk = normalize(keyPos() - p);\n  float sh = softShadow(p + gn * 0.012, lk);\n  vec3 dl = diffuseLight(p, n, sh, ao);\n  vec3 col = m.emi + m.alb * dl;\n  vec3 F = fres(m.f0, dot(-rd, n));\n  vec3 r = reflect(rd, n);\n  vec3 jr = normalize(r + (vec3(rnd.x, rnd.y, rnd.x * rnd.y * 2.0) ) * m.rough * 0.5);\n  vec3 s;\n  if (m.kind == 3.0) s = envAniso(p, rd, n, m.bit, m.spread, m.rough);\n  else if (m.kind == 5.0) s = mix(envAniso(p, rd, n, m.bit, m.spread, m.rough), trace(p + gn * 0.006, jr, m.rough), 0.5);\n  else if (m.kind == 6.0) s = envAniso(p, rd, n, m.bit, m.spread, m.rough) * (0.3 + 0.7 * ao);\n  else if (m.kind == 1.0) s = envFloor(p, r, m.rough) * (0.35 + 0.65 * ao);\n  else s = trace(p + gn * 0.006, jr, m.rough);\n  float so = m.kind == 3.0 ? 1.0 : (0.45 + 0.55 * ao);\n  return col + F * (s * so + m.wide * dl) * m.occ;\n}\nvec3 shadeGround(vec3 gp, vec3 rd, vec2 rnd, float dist){\n  float near = step(dot(gp.xz, gp.xz), 60.0);\n  vec3 n = normalize(vec3((n2(gp.xz * 0.7) - 0.5) * 0.0012, 1.0, (n2(gp.zx * 0.7 + 5.0) - 0.5) * 0.0012));\n  vec3 r = reflect(rd, n);\n  r = normalize(r + vec3(rnd.x, 0.0, rnd.y) * 0.016);\n  float f = 0.05 + 0.95 * pow(1.0 - clamp(dot(-rd, n), 0.0, 1.0), 5.0);\n  float occ = 1.0; float sh = 1.0;\n  if (near > 0.5) {\n    occ = smoothstep(-0.02, 0.6, map(gp + vec3(0.0, 0.03, 0.0)));\n    sh = softShadow(gp + vec3(0.0, 0.01, 0.0), normalize(keyPos() - gp));\n  }\n  vec3 refl;\n  float t = near > 0.5 ? marchLite(gp + vec3(0.0, 0.002, 0.0), r) : -1.0;\n  if (t > 0.0) { vec3 hp = gp + r * t; vec3 hn = calcN(hp); refl = shadeLite(hp, hn, r); }\n  else refl = env(gp, r, 0.2) * (0.3 + 0.7 * occ);\n  // the glass: nearly no diffuse, a few specks of dust that catch the key\n  vec3 lk = normalize(keyPos() - gp);\n  vec2 cell = floor(gp.xz * 19.0); float hsp = h21(cell);\n  vec2 cp = (cell + 0.2 + 0.6 * vec2(h21(cell + 3.1), h21(cell + 7.7))) / 19.0;\n  float speck = step(0.992, hsp) * smoothstep(0.004 + 0.009 * h21(cell + 1.3), 0.0, length(gp.xz - cp));\n  float pool = smoothstep(8.5, 0.8, length(gp.xz - vec2(0.4, 0.2)));\n  vec3 alb = vec3(0.04, 0.042, 0.05) * (0.9 + 0.2 * n2(gp.xz * 3.0)) + speck * 0.25;\n  vec3 col = alb * (vec3(1.0, 0.965, 0.93) * 2.2 * max(lk.y, 0.0) * sh * pool * pool + vec3(0.1) * occ) * occ;\n  col += f * refl * (0.95 + 0.1 * n2(gp.xz * 0.8 + 3.0));\n  // room falls away into the dark behind\n  col *= mix(1.0, 0.0, smoothstep(14.0, 46.0, dist));\n  return col;\n}\n\nvoid main(){\n  vec2 fc = gl_FragCoord.xy + uJit.xy;\n  vec2 uv = (fc - 0.5 * uRes) / uRes.y - uShift;\n  vec3 fw = normalize(uTgt - uCam); vec3 rt = normalize(cross(vec3(0.0, 1.0, 0.0), fw)); vec3 up = cross(fw, rt);\n  vec3 rd = normalize(fw * uFoc + rt * uv.x + up * uv.y);\n  vec3 ro = uCam;\n  vec3 fp = ro + rd * (uFocus / dot(rd, fw));\n  ro += rt * uJit.z + up * uJit.w; rd = normalize(fp - ro);\n  float ign = fract(52.9829189 * fract(dot(gl_FragCoord.xy, vec2(0.06711056, 0.00583715))) + uSeed);\n  float ang = ign * 6.2831853; float rr = sqrt(fract(ign * 7.13 + uSeed * 3.7));\n  vec2 rnd = vec2(cos(ang), sin(ang)) * rr * step(0.001, uSeed);\n  float t = march(ro, rd);\n  float tg = rd.y < 0.0 ? -ro.y / rd.y : 1e9;\n  vec3 col;\n  if (t > 0.0 && t < tg) { vec3 p = ro + rd * t; vec3 n = calcN(p); col = shadeFull(p, n, rd, rnd); }\n  else if (tg < 1e8) col = shadeGround(ro + rd * tg, rd, rnd, tg);\n  else col = env(ro, rd, 0.0);\n  col = min(col, vec3(24.0));\n  if (uDirect > 0.5) col = tone(col);\n  outC = vec4(col, 1.0);\n}\n`;\n\n// Developing the negative: bloom off the hot highlights, filmic curve, vignette, grain.\nconst POST = `\nprecision highp float;\nuniform sampler2D uAcc; uniform vec2 uRes; uniform float uGrain; uniform vec2 uCentre;\n${TONE}\nfloat h21(vec2 p){ vec3 q = fract(vec3(p.xyx) * 0.1031); q += dot(q, q.yzx + 33.33); return fract((q.x + q.y) * q.z); }\nvoid main(){\n  vec2 uv = gl_FragCoord.xy / uRes;\n  vec3 c = texture2D(uAcc, uv).rgb;\n  // a light unsharp mask, the way a print is finished\n  vec2 px = 1.0 / uRes;\n  vec3 nb = texture2D(uAcc, uv + vec2(px.x, 0.0)).rgb + texture2D(uAcc, uv - vec2(px.x, 0.0)).rgb + texture2D(uAcc, uv + vec2(0.0, px.y)).rgb + texture2D(uAcc, uv - vec2(0.0, px.y)).rgb;\n  c = max(c + (c - nb * 0.25) * 0.55, 0.0);\n  vec3 b = vec3(0.0);\n  for (int i = 0; i < 20; i++) {\n    float a = float(i) * 2.39996; float r = sqrt((float(i) + 0.5) / 20.0);\n    vec3 s = texture2D(uAcc, uv + vec2(cos(a), sin(a)) * r * 0.024 * vec2(uRes.y / uRes.x, 1.0)).rgb;\n    b += min(max(s - 1.1, 0.0), vec3(6.0)) * (1.0 - 0.6 * r);\n  }\n  c += b * 0.009;\n  vec2 q = (uv - uCentre) * vec2(uRes.x / uRes.y, 1.0);\n  c *= 1.0 - 0.42 * smoothstep(0.25, 1.15, length(q));\n  c = tone(c);\n  float g = h21(gl_FragCoord.xy + uGrain * 91.7) - 0.5;\n  c += g * 0.022 * (1.0 - 0.5 * c.g);\n  outC = vec4(c, 1.0);\n}\n`;\n\n// The engraving, drawn once as a top-down plan of the plate: ticks, numerals and two small legends.\nfunction engraving() {\n  const S = 2048, RBASE = 1.78;\n  const cv = document.createElement(\"canvas\");\n  cv.width = S; cv.height = S;\n  const g = cv.getContext(\"2d\");\n  if (!g) return cv;\n  const px = S / (2 * RBASE);\n  g.fillStyle = \"#000\"; g.fillRect(0, 0, S, S);\n  g.translate(S / 2, S / 2);\n  g.fillStyle = \"#fff\"; g.strokeStyle = \"#fff\"; g.textAlign = \"center\"; g.textBaseline = \"middle\";\n  const n = stops.length, minor = 5;\n  for (let i = 0; i <= (n - 1) * minor; i++) {\n    const major = i % minor === 0;\n    const a = rad((i / ((n - 1) * minor) - 0.5) * SWEEP);\n    g.save(); g.rotate(-a);\n    g.fillRect((-(major ? 0.03 : 0.012) * px) / 2, 1.2 * px, (major ? 0.03 : 0.012) * px, (major ? 0.15 : 0.075) * px);\n    if (major) {\n      g.font = `600 ${0.2 * px}px ${FONT}`;\n      g.fillText(String(i / minor), 0, 1.52 * px);\n    }\n    g.restore();\n  }\n  const arc = (text: string, at: number, r: number, size: number, gap: number) => {\n    g.font = `600 ${size * px}px ${FONT}`;\n    const chars = text.split(\"\");\n    chars.forEach((c, k) => { g.save(); g.rotate(-(at + (k - (chars.length - 1) / 2) * gap)); g.fillText(c, 0, r * px); g.restore(); });\n  };\n  arc(\"OUTPUT\", rad(-SWEEP / 2 - 30), 1.36, 0.085, 0.058);\n  arc(\"FIVE STOP\", rad(SWEEP / 2 + 34), 1.36, 0.085, 0.058);\n  return cv;\n}\n\n// No WebGL: the same pose as a still. Every round part is a circle inside a plane squashed to the camera's tilt.\nconst TILT = 0.42;\nfunction Still({ mode }: { mode: number }) {\n  const glow = stops[mode].level;\n  const deg = (i: number, n: number) => (i / n - 0.5) * SWEEP;\n  const plane = (d: number, top: number) => ({ width: d, height: d, left: 260 - d / 2, top: top - d / 2, transform: `scaleY(${TILT})` });\n  const metal = \"linear-gradient(90deg,#0d0e11 0%,#565c66 7%,#c9d0d9 15%,#f3f5f7 19%,#7f8691 27%,#2b2e34 41%,#3a3e45 55%,#8e8a84 70%,#e7d9c6 79%,#6d665e 87%,#16171a 100%)\";\n  return (\n    <div aria-hidden=\"true\" className=\"absolute left-1/2 top-[33%] h-[420px] w-[520px] -translate-x-1/2 -translate-y-1/2 scale-[0.66] sm:scale-90 lg:left-[66.5%] lg:top-[51%] lg:scale-[1.22]\">\n      <div className=\"absolute rounded-full blur-2xl\" style={{ ...plane(620, 318), background: \"radial-gradient(closest-side, rgba(190,200,220,0.2), transparent)\" }} />\n      <div className=\"absolute rounded-full\" style={{ ...plane(500, 282), background: \"#020203\", boxShadow: \"0 40px 70px rgba(0,0,0,0.9)\" }} />\n      <div className=\"absolute rounded-full\" style={{ ...plane(500, 270), background: \"radial-gradient(circle at 50% 30%, #2c2e34 0%, #17181c 55%, #0e0f12 100%)\", boxShadow: \"inset 0 0 0 7px #7d848e, inset 0 6px 0 7px rgba(255,255,255,0.75), inset 0 0 0 9px rgba(0,0,0,0.5)\" }}>\n        {Array.from({ length: (stops.length - 1) * 5 + 1 }, (_, i) => (\n          <span key={i} className=\"absolute left-1/2 top-1/2 block\" style={{ width: i % 5 ? 2 : 4, marginLeft: i % 5 ? -1 : -2, height: i % 5 ? 10 : 21, background: i === mode * 5 ? \"#ffa13d\" : \"#d7d8d6\", transform: `rotate(${-deg(i, (stops.length - 1) * 5)}deg) translateY(169px)` }} />\n        ))}\n        {stops.map((_, i) => (\n          <span key={i} className=\"absolute left-1/2 top-1/2 -ml-5 -mt-5 flex h-10 w-10 items-center justify-center text-[28px] font-semibold\" style={{ color: i === mode ? \"#ffa13d\" : \"#d7d8d6\", textShadow: i === mode ? \"0 0 14px rgba(255,150,46,0.9)\" : \"none\", transform: `rotate(${-deg(i, stops.length - 1)}deg) translateY(213px)` }}>{i}</span>\n        ))}\n        <span className=\"absolute left-1/2 top-1/2 block rounded-full\" style={{ width: 318, height: 318, margin: -159, border: \"11px solid\", borderColor: `rgb(${Math.round(90 + glow * 165)},${Math.round(72 + glow * 118)},${Math.round(58 + glow * 50)})`, boxShadow: `0 0 ${glow * 38}px ${glow * 12}px rgba(255,150,46,${glow * 0.75}), inset 0 0 ${glow * 20}px rgba(255,170,80,${glow})` }} />\n      </div>\n      <div className=\"absolute left-[120px] top-[149px] h-[180px] w-[280px] overflow-hidden\" style={{ borderRadius: \"0 0 140px 140px / 0 0 59px 59px\", background: metal }}>\n        <div className=\"absolute inset-x-0 top-[38px] h-[104px]\" style={{ borderRadius: \"0 0 140px 140px / 0 0 59px 59px\", background: \"repeating-linear-gradient(58deg, rgba(0,0,0,0.62) 0 2.5px, rgba(255,255,255,0.1) 2.5px 4px, transparent 4px 9px), repeating-linear-gradient(-58deg, rgba(0,0,0,0.62) 0 2.5px, rgba(255,255,255,0.16) 2.5px 4px, transparent 4px 9px)\" }} />\n        <div className=\"absolute inset-x-0 bottom-0 h-[30px]\" style={{ background: `linear-gradient(180deg, transparent, rgba(255,150,46,${glow * 0.55}))` }} />\n      </div>\n      <div className=\"absolute rounded-full\" style={{ ...plane(280, 149), background: \"#aeb5be\", boxShadow: \"inset 0 0 0 2px rgba(255,255,255,0.7)\" }} />\n      <div className=\"absolute rounded-full\" style={{ ...plane(250, 147), background: \"conic-gradient(from 8deg, #f6f7f8 0deg, #c5cad1 16deg, #7d838c 40deg, #6b7079 90deg, #868c95 140deg, #d8dce1 168deg, #f2f3f5 184deg, #b9bec6 204deg, #747a83 235deg, #676c75 280deg, #8f959e 325deg, #f6f7f8 360deg)\" }}>\n        <span className=\"absolute left-1/2 top-1/2 block rounded-sm\" style={{ width: 10, marginLeft: -5, height: 82, marginTop: 44, background: \"#f39a2c\", boxShadow: \"inset 1px 0 0 rgba(0,0,0,0.4)\", transformOrigin: \"50% -44px\", transform: `rotate(${-deg(mode, stops.length - 1)}deg)` }} />\n      </div>\n    </div>\n  );\n}\n\nexport default function RayMarchedKnurledDial() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const radios = useRef<(HTMLButtonElement | null)[]>([]);\n  const [mode, setMode] = useState(START);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const modeRef = useRef(START);\n  const api = useRef<{ go: (i: number, fromUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current, pad = hit.current;\n    if (!box || !cv || !pad) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const opts = { antialias: false, alpha: false, powerPreference: \"high-performance\" as const };\n    const gl2 = cv.getContext(\"webgl2\", opts) as WebGL2RenderingContext | null;\n    const gl = (gl2 ?? cv.getContext(\"webgl\", opts)) as WebGLRenderingContext | null;\n    if (!gl) { setShown(\"still\"); return; }\n    const canAcc = !!gl2 && (!!gl2.getExtension(\"EXT_color_buffer_float\") || !!gl2.getExtension(\"EXT_color_buffer_half_float\"));\n    const info = gl.getExtension(\"WEBGL_debug_renderer_info\");\n    const renderer = info ? String(gl.getParameter(info.UNMASKED_RENDERER_WEBGL)) : \"\";\n    const software = /swiftshader|llvmpipe|software|basic render/i.test(renderer);\n    const still = reduce || software; // no idle drift: draw, refine, then rest\n\n    const head = gl2 ? \"#version 300 es\\n#define ATTR in\\n#define texture2D texture\\n\" : \"#define ATTR attribute\\n\";\n    const fhead = gl2 ? \"#version 300 es\\n#define texture2D texture\\nprecision highp float;\\nout vec4 outC;\\n\" : \"#define outC gl_FragColor\\n\";\n    const compile = (type: number, src: string) => {\n      const sh = gl.createShader(type);\n      if (!sh) return null;\n      gl.shaderSource(sh, src); gl.compileShader(sh);\n      return gl.getShaderParameter(sh, gl.COMPILE_STATUS) ? sh : null;\n    };\n    const link = (frag: string) => {\n      const v = compile(gl.VERTEX_SHADER, head + VERT), f = compile(gl.FRAGMENT_SHADER, fhead + frag);\n      const p = gl.createProgram();\n      if (!v || !f || !p) return null;\n      gl.attachShader(p, v); gl.attachShader(p, f); gl.bindAttribLocation(p, 0, \"aPos\"); gl.linkProgram(p);\n      return gl.getProgramParameter(p, gl.LINK_STATUS) ? p : null;\n    };\n    const scene = link(SCENE), post = canAcc ? link(POST) : null;\n    if (!scene || (canAcc && !post)) { setShown(\"still\"); return; }\n    const U = (p: WebGLProgram, n: string) => gl.getUniformLocation(p, n);\n    const su = Object.fromEntries([\"uRes\", \"uJit\", \"uSeed\", \"uAng\", \"uLvl\", \"uCam\", \"uTgt\", \"uFoc\", \"uShift\", \"uFocus\", \"uKey\", \"uFlash\", \"uLamp\", \"uTex\", \"uDirect\"].map((n) => [n, U(scene, n)]));\n    const pu = post ? Object.fromEntries([\"uAcc\", \"uRes\", \"uGrain\", \"uCentre\"].map((n) => [n, U(post, n)])) : {};\n\n    const quad = gl.createBuffer();\n    gl.bindBuffer(gl.ARRAY_BUFFER, quad);\n    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);\n    gl.enableVertexAttribArray(0); gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);\n\n    const tex = gl.createTexture();\n    gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, tex);\n    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, engraving());\n    gl.generateMipmap(gl.TEXTURE_2D);\n    const aniso = gl.getExtension(\"EXT_texture_filter_anisotropic\");\n    if (aniso) gl.texParameterf(gl.TEXTURE_2D, aniso.TEXTURE_MAX_ANISOTROPY_EXT, 8);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR);\n    gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n\n    // The accumulation plate: every frame is one more exposure blended onto it.\n    const acc = canAcc ? gl.createTexture() : null;\n    const fbo = canAcc ? gl.createFramebuffer() : null;\n    let W = 0, H = 0, scale = 1, cssW = 1, cssH = 1;\n    const size = () => {\n      const r = box.getBoundingClientRect();\n      cssW = Math.max(1, r.width); cssH = Math.max(1, r.height);\n      const dpr = Math.min(software ? 1 : 1.5, window.devicePixelRatio || 1) * scale;\n      // never more than about a million pixels of ray marching\n      const k = Math.min(1, Math.sqrt(1.45e6 / (cssW * cssH * dpr * dpr)));\n      const w = Math.max(2, Math.round(cssW * dpr * k)), h = Math.max(2, Math.round(cssH * dpr * k));\n      if (w === W && h === H) return;\n      W = w; H = h; cv.width = W; cv.height = H;\n      if (gl2 && acc && fbo) {\n        gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, acc);\n        gl2.texImage2D(gl.TEXTURE_2D, 0, gl2.RGBA16F, W, H, 0, gl.RGBA, gl2.HALF_FLOAT, null);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);\n        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, fbo);\n        gl.framebufferTexture2D(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, acc, 0);\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n      }\n      s.count = 0; s.fresh = true;\n    };\n\n    const a0 = stopAngle(START);\n    const s = {\n      a: a0, v: 0, target: a0, raw: a0, drag: false, lvl: stops[START].level,\n      flash: 0, armed: false, px: 0, py: 0, sx: 0, sy: 0, key: 1.2, count: 0, fresh: true, frame: 0, slow: 0,\n      visible: true, raf: 0, last: 0, t0: 0, touched: false, demoAt: 0, demoDir: -1, first: true,\n    };\n    const halton = (i: number, b: number) => { let f = 1, r = 0; while (i > 0) { f /= b; r += f * (i % b); i = Math.floor(i / b); } return r; };\n\n    const draw = (alpha: number, sample: number) => {\n      const wide = cssW >= 1024, aspect = cssW / cssH;\n      const el = 0.42, az = -0.2 + s.sx * 0.07, dist = 9;\n      const tgt = [0, 0.5, 0];\n      const cam = [tgt[0] + dist * Math.sin(az) * Math.cos(el), tgt[1] + dist * Math.sin(el) - s.sy * 0.25, tgt[2] - dist * Math.cos(az) * Math.cos(el)];\n      const foc = 2.12 * Math.min(1, aspect / 0.93);\n      const jx = sample ? halton(sample, 2) - 0.5 : 0, jy = sample ? halton(sample, 3) - 0.5 : 0;\n      const la = halton(sample, 5) * Math.PI * 2, lr = sample ? Math.sqrt(halton(sample, 7)) * 0.028 : 0;\n      gl.useProgram(scene);\n      gl.bindFramebuffer(gl.FRAMEBUFFER, canAcc ? fbo : null);\n      gl.viewport(0, 0, W, H);\n      if (canAcc && alpha < 1) { gl.enable(gl.BLEND); gl.blendColor(0, 0, 0, alpha); gl.blendFunc(gl.CONSTANT_ALPHA, gl.ONE_MINUS_CONSTANT_ALPHA); } else gl.disable(gl.BLEND);\n      gl.uniform2f(su.uRes, W, H);\n      gl.uniform4f(su.uJit, canAcc ? jx : 0, canAcc ? jy : 0, canAcc ? Math.cos(la) * lr : 0, canAcc ? Math.sin(la) * lr : 0);\n      gl.uniform1f(su.uSeed, canAcc && sample ? (sample * 0.61803) % 1 || 0.5 : 0);\n      gl.uniform1f(su.uAng, s.a); gl.uniform1f(su.uLvl, s.lvl);\n      gl.uniform3f(su.uCam, cam[0], cam[1], cam[2]); gl.uniform3f(su.uTgt, tgt[0], tgt[1], tgt[2]);\n      gl.uniform1f(su.uFoc, foc);\n      gl.uniform2f(su.uShift, wide ? 0.165 * aspect : 0, wide ? 0.012 : 0.135);\n      gl.uniform1f(su.uFocus, 8.05);\n      gl.uniform1f(su.uKey, s.key); gl.uniform1f(su.uFlash, s.flash);\n      gl.uniform3f(su.uLamp, LAMP[0], LAMP[1], LAMP[2]);\n      gl.uniform1i(su.uTex, 0);\n      gl.uniform1f(su.uDirect, canAcc ? 0 : 1);\n      gl.drawArrays(gl.TRIANGLES, 0, 3);\n      if (canAcc && post) {\n        gl.bindFramebuffer(gl.FRAMEBUFFER, null);\n        gl.disable(gl.BLEND);\n        gl.useProgram(post);\n        gl.uniform1i(pu.uAcc, 1); gl.uniform2f(pu.uRes, W, H);\n        gl.uniform1f(pu.uGrain, still ? 1 : s.frame % 64);\n        gl.uniform2f(pu.uCentre, wide ? 0.5 + 0.17 : 0.5, wide ? 0.5 : 0.62);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n    };\n\n    const go = (i: number, fromUser: boolean) => {\n      const k = Math.max(0, Math.min(stops.length - 1, i));\n      if (fromUser) s.touched = true;\n      if (Math.abs(stopAngle(k) - s.a) > 0.05) s.armed = true;\n      s.target = stopAngle(k);\n      if (k !== modeRef.current) { modeRef.current = k; setMode(k); }\n      if (reduce) { s.a = s.target; s.v = 0; s.lvl = stops[k].level; s.count = 0; }\n      wake();\n    };\n    api.current = { go };\n\n    const frame = (now: number) => {\n      s.raf = 0;\n      if (!s.visible) return;\n      const dtRaw = s.last ? (now - s.last) / 1000 : 0.016;\n      const dt = Math.min(0.033, dtRaw);\n      s.last = now; s.frame++;\n      if (!s.t0) { s.t0 = now; s.demoAt = now + 3600; }\n\n      // the self-running demo: it opens finished, holds, then walks the stops until someone touches it\n      if (!still && !s.touched && !s.drag && now > s.demoAt) {\n        let k = modeRef.current + s.demoDir;\n        if (k < 0 || k >= stops.length) { s.demoDir *= -1; k = modeRef.current + s.demoDir; }\n        go(k, false);\n        s.demoAt = now + (k === 0 || k === stops.length - 1 ? 2500 : 1700);\n      }\n\n      let moving = false;\n      if (!reduce) {\n        // heavy brass-on-steel feel: a stiff spring that overshoots the stop once and settles\n        const want = s.drag ? s.raw - Math.sin(((s.raw - stopAngle(0)) / (rad(SWEEP) / (stops.length - 1))) * Math.PI * 2) * (rad(SWEEP) / (stops.length - 1) / (Math.PI * 2)) * 0.8 : s.target;\n        const stiff = s.drag ? 900 : 300, damp = s.drag ? 46 : 15.5;\n        const steps = 3;\n        for (let i = 0; i < steps; i++) { s.v += (stiff * (want - s.a) - damp * s.v) * (dt / steps); s.a += s.v * (dt / steps); }\n        if (Math.abs(s.v) > 0.002 || Math.abs(want - s.a) > 0.0004) moving = true; else { s.a = want; s.v = 0; }\n        // the click: as the pointer first crosses its stop the numeral and the ring kick, then settle\n        if (!s.drag && s.armed && Math.abs(s.target - s.a) < 0.02) { s.armed = false; s.flash = 1; }\n        if (s.flash > 0.004) { s.flash *= Math.exp(-dt * 6.5); moving = true; } else s.flash = 0;\n        const lt = levelAt(s.a), dl = lt - s.lvl;\n        // the lamp warms up a touch behind the hand\n        if (Math.abs(dl) > 0.0015) { s.lvl += dl * (1 - Math.exp(-dt * 7)); moving = true; } else s.lvl = lt;\n      }\n      if (!still) {\n        s.sx += (s.px - s.sx) * (1 - Math.exp(-dt * 3)); s.sy += (s.py - s.sy) * (1 - Math.exp(-dt * 3));\n        const t = (now - s.t0) / 1000;\n        s.key = 1.2 + Math.sin(t * 0.42 + 0.6) * 2.1 + s.sx * 2.2;\n        moving = true;\n      }\n\n      if (s.fresh || !canAcc) { draw(1, 0); s.fresh = false; s.count = 1; }\n      else if (moving) { draw(Math.abs(s.v) > 0.25 ? 0.5 : still ? 0.5 : 0.085, 1 + (s.frame % 64)); s.count = still ? 1 : 6; }\n      else if (s.count < 40) { s.count++; draw(1 / s.count, s.count); }\n      if (s.first) { s.first = false; setShown(\"gl\"); }\n\n      // measured, not guessed: if frames run long for a while, give up some resolution\n      if (!still && dtRaw > 0.026) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n      if (s.slow > 45 && scale > 0.5) { scale *= 0.82; s.slow = 0; size(); }\n\n      if (moving || s.count < 40 || (!still && !s.touched)) s.raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!s.raf && s.visible) { s.last = 0; s.raf = requestAnimationFrame(frame); } };\n\n    size();\n    const ro = new ResizeObserver(() => { size(); wake(); });\n    ro.observe(box);\n    const io = new IntersectionObserver(([e]) => { s.visible = e.isIntersecting && !document.hidden; if (s.visible) wake(); }, { threshold: 0.05 });\n    io.observe(box);\n    const vis = () => { s.visible = !document.hidden; if (s.visible) wake(); };\n    document.addEventListener(\"visibilitychange\", vis);\n\n    let downX = 0, moved = 0, id = -1;\n    const down = (e: PointerEvent) => {\n      id = e.pointerId; downX = e.clientX; moved = 0; s.drag = true; s.touched = true; s.raw = s.a;\n      try { pad.setPointerCapture(id); } catch { /* the pointer is already gone */ }\n      wake();\n    };\n    const move = (e: PointerEvent) => {\n      const r = box.getBoundingClientRect();\n      s.px = ((e.clientX - r.left) / r.width) * 2 - 1; s.py = ((e.clientY - r.top) / r.height) * 2 - 1;\n      if (s.drag && e.pointerId === id) {\n        const dx = e.clientX - downX; downX = e.clientX; moved += Math.abs(dx);\n        let raw = s.raw + dx * 0.0105;\n        const lo = stopAngle(0), hi = stopAngle(stops.length - 1);\n        // past the end stop it leans against the spring and gives very little\n        if (raw < lo) raw = lo - (1 - Math.exp((raw - lo) * 2)) * 0.09;\n        if (raw > hi) raw = hi + (1 - Math.exp((hi - raw) * 2)) * 0.09;\n        s.raw = raw;\n        const k = Math.round(((Math.max(lo, Math.min(hi, raw)) - lo) / (hi - lo)) * (stops.length - 1));\n        if (k !== modeRef.current) { modeRef.current = k; setMode(k); }\n      }\n      if (!still || s.drag) wake();\n    };\n    const upFn = (e: PointerEvent) => {\n      if (!s.drag || e.pointerId !== id) return;\n      s.drag = false;\n      if (moved < 5) {\n        let k = modeRef.current + s.demoDir;\n        if (k < 0 || k >= stops.length) { s.demoDir *= -1; k = modeRef.current + s.demoDir; }\n        go(k, true);\n      } else go(modeRef.current, true);\n    };\n    const leave = () => { s.px = 0; s.py = 0; };\n    pad.addEventListener(\"pointerdown\", down);\n    box.addEventListener(\"pointermove\", move);\n    pad.addEventListener(\"pointerup\", upFn);\n    pad.addEventListener(\"pointercancel\", upFn);\n    box.addEventListener(\"pointerleave\", leave);\n    const lost = (e: Event) => { e.preventDefault(); setShown(\"still\"); };\n    cv.addEventListener(\"webglcontextlost\", lost);\n    wake();\n\n    return () => {\n      cancelAnimationFrame(s.raf); ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", vis);\n      pad.removeEventListener(\"pointerdown\", down); box.removeEventListener(\"pointermove\", move);\n      pad.removeEventListener(\"pointerup\", upFn); pad.removeEventListener(\"pointercancel\", upFn);\n      box.removeEventListener(\"pointerleave\", leave); cv.removeEventListener(\"webglcontextlost\", lost);\n      api.current = null;\n    };\n  }, []);\n\n  const pick = (i: number) => {\n    const k = Math.max(0, Math.min(stops.length - 1, i));\n    if (api.current) api.current.go(k, true); else { modeRef.current = k; setMode(k); }\n  };\n  const onKey = (e: ReactKeyboardEvent<HTMLButtonElement>, i: number) => {\n    let k = i;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowUp\") k = i + 1;\n    else if (e.key === \"ArrowLeft\" || e.key === \"ArrowDown\") k = i - 1;\n    else if (e.key === \"Home\") k = 0;\n    else if (e.key === \"End\") k = stops.length - 1;\n    else return;\n    e.preventDefault();\n    k = Math.max(0, Math.min(stops.length - 1, k));\n    pick(k);\n    radios.current[k]?.focus();\n  };\n\n  const still = shown === \"still\";\n\n  return (\n    <section className=\"relative isolate overflow-hidden bg-[#050608] text-white\" style={{ fontFamily: FONT }}>\n      <style>{`\n        @keyframes ray-marched-knurled-dial-rise { from { opacity: 0; transform: translateY(0.18em); filter: blur(6px); } to { opacity: 1; transform: none; filter: blur(0); } }\n        .ray-marched-knurled-dial-word { animation: ray-marched-knurled-dial-rise 520ms cubic-bezier(0.2, 0.9, 0.2, 1) both; }\n        @media (prefers-reduced-motion: reduce) { .ray-marched-knurled-dial-word { animation: none; } }\n      `}</style>\n      <div ref={stage} className=\"relative mx-auto h-[720px] w-full max-w-[1600px] lg:h-[760px]\" style={{ background: \"radial-gradient(90% 70% at 62% 30%, #15171c 0%, #090a0d 55%, #050608 100%)\" }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className={`absolute inset-0 h-full w-full transition-opacity duration-700 motion-reduce:transition-none ${shown === \"gl\" ? \"opacity-100\" : \"opacity-0\"}`} />\n\n        {still && <Still mode={mode} />}\n\n        <div ref={hit} className=\"absolute inset-x-0 top-0 h-[62%] cursor-grab select-none active:cursor-grabbing lg:inset-y-0 lg:left-[36%] lg:h-full\" style={{ touchAction: \"pan-y\" }} />\n\n        <div className=\"pointer-events-none absolute inset-0 flex flex-col justify-end px-6 pb-7 sm:px-10 lg:justify-between lg:px-16 lg:pb-14 lg:pt-14\">\n          <p className=\"hidden text-[11px] font-medium uppercase tracking-[0.34em] text-white/55 lg:block\">Hallam lamp <span className=\"mx-2 text-white/25\">/</span> output selector</p>\n          <div>\n            <p className=\"mb-3 flex items-baseline gap-3 text-[11px] font-medium uppercase tracking-[0.3em] text-white/55 lg:mb-5\">\n              <span className=\"tabular-nums text-[#ffa24a]\">{String(mode).padStart(2, \"0\")}</span>\n              <span className=\"h-px w-8 bg-white/25\" />\n              <span className=\"tabular-nums\">{String(stops.length - 1).padStart(2, \"0\")}</span>\n            </p>\n            <p aria-live=\"polite\" className=\"max-w-[9ch]\">\n              <span key={mode} className=\"ray-marched-knurled-dial-word block text-[72px] font-semibold leading-[0.86] tracking-[-0.055em] text-white sm:text-[96px] lg:text-[136px]\">{stops[mode].label}</span>\n              <span className=\"mt-4 block w-[24rem] max-w-[80vw] text-[19px] italic leading-snug text-white/70 lg:mt-7 lg:text-[23px]\" style={{ fontFamily: SERIF }}>{stops[mode].note}</span>\n            </p>\n            <div role=\"radiogroup\" aria-label=\"Lamp output\" className=\"pointer-events-auto mt-6 flex w-fit gap-1 rounded-full border border-white/10 bg-white/[0.04] p-1 backdrop-blur-sm lg:mt-9\">\n              {stops.map((st, i) => (\n                <button\n                  key={st.label}\n                  ref={(el) => { radios.current[i] = el; }}\n                  type=\"button\"\n                  role=\"radio\"\n                  aria-checked={i === mode}\n                  aria-label={`${st.label}: ${st.note}`}\n                  tabIndex={i === mode ? 0 : -1}\n                  onClick={() => pick(i)}\n                  onKeyDown={(e) => onKey(e, i)}\n                  className={`h-11 min-w-[52px] rounded-full px-3 text-[13px] font-medium tracking-[-0.01em] outline-none transition-colors duration-200 focus-visible:ring-2 focus-visible:ring-[#ffa24a] focus-visible:ring-offset-2 focus-visible:ring-offset-black sm:px-4 ${i === mode ? \"bg-white text-black\" : \"text-white/70 hover:bg-white/10 hover:text-white\"}`}\n                >\n                  <span className=\"tabular-nums sm:hidden\">{i}</span>\n                  <span className=\"hidden sm:inline\">{st.label}</span>\n                </button>\n              ))}\n            </div>\n          </div>\n        </div>\n        <p className=\"pointer-events-none absolute bottom-7 right-6 hidden text-[11px] uppercase tracking-[0.3em] text-white/40 sm:right-10 lg:bottom-14 lg:right-16 lg:block\">Drag to turn</p>\n      </div>\n    </section>\n  );\n}\n"}]}
