{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"ray-marched-stoneware-mug-product-showcase-you-turn-by-dragging-with-glaze-that-pours-down-in-the-colour-you-pick-on-a-yellow-sweep","type":"registry:block","title":"Ray-Marched Stoneware Mug Product Showcase You Turn by Dragging, With Glaze That Pours Down in the Colour You Pick on a Yellow Sweep","description":"Free React product showcase: a stoneware mug traced in a WebGL shader that you turn by dragging, with five dishes of glaze that pour a new colour down it.","dependencies":[],"files":[{"path":"components/we-inc/ray-marched-stoneware-mug-product-showcase-you-turn-by-dragging-with-glaze-that-pours-down-in-the-colour-you-pick-on-a-yellow-sweep.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor would edit first.\nconst product = {\n  maker: \"Holm & Ash\",\n  kind: \"Stoneware, thrown by hand\",\n  name: [\"Daily\", \"Mug\"],\n  price: \"$38\",\n  cta: \"Add to bag\",\n  note: \"350 ml, dishwasher safe\",\n};\n// The glazes on offer. `rgb` is the fired colour in linear light (0 to 1); `css` is the same colour for the plain-HTML version.\nconst glazes = [\n  { name: \"Cobalt\", rgb: [0.006, 0.03, 0.34], css: \"#1f45c8\" },\n  { name: \"Rose\", rgb: [0.72, 0.27, 0.27], css: \"#eba6a4\" },\n  { name: \"Celadon\", rgb: [0.12, 0.29, 0.2], css: \"#6fa888\" },\n  { name: \"Oat\", rgb: [0.72, 0.62, 0.45], css: \"#ead9bb\" },\n  { name: \"Ink\", rgb: [0.012, 0.013, 0.02], css: \"#23252c\" },\n];\n// Colours of the set, in linear light.\nconst PAPER = [0.9, 0.36, 0.012]; // the paper sweep\nconst PLINTH = [0.93, 0.17, 0.035]; // the round card the mug stands on\nconst CLAY = [0.5, 0.35, 0.23]; // the bare foot of the mug\nconst LINER = [0.84, 0.78, 0.66]; // the glaze inside it\nconst HERO = -0.5; // resting turn of the mug in radians (the handle points right and a little toward the lens)\nconst POUR = 1.9; // seconds a new glaze takes to run down\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 v3 = (c: number[]) => `vec3(${c.map((n) => n.toFixed(4)).join(\", \")})`;\n\n// Where the camera stands for a frame of this size, and where the five glaze dishes sit on the floor.\nfunction frameOf(w: number, h: number) {\n  const desk = w >= 1024, wide = w >= 768, aspect = w / h;\n  const dist = 8.4, el = desk ? 0.33 : 0.36;\n  const foc = desk ? Math.min(2.8, 1.66 * aspect) : wide ? 1.9 * aspect : Math.min(2.4, (dist * aspect) / 3.12);\n  const tgt = [0, 0.72, 0];\n  const cam = [0, tgt[1] + dist * Math.sin(el), -dist * Math.cos(el)];\n  const fx = desk ? 0.655 : wide ? 0.63 : 0.5, fy = desk ? 0.4 : wide ? 0.4 : 0.25;\n  const shift = [(fx - 0.5) * aspect, 0.5 - fy];\n  const span = desk ? [-33, 47] : wide ? [-36, 42] : [-44, 44], rad = desk ? 1.7 : wide ? 1.7 : 1.56;\n  const dishes = glazes.map((_, i) => {\n    const a = ((span[0] + ((span[1] - span[0]) * i) / (glazes.length - 1)) * Math.PI) / 180;\n    return [rad * Math.sin(a), -rad * Math.cos(a)];\n  });\n  // the same lens in plain arithmetic, so the buttons can sit exactly over the dishes\n  const fw = [0, -Math.sin(el), Math.cos(el)], up = [0, Math.cos(el), Math.sin(el)];\n  const project = (x: number, y: number, z: number) => {\n    const v = [x - cam[0], y - cam[1], z - cam[2]];\n    const d = v[1] * fw[1] + v[2] * fw[2];\n    return { x: w / 2 + ((v[0] / d) * foc + shift[0]) * h, y: h / 2 - (((v[1] * up[1] + v[2] * up[2]) / d) * foc + shift[1]) * h, k: (foc / d) * h };\n  };\n  return { dist, el, foc, tgt, cam, shift, dishes, project };\n}\n\nconst VERT = `ATTR vec2 aPos; void main(){ gl_Position = vec4(aPos, 0.0, 1.0); }`;\n\nconst TONE = `\nvec3 tone(vec3 x){ vec3 c = 1.0 - exp(-max(x, 0.0) * 1.0); c = mix(c, c * c * (3.0 - 2.0 * c), 0.5); return pow(c, vec3(1.0 / 2.2)); }\n`;\n\n// The photograph: a paper sweep in hard window light, a hand-thrown mug and five dishes of wet glaze.\nconst SCENE = `\nprecision highp float;\nuniform vec2 uRes; uniform vec4 uJit; uniform float uSeed; uniform float uDirect;\nuniform vec3 uCam; uniform vec3 uTgt; uniform float uFoc; uniform vec2 uShift; uniform float uFocus;\nuniform float uRot; uniform float uLift; uniform vec3 uSun;\nuniform vec3 uOld; uniform vec3 uNew; uniform float uProg; uniform float uBead; uniform float uGseed;\nuniform vec3 uDish[5]; uniform vec3 uTint[5]; uniform float uRipT;\nvec3 gRnd;\nconst float ZW = 3.4; const float RC = 1.2; const float TOP = 9.0; const float TOPR = 1.5;\nconst float R = 0.7; const float YC = 0.66; const float HY = 0.78; const float YTOP = 1.22; const float WALL = 0.06; const float PH = 0.13; const float PR = 1.2;\nconst vec3 PAPER = ${v3(PAPER)}; const vec3 PLINTH = ${v3(PLINTH)}; const vec3 CLAY = ${v3(CLAY)}; const vec3 LINER = ${v3(LINER)};\nconst vec3 SUNC = vec3(1.0, 0.95, 0.86); const float SUNI = 2.7;\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); }\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 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 smin(float a, float b, float k){ float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); return mix(b, a, h) - k * h * (1.0 - h); }\nfloat smax(float a, float b, float k){ return -smin(-a, -b, k); }\n\n// The mug has its own frame: it turns about its axis and can leave the floor.\nvec3 toLocalDir(vec3 v){ float s = sin(uRot); float c = cos(uRot); return vec3(c * v.x + s * v.z, v.y, -s * v.x + c * v.z); }\nvec3 toWorldDir(vec3 v){ float s = sin(uRot); float c = cos(uRot); return vec3(c * v.x - s * v.z, v.y, s * v.x + c * v.z); }\nvec3 toLocal(vec3 p){ return toLocalDir(vec3(p.x, p.y - uLift - PH, p.z)); }\n\n// Where the glaze stops above the bare foot, and how far a fresh pour has run, at each angle round the mug.\nfloat gline(float a){ return 0.25 + 0.03 * sin(a * 2.0 + 0.7) + 0.017 * sin(a * 5.0 + 2.1) + 0.008 * sin(a * 11.0 + 4.0); }\n// 1 down the middle of a run of glaze, 0 between runs: tongues with round ends, not icicles\nfloat runs(float a){ return smoothstep(0.3, 0.8, 0.5 + 0.5 * sin(a * 9.0 + uGseed * 1.7)) * (0.55 + 0.45 * sin(a * 2.0 + uGseed * 3.1)); }\nfloat gfront(float a){\n  float n = 0.3 * (0.5 + 0.5 * sin(a * 3.0 + uGseed)) + 0.3 * smoothstep(0.4, 0.9, 0.5 + 0.5 * sin(a * 5.0 + uGseed * 2.3)) + 0.55 * runs(a);\n  float x = clamp(uProg * (1.0 + 0.95 * n), 0.0, 1.0);\n  float e = x * x * (3.0 - 2.0 * x);\n  return mix(YTOP + 0.06, gline(a), 1.0 - pow(1.0 - e, 1.7));\n}\n// The thrown shape before it is hollowed: a soft barrel with no flat anywhere on its side, standing on a cut foot.\nfloat dShape(vec3 q){\n  vec2 w = vec2(length(q.xz) / R, abs(q.y - YC) / HY);\n  float f = pow(pow(w.x, 2.5) + pow(w.y, 2.5), 0.4);\n  return smax((f - 1.0) * 0.64, -q.y, 0.07);\n}\nfloat dMug(vec3 q){\n  float o = dShape(q);\n  vec3 h = q - vec3(R + 0.07, 0.73, 0.0);\n  float ring = length(vec2(h.x * 1.12, h.y * 0.9)) - 0.33;\n  float hd = length(vec2(ring, h.z * 0.66)) - 0.062;\n  float solid = smin(o, hd, 0.1);\n  // glaze has thickness: a lip where it stops, and a fat bead riding the front of a pour\n  if (o > -0.03) {\n    float a = atan(q.z, q.x); float yg = gline(a);\n    float t = 0.007 * smoothstep(yg - 0.02, yg + 0.012, q.y);\n    if (uBead > 0.001) { float dy = q.y - gfront(a); t += uBead * ((0.011 + 0.012 * runs(a)) * exp(-dy * dy * 380.0) + 0.004 * smoothstep(-0.02, 0.05, dy)); }\n    solid -= t;\n  }\n  return smax(max(solid, -(o + WALL)), q.y - YTOP, 0.022);\n}\nvec3 nMug(vec3 q){ vec2 e = vec2(0.0018, -0.0018); return normalize(e.xyy * dMug(q + e.xyy) + e.yyx * dMug(q + e.yyx) + e.yxy * dMug(q + e.yxy) + e.xxx * dMug(q + e.xxx)); }\n\n// Five small dishes of glaze on the floor.\nfloat dDishAt(vec3 p, vec2 c){\n  float r = length(p.xz - c);\n  vec2 w = vec2(r - 0.235 + 0.022 + 0.3 * (0.08 - p.y), abs(p.y - 0.04) - 0.04 + 0.022);\n  float d = min(max(w.x, w.y), 0.0) + length(max(w, 0.0)) - 0.022;\n  vec2 v = vec2(r - 0.205, 0.064 - p.y);\n  return smax(d, -(min(max(v.x, v.y), 0.0) + length(max(v, 0.0))), 0.01);\n}\nfloat dDish(vec3 p){\n  float best = 1e9; vec2 c = vec2(0.0);\n  for (int i = 0; i < 5; i++) { vec2 o = p.xz - uDish[i].xy; float d2 = dot(o, o); if (d2 < best) { best = d2; c = uDish[i].xy; } }\n  return dDishAt(p, c);\n}\n// The round card the mug stands on, a finger thick.\nfloat dCard(vec3 p){ vec2 w = vec2(length(p.xz) - PR + 0.02, abs(p.y - 0.5 * PH) - 0.5 * PH + 0.02); return min(max(w.x, w.y), 0.0) + length(max(w, 0.0)) - 0.02; }\nfloat dLow(vec3 p){ return min(dDish(p), dCard(p)); }\nvec3 nLow(vec3 p){ vec2 e = vec2(0.0016, -0.0016); return normalize(e.xyy * dLow(p + e.xyy) + e.yyx * dLow(p + e.yyx) + e.yxy * dLow(p + e.yxy) + e.xxx * dLow(p + e.xxx)); }\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) - 1.5;\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  float y0 = PH + uLift - 0.01; float y1 = PH + uLift + YTOP + 0.03;\n  if (abs(rd.y) > 1e-5) { float ya = (y0 - ro.y) / rd.y; float yb = (y1 - ro.y) / rd.y; t0 = max(t0, min(ya, yb)); t1 = min(t1, max(ya, yb)); }\n  else if (ro.y < y0 || ro.y > y1) 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 < 84; i++) { float d = dMug(toLocal(ro + rd * t)); if (d < 0.0007) return t; t += d * 0.85; if (t > tb.y) break; }\n  return -1.0;\n}\nfloat marchLo(vec3 ro, vec3 rd){\n  vec2 tb = bound(ro, rd); if (tb.x > tb.y) return -1.0;\n  float t = max(tb.x, 0.012);\n  for (int i = 0; i < 30; i++) { float d = dMug(toLocal(ro + rd * t)); if (d < 0.002) return t; t += d * 0.9; if (t > tb.y) break; }\n  return -1.0;\n}\nfloat marchLow(vec3 ro, vec3 rd){\n  if (rd.y > -1e-4) return -1.0;\n  float t = max((PH + 0.01 - ro.y) / rd.y, 0.0); float t1 = -ro.y / rd.y;\n  for (int i = 0; i < 40; i++) { float d = dLow(ro + rd * t); if (d < 0.0008) return t; t += d * 0.9; if (t > t1) break; }\n  return -1.0;\n}\n\n// The room the camera stands in: dim and warm, with a window, a strip light, a scrim overhead and the sun in the window.\nvec3 env(vec3 rd){\n  float el = asin(clamp(rd.y, -1.0, 1.0)); float az = atan(rd.x, -rd.z);\n  vec3 c = mix(vec3(0.16, 0.1, 0.04), vec3(0.5, 0.42, 0.3), smoothstep(-0.1, 0.6, rd.y));\n  c += vec3(1.0, 0.985, 0.96) * (0.45 * smoothstep(0.42, 0.8, el) + 2.2 * smoothstep(0.86, 1.2, el));\n  vec2 w = abs(vec2(az + 0.82, el - 0.5)) - vec2(0.36, 0.3);\n  float bars = smoothstep(0.012, 0.026, abs(az + 0.82)) * smoothstep(0.012, 0.026, abs(el - 0.5));\n  c += vec3(1.0, 0.98, 0.94) * 7.0 * smoothstep(0.02, -0.02, max(w.x, w.y)) * bars;\n  vec2 w2 = abs(vec2(az - 0.95, el - 0.48)) - vec2(0.11, 0.4);\n  c += vec3(0.95, 0.98, 1.0) * 4.2 * smoothstep(0.03, -0.03, max(w2.x, w2.y));\n  // a long softbox hung above the back of the sweep: wet glaze lying flat in a dish looks straight up into its lower edge\n  c += vec3(1.0, 0.985, 0.96) * 1.5 * smoothstep(0.3, 0.36, el) * smoothstep(0.455, 0.42, el) * smoothstep(1.3, 1.7, abs(az));\n  c += SUNC * 30.0 * smoothstep(0.9965, 0.9992, dot(rd, uSun));\n  return c;\n}\n// light from everywhere else: the room from above, and from below whatever the thing stands on\nvec3 ambient(vec3 n, vec3 ground){ return mix(ground * 1.25 + 0.02, vec3(1.0, 0.985, 0.96) * 0.7, 0.5 + 0.5 * n.y); }\n\n// How much of the sun reaches a point: soft where the thing in the way is far off, crisp where it touches.\nfloat sunVis(vec3 p){\n  float res = 1.0;\n  vec2 tb = bound(p, uSun);\n  if (tb.x < tb.y) {\n    float t = max(tb.x, 0.02);\n    for (int i = 0; i < 26; i++) { float h = dMug(toLocal(p + uSun * t)); res = min(res, 9.0 * h / t); t += clamp(h, 0.02, 0.3); if (res < 0.004 || t > tb.y) break; }\n  }\n  if (p.y < PH - 0.02) {\n    float t = 0.015;\n    for (int i = 0; i < 10; i++) { float h = dLow(p + uSun * t); res = min(res, 12.0 * h / t); t += clamp(h, 0.012, 0.08); if (res < 0.004 || t > 0.4) break; }\n  }\n  res = clamp(res, 0.0, 1.0); return res * res * (3.0 - 2.0 * res);\n}\n\n// Everything that is not an object: floor, cove and wall of one sheet of paper.\nfloat hitStage(vec3 ro, vec3 rd, float top, out vec3 n){\n  n = vec3(0.0, 1.0, 0.0);\n  if (rd.y < 0.0) { float tf = -ro.y / rd.y; float z = ro.z + rd.z * tf; if (z < ZW - RC) return tf; }\n  if (rd.z > 0.0) { float tw = (ZW - ro.z) / rd.z; float y = ro.y + rd.y * tw; if (y > top) return -1.0; if (y > RC) { n = vec3(0.0, 0.0, -1.0); return tw; } }\n  vec2 o = ro.yz - vec2(RC, ZW - RC); float a = dot(rd.yz, rd.yz); float b = dot(o, rd.yz); float h = b * b - a * (dot(o, o) - RC * RC);\n  if (h > 0.0 && a > 1e-7) { float tc = (-b + sqrt(h)) / a; vec2 pc = ro.yz + rd.yz * tc; if (tc > 0.0 && pc.x < RC + 0.02 && pc.y > ZW - RC - 0.02) { vec2 nn = (vec2(RC, ZW - RC) - pc) / RC; n = vec3(0.0, nn.x, nn.y); return tc; } }\n  return -1.0;\n}\nvec3 stage(vec3 ro, vec3 rd, float full){\n  vec3 n; float t = hitStage(ro, rd, full > 0.5 ? TOP : TOPR, n);\n  if (t < 0.0) return env(rd);\n  vec3 p = ro + rd * t;\n  vec2 pq = p.xy + p.z * vec2(0.37, 0.81);\n  float tooth = 0.5 * (n2(pq * 150.0) - 0.5) + 0.3 * (n2(pq * 61.0 + 7.0) - 0.5) + 0.35 * (n2(pq * vec2(9.0, 240.0)) - 0.5); float mott = n2(pq * 1.7) - 0.5;\n  vec3 alb = PAPER * (1.0 + 0.07 * tooth + 0.1 * mott);\n  float ndl = max(dot(n, uSun), 0.0);\n  float vis = 1.0; float occ = 1.0;\n  if (full > 0.5) {\n    vis = ndl > 0.0 ? sunVis(p + n * 0.004) : 0.0;\n    float dd = dLow(p + n * 0.02);\n    occ = 0.5 + 0.5 * smoothstep(0.0, 0.12, dd);\n  } else {\n    // seen in the glaze: the card under the mug, and a black flag laid on the floor in front of the set to keep the colour deep\n    if (n.y > 0.9) { alb = mix(alb, PLINTH, smoothstep(PR + 0.2, PR - 0.2, length(p.xz))); alb *= mix(0.03, 1.0, smoothstep(-3.1, -0.8, p.z)); }\n    vec3 c = vec3(0.0, 0.75 + uLift, 0.0) - p; float b = dot(c, uSun); float d2 = dot(c, c) - b * b;\n    vis = b > 0.0 ? smoothstep(0.5, 0.92, sqrt(max(d2, 0.0))) : 1.0;\n    occ = 0.5 + 0.5 * smoothstep(0.35, 1.1, length(p.xz));\n  }\n  // a second lamp washes the backdrop behind the mug and falls away to the corners\n  vec2 wq = p.xy - vec2(0.3, 1.1); float wash = exp(-dot(wq, wq) / 20.0);\n  vec3 fill = mix(PAPER * 1.5 + 0.1, vec3(1.0, 0.985, 0.96), 0.5 + 0.5 * n.y) * (1.0 + (1.3 * wash - 0.3) * (1.0 - n.y));\n  // a flag over the window keeps the top of the backdrop out of the sun, so the light gathers low round the mug\n  ndl *= 1.0 - 0.5 * smoothstep(0.5, 2.6, p.y);\n  // the key light is a pool, brightest round the mug\n  vec2 kq = p.xz - vec2(0.3, -0.2); float pool = 0.7 + 0.42 * exp(-dot(kq, kq) / 26.0);\n  return alb * (SUNC * SUNI * pool * ndl * vis + fill * occ * (1.0 - 0.18 * n.y));\n}\n\n// What the mug is made of at a point: bare clay, the coloured glaze outside or the pale one inside.\nvec4 mugSkin(vec3 q){\n  float r = length(q.xz); float a = atan(q.z, q.x);\n  float o = dShape(q);\n  float sp = 0.0;\n  vec3 g = q * 34.0; vec3 gi = floor(g); float hh = h31(gi);\n  if (hh < 0.34) { vec3 off = (vec3(h31(gi + 7.1), h31(gi + 13.7), h31(gi + 29.3)) - 0.5) * 0.5; float rr = 0.05 + 0.16 * h31(gi + 3.3); sp = smoothstep(rr, rr * 0.45, length(fract(g) - 0.5 - off)); }\n  if (o < -0.03 || (q.y > YTOP - 0.012 && o < -0.5 * WALL)) return vec4(LINER * (1.0 - 0.5 * sp * step(hh, 0.12)), 1.0);\n  float yg = gline(a);\n  float glazed = smoothstep(yg - 0.004, yg + 0.004, q.y);\n  vec3 clay = CLAY * (0.7 + 0.5 * n2(vec2(a * 30.0, q.y * 90.0)) * n2(vec2(a * 7.0, q.y * 21.0)) + 0.16 * n2(vec2(a * 2.0, q.y * 150.0))) * (1.0 - 0.6 * sp * step(hh, 0.22)) + 0.1 * sp * step(0.27, hh);\n  vec3 col = mix(uOld, uNew, smoothstep(0.006, -0.006, gfront(a) - q.y));\n  // glaze runs thin over the lip and breaks to toffee there, and pools darker where it stops\n  float lum = dot(col, vec3(0.3, 0.6, 0.1));\n  col = mix(col, mix(vec3(0.4, 0.19, 0.06), col, 0.12) * (0.35 + 1.6 * lum), 0.85 * smoothstep(YTOP - 0.024, YTOP - 0.008, q.y));\n  col *= 1.0 - 0.3 * smoothstep(0.05, 0.0, q.y - yg) * glazed;\n  col *= 1.0 - 0.6 * sp * step(hh, 0.1);\n  // and it is never one flat colour: thinner over each throwing ring, cloudy where it pooled in the kiln\n  col *= (0.88 + 0.24 * n2(vec2(a * 3.0, q.y * 5.0))) * (1.0 + 0.035 * sin(q.y * 34.0 + 1.5 * n2(vec2(a * 2.0, q.y * 3.0)))) * (0.96 + 0.08 * n2(vec2(a * 38.0, q.y * 55.0)));\n  return vec4(mix(clay, col, glazed), glazed);\n}\n\n// A mirror ray off glaze: the mug again, or the set.\nvec3 mirror(vec3 ro, vec3 rd){\n  float t = marchLo(ro, rd);\n  if (t > 0.0) {\n    vec3 q = toLocal(ro + rd * t); vec3 n = toWorldDir(nMug(q)); vec4 sk = mugSkin(q);\n    return sk.rgb * (SUNC * SUNI * max(dot(n, uSun), 0.0) * 0.55 + ambient(n, PLINTH) * 0.6);\n  }\n  return stage(ro, rd, 0.0);\n}\n\nfloat aoMug(vec3 p, vec3 n){\n  float a = 0.0; float w = 1.0;\n  for (int i = 1; i <= 4; i++) { float d = 0.045 * float(i) + 0.012 * float(i * i); float h = min(dMug(toLocal(p + n * d)), p.y + n.y * d - PH); a += w * (d - h); w *= 0.62; }\n  return clamp(1.0 - 2.1 * a, 0.0, 1.0);\n}\n\nvec3 shadeMug(vec3 p, vec3 rd){\n  vec3 q = toLocal(p); vec3 nl = nMug(q); vec3 n = toWorldDir(nl);\n  vec4 sk = mugSkin(q);\n  float ndl = max(dot(n, uSun), 0.0);\n  float vis = ndl > 0.0 ? sunVis(p + n * 0.012) : 0.0;\n  float ao = aoMug(p, n);\n  vec3 col = sk.rgb * (SUNC * SUNI * ndl * vis + ambient(n, dShape(q) < -0.03 ? vec3(0.8, 0.6, 0.36) : PLINTH) * ao);\n  // fired glaze is never optically flat: a slow wobble in the surface, and the rings the potter's fingers left\n  float a = atan(q.z, q.x);\n  vec3 wob = vec3(n2(vec2(a * 5.0, q.y * 7.0)) - 0.5, 0.0, n2(vec2(a * 5.0 + 17.0, q.y * 7.0)) - 0.5) * 0.035;\n  wob.y = 0.014 * sin(q.y * 34.0 + 1.5 * n2(vec2(a * 2.0, q.y * 3.0))) * step(-0.03, dShape(q));\n  vec3 ng = normalize(n + toWorldDir(wob) * sk.a);\n  float nv = clamp(dot(-rd, ng), 0.0, 1.0);\n  float f = (0.04 + 0.96 * pow(1.0 - nv, 5.0)) * (0.06 + 0.94 * sk.a);\n  vec3 refl = mirror(p + n * 0.012, normalize(reflect(rd, ng) + gRnd * 0.01));\n  return col * (1.0 - f) + refl * f * (0.35 + 0.65 * ao);\n}\n\nvec3 shadeLow(vec3 p, vec3 rd){\n  if (dCard(p) < dDish(p)) {\n    vec3 n = nLow(p);\n    float tooth = 0.5 * (n2(p.xz * 150.0) - 0.5) + 0.3 * (n2(p.xz * 61.0 + 7.0) - 0.5) + 0.35 * (n2(p.xz * vec2(9.0, 240.0)) - 0.5);\n    vec3 alb = PLINTH * (1.0 + 0.09 * tooth + 0.1 * (n2(p.xz * 2.3) - 0.5));\n    float ndl = max(dot(n, uSun), 0.0);\n    float vis = ndl > 0.0 ? sunVis(p + n * 0.004) : 0.0;\n    float dm = dMug(toLocal(p + n * 0.05));\n    float occ = (0.38 + 0.62 * smoothstep(0.0, 0.5, dm)) * (0.6 + 0.4 * smoothstep(0.0, 0.1, p.y + 0.06 * n.y));\n    // the glaze throws a little of its own colour back onto the card beside it\n    vec3 bleed = mix(uOld, uNew, uProg) * 0.7 * exp(-max(dm, 0.0) * 4.5);\n    vec3 fill = mix(PAPER * 1.5 + 0.1, vec3(1.0, 0.985, 0.96), 0.5 + 0.5 * n.y);\n    return alb * (SUNC * SUNI * 1.1 * ndl * vis + fill * occ * (1.0 - 0.18 * n.y) + bleed);\n  }\n  float best = 1e9; vec3 c = vec3(0.0); vec3 tint = vec3(0.0);\n  for (int i = 0; i < 5; i++) { vec2 o = p.xz - uDish[i].xy; float d2 = dot(o, o); if (d2 < best) { best = d2; c = uDish[i]; tint = uTint[i]; } }\n  vec2 o = p.xz - c.xy; float r = length(o);\n  vec3 n = nLow(p);\n  float wet = step(r, 0.197) * step(p.y, 0.069);\n  vec3 alb = mix(LINER * (0.9 + 0.2 * n2(p.xz * 40.0)) * (1.0 - 0.5 * step(0.93, h21(floor(p.xz * 190.0)))), tint * 0.85, wet);\n  // wet glaze climbs the wall of the dish a little, and the dish you picked is still rocking\n  if (wet > 0.5) { float m = smoothstep(0.15, 0.197, r); float k = c.z * 0.2 * sin(r * 62.0 - uRipT * 13.0) * smoothstep(0.19, 0.1, r) - 0.7 * m * m; n = normalize(vec3(o.x / max(r, 1e-3) * k, 1.0, o.y / max(r, 1e-3) * k)); }\n  float ndl = max(dot(n, uSun), 0.0);\n  float vis = ndl > 0.0 ? sunVis(p + n * 0.006) : 0.0;\n  float ao = mix(0.55 + 0.45 * smoothstep(0.0, 0.07, p.y), 0.8 + 0.2 * smoothstep(0.2, 0.1, r), step(0.06, p.y));\n  vec3 col = alb * (SUNC * SUNI * ndl * vis + ambient(n, PAPER) * ao);\n  float nv = clamp(dot(-rd, n), 0.0, 1.0);\n  float f = (0.04 + 0.96 * pow(1.0 - nv, 5.0)) * (0.55 + 0.45 * wet);\n  return col * (1.0 - f) + mirror(p + n * 0.006, reflect(rd, n)) * f;\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  gRnd = uSeed > 0.001 ? (vec3(h21(gl_FragCoord.xy + ign * 17.0), h21(gl_FragCoord.yx + ign * 31.0), ign) - 0.5) * 2.0 : vec3(0.0);\n  float tm = march(ro, rd); float td = marchLow(ro, rd);\n  vec3 col;\n  if (tm > 0.0 && (td < 0.0 || tm < td)) col = shadeMug(ro + rd * tm, rd);\n  else if (td > 0.0) col = shadeLow(ro + rd * td, rd);\n  else col = stage(ro, rd, 1.0);\n  col = min(col, vec3(14.0));\n  if (uDirect > 0.5) col = tone(col);\n  outC = vec4(col, 1.0);\n}\n`;\n\n// Developing the negative: a little bloom off the hot glints, the film curve, a soft vignette and 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.4, 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.02 * 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.014;\n  vec2 q = (uv - uCentre) * vec2(uRes.x / uRes.y, 1.0);\n  c *= 1.0 - 0.16 * smoothstep(0.4, 1.3, length(q));\n  c = tone(c);\n  float g = h21(gl_FragCoord.xy + uGrain * 91.7) - 0.5;\n  c += g * 0.018 * (1.0 - 0.4 * c.g);\n  outC = vec4(c, 1.0);\n}\n`;\n\ntype Spot = { x: number; y: number; k: number };\n\n// No WebGL: the same set built from CSS at the same spot and scale (1em is one unit of the scene), and the new glaze still runs down from the lip.\nfunction Still({ at, sel }: { at: Spot; sel: number }) {\n  const side = \"linear-gradient(90deg, rgba(255,255,255,0.22) 0%, rgba(255,255,255,0.1) 16%, rgba(0,0,0,0) 40%, rgba(0,0,10,0.28) 80%, rgba(0,0,10,0.5) 100%), linear-gradient(180deg, rgba(0,0,20,0.25) 0%, rgba(0,0,0,0) 22%, rgba(0,0,0,0) 62%, rgba(120,40,0,0.3) 100%)\";\n  return (\n    <div aria-hidden=\"true\" className=\"absolute h-0 w-0\" style={{ left: at.x, top: at.y, fontSize: at.k }}>\n      {/* the card, with its edge */}\n      <div className=\"absolute left-[-1.2em] top-[-0.27em] h-[0.78em] w-[2.4em] rounded-[50%] bg-[#d9601a] shadow-[0.1em_0.12em_0.12em_rgba(140,70,0,0.5)]\" />\n      <div className=\"absolute left-[-1.2em] top-[-0.39em] h-[0.78em] w-[2.4em] overflow-hidden rounded-[50%] bg-[#f58a2c]\">\n        <div className=\"absolute left-[1.05em] top-[0.02em] h-[0.5em] w-[1.5em] -rotate-[16deg] rounded-[50%] bg-[#a8470e] opacity-80 blur-[0.05em]\" />\n      </div>\n      <div className=\"absolute left-[1.02em] top-[-0.62em] h-[0.5em] w-[1.3em] -rotate-[16deg] rounded-[50%] bg-[#b07a10] opacity-60 blur-[0.07em]\" />\n      {/* handle, foot and body */}\n      {glazes.map((g, i) => (\n        <div key={g.name} className=\"absolute left-[0.52em] top-[-0.98em] h-[0.7em] w-[0.6em] rounded-[50%] border-[0.125em] transition-opacity delay-500 duration-700 motion-reduce:transition-none\" style={{ borderColor: g.css, opacity: i === sel ? 1 : 0, boxShadow: \"inset -0.03em -0.03em 0.05em rgba(0,0,20,0.35), inset 0.02em 0.03em 0.03em rgba(255,255,255,0.5)\" }} />\n      ))}\n      <div className=\"absolute left-[-0.456em] top-[-0.148em] h-[0.296em] w-[0.912em] rounded-[50%] bg-[#6f4d2f]\" />\n      <div className=\"absolute left-[-0.7em] top-[-1.154em] h-[1.154em] w-[1.4em] isolate overflow-hidden rounded-[0.144em_0.144em_0.244em_0.244em/0.55em_0.55em_0.6em_0.6em] bg-[#a17a55]\">\n        {glazes.map((g, i) => (\n          <div key={g.name} className=\"absolute inset-x-[-0.1em] top-[-0.2em] h-[1.16em] rounded-b-[50%_0.22em] transition-[clip-path] duration-[1400ms] ease-out motion-reduce:transition-none\" style={{ backgroundColor: g.css, zIndex: i === sel ? 2 : 1, clipPath: i === sel ? \"inset(0 0 0 0)\" : \"inset(0 0 100% 0)\", transitionDelay: i === sel ? \"0ms\" : \"1400ms\" }} />\n        ))}\n        <div className=\"absolute inset-0 z-[3]\" style={{ background: side }} />\n        <div className=\"absolute left-[0.3em] top-[0.2em] z-[3] grid h-[0.24em] w-[0.2em] -skew-y-12 grid-cols-2 gap-[0.016em] opacity-60\">\n          <span className=\"rounded-[0.01em] bg-white\" /><span className=\"rounded-[0.01em] bg-white\" /><span className=\"rounded-[0.01em] bg-white\" /><span className=\"rounded-[0.01em] bg-white\" />\n        </div>\n        <div className=\"absolute right-[0.2em] top-[0.14em] z-[3] h-[0.4em] w-[0.035em] rotate-[8deg] rounded-full bg-white opacity-40 blur-[0.01em]\" />\n      </div>\n      {/* the mouth: lip, then the pale glaze inside */}\n      {glazes.map((g, i) => (\n        <div key={g.name} className=\"absolute left-[-0.572em] top-[-1.35em] h-[0.39em] w-[1.144em] rounded-[50%] transition-opacity duration-500 motion-reduce:transition-none\" style={{ backgroundColor: g.css, opacity: i === sel ? 1 : 0, boxShadow: \"inset 0 0.03em 0.03em rgba(0,0,20,0.4)\" }} />\n      ))}\n      <div className=\"absolute left-[-0.53em] top-[-1.325em] h-[0.34em] w-[1.06em] rounded-[50%] bg-[#f3ead6]\" />\n      <div className=\"absolute left-[-0.5em] top-[-1.305em] h-[0.31em] w-[1em] rounded-[50%]\" style={{ background: \"radial-gradient(ellipse 70% 90% at 62% 18%, #f1e2c2 0%, #dcc49a 46%, #a98c62 100%)\" }} />\n    </div>\n  );\n}\n\nexport default function RayMarchedStonewareMugShowcase() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLDivElement>(null);\n  const [shown, setShown] = useState<\"wait\" | \"gl\" | \"still\">(\"wait\");\n  const [sel, setSel] = useState(0);\n  const [bag, setBag] = useState(0);\n  const [spots, setSpots] = useState<{ mug: Spot; dishes: Spot[] } | null>(null);\n  const api = useRef<{ pour: (i: number) => void; hop: () => void; turn: () => void } | null>(null);\n\n  // Where the lens puts the mug and each dish, in CSS pixels, so real buttons can sit on top of them.\n  useEffect(() => {\n    const box = stage.current;\n    if (!box) return;\n    const place = () => {\n      const r = box.getBoundingClientRect();\n      const f = frameOf(Math.max(1, r.width), Math.max(1, r.height));\n      setSpots({ mug: f.project(0, 0.13, 0), dishes: f.dishes.map(([x, z]) => f.project(x, 0.06, z)) });\n    };\n    place();\n    const ro = new ResizeObserver(place);\n    ro.observe(box);\n    return () => ro.disconnect();\n  }, []);\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; // nothing plays by itself: draw, refine, then rest\n\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      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\", \"uDirect\", \"uCam\", \"uTgt\", \"uFoc\", \"uShift\", \"uFocus\", \"uRot\", \"uLift\", \"uSun\", \"uOld\", \"uNew\", \"uProg\", \"uBead\", \"uGseed\", \"uDish\", \"uTint\", \"uRipT\"].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    // 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.75, window.devicePixelRatio || 1) * scale;\n      // never more than about two million pixels of tracing\n      const k = Math.min(1, Math.sqrt(1.9e6 / (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 s = {\n      a: HERO, v: 0, rest: HERO as number | null, drag: false, spinT: -1, spinFrom: HERO, spinTo: HERO, spinLen: 1,\n      cur: 0, old: 0, prog: 1, bead: 0, gseed: 1.3, rip: 0, ripT: 0, // the pour and the dish it came from\n      ly: 0, lv: 0, // height off the floor after a hop\n      count: 0, fresh: true, frame: 0, slow: 0,\n      visible: true, raf: 0, last: 0, t0: 0, touched: false, demoAt: 0, first: true, alive: 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 tints = new Float32Array(glazes.flatMap((g) => g.rgb));\n\n    const draw = (alpha: number, sample: number) => {\n      const f = frameOf(cssW, cssH);\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.05 : 0;\n      // the sun is a disc, not a point: each exposure looks at a slightly different part of it\n      const sj = sample ? 0.02 : 0, sa = -0.9 + (halton(sample, 11) - 0.5) * sj, se = 0.6 + (halton(sample, 13) - 0.5) * sj;\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.uDirect, canAcc ? 0 : 1);\n      gl.uniform3f(su.uCam, f.cam[0], f.cam[1], f.cam[2]); gl.uniform3f(su.uTgt, f.tgt[0], f.tgt[1], f.tgt[2]);\n      gl.uniform1f(su.uFoc, f.foc);\n      gl.uniform2f(su.uShift, f.shift[0], f.shift[1]);\n      gl.uniform1f(su.uFocus, f.dist);\n      gl.uniform1f(su.uRot, s.a); gl.uniform1f(su.uLift, s.ly);\n      // the window is camera left and a little in front of the set, so shadows fall back and to the right\n      gl.uniform3f(su.uSun, Math.sin(sa) * Math.cos(se), Math.sin(se), -Math.cos(sa) * Math.cos(se));\n      const o = glazes[s.old].rgb, n = glazes[s.cur].rgb;\n      gl.uniform3f(su.uOld, o[0], o[1], o[2]); gl.uniform3f(su.uNew, n[0], n[1], n[2]);\n      gl.uniform1f(su.uProg, s.prog); gl.uniform1f(su.uBead, s.bead); gl.uniform1f(su.uGseed, s.gseed);\n      gl.uniform3fv(su.uDish, new Float32Array(f.dishes.flatMap(([x, z], i) => [x, z, i === s.cur ? s.rip : 0])));\n      gl.uniform3fv(su.uTint, tints);\n      gl.uniform1f(su.uRipT, s.ripT);\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, cssW >= 768 ? 0.6 : 0.5, 0.5);\n        gl.drawArrays(gl.TRIANGLES, 0, 3);\n      }\n    };\n\n    // a turn with a wind-up before it and a lean past the mark after it\n    const back = (x: number) => { const c = 1.15; return x < 0.5 ? (Math.pow(2 * x, 2) * ((c + 1) * 2 * x - c)) / 2 : (Math.pow(2 * x - 2, 2) * ((c + 1) * (x * 2 - 2) + c) + 2) / 2; };\n    const spin = (by: number, secs: number) => {\n      if (s.drag) return;\n      const from = s.spinT >= 0 ? s.spinTo : s.rest ?? s.a;\n      s.spinT = 0; s.spinFrom = s.a; s.spinTo = from + by; s.spinLen = secs; s.rest = s.spinTo;\n    };\n    const pour = (i: number, fromUser: boolean) => {\n      if (fromUser) s.touched = true;\n      if (i === s.cur) return;\n      s.old = s.cur; s.cur = i; s.gseed = Math.random() * 6.283;\n      if (reduce) { s.prog = 1; s.bead = 0; } else { s.prog = 0; s.bead = 1; s.rip = 1; s.ripT = 0; spin(Math.PI * 2, 2.3); }\n      s.count = 0; s.fresh = reduce;\n      wake();\n    };\n    api.current = {\n      pour: (i) => pour(i, true),\n      hop: () => { s.touched = true; if (!reduce && s.ly < 0.01) { s.lv = 3.5; s.ly = 0.001; s.v += 2.4; wake(); } },\n      turn: () => { s.touched = true; if (reduce) { s.a += Math.PI / 2; s.rest = s.a; s.count = 0; s.fresh = true; } else spin(Math.PI / 2, 0.9); wake(); },\n    };\n\n    const frame = (now: number) => {\n      s.raf = 0;\n      if (!s.visible || !s.alive) 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      // it opens finished and holds; then, until someone touches it, it tries the next glaze every few seconds\n      if (!s.t0) { s.t0 = now; s.demoAt = now + 4600; }\n      if (!still && !s.touched && now > s.demoAt) { const next = (s.cur + 1) % glazes.length; pour(next, false); setSel(next); s.demoAt = now + 6400; }\n\n      let moving = false;\n      if (!reduce) {\n        if (s.spinT >= 0) { s.spinT += dt / s.spinLen; if (s.spinT >= 1) s.spinT = -1; }\n        if (!s.drag) {\n          if (s.rest !== null) {\n            const want = s.spinT >= 0 ? s.spinFrom + (s.spinTo - s.spinFrom) * back(s.spinT) : s.rest;\n            const stiff = s.spinT >= 0 ? 150 : 46, damp = s.spinT >= 0 ? 22 : 8;\n            for (let i = 0; i < 3; i++) { s.v += (stiff * (want - s.a) - damp * s.v) * (dt / 3); s.a += s.v * (dt / 3); }\n            if (s.spinT < 0 && Math.abs(s.v) < 0.003 && Math.abs(want - s.a) < 0.0006) { s.a = want; s.v = 0; } else moving = true;\n          } else {\n            // let go after a drag: it coasts like something heavy on a banding wheel\n            s.v *= Math.exp(-dt * 2.4); s.a += s.v * dt;\n            if (Math.abs(s.v) < 0.012) s.v = 0; else moving = true;\n          }\n        } else moving = true;\n        if (s.prog < 1) { s.prog = Math.min(1, s.prog + dt / POUR); moving = true; }\n        else if (s.bead > 0.002) { s.bead *= Math.exp(-dt * 3.2); moving = true; } else s.bead = 0;\n        if (s.rip > 0.004) { s.rip *= Math.exp(-dt * 1.5); s.ripT += dt; moving = true; } else s.rip = 0;\n        if (s.ly > 0 || s.lv !== 0) {\n          s.lv -= 26 * dt; s.ly += s.lv * dt;\n          if (s.ly <= 0) { s.ly = 0; s.lv = s.lv < -1.3 ? -s.lv * 0.3 : 0; }\n          moving = true;\n        }\n      }\n      if (s.fresh || !canAcc) { draw(1, 0); s.fresh = false; s.count = 1; }\n      else if (moving) { draw(0.6, 1 + (s.frame % 64)); s.count = 3; }\n      else if (s.count < 56) { 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 && moving && dtRaw > 0.026) s.slow++; else s.slow = Math.max(0, s.slow - 2);\n      if (s.slow > 40 && scale > 0.5) { scale *= 0.82; s.slow = 0; size(); }\n\n      if (moving || s.count < 56 || (!still && !s.touched)) s.raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!s.raf && s.visible && s.alive) { 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, id = -1, lastT = 0;\n    const down = (e: PointerEvent) => {\n      id = e.pointerId; downX = e.clientX; lastT = e.timeStamp; s.drag = true; s.touched = true; s.spinT = -1; s.v = 0; s.rest = null;\n      try { pad.setPointerCapture(id); } catch { /* the pointer is already gone */ }\n      wake();\n    };\n    const move = (e: PointerEvent) => {\n      if (!s.drag || e.pointerId !== id) return;\n      const dx = e.clientX - downX; downX = e.clientX;\n      const da = dx * 0.011, dtp = Math.max(0.008, (e.timeStamp - lastT) / 1000); lastT = e.timeStamp;\n      s.a += da; s.v = s.v * 0.5 + (da / dtp) * 0.5;\n      if (reduce) { s.count = 0; s.fresh = true; }\n      wake();\n    };\n    const upFn = (e: PointerEvent) => {\n      if (!s.drag || e.pointerId !== id) return;\n      s.drag = false;\n      s.v = reduce ? 0 : Math.max(-12, Math.min(12, s.v));\n      wake();\n    };\n    pad.addEventListener(\"pointerdown\", down);\n    pad.addEventListener(\"pointermove\", move);\n    pad.addEventListener(\"pointerup\", upFn);\n    pad.addEventListener(\"pointercancel\", upFn);\n    const lost = (e: Event) => { e.preventDefault(); setShown(\"still\"); };\n    cv.addEventListener(\"webglcontextlost\", lost);\n    wake();\n\n    return () => {\n      s.alive = false;\n      cancelAnimationFrame(s.raf); ro.disconnect(); io.disconnect();\n      document.removeEventListener(\"visibilitychange\", vis);\n      pad.removeEventListener(\"pointerdown\", down); pad.removeEventListener(\"pointermove\", move);\n      pad.removeEventListener(\"pointerup\", upFn); pad.removeEventListener(\"pointercancel\", upFn);\n      cv.removeEventListener(\"webglcontextlost\", lost);\n      api.current = null;\n    };\n  }, []);\n\n  const pick = (i: number) => { setSel(i); api.current?.pour(i); };\n  const add = () => { setBag((b) => b + 1); api.current?.hop(); };\n  const glaze = glazes[sel];\n\n  return (\n    <section className=\"relative isolate overflow-hidden bg-[#f3b81f] text-[#1d1403]\" style={{ fontFamily: FONT }}>\n      <style>{`\n        @keyframes ray-marched-stoneware-mug-rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }\n        .ray-marched-stoneware-mug-rise { animation: ray-marched-stoneware-mug-rise 700ms cubic-bezier(0.2, 0.9, 0.2, 1) both; }\n        @keyframes ray-marched-stoneware-mug-name { from { opacity: 0; transform: translateY(0.5em); } to { opacity: 1; transform: none; } }\n        .ray-marched-stoneware-mug-name { display: inline-block; animation: ray-marched-stoneware-mug-name 520ms cubic-bezier(0.2, 1.2, 0.3, 1) both; }\n        @keyframes ray-marched-stoneware-mug-count { 0% { transform: scale(1); } 35% { transform: scale(1.45); } 100% { transform: scale(1); } }\n        .ray-marched-stoneware-mug-count { animation: ray-marched-stoneware-mug-count 420ms cubic-bezier(0.2, 0.9, 0.2, 1) both; }\n        @media (prefers-reduced-motion: reduce) { .ray-marched-stoneware-mug-rise, .ray-marched-stoneware-mug-name, .ray-marched-stoneware-mug-count { animation: none; } }\n      `}</style>\n      <div ref={stage} className=\"relative mx-auto h-[750px] w-full max-w-[1600px] md:h-[760px]\" style={{ background: \"linear-gradient(180deg, #e9a814 0%, #f2b71d 26%, #f8cc35 40%, #f9d445 100%)\" }}>\n        {shown === \"still\" && spots && <Still at={spots.mug} sel={sel} />}\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        <div ref={hit} aria-hidden=\"true\" className=\"absolute inset-0 cursor-grab select-none active:cursor-grabbing\" style={{ touchAction: \"pan-y\" }} />\n\n        {/* One real button over each dish of glaze. */}\n        <div role=\"group\" aria-label=\"Glaze colour\">\n          {spots?.dishes.map((d, i) => {\n            const w = Math.max(44, d.k * 0.5), h = Math.max(44, d.k * 0.23);\n            return (\n              <button key={glazes[i].name} type=\"button\" aria-pressed={i === sel} aria-label={`${glazes[i].name} glaze`} onClick={() => pick(i)} className=\"group absolute -translate-x-1/2 -translate-y-1/2 cursor-pointer rounded-[50%] outline-none focus-visible:ring-2 focus-visible:ring-[#1d1403] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f8cc35]\" style={{ left: d.x, top: d.y, width: w, height: h }}>\n                {shown === \"still\" && (\n                  <span className=\"absolute inset-0 rounded-[50%] border-[#efe6d0] shadow-[0.4em_0.5em_0.5em_-0.1em_rgba(120,60,0,0.55),inset_0_0.5em_0.4em_-0.3em_rgba(0,0,20,0.35)]\" style={{ backgroundColor: glazes[i].css, fontSize: w / 10, borderWidth: w / 11, borderBottomWidth: w / 8 }} />\n                )}\n                <span aria-hidden=\"true\" className={`absolute left-1/2 top-full mt-1 -translate-x-1/2 whitespace-nowrap text-[10px] font-bold uppercase tracking-[0.26em] transition-opacity duration-300 motion-reduce:transition-none ${i === sel ? \"opacity-100\" : \"opacity-0 group-hover:opacity-60 group-focus-visible:opacity-60\"}`}>{glazes[i].name}</span>\n              </button>\n            );\n          })}\n        </div>\n\n        <div className=\"pointer-events-none absolute inset-0 flex flex-col px-6 pb-7 pt-6 md:px-10 md:pb-10 md:pt-8 lg:px-16 lg:pb-12 lg:pt-10\">\n          <p className=\"flex items-center justify-between text-[11px] font-semibold uppercase tracking-[0.32em]\">\n            <span>{product.maker}</span>\n            <span className=\"flex items-center gap-3\">\n              Bag\n              <span key={bag} className={`grid h-7 min-w-[28px] place-items-center rounded-full bg-[#1d1403] px-2 text-[12px] tracking-normal text-[#ffd54a] ${bag ? \"ray-marched-stoneware-mug-count\" : \"\"}`}>{bag}</span>\n            </span>\n          </p>\n          <div className=\"ray-marched-stoneware-mug-rise mt-auto md:mb-auto md:mt-[58px]\">\n            <p className=\"text-[17px] italic md:text-[19px] lg:text-[21px]\" style={{ fontFamily: SERIF }}>{product.kind}</p>\n            <h2 className=\"mt-1 text-[68px] font-extrabold leading-[0.82] tracking-[-0.055em] md:mt-3 md:text-[104px] lg:text-[148px]\">\n              {product.name.map((line) => <span key={line} className=\"block\">{line}</span>)}\n            </h2>\n            <p className=\"mt-3 text-[20px] italic leading-none md:mt-5 md:text-[25px] lg:mt-6 lg:text-[30px]\" style={{ fontFamily: SERIF }}>\n              dipped in <span key={glaze.name} className=\"ray-marched-stoneware-mug-name not-italic font-extrabold tracking-[-0.03em]\" style={{ fontFamily: FONT }}>{glaze.name}</span>\n            </p>\n          </div>\n          <div className=\"ray-marched-stoneware-mug-rise mt-5 flex flex-wrap items-center gap-x-5 gap-y-3 md:mt-0\">\n            <span className=\"text-[52px] font-extrabold leading-none tracking-[-0.055em] md:text-[64px] lg:text-[88px]\">{product.price}</span>\n            <button type=\"button\" onClick={add} className=\"pointer-events-auto inline-flex h-[60px] items-center gap-3 whitespace-nowrap rounded-full bg-[#1d1403] pl-8 pr-7 text-[19px] font-bold tracking-[-0.02em] text-[#ffd54a] shadow-[0_1px_0_rgba(255,255,255,0.25)_inset,0_16px_30px_-14px_rgba(90,50,0,0.9)] outline-none transition-transform duration-200 hover:-translate-y-0.5 focus-visible:ring-2 focus-visible:ring-[#1d1403] focus-visible:ring-offset-2 focus-visible:ring-offset-[#f8cc35] active:translate-y-0.5 active:scale-[0.98] motion-reduce:transition-none lg:h-[68px] lg:text-[22px]\">\n              {product.cta}\n              <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-5 w-5\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d=\"M4.5 7h11l-.8 9.5H5.3L4.5 7ZM7.3 7V5.7a2.7 2.7 0 0 1 5.4 0V7\" /></svg>\n            </button>\n            <span className=\"w-full text-[11px] font-semibold uppercase tracking-[0.24em] lg:mt-1\">{product.note}</span>\n          </div>\n        </div>\n        <p aria-live=\"polite\" className=\"sr-only\">{bag ? `${bag} in your bag. ${product.name.join(\" \")} in ${glaze.name} added.` : \"\"}</p>\n        {shown === \"gl\" && (\n          <button type=\"button\" onClick={() => api.current?.turn()} className=\"absolute right-3 top-[58px] flex h-10 items-center gap-2 rounded-full px-3 text-[10px] font-semibold uppercase tracking-[0.28em] outline-none hover:text-[#1f45c8] focus-visible:ring-2 focus-visible:ring-[#1d1403] md:right-7 md:top-[66px] md:text-[11px] lg:right-[52px] lg:top-[78px]\">\n            <svg aria-hidden=\"true\" viewBox=\"0 0 20 20\" className=\"h-4 w-4\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.8\" strokeLinecap=\"round\" strokeLinejoin=\"round\"><path d=\"M16.5 10a6.5 6.5 0 1 1-2.2-4.9M16.6 3.2v3.4h-3.4\" /></svg>\n            <span><span className=\"hidden md:inline\">Drag or press to turn</span><span className=\"md:hidden\">Turn</span></span>\n          </button>\n        )}\n      </div>\n    </section>\n  );\n}\n"}]}
