{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-marched-hourglass-loader-in-a-turned-ash-frame-on-a-sunlit-desk-that-pours-coral-sand-into-a-growing-cone-and-flips-over-when-full","type":"registry:block","title":"Ray-Marched Hourglass Loader in a Turned Ash Frame on a Sunlit Desk That Pours Coral Sand into a Growing Cone and Flips Over When Full","description":"Free React progress loader in one WebGL shader: a glass hourglass in a turned wooden frame pours coral sand into a cone, then flips itself over at 100%.","dependencies":[],"files":[{"path":"components/we-inc/ray-marched-hourglass-loader-in-a-turned-ash-frame-on-a-sunlit-desk-that-pours-coral-sand-into-a-growing-cone-and-flips-over-when-full.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// What the loader says while it works. `until` is the last percent that line covers.\nconst stages = [\n  { until: 16, note: \"Gathering the photographs\" },\n  { until: 41, note: \"Setting the type on every page\" },\n  { until: 67, note: \"Compressing the gallery\" },\n  { until: 90, note: \"Writing the contact form\" },\n  { until: 99, note: \"Proofing it on a small screen\" },\n  { until: 100, note: \"Done. Turning the glass over\" },\n];\n// The self-running demo: [percent to reach, seconds to get there]. A repeated percent is a pause.\n// To show real progress, delete the demo block in the frame loop and write your own number to `s.goal`.\nconst demo: [number, number][] = [\n  [74, 2.6], [74, 0.6], [88, 1.6], [96, 1.2], [100, 1.0],\n  [100, 99], // held here until the glass has been turned\n  [14, 1.6], [14, 0.5], [33, 1.8], [47, 1.4], [47, 0.5], [62, 1.6],\n];\nconst START = 62; // the percent it opens on: both bulbs hold sand and the stream is running\nconst RESTART = 6; // index in `demo` that follows a flip\nconst SAND = [0.78, 0.17, 0.065]; // the dyed sand, 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';\nconst TOPF = 0.93; // how high a full upper bulb is filled, in local units (the glass runs to 1.06)\n\n// Sand heights for the fraction that has run through, p from 0 to 1.\nconst heights = (p: number) => {\n  const q = Math.max(0, Math.min(1, p));\n  return {\n    top: TOPF * Math.pow(1 - q, 0.8),\n    crater: 0.24 * Math.min(1, q * 9) * Math.pow(1 - q, 0.3),\n    base: -1.06 + 0.42 * q,\n    peak: -1.06 + 0.8 * Math.pow(q, 0.55),\n  };\n};\n\nconst VERT = `ATTR vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;\nconst TONE = `\nvec3 tone(vec3 x){ x *= 0.9; 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 photograph. The hourglass is ray marched in its own frame; glass bends each ray in and out,\n// sand is a solid with grains in it, and the sun comes through a window out of frame on the left.\nconst SCENE = `\nprecision highp float;\nuniform vec2 uRes; uniform vec4 uJit; uniform vec3 uCam; uniform vec3 uTgt; uniform float uFoc; uniform vec2 uShift; uniform float uFocus;\nuniform float uAng; uniform float uLift; uniform float uTop; uniform float uCrat; uniform float uBase; uniform float uPeak; uniform float uHi;\nuniform vec3 uFlip; uniform float uFlow; uniform float uTime; uniform float uDirect; uniform vec3 uSand;\nconst float HC = 1.19;       // centre of the hourglass above the desk at rest\nconst float WZ = 2.55;       // the wall behind the desk\nconst float WX = -5.2;       // the window in the left wall\nconst vec3 SUN = vec3(-0.772, 0.574, -0.273);\nconst vec3 SUNC = vec3(1.0, 0.86, 0.68) * 3.4;\nconst vec3 SKY = vec3(0.62, 0.72, 0.9);\nconst float NG = 1.5;\n${TONE}\nfloat h31(vec3 p){ p = fract(p * 0.1031); p += dot(p, p.zyx + 31.32); return fract((p.x + p.y) * p.z); }\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 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); }\nfloat fbm(vec2 p){ return 0.5 * n2(p) + 0.25 * n2(p * 2.03 + 7.1) + 0.125 * n2(p * 4.01 + 3.3) + 0.0625 * n2(p * 8.1 + 1.7); }\n\n// ---- the frame of the object --------------------------------------------------------------------\nvec3 toLocal(vec3 p){ p.y -= HC + uLift; float c = cos(uAng), s = sin(uAng); return vec3(c * p.x + s * p.y, -s * p.x + c * p.y, p.z); }\nvec3 dirLocal(vec3 d){ float c = cos(uAng), s = sin(uAng); return vec3(c * d.x + s * d.y, -s * d.x + c * d.y, d.z); }\nvec3 dirWorld(vec3 d){ float c = cos(uAng), s = sin(uAng); return vec3(c * d.x - s * d.y, s * d.x + c * d.y, d.z); }\n\n// The blown profile: a long taper into the neck, a full belly, and a mouth that meets the wooden plate.\nfloat prof(float y){ float t = clamp(abs(y) / 1.06, 0.0, 1.0); return 0.044 + 0.405 * pow(sin(3.14159 * 0.86 * pow(t, 0.85)), 1.3); }\nfloat glassD(vec3 q){ return max((length(q.xz) - prof(q.y)) * 0.6, abs(q.y) - 1.07); }\n\nfloat rcyl(vec2 q, float r, float h, float rr){ vec2 d = vec2(q.x - r + rr, abs(q.y) - h + rr); return min(max(d.x, d.y), 0.0) + length(max(d, 0.0)) - rr; }\n// Turned on a lathe: a beaded spindle, fat at the ends, a collar halfway, a ring at the waist.\nfloat spR(float y){ float a = abs(y); return 0.03 + 0.017 * smoothstep(0.86, 1.02, a) + 0.016 * exp(-(a - 0.6) * (a - 0.6) * 260.0) + 0.009 * exp(-a * a * 300.0) + 0.006 * exp(-(a - 0.75) * (a - 0.75) * 900.0); }\nvec2 frame(vec3 q){\n  float rho = length(q.xz); float a = abs(q.y);\n  // the two plates: a broad board with a rounded edge and a groove, and a raised boss that holds the glass\n  float plate = rcyl(vec2(rho, a - 1.135), 0.66, 0.055, 0.026);\n  plate = max(plate, -(length(vec2(rho - 0.66, a - 1.135)) - 0.013));\n  plate = min(plate, rcyl(vec2(rho, a - 1.065), 0.54, 0.03, 0.012));\n  float sp = 1e9;\n  for (int k = 0; k < 3; k++) {\n    float an = 1.5708 + float(k) * 2.0944;\n    sp = min(sp, length(q.xz - 0.53 * vec2(cos(an), sin(an))) - spR(q.y));\n  }\n  sp = max(sp, a - 1.09);\n  return plate < sp ? vec2(plate, 1.0) : vec2(sp * 0.9, 2.0);\n}\n\n// ---- the sand ----------------------------------------------------------------------------------\nfloat sandD(vec3 q){\n  float rho = length(q.xz); float y = q.y;\n  float wall = (rho - (prof(y) - 0.026)) * 0.6;\n  // the upper bulb: from the neck (or, just after a flip, from the cone it was) up to a surface with a crater\n  float low = mix(min(uFlip.x, uFlip.y + 0.78 * rho), 0.0, uFlip.z);\n  float hi = uHi - uCrat * (1.0 - smoothstep(0.0, 0.42, rho));\n  float top = max(max(wall, (y - hi) * 0.7), max((low - y) * 0.7, -y));\n  // the lower bulb: a cone standing on a level bed\n  float pile = max(uBase, uPeak - 0.78 * rho);\n  float bot = max(max(wall, (y - pile) * 0.62), max(y, -y - 1.06));\n  float d = min(top, bot);\n  if (uFlow > 0.001) {\n    // the stream, thickening and thinning as clumps go through, with stray grains beside it\n    float sr = 0.0135 * uFlow * (0.7 + 0.6 * n2(vec2(y * 55.0 + uTime * 30.0, 0.5)));\n    d = min(d, max(rho - sr, max(y - 0.03, uPeak - 0.02 - y)));\n    float yy = y + uTime * 1.9; float cell = floor(yy / 0.045);\n    float h = h21(vec2(cell, 3.7));\n    vec3 g = vec3((h - 0.5) * 0.05, (cell + 0.5) * 0.045 - uTime * 1.9, (h21(vec2(cell, 9.1)) - 0.5) * 0.05);\n    float gr = length(q - g) - 0.0065 * step(0.35, h);\n    if (y < 0.0 && y > uPeak) d = min(d, gr);\n  }\n  return d;\n}\n\n// ---- the window the sun comes through --------------------------------------------------------\nfloat pane(vec2 w, float soft){\n  float inside = smoothstep(-soft, soft, w.x - 0.25) * smoothstep(-soft, soft, 6.6 - w.x) * smoothstep(-soft, soft, w.y + 9.0) * smoothstep(-soft, soft, 3.4 - w.y);\n  vec2 c = vec2((w.x - 0.25) / 1.45, (w.y + 9.0) / 1.7);\n  vec2 e = (0.5 - abs(fract(c) - 0.5)) * vec2(1.45, 1.7);\n  return inside * smoothstep(0.045 - soft, 0.045 + soft, e.x) * smoothstep(0.045 - soft, 0.045 + soft, e.y);\n}\nfloat sunMask(vec3 p){ float t = (WX - p.x) / SUN.x; vec3 h = p + SUN * t; return pane(h.yz, 0.015 + t * 0.011); }\n\n// The room as the glass and the varnish see it: warm plaster, the bright window, the desk below.\nvec3 env(vec3 rd){\n  vec3 c = mix(vec3(0.95, 0.82, 0.66), vec3(0.8, 0.84, 0.92), smoothstep(-0.1, 0.9, rd.y)) * 0.95;\n  if (rd.y < -0.02) c = vec3(0.9, 0.62, 0.38) * 1.1;\n  if (rd.x < -0.05) {\n    float t = (WX) / rd.x; vec2 w = vec2(HC + rd.y * t, rd.z * t);\n    c = mix(c, vec3(3.4, 3.6, 3.9), pane(w, 0.06));\n  }\n  c += vec3(2.5, 2.5, 2.4) * smoothstep(0.82, 0.9, rd.y) * smoothstep(0.3, 0.0, abs(rd.x));\n  return c;\n}\n\n// Two tall white cards standing out of frame: only glass sees them, as long bright lines down each bulb.\nfloat card(vec3 rd, vec3 dir, float w, float hgt){ float c = dot(rd, dir); if (c < 0.5) return 0.0; vec3 r = rd / c - dir; float x = length(r - vec3(0.0, r.y, 0.0)); return smoothstep(w, w * 0.4, x) * smoothstep(hgt, hgt * 0.7, abs(r.y)); }\nvec3 strips(vec3 rd){\n  return vec3(4.2, 4.0, 3.7) * card(rd, normalize(vec3(-0.85, 0.1, -0.53)), 0.07, 0.9) + vec3(2.2, 2.3, 2.4) * card(rd, normalize(vec3(0.9, 0.15, -0.42)), 0.05, 0.8);\n}\n\nfloat shadow(vec3 ro, vec3 rd){\n  vec3 c = vec3(0.0, HC + uLift, 0.0); vec3 oc = ro - c; float b = dot(oc, rd); float h = b * b - dot(oc, oc) + 2.2;\n  if (h < 0.0) return 1.0;\n  h = sqrt(h); float tEnd = -b + h; float t = max(0.004, -b - h);\n  float res = 1.0; float through = 1.0;\n  for (int i = 0; i < 56; i++) {\n    if (t > tEnd) break;\n    vec3 q = toLocal(ro + rd * t);\n    float d = min(frame(q).x, sandD(q)); float g = glassD(q);\n    if (g < 0.0) through = 0.78;\n    res = min(res, 9.0 * d / t);\n    if (res < 0.004) break;\n    t += clamp(min(d, abs(g) + 0.01), 0.006, 0.12);\n  }\n  return clamp(res, 0.0, 1.0) * through;\n}\nfloat objAO(vec3 p, vec3 n){\n  float o = 0.0; float w = 1.0;\n  for (int i = 1; i <= 4; i++) { float h = 0.05 * float(i * i); vec3 q = toLocal(p + n * h); float d = min(frame(q).x, max(glassD(q), -0.02)); o += w * max(h - d, 0.0); w *= 0.6; }\n  return clamp(1.0 - 1.6 * o, 0.0, 1.0);\n}\n\n// ---- materials ------------------------------------------------------------------------------------\nvec3 desk(vec3 p, vec3 rd){\n  vec3 n = vec3(0.0, 1.0, 0.0);\n  // pale oak boards running across the frame\n  float board = floor(p.z / 0.62); float bz = fract(p.z / 0.62);\n  float g = fbm(vec2(p.x * 0.7 + board * 13.0, p.z * 9.0)); float fine = n2(vec2(p.x * 3.0, p.z * 120.0));\n  float ring = sin((p.z + 0.4 * fbm(vec2(p.x * 0.35, board))) * 60.0 + g * 9.0) * 0.5 + 0.5;\n  vec3 alb = vec3(0.70, 0.5, 0.31) * (0.82 + 0.22 * g + 0.08 * fine - 0.1 * ring * ring) * (0.95 + 0.1 * h21(vec2(board, 2.0)));\n  alb *= 1.0 - 0.55 * smoothstep(0.012, 0.0, min(bz, 1.0 - bz) * 0.62);\n  float sh = shadow(p + n * 0.002, SUN) * sunMask(p);\n  float ao = objAO(p, n);\n  vec3 col = alb * (SUNC * SUN.y * sh + SKY * 0.42 * ao + vec3(0.9, 0.7, 0.5) * 0.1);\n  // the sand throws a little of its colour onto the boards\n  vec3 tc = p - vec3(0.0, HC + uLift, 0.0);\n  col += alb * uSand * 0.28 * exp(-dot(tc.xz, tc.xz) * 2.2) * sh;\n  // oiled finish: a soft copy of the window\n  vec3 r = reflect(rd, normalize(n + vec3(fine - 0.5, 0.0, g - 0.5) * 0.04));\n  float F = 0.04 + 0.96 * pow(1.0 - max(dot(-rd, n), 0.0), 5.0);\n  col += F * 0.55 * env(r) * mix(0.5, 1.0, ao);\n  return col;\n}\nvec3 wall(vec3 p, vec3 rd){\n  vec3 n = vec3(0.0, 0.0, -1.0);\n  float m = fbm(p.xy * 1.6); float f = n2(p.xy * 60.0);\n  vec3 alb = vec3(0.9, 0.84, 0.75) * (0.9 + 0.12 * m + 0.04 * f);\n  float sh = shadow(p + n * 0.002, SUN) * sunMask(p);\n  float skirt = smoothstep(0.06, 0.0, p.y);\n  return alb * (SUNC * max(dot(n, SUN), 0.0) * sh + SKY * 0.42 + vec3(1.0, 0.8, 0.6) * 0.22 + vec3(0.9, 0.6, 0.35) * 0.28 * smoothstep(1.2, 0.0, p.y)) * (1.0 - 0.3 * skirt);\n}\nvec3 room(vec3 ro, vec3 rd){\n  float t = 1e9; float kind = 0.0;\n  if (rd.y < 0.0) { float tf = -ro.y / rd.y; if (ro.z + rd.z * tf < WZ) { t = tf; kind = 1.0; } }\n  if (kind < 0.5 && rd.z > 0.0) { t = (WZ - ro.z) / rd.z; kind = 2.0; }\n  if (kind < 0.5) return env(rd);\n  vec3 p = ro + rd * t;\n  return kind < 1.5 ? desk(p, rd) : wall(p, rd);\n}\n\nvec3 wood(vec3 p, vec3 q, vec3 nW, vec3 rd, float id){\n  vec3 nL = dirLocal(nW);\n  float g;\n  if (id < 1.5) {\n    // the plates: one board each, its grain running across, and the lathe's faint rings on the edge\n    g = fbm(vec2(q.x * 1.6, q.z * 14.0 + 2.0 * fbm(q.xz * 2.0)));\n    g = 0.6 * g + 0.4 * (0.5 + 0.5 * sin(q.z * 70.0 + g * 10.0));\n    g += 0.12 * n2(vec2(length(q.xz) * 900.0, 1.0)) * (1.0 - abs(nL.y));\n  } else {\n    float an = atan(q.z, q.x);\n    g = fbm(vec2(an * 4.0, q.y * 7.0)); g = 0.6 * g + 0.4 * (0.5 + 0.5 * sin(an * 26.0 + q.y * 9.0 + g * 8.0));\n  }\n  vec3 alb = mix(vec3(0.42, 0.22, 0.09), vec3(0.8, 0.52, 0.27), smoothstep(0.2, 0.8, g));\n  float sh = shadow(p + nW * 0.004, SUN) * sunMask(p);\n  float ao = objAO(p, nW);\n  float dl = max(dot(nW, SUN), 0.0);\n  vec3 col = alb * (SUNC * dl * sh + SKY * 0.45 * (0.6 + 0.4 * nW.y) * ao + vec3(0.9, 0.6, 0.35) * 0.35 * max(-nW.y, 0.0) * ao);\n  // hand-rubbed oil: a broad sheen and a tighter highlight\n  vec3 hv = normalize(SUN - rd);\n  col += SUNC * sh * (0.12 * pow(max(dot(nW, hv), 0.0), 18.0) + 0.5 * pow(max(dot(nW, hv), 0.0), 140.0));\n  float F = 0.04 + 0.96 * pow(1.0 - max(dot(-rd, nW), 0.0), 5.0);\n  col += F * 0.5 * env(reflect(rd, nW)) * ao;\n  return col;\n}\n\nvec3 sandShade(vec3 q, vec3 nL, vec3 rd, vec3 pw){\n  float rho = length(q.xz);\n  // grains slide down the crater and the cone: shift the grain lookup along the slope over time\n  vec3 g = q;\n  if (q.y > 0.0) g.y += uTime * 0.05 * uFlow; else g += vec3(q.x, 0.0, q.z) / max(rho, 0.01) * uTime * 0.025 * uFlow * smoothstep(-0.02, 0.0, q.y - max(uBase, uPeak - 0.78 * rho) + 0.01);\n  vec3 c = floor(g * 420.0);\n  float a = h31(c); float b = h31(c + 17.0);\n  vec3 alb = uSand * (0.72 + 0.5 * a);\n  alb = mix(alb, vec3(0.98, 0.86, 0.7), step(0.975, b));      // pale quartz grains\n  alb = mix(alb, uSand * vec3(0.5, 0.35, 0.4), step(b, 0.05)); // dark grains\n  vec3 jitter = (vec3(h31(c + 3.0), h31(c + 5.0), h31(c + 7.0)) - 0.5) * 0.9;\n  vec3 n = normalize(nL + jitter * 0.3);\n  vec3 nW = dirWorld(n);\n  float sh = shadow(pw + dirWorld(nL) * 0.01, SUN) * sunMask(pw);\n  float dl = max(dot(nW, SUN), 0.0) * 0.9 * 0.92 + 0.0;\n  vec3 col = alb * (SUNC * dl * sh * 0.62 + SKY * 0.3 + uSand * 0.2);\n  // the odd grain catches the sun like a mirror\n  vec3 hv = normalize(SUN - rd);\n  col += SUNC * sh * 1.6 * step(0.993, a) * pow(max(dot(nW, hv), 0.0), 30.0);\n  return col;\n}\n\n// ---- the primary ray ---------------------------------------------------------------------------\nvec2 objMap(vec3 q){ vec2 f = frame(q); float g = glassD(q); return g < f.x ? vec2(g, 3.0) : f; }\nvec3 nObj(vec3 q){ vec2 e = vec2(0.0008, 0.0); return normalize(vec3(objMap(q + e.xyy).x - objMap(q - e.xyy).x, objMap(q + e.yxy).x - objMap(q - e.yxy).x, objMap(q + e.yyx).x - objMap(q - e.yyx).x)); }\nvec3 nFrame(vec3 q){ vec2 e = vec2(0.0008, 0.0); return normalize(vec3(frame(q + e.xyy).x - frame(q - e.xyy).x, frame(q + e.yxy).x - frame(q - e.yxy).x, frame(q + e.yyx).x - frame(q - e.yyx).x)); }\nvec3 nGlass(vec3 q){ vec2 e = vec2(0.0008, 0.0); return normalize(vec3(glassD(q + e.xyy) - glassD(q - e.xyy), glassD(q + e.yxy) - glassD(q - e.yxy), glassD(q + e.yyx) - glassD(q - e.yyx))); }\nvec3 nSand(vec3 q){ vec2 e = vec2(0.0012, 0.0); return normalize(vec3(sandD(q + e.xyy) - sandD(q - e.xyy), sandD(q + e.yxy) - sandD(q - e.yxy), sandD(q + e.yyx) - sandD(q - e.yyx))); }\n\n// march the wooden frame alone (for rays that have been through the glass)\nvec3 behind(vec3 ro, vec3 rd){\n  vec3 c = vec3(0.0, HC + uLift, 0.0); vec3 oc = ro - c; float b = dot(oc, rd); float h = b * b - dot(oc, oc) + 2.1;\n  if (h > 0.0) {\n    h = sqrt(h); float t = max(0.0, -b - h); float tEnd = -b + h;\n    for (int i = 0; i < 64; i++) {\n      vec3 p = ro + rd * t; vec2 f = frame(toLocal(p));\n      if (f.x < 0.0008) { vec3 q = toLocal(p); return wood(p, q, dirWorld(nFrame(q)), rd, f.y); }\n      t += f.x; if (t > tEnd) break;\n    }\n  }\n  return room(ro, rd);\n}\n\nvec3 render(vec3 ro, vec3 rd){\n  vec3 c = vec3(0.0, HC + uLift, 0.0); vec3 oc = ro - c; float b = dot(oc, rd); float h = b * b - dot(oc, oc) + 2.1;\n  if (h < 0.0) return room(ro, rd);\n  h = sqrt(h); float t = max(0.0, -b - h); float tEnd = -b + h;\n  vec2 hit = vec2(0.0);\n  for (int i = 0; i < 110; i++) {\n    vec2 m = objMap(toLocal(ro + rd * t));\n    if (m.x < 0.0006 * (1.0 + t * 0.1)) { hit = vec2(t, m.y); break; }\n    t += m.x; if (t > tEnd) break;\n  }\n  if (hit.y < 0.5) return room(ro, rd);\n  vec3 p = ro + rd * hit.x; vec3 q = toLocal(p);\n  if (hit.y < 2.5) return wood(p, q, dirWorld(nFrame(q)), rd, hit.y);\n\n  // the glass: reflect the room, bend in through the wall, look for sand, bend out the far side\n  vec3 n = dirWorld(nGlass(q));\n  float ci = clamp(dot(-rd, n), 0.0, 1.0);\n  float F = 0.04 + 0.96 * pow(1.0 - ci, 5.0);\n  vec3 rf = reflect(rd, n);\n  vec3 col = F * (env(rf) + strips(rf));\n  vec3 hv = normalize(SUN - rd);\n  col += SUNC * 4.0 * pow(max(dot(n, hv), 0.0), 2600.0) * sunMask(p);\n  vec3 T = vec3(1.0 - F) * vec3(0.985, 0.995, 0.99);\n  vec3 d = refract(rd, n, 1.0 / NG);\n  vec3 pp = p + d * (0.026 / max(dot(-n, d), 0.35));\n  T *= exp(-vec3(1.1, 0.55, 0.8) * 0.06 / max(ci, 0.06)); // the green of thick glass seen edge on\n  d = refract(d, n, NG * 0.97);\n  if (dot(d, d) < 0.5) d = reflect(rd, n);\n  // inside: march the sand, or until the ray reaches the far wall\n  float s = 0.002; bool inside = false;\n  for (int i = 0; i < 110; i++) {\n    vec3 x = pp + d * s; vec3 xq = toLocal(x);\n    float ds = sandD(xq); float dw = -(length(xq.xz) - (prof(xq.y) - 0.026)) * 0.6;\n    dw = min(dw, 1.07 - abs(xq.y));\n    // the far side of a sloping wall can still be under the entry point: walk on until clear of the glass\n    if (!inside) { if (dw > 0.002) inside = true; else { s += 0.006; continue; } }\n    if (ds < 0.0007) {\n      vec3 nL = nSand(xq);\n      return col + T * sandShade(xq, nL, dirLocal(d), x);\n    }\n    if (dw < 0.0007) { pp = x; break; }\n    s += max(min(ds, dw), 0.0015);\n    if (s > 3.0) break;\n  }\n  // out through the far wall\n  vec3 qx = toLocal(pp); vec3 n2w = dirWorld(nGlass(qx));\n  float c2 = clamp(dot(d, n2w), 0.0, 1.0);\n  float F2 = 0.04 + 0.96 * pow(1.0 - c2, 5.0);\n  col += T * F2 * 0.45 * env(reflect(d, -n2w));\n  T *= (1.0 - F2) * exp(-vec3(1.1, 0.55, 0.8) * 0.06 / max(c2, 0.06));\n  vec3 d2 = refract(d, -n2w, 1.0 / NG);\n  if (dot(d2, d2) < 0.5) d2 = d;\n  d2 = refract(d2, -n2w, NG * 0.98); if (dot(d2, d2) < 0.5) d2 = d;\n  return col + T * behind(pp + n2w * 0.04, normalize(d2));\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  vec3 col = min(render(ro, rd), vec3(30.0));\n  if (uDirect > 0.5) col = tone(col);\n  outC = vec4(col, 1.0);\n}\n`;\n\n// Developing the frame: a little bloom off the sun glints, filmic curve, warm 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  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.25, 0.0);\n  vec3 b = vec3(0.0);\n  for (int i = 0; i < 16; i++) {\n    float a = float(i) * 2.39996; float r = sqrt((float(i) + 0.5) / 16.0);\n    vec3 s = texture2D(uAcc, uv + vec2(cos(a), sin(a)) * r * 0.012 * vec2(uRes.y / uRes.x, 1.0)).rgb;\n    b += min(max(s - 3.0, 0.0), vec3(8.0)) * (1.0 - 0.6 * r);\n  }\n  c += b * 0.035;\n  vec2 q = (uv - uCentre) * vec2(uRes.x / uRes.y, 1.0);\n  c *= mix(vec3(1.0), vec3(0.78, 0.7, 0.62), smoothstep(0.45, 1.25, length(q)));\n  c = tone(c);\n  float g = h21(gl_FragCoord.xy + uGrain * 91.7) - 0.5;\n  c += g * 0.022;\n  outC = vec4(c, 1.0);\n}\n`;\n\n// No WebGL, or reduced motion before the first frame: the same desk as a still made of gradients.\nfunction Still({ pct }: { pct: number }) {\n  const p = pct / 100;\n  return (\n    <div aria-hidden=\"true\" className=\"absolute inset-0 overflow-hidden\" style={{ background: \"linear-gradient(180deg, #e9ddc9 0%, #e3d3bb 58%, #b98a5c 58.2%, #c99b6b 100%)\" }}>\n      <div className=\"absolute left-0 top-0 h-[58%] w-full\" style={{ background: \"repeating-linear-gradient(115deg, rgba(255,240,214,0) 0 90px, rgba(255,244,222,0.55) 90px 260px, rgba(255,240,214,0) 260px 300px)\" }} />\n      <div className=\"absolute left-1/2 top-[48%] h-[400px] w-[230px] -translate-x-1/2 -translate-y-1/2 sm:h-[470px] sm:w-[270px] lg:left-[68%]\">\n        <div className=\"absolute -bottom-[14px] left-[40%] h-[40px] w-[150%] rounded-[50%] bg-[#6b4526]/35 blur-xl\" />\n        {[0, 1].map((i) => (\n          <div key={i} className={`absolute left-0 h-[7%] w-full rounded-[10px] ${i ? \"bottom-0\" : \"top-0\"}`} style={{ background: \"linear-gradient(180deg,#d9a467,#a8703c 55%,#7a4c25)\", boxShadow: \"0 3px 6px rgba(80,45,20,0.35)\" }} />\n        ))}\n        {[8, 46, 84].map((x) => (\n          <div key={x} className=\"absolute top-[7%] h-[86%] w-[5%] rounded-full\" style={{ left: `${x}%`, background: \"linear-gradient(90deg,#7a4c25,#d9a467 45%,#9a6533)\" }} />\n        ))}\n        <div className=\"absolute left-[16%] top-[7%] h-[43%] w-[68%] overflow-hidden\" style={{ clipPath: \"ellipse(50% 62% at 50% 34%)\", background: \"rgba(255,255,255,0.18)\" }}>\n          <div className=\"absolute inset-x-0 bottom-0\" style={{ height: `${(1 - p) * 82}%`, background: \"linear-gradient(180deg,#f07a52,#d4512f)\" }} />\n        </div>\n        <div className=\"absolute bottom-[7%] left-[16%] h-[43%] w-[68%] overflow-hidden\" style={{ clipPath: \"ellipse(50% 62% at 50% 66%)\", background: \"rgba(255,255,255,0.18)\" }}>\n          <div className=\"absolute inset-x-0 bottom-0\" style={{ height: `${p * 70}%`, background: \"linear-gradient(180deg,#f07a52,#c94a2a)\", clipPath: \"polygon(0 35%, 50% 0, 100% 35%, 100% 100%, 0 100%)\" }} />\n        </div>\n      </div>\n    </div>\n  );\n}\n\nexport default function HourglassSandLoader() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [pct, setPct] = useState(START);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const api = useRef<{ turn: () => void } | null>(null);\n\n  useEffect(() => {\n    const box = stage.current, cv = canvas.current;\n    if (!box || !cv) 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    const canAcc = !!gl2 && (!!gl2.getExtension(\"EXT_color_buffer_float\") || !!gl2.getExtension(\"EXT_color_buffer_half_float\"));\n    const info = gl ? gl.getExtension(\"WEBGL_debug_renderer_info\") : null;\n    const software = /swiftshader|llvmpipe|software|basic render/i.test(gl && info ? String(gl.getParameter(info.UNMASKED_RENDERER_WEBGL)) : \"\");\n\n    let scene: WebGLProgram | null = null, post: WebGLProgram | null = null;\n    if (gl) {\n      const head = gl2 ? \"#version 300 es\\n#define ATTR in\\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        if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { console.warn(gl.getShaderInfoLog(sh)); return null; }\n        return sh;\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      scene = link(SCENE); post = canAcc ? link(POST) : null;\n    }\n    const live = !!gl && !!scene && (!canAcc || !!post);\n    if (!live) setShown(\"still\");\n\n    const su: Record<string, WebGLUniformLocation | null> = {}, pu: Record<string, WebGLUniformLocation | null> = {};\n    let acc: WebGLTexture | null = null, fbo: WebGLFramebuffer | null = null;\n    if (live && gl && scene) {\n      for (const n of [\"uRes\", \"uJit\", \"uCam\", \"uTgt\", \"uFoc\", \"uShift\", \"uFocus\", \"uAng\", \"uLift\", \"uTop\", \"uCrat\", \"uBase\", \"uPeak\", \"uHi\", \"uFlip\", \"uFlow\", \"uTime\", \"uDirect\", \"uSand\"]) su[n] = gl.getUniformLocation(scene, n);\n      if (post) for (const n of [\"uAcc\", \"uRes\", \"uGrain\", \"uCentre\"]) pu[n] = gl.getUniformLocation(post, 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      if (canAcc) { acc = gl.createTexture(); fbo = gl.createFramebuffer(); }\n    }\n\n    let W = 0, H = 0, scale = 1, cssW = 1, cssH = 1;\n    const h0 = heights(START / 100);\n    const s = {\n      goal: START, seg: 0, segT: 0, from: START, running: !reduce,\n      p: START / 100, flow: reduce ? 0 : 1,\n      // the turn: phase 0 pouring, 1 holding full, 2 turning, 3 slumping into the neck\n      phase: 0, hold: 0, ang: 0, angV: 0, lift: 0, liftV: 0, slump: 1, flip: [0, 0], aim: 0, turnT: 0,\n      top: h0.top, sx: 0, sy: 0, px: 0, py: 0,\n      count: 0, fresh: true, frame: 0, slow: 0, fps: 0, fpsN: 0, fpsT: 0,\n      visible: true, raf: 0, last: 0, t: 0, pct: START, first: true,\n    };\n    (box as HTMLDivElement & { __fps?: () => number }).__fps = () => s.fps;\n    const size = () => {\n      const r = box.getBoundingClientRect();\n      cssW = Math.max(1, r.width); cssH = Math.max(1, r.height);\n      if (!live || !gl) return;\n      const dpr = Math.min(software ? 0.55 : 1.25, window.devicePixelRatio || 1) * scale;\n      const k = Math.min(1, Math.sqrt(1.0e6 / (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    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    const norm = (v: number[]) => { const l = Math.hypot(v[0], v[1], v[2]) || 1; return [v[0] / l, v[1] / l, v[2] / l]; };\n\n    // A longish lens a little above the desk, the hourglass to the right with the figures beside it.\n    const camera = () => {\n      const aspect = cssW / cssH, wide = cssW >= 1024, roomy = cssW >= 640;\n      const el = 0.2 - s.sy * 0.04, az = -0.16 + s.sx * 0.08, dist = 10.5;\n      const tgt = [0, 1.22, 0];\n      const cam = [tgt[0] + dist * Math.sin(az) * Math.cos(el), tgt[1] + dist * Math.sin(el), tgt[2] - dist * Math.cos(az) * Math.cos(el)];\n      const foc = wide ? 2.95 : roomy ? 2.7 : Math.min(2.4, 1.75 + aspect * 0.9);\n      const shift = wide ? [0.19 * aspect, -0.01] : roomy ? [0.2 * aspect, 0.04] : [0.0, 0.13];\n      return { aspect, wide, cam, tgt, foc, shift };\n    };\n\n    const draw = (alpha: number, sample: number) => {\n      if (!live || !gl || !scene) return;\n      const c = camera();\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.06 : 0;\n      const hh = heights(s.p);\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.uniform3f(su.uCam, c.cam[0], c.cam[1], c.cam[2]); gl.uniform3f(su.uTgt, c.tgt[0], c.tgt[1], c.tgt[2]);\n      gl.uniform1f(su.uFoc, c.foc); gl.uniform2f(su.uShift, c.shift[0], c.shift[1]);\n      gl.uniform1f(su.uFocus, Math.hypot(c.cam[0], c.cam[1] - 1.2, c.cam[2]) - 0.3);\n      gl.uniform1f(su.uAng, s.ang); gl.uniform1f(su.uLift, s.lift);\n      // while the glass turns and slumps the upper bulb is described by the old cone, not the pour\n      const slumping = s.phase === 3;\n      gl.uniform1f(su.uTop, hh.top);\n      gl.uniform1f(su.uHi, slumping ? 1.06 + (TOPF - 1.06) * s.slump : hh.top);\n      gl.uniform1f(su.uCrat, slumping ? 0 : hh.crater);\n      gl.uniform1f(su.uBase, hh.base); gl.uniform1f(su.uPeak, hh.peak);\n      gl.uniform3f(su.uFlip, s.flip[0], s.flip[1], slumping ? s.slump : 1);\n      gl.uniform1f(su.uFlow, s.flow); gl.uniform1f(su.uTime, s.t);\n      gl.uniform1f(su.uDirect, canAcc ? 0 : 1);\n      gl.uniform3f(su.uSand, SAND[0], SAND[1], SAND[2]);\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.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, acc);\n        gl.uniform1i(pu.uAcc, 1); gl.uniform2f(pu.uRes, W, H);\n        gl.uniform1f(pu.uGrain, reduce ? 1 : s.frame % 64);\n        gl.uniform2f(pu.uCentre, 0.5 + c.shift[0] * (H / W), 0.5 + c.shift[1]);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n    };\n\n    // turn it over by hand: the button, or the demo reaching one hundred\n    const startTurn = () => {\n      if (s.phase === 2 || s.phase === 3) return;\n      s.phase = 2; s.turnT = 0; s.angV = 0; s.aim = -0.14; s.flow = 0;\n      wake();\n    };\n    const turn = () => {\n      if (reduce) {\n        // no motion: step the number on and redraw once\n        s.goal = s.goal >= 100 ? 0 : Math.min(100, Math.round(s.goal / 20) * 20 + 20);\n        s.p = s.goal / 100; s.count = 0; s.fresh = true; wake();\n        return;\n      }\n      if (s.phase === 0 || s.phase === 1) { s.p = Math.max(s.p, 0.0); s.goal = 100; s.seg = 5; s.segT = 0; s.from = 100; startTurn(); }\n    };\n    api.current = { turn };\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++; s.t += dt;\n      s.fpsN++; s.fpsT += dtRaw; if (s.fpsT > 1) { s.fps = s.fpsN / s.fpsT; s.fpsN = 0; s.fpsT = 0; }\n\n      // ---- the demo: walk the script. Replace this block with your own `s.goal = percent`.\n      if (s.running && s.phase === 0) {\n        s.segT += dt;\n        let [to, secs] = demo[s.seg];\n        while (s.segT >= secs && s.seg !== 5) {\n          s.segT -= secs; s.from = to; s.seg = (s.seg + 1) % demo.length;\n          [to, secs] = demo[s.seg];\n        }\n        const k = Math.min(1, s.segT / secs), e = k * k * (3 - 2 * k);\n        s.goal = s.from + (to - s.from) * e;\n      }\n\n      let moving = false;\n      if (!reduce) {\n        moving = true;\n        // sand runs at its own pace: it follows the number with a lag, never faster than the neck allows\n        const before = s.p;\n        if (s.phase === 0) {\n          const want = s.goal / 100;\n          s.p += Math.max(-0.5 * dt, Math.min(0.32 * dt, (want - s.p) * (1 - Math.exp(-dt * 3.2))));\n          const rate = Math.abs(s.p - before) / Math.max(dt, 1e-3);\n          s.flow += ((s.p < 0.997 ? 0.45 + 0.55 * Math.min(1, rate * 9) : 0) - s.flow) * (1 - Math.exp(-dt * 6));\n          if (s.goal >= 100 && s.p > 0.996) { s.phase = 1; s.hold = 0; s.p = 1; }\n        } else if (s.phase === 1) {\n          s.flow *= Math.exp(-dt * 8);\n          s.hold += dt;\n          if (s.hold > 0.7) startTurn();\n        } else if (s.phase === 2) {\n          // a short tip back to take the weight, then over: a heavy spring that runs a touch past and settles\n          s.turnT += dt;\n          if (s.turnT > 0.22) s.aim = Math.PI;\n          const K = s.aim < 0 ? 90 : 34, D = s.aim < 0 ? 16 : 7.4;\n          s.angV += (K * (s.aim - s.ang) - D * s.angV) * dt; s.ang += s.angV * dt;\n          const up = Math.max(0, Math.sin(Math.min(1, Math.max(0, s.ang / Math.PI)) * Math.PI));\n          s.liftV += (260 * (0.3 * up - s.lift) - 20 * s.liftV) * dt; s.lift += s.liftV * dt;\n          if (s.lift < 0) { s.lift = 0; s.liftV = Math.abs(s.liftV) * 0.22; }\n          if (s.aim > 0 && Math.abs(Math.PI - s.ang) < 0.004 && Math.abs(s.angV) < 0.05 && s.lift < 0.003) {\n            // turned: the full lower bulb is now on top, still in the shape of its cone; let it slump\n            const full = heights(1);\n            s.ang = 0; s.angV = 0; s.lift = 0; s.liftV = 0;\n            s.flip = [-full.base, -full.peak]; s.slump = 0; s.phase = 3;\n            s.p = 0; s.goal = 0; s.seg = RESTART; s.segT = 0; s.from = 0;\n          }\n        } else if (s.phase === 3) {\n          s.slump = Math.min(1, s.slump + dt * (0.6 + 2.6 * s.slump));\n          s.flow += (0.8 * s.slump - s.flow) * (1 - Math.exp(-dt * 8));\n          if (s.slump >= 1) { s.phase = 0; }\n        }\n        s.sx += (s.px - s.sx) * (1 - Math.exp(-dt * 2.5)); s.sy += (s.py - s.sy) * (1 - Math.exp(-dt * 2.5));\n      } else {\n        s.p = s.goal / 100; s.flow = 0;\n      }\n\n      const pctNow = s.phase >= 2 ? (s.phase === 2 ? 100 : 0) : Math.round(Math.min(100, s.phase === 1 ? 100 : s.p * 100 + 0.0001));\n      if (pctNow !== s.pct) { s.pct = pctNow; setPct(pctNow); }\n\n      if (live) {\n        if (s.fresh || !canAcc) { draw(1, 0); s.fresh = false; s.count = 1; }\n        else if (moving) draw(0.42, 1 + (s.frame % 32));\n        else if (s.count < 40) { s.count++; draw(1 / s.count, s.count); }\n        if (s.first) { s.first = false; setShown(\"gl\"); }\n        if (!reduce && !software && dtRaw > 0.026) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n        if (s.slow > 40 && scale > 0.45) { scale *= 0.82; s.slow = 0; size(); }\n      }\n      if (moving || (live && s.count < 40)) 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    // the pointer moves the camera a little round the desk\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      wake();\n    };\n    const leave = () => { s.px = 0; s.py = 0; };\n    box.addEventListener(\"pointermove\", move);\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      box.removeEventListener(\"pointermove\", move); box.removeEventListener(\"pointerleave\", leave);\n      cv.removeEventListener(\"webglcontextlost\", lost);\n      api.current = null;\n    };\n  }, []);\n\n  const at = Math.max(0, stages.findIndex((st) => pct <= st.until));\n\n  return (\n    <section className=\"relative isolate overflow-hidden bg-[#e6d8c2] text-[#2b1c12]\" style={{ fontFamily: FONT }}>\n      <style>{`\n        @keyframes ray-marched-hourglass-loader-rise { from { opacity: 0; transform: translateY(0.4em); filter: blur(4px); } to { opacity: 1; transform: none; filter: blur(0); } }\n        .ray-marched-hourglass-loader-note { animation: ray-marched-hourglass-loader-rise 560ms cubic-bezier(0.2, 0.9, 0.2, 1) both; }\n        @media (prefers-reduced-motion: reduce) { .ray-marched-hourglass-loader-note { animation: none; } }\n      `}</style>\n      <div ref={stage} className=\"relative mx-auto h-[720px] w-full max-w-[1600px] lg:h-[760px]\" style={{ background: \"linear-gradient(180deg, #ece0cc 0%, #e4d4bb 56%, #c09468 56%, #b98a5c 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        {shown === \"still\" && <Still pct={pct} />}\n\n        <div className=\"pointer-events-none absolute inset-0 flex flex-col justify-between px-6 pb-7 pt-7 sm:px-10 lg:px-16 lg:pb-14 lg:pt-14\">\n          <p className=\"text-[11px] font-medium uppercase tracking-[0.32em] text-[#2b1c12]/65\">Marlow Ashby Studio <span className=\"mx-2 text-[#2b1c12]/30\">/</span> exporting the portfolio</p>\n          <div className=\"max-w-[560px]\">\n            <div\n              role=\"progressbar\"\n              aria-label=\"Exporting the portfolio\"\n              aria-valuemin={0}\n              aria-valuemax={100}\n              aria-valuenow={pct}\n              aria-valuetext={`${pct} percent. ${stages[at].note}`}\n              className=\"flex items-start font-semibold leading-[0.8] tracking-[-0.065em] text-[#2b1c12]\"\n              style={{ fontVariantNumeric: \"tabular-nums\", fontSize: \"clamp(112px, 17vw, 248px)\" }}\n            >\n              <span>{pct}</span>\n              <span className=\"ml-[0.06em] mt-[0.04em] text-[0.34em] tracking-normal text-[#c4502f]\">%</span>\n            </div>\n            <div className=\"mt-5 flex items-baseline gap-3 text-[11px] font-medium uppercase tracking-[0.3em] text-[#2b1c12]/60 lg:mt-7\">\n              <span className=\"tabular-nums text-[#b8472a]\">{String(at + 1).padStart(2, \"0\")}</span>\n              <span className=\"h-px w-8 self-center bg-[#2b1c12]/25\" />\n              <span className=\"tabular-nums\">{String(stages.length).padStart(2, \"0\")}</span>\n            </div>\n            <p aria-live=\"polite\" className=\"mt-2 h-[1.3em] text-[22px] italic leading-snug text-[#2b1c12] lg:text-[28px]\" style={{ fontFamily: SERIF }}>\n              <span key={at} className=\"ray-marched-hourglass-loader-note block\">{stages[at].note}</span>\n            </p>\n            <button\n              type=\"button\"\n              onClick={() => api.current?.turn()}\n              className=\"pointer-events-auto mt-6 inline-flex h-11 items-center gap-2.5 rounded-full bg-[#2b1c12] pl-4 pr-5 text-[13px] font-medium tracking-[-0.01em] text-[#f6ecdc] shadow-[0_8px_20px_-8px_rgba(60,30,10,0.6)] outline-none transition-colors duration-200 hover:bg-[#45301f] focus-visible:ring-2 focus-visible:ring-[#c4502f] focus-visible:ring-offset-2 focus-visible:ring-offset-[#e6d8c2] lg:mt-8\"\n            >\n              <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-4 w-4\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.6\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d=\"M6 3h8M6 17h8M7 3c0 4 6 4 6 7s-6 3-6 7M13 3c0 4-6 4-6 7s6 3 6 7\" /></svg>\n              Turn the glass over\n            </button>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n"}]}
