{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"painted-alphabet-blocks-that-tumble-and-stack-to-spell-the-word-you-type","type":"registry:component","title":"Painted Alphabet Blocks That Tumble and Stack to Spell the Word You Type","description":"Text field whose word is spelled in wooden alphabet blocks on a canvas: cubes drop, tumble, bounce and stack as you type. Free React and HTML code to copy.","dependencies":[],"files":[{"path":"components/we-inc/painted-alphabet-blocks-that-tumble-and-stack-to-spell-the-word-you-type.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// The first word is on the table when the page loads. The rest play in turn until someone types.\nconst WORDS = [\"PLAYTIME\", \"HOMEWORK\", \"NOTEBOOK\", \"BOOKWORM\"];\nconst MAX = 8; // most blocks on the table: four along the bottom, four on top\nconst PAINTS = [\"#c9402e\", \"#2c6aa8\", \"#dfa322\", \"#3d8a5a\", \"#d8642b\", \"#77508f\"]; // one milk paint per face\nconst MAPLE: [number, number, number] = [230, 197, 148];\nconst DEMO = true; // set to false to stop the self-running words\nconst LABEL = \"Spell a word\";\n\ntype V3 = [number, number, number];\ntype Q = [number, number, number, number];\n\nconst clamp = (v: number, a: number, b: number) => Math.max(a, Math.min(b, v));\nconst clean = (s: string) => s.toUpperCase().replace(/[^A-Z0-9]/g, \"\").slice(0, MAX);\nconst hash = (x: number, y: number, s: number) => { const n = Math.sin(x * 127.1 + y * 311.7 + s * 74.7) * 43758.5453; return n - Math.floor(n); };\nconst noise = (x: number, y: number, s: number) => {\n  const xi = Math.floor(x), yi = Math.floor(y), xf = x - xi, yf = y - yi, u = xf * xf * (3 - 2 * xf), v = yf * yf * (3 - 2 * yf);\n  const a = hash(xi, yi, s), b = hash(xi + 1, yi, s), c = hash(xi, yi + 1, s), d = hash(xi + 1, yi + 1, s);\n  return a + (b - a) * u + (c - a) * v + (a - b - c + d) * u * v;\n};\n// Quaternions: x is right, y is down, z comes towards you.\nconst qmul = (a: Q, b: Q): Q => [a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1], a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0], a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3], a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2]];\nconst qaxis = (x: number, y: number, z: number, ang: number): Q => { const s = Math.sin(ang / 2); return [x * s, y * s, z * s, Math.cos(ang / 2)]; };\nconst qrot = (q: Q, v: V3): V3 => {\n  const [x, y, z, w] = q, tx = 2 * (y * v[2] - z * v[1]), ty = 2 * (z * v[0] - x * v[2]), tz = 2 * (x * v[1] - y * v[0]);\n  return [v[0] + w * tx + (y * tz - z * ty), v[1] + w * ty + (z * tx - x * tz), v[2] + w * tz + (x * ty - y * tx)];\n};\nconst qslerp = (a: Q, b0: Q, t: number): Q => {\n  let d = a[0] * b0[0] + a[1] * b0[1] + a[2] * b0[2] + a[3] * b0[3];\n  const b: Q = d < 0 ? [-b0[0], -b0[1], -b0[2], -b0[3]] : b0; d = Math.abs(d);\n  let wa = 1 - t, wb = t;\n  if (d < 0.9995) { const th = Math.acos(d), s = Math.sin(th); wa = Math.sin((1 - t) * th) / s; wb = Math.sin(t * th) / s; }\n  const r: Q = [a[0] * wa + b[0] * wb, a[1] * wa + b[1] * wb, a[2] * wa + b[2] * wb, a[3] * wa + b[3] * wb], l = Math.hypot(r[0], r[1], r[2], r[3]) || 1;\n  return [r[0] / l, r[1] / l, r[2] / l, r[3] / l];\n};\n// The turn that brings each of the six faces round to the front with its letter upright.\nconst FRONT: Q[] = [[0, 0, 0, 1], qaxis(0, 1, 0, -Math.PI / 2), qaxis(0, 1, 0, Math.PI), qaxis(0, 1, 0, Math.PI / 2), qaxis(1, 0, 0, -Math.PI / 2), qaxis(1, 0, 0, Math.PI / 2)];\nconst FACES = FRONT.map((q) => { const c: Q = [-q[0], -q[1], -q[2], q[3]]; return { n: qrot(c, [0, 0, 1]), u: qrot(c, [1, 0, 0]), v: qrot(c, [0, 1, 0]) }; });\nconst TILT = 0.3, CT = Math.cos(TILT), ST = Math.sin(TILT); // how far the camera looks down at the table\nconst view = (p: V3): V3 => [p[0], p[1] * CT + p[2] * ST, -p[1] * ST + p[2] * CT];\nconst LIGHT: V3 = [-0.46, -0.76, 0.46];\nconst T = 192; // face texture size\n\ntype Anim = { kind: \"hop\" | \"drop\" | \"leave\"; start: number; dur: number; from: V3; to: V3; qa: Q; qb: Q; spin: number; axis: V3; lift: number };\ntype Block = { on: boolean; p: V3; q: Q; face: number; chars: string[]; anim: Anim | null; rest: number };\n\nexport default function PaintedAlphabetBlocks() {\n  const wrap = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [word, setWord] = useState(WORDS[0]);\n  const api = useRef<{ spell: (w: string, fromUser: boolean) => void } | null>(null);\n\n  useEffect(() => {\n    const box = wrap.current, cv = canvas.current;\n    if (!box || !cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const dpr = Math.min(2, window.devicePixelRatio || 1);\n    let W = 0, H = 0, h = 50, ox = 0, oy = 0;\n    const soft = document.createElement(\"canvas\"), sx = soft.getContext(\"2d\"); // cast shadows, drawn small and scaled up so they blur\n    const SH = 10;\n\n    // Three boards of maple, so neighbouring blocks are not cut from the same stick.\n    const boards = [3, 8, 13].map((seed, bi) => {\n      const c = document.createElement(\"canvas\"); c.width = c.height = T;\n      const x = c.getContext(\"2d\");\n      if (!x) return c;\n      const im = x.createImageData(T, T);\n      for (let py = 0; py < T; py++) for (let px = 0; px < T; px++) {\n        const g = bi === 1 ? py : px, a = bi === 1 ? px : py;\n        const warp = noise(g * 0.006, a * 0.03, seed) * 6 + noise(g * 0.03, a * 0.12, seed + 3) * 1.1;\n        const ring = Math.pow(0.5 + 0.5 * Math.sin((a * 0.16 + warp) * 2.3), 3);\n        const fibre = noise(g * 0.06, a * 1.9, seed + 9) - 0.5, tone = noise(g * 0.008, a * 0.02, seed + 5) - 0.5;\n        const k = 1 - 0.065 * ring - 0.05 * fibre + 0.07 * tone, i = (py * T + px) * 4;\n        im.data[i] = clamp(MAPLE[0] * k + 4 * ring, 0, 255); im.data[i + 1] = clamp(MAPLE[1] * k, 0, 255); im.data[i + 2] = clamp(MAPLE[2] * k - 8 * ring, 0, 255); im.data[i + 3] = 255;\n      }\n      x.putImageData(im, 0, 0);\n      return c;\n    });\n\n    // One printed face: raised paint on bare wood, with the chips a few years of play knock out of it.\n    const cache = new Map<string, HTMLCanvasElement>();\n    const faceTex = (ch: string, paint: string, solid: boolean, board: number) => {\n      const key = `${ch}${paint}${solid ? 1 : 0}${board}`, hit = cache.get(key);\n      if (hit) return hit;\n      const c = document.createElement(\"canvas\"); c.width = c.height = T;\n      const p = document.createElement(\"canvas\"); p.width = p.height = T;\n      const x = c.getContext(\"2d\"), y = p.getContext(\"2d\");\n      cache.set(key, c);\n      if (!x || !y) return c;\n      const rr = (g: CanvasRenderingContext2D, inset: number, r: number) => { g.beginPath(); if (g.roundRect) g.roundRect(inset, inset, T - inset * 2, T - inset * 2, r); else g.rect(inset, inset, T - inset * 2, T - inset * 2); };\n      const seed = ch.charCodeAt(0) + board * 31 + (solid ? 7 : 0);\n      y.font = `900 ${T * 0.66}px Georgia, \"Times New Roman\", serif`; y.textAlign = \"center\"; y.textBaseline = \"alphabetic\";\n      const m = y.measureText(ch), ty = T / 2 + ((m.actualBoundingBoxAscent || T * 0.45) - (m.actualBoundingBoxDescent || 0)) / 2;\n      y.fillStyle = paint; y.strokeStyle = paint;\n      if (solid) { rr(y, 15, 9); y.fill(); y.globalCompositeOperation = \"destination-out\"; y.fillText(ch, T / 2, ty); y.lineWidth = 2.4; rr(y, 25, 5); y.stroke(); }\n      else { y.lineWidth = 11; rr(y, 20, 9); y.stroke(); y.fillText(ch, T / 2, ty); }\n      // brushed gloss: paint is lighter where the light skims it\n      y.globalCompositeOperation = \"source-atop\";\n      const gl = y.createLinearGradient(0, 0, T, T);\n      gl.addColorStop(0, \"rgba(255,255,255,0.3)\"); gl.addColorStop(0.45, \"rgba(255,255,255,0.02)\"); gl.addColorStop(1, \"rgba(0,0,0,0.2)\");\n      y.fillStyle = gl; y.fillRect(0, 0, T, T);\n      for (let i = 0; i < 46; i++) { y.fillStyle = `rgba(${hash(i, 2, seed) > 0.5 ? \"255,255,255\" : \"0,0,0\"},0.05)`; y.fillRect(0, hash(i, 1, seed) * T, T, 1 + hash(i, 3, seed) * 3); }\n      // chips and scuffs, most of them along the edges where blocks knock together\n      y.globalCompositeOperation = \"destination-out\"; y.fillStyle = \"#000\";\n      for (let i = 0; i < 44; i++) {\n        const edge = hash(i, 4, seed) < 0.72, side = Math.floor(hash(i, 5, seed) * 4), along = 14 + hash(i, 6, seed) * (T - 28), off = 14 + hash(i, 7, seed) * 16;\n        const cx = edge ? (side === 0 ? off : side === 1 ? T - off : along) : 20 + hash(i, 8, seed) * (T - 40);\n        const cy = edge ? (side === 2 ? off : side === 3 ? T - off : along) : 20 + hash(i, 9, seed) * (T - 40);\n        const r = 1 + Math.pow(hash(i, 10, seed), 2.4) * (edge ? 7.5 : 4);\n        y.beginPath();\n        for (let k = 0; k < 7; k++) { const a = (k / 7) * Math.PI * 2, rad = r * (0.45 + hash(i, 11 + k, seed) * 0.7); if (k === 0) y.moveTo(cx + Math.cos(a) * rad, cy + Math.sin(a) * rad); else y.lineTo(cx + Math.cos(a) * rad, cy + Math.sin(a) * rad); }\n        y.closePath(); y.fill();\n      }\n      y.lineWidth = 0.9; y.strokeStyle = \"rgba(0,0,0,0.55)\";\n      for (let i = 0; i < 4; i++) { const ax = hash(i, 20, seed) * T, ay = hash(i, 21, seed) * T, ang = hash(i, 22, seed) * 6.3, len = 18 + hash(i, 23, seed) * 46; y.beginPath(); y.moveTo(ax, ay); y.lineTo(ax + Math.cos(ang) * len, ay + Math.sin(ang) * len); y.stroke(); }\n\n      x.save(); rr(x, 1.5, 15); x.clip();\n      x.drawImage(boards[board], 0, 0);\n      x.save(); x.shadowColor = \"rgba(62,32,8,0.6)\"; x.shadowBlur = 2.4; x.shadowOffsetX = 1.2; x.shadowOffsetY = 2; x.drawImage(p, 0, 0); x.restore();\n      x.globalAlpha = 0.3; x.globalCompositeOperation = \"lighter\"; x.drawImage(p, -0.9, -1.1); x.globalAlpha = 1; x.globalCompositeOperation = \"source-over\"; x.drawImage(p, 0, 0);\n      // corners worn round and a little darker with handling\n      const worn = x.createRadialGradient(T / 2, T / 2, T * 0.42, T / 2, T / 2, T * 0.74);\n      worn.addColorStop(0, \"rgba(90,50,14,0)\"); worn.addColorStop(1, \"rgba(90,50,14,0.34)\");\n      x.fillStyle = worn; x.fillRect(0, 0, T, T);\n      x.lineWidth = 3; x.strokeStyle = \"rgba(255,246,226,0.55)\"; x.beginPath(); x.moveTo(3, T - 16); x.lineTo(3, 16); x.quadraticCurveTo(3, 3, 16, 3); x.lineTo(T - 16, 3); x.stroke();\n      x.strokeStyle = \"rgba(70,36,8,0.4)\"; x.beginPath(); x.moveTo(T - 3, 16); x.lineTo(T - 3, T - 16); x.quadraticCurveTo(T - 3, T - 3, T - 16, T - 3); x.lineTo(16, T - 3); x.stroke();\n      x.restore();\n      return c;\n    };\n\n    const extra = \"QXZKJVW5M8R3GH2\";\n    const blocks: Block[] = Array.from({ length: MAX }, (_, i) => ({ on: false, p: [0, 0, 0], q: [0, 0, 0, 1], face: 0, chars: Array.from({ length: 6 }, (_, f) => extra[(i * 5 + f * 3) % extra.length]), anim: null, rest: 0 }));\n    const paintOf = (i: number, f: number) => PAINTS[(i * 2 + f * 5 + (f > 3 ? 1 : 0)) % PAINTS.length];\n    let current = \"\";\n\n    const slots = (n: number) => {\n      const bottom = n <= 4 ? n : Math.ceil(n / 2), top = n - bottom, gap = h * 2.2;\n      return Array.from({ length: n }, (_, i) => {\n        const up = i < top, idx = up ? i : i - top, cnt = up ? top : bottom;\n        return { p: [(idx - (cnt - 1) / 2) * gap + (hash(i, 3, 1) - 0.5) * 0.1 * h, up ? -3 * h : -h, (hash(i, 5, 2) - 0.5) * (up ? 0.16 : 0.36) * h] as V3, yaw: (hash(i, 7, 3) - 0.5) * (up ? 0.3 : 0.2) };\n      });\n    };\n    const hidden = (b: Block, yaw: Q) => { let best = 0, by = -9; FACES.forEach((f, g) => { const y = qrot(qmul(yaw, FRONT[b.face]), f.n)[1]; if (y > by) { by = y; best = g; } }); return best; };\n\n    const spell = (next: string, now: number, instant: boolean) => {\n      const S = slots(next.length);\n      blocks.forEach((b, i) => {\n        if (i < next.length) {\n          const s = S[i], yaw = qaxis(0, 1, 0, s.yaw);\n          if (!b.on) {\n            b.on = true; b.chars[b.face] = next[i]; b.rest = s.p[1];\n            const qb = qmul(yaw, FRONT[b.face]);\n            if (instant) { b.p = s.p; b.q = qb; b.anim = null; return; }\n            const ax: V3 = [hash(i, 1, now) - 0.5, hash(i, 2, now) - 0.5, hash(i, 3, now) - 0.2], l = Math.hypot(ax[0], ax[1], ax[2]) || 1;\n            b.p = [s.p[0] + (hash(i, 4, now) - 0.5) * h, -(oy + 3 * h) / CT, s.p[2]]; b.q = qb;\n            b.anim = { kind: \"drop\", start: now + 0.16 + i * 0.09, dur: 0.95, from: b.p, to: s.p, qa: qb, qb, spin: 2.2 + hash(i, 5, now), axis: [ax[0] / l, ax[1] / l, ax[2] / l], lift: 0 };\n          } else {\n            const changed = b.chars[b.face] !== next[i], moved = Math.hypot(b.p[0] - s.p[0], b.p[1] - s.p[1]) > 1;\n            if (!changed && !moved && !b.anim) return;\n            if (changed) { const g = hidden(b, yaw); b.chars[g] = next[i]; b.face = g; }\n            const qb = qmul(yaw, FRONT[b.face]); b.rest = s.p[1];\n            if (instant) { b.p = s.p; b.q = qb; b.anim = null; return; }\n            b.anim = { kind: \"hop\", start: now + (s.p[1] < -2 * h ? 0.2 : 0) + i * 0.06, dur: 0.72, from: b.p, to: s.p, qa: b.q, qb, spin: 0, axis: [1, 0, 0], lift: h * (1.2 + (i % 3) * 0.3) + Math.abs(b.p[0] - s.p[0]) * 0.18 };\n          }\n        } else if (b.on) {\n          if (instant) { b.on = false; b.anim = null; return; }\n          b.anim = { kind: \"leave\", start: now + (MAX - i) * 0.04, dur: 0.85, from: b.p, to: [W / 2 + 3 * h, -h, b.p[2] + h], qa: b.q, qb: b.q, spin: 0, axis: [0, 0, 1], lift: h * 1.3 };\n        }\n      });\n      current = next;\n    };\n\n    const advance = (b: Block, now: number) => {\n      const a = b.anim;\n      if (!a) return;\n      const u = clamp((now - a.start) / a.dur, 0, 1);\n      if (u <= 0) return;\n      const mixp = (e: number): V3 => [a.from[0] + (a.to[0] - a.from[0]) * e, a.from[1] + (a.to[1] - a.from[1]) * e, a.from[2] + (a.to[2] - a.from[2]) * e];\n      let rock = 0;\n      if (a.kind === \"hop\") {\n        const k = Math.min(1, u / 0.78), e = k * k * (3 - 2 * k), w = (u - 0.78) / 0.22;\n        b.p = mixp(e); b.p[1] -= a.lift * 4 * k * (1 - k);\n        if (u > 0.78) { b.p[1] = a.to[1] - 0.12 * h * Math.sin(Math.PI * w); rock = 0.05 * Math.sin(Math.PI * 2 * w) * (1 - w); }\n        b.q = qmul(qslerp(a.qa, a.qb, e), qaxis(a.axis[0], a.axis[1], a.axis[2], Math.PI * 2 * a.spin * (1 - e)));\n      } else if (a.kind === \"drop\") {\n        const k = Math.min(1, u / 0.56);\n        b.p = mixp(k); b.p[1] = a.from[1] + (a.to[1] - a.from[1]) * k * k;\n        if (u > 0.56 && u <= 0.84) { const w = (u - 0.56) / 0.28; b.p[1] = a.to[1] - 0.5 * h * 4 * w * (1 - w); rock = 0.1 * Math.sin(Math.PI * 2 * w); }\n        if (u > 0.84) { const w = (u - 0.84) / 0.16; b.p[1] = a.to[1] - 0.1 * h * 4 * w * (1 - w); rock = -0.03 * Math.sin(Math.PI * 2 * w); }\n        b.q = qmul(a.qb, qaxis(a.axis[0], a.axis[1], a.axis[2], a.spin * Math.PI * (1 - k) * (1 - k * 0.3)));\n      } else {\n        const e = u * u;\n        b.p = mixp(e); b.p[1] = a.from[1] + (a.to[1] - a.from[1]) * Math.min(1, u * 1.6) - a.lift * Math.sin(Math.PI * Math.min(1, u * 1.5)) * (1 - u * 0.4);\n        b.q = qmul(qaxis(0, 0, 1, Math.PI * 3 * e), a.qa);\n      }\n      if (rock) b.q = qmul(qaxis(0, 0, 1, rock), b.q);\n      if (u >= 1) { if (a.kind === \"leave\") b.on = false; else { b.p = a.to; b.q = a.qb; } b.anim = null; }\n    };\n\n    const draw = () => {\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.clearRect(0, 0, W, H);\n      const live = blocks.map((b, i) => ({ b, i, c: view(b.p) })).filter((o) => o.b.on).sort((m, n) => m.c[2] - n.c[2]);\n      // cast shadows: every corner slides down the light onto the table\n      if (sx) {\n        sx.setTransform(1, 0, 0, 1, 0, 0); sx.clearRect(0, 0, soft.width, soft.height);\n        for (const { b } of live) for (const f of FACES) {\n          // a block high in the air throws a weaker shadow\n          sx.fillStyle = `rgba(42,28,12,${clamp(1.1 - (b.rest - b.p[1]) / (h * 3.4), 0, 1)})`; sx.beginPath();\n          [[-1, -1], [1, -1], [1, 1], [-1, 1]].forEach(([su, sv], k) => {\n            const l = qrot(b.q, [(f.n[0] + f.u[0] * su + f.v[0] * sv) * h, (f.n[1] + f.u[1] * su + f.v[1] * sv) * h, (f.n[2] + f.u[2] * su + f.v[2] * sv) * h]);\n            const py = Math.min(0, b.p[1] + l[1]), s = view([b.p[0] + l[0] - py * (0.27 - sway * 0.5), 0, b.p[2] + l[2] - py * 0.3]);\n            const X = (ox + s[0]) / SH, Y = (oy + s[1]) / SH;\n            if (k === 0) sx.moveTo(X, Y); else sx.lineTo(X, Y);\n          });\n          sx.closePath(); sx.fill();\n        }\n        ctx.save(); ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = \"high\"; ctx.globalAlpha = 0.085;\n        ctx.drawImage(soft, -3, -2, W + 6, H + 4); ctx.drawImage(soft, 3, 2, W - 6, H - 4); ctx.drawImage(soft, 0, 0, W, H); ctx.restore();\n      }\n      for (const { b, i, c } of live) {\n        // where a block touches what is under it, the light cannot get in\n        const base = view([b.p[0], b.p[1] + h, b.p[2]]), near = clamp(1 - (b.rest - b.p[1]) / (h * 1.6), 0, 1);\n        ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n        if (near > 0.02) {\n          ctx.save(); ctx.translate(ox + base[0], oy + base[1] + h * 0.06); ctx.scale(1, 0.36);\n          const reach = b.rest < -1.5 * h ? h * 1.04 : h * 1.5, g = ctx.createRadialGradient(0, 0, h * 0.5, 0, 0, reach);\n          g.addColorStop(0, `rgba(48,28,8,${0.6 * near})`); g.addColorStop(1, \"rgba(48,28,8,0)\");\n          ctx.fillStyle = g; ctx.beginPath(); ctx.arc(0, 0, reach, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n        }\n        FACES.forEach((f, fi) => {\n          const nw = qrot(b.q, f.n), n = view(nw);\n          if (n[2] <= 0.004) return;\n          const u = view(qrot(b.q, f.u)), v = view(qrot(b.q, f.v)), k = (2 * h) / T;\n          ctx.setTransform(dpr * u[0] * k, dpr * u[1] * k, dpr * v[0] * k, dpr * v[1] * k, dpr * (ox + c[0] + h * (n[0] - u[0] - v[0])), dpr * (oy + c[1] + h * (n[1] - u[1] - v[1])));\n          ctx.fillStyle = \"#b98d55\"; ctx.fillRect(0, 0, T, T);\n          ctx.drawImage(faceTex(b.chars[fi], paintOf(i, fi), (i + fi) % 3 === 1, (i + (fi >> 1)) % 3), 0, 0);\n          const d = (nw[0] * (LIGHT[0] + sway) + nw[1] * LIGHT[1] + nw[2] * LIGHT[2]) - 0.46;\n          ctx.fillStyle = d > 0 ? `rgba(255,248,232,${Math.min(0.3, d * 0.5)})` : `rgba(36,18,4,${Math.min(0.55, -d * 0.5)})`;\n          ctx.fillRect(0, 0, T, T);\n        });\n      }\n    };\n\n    let sway = 0;\n    let raf = 0, seen = true, touched = -1e9, nextDemo = 4.6, demoAt = 0, t0 = 0, greeted = false;\n    const clock = () => performance.now() / 1000;\n    const frame = () => {\n      raf = 0;\n      const now = clock();\n      if (!t0) t0 = now;\n      const t = now - t0;\n      if (!greeted && t > 2.7) { greeted = true; flip(1, now); }\n      sway = Math.sin(t / 1.7) * 0.22;\n      if (DEMO && now - touched > 1e8 && t > nextDemo) { demoAt = (demoAt + 1) % WORDS.length; const w = clean(WORDS[demoAt]); spell(w, now, false); setWord(w); nextDemo = t + 3.4; }\n      blocks.forEach((b) => advance(b, now));\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) raf = requestAnimationFrame(frame); };\n    const flip = (i: number, now: number) => {\n      const b = blocks[i];\n      if (!b || !b.on || b.anim) return;\n      const ax: V3 = hash(i, now, 4) > 0.5 ? [1, 0, 0] : [0, 1, 0];\n      b.anim = { kind: \"hop\", start: now, dur: 0.8, from: b.p, to: [b.p[0], b.rest, b.p[2]], qa: b.q, qb: b.q, spin: hash(i, now, 5) > 0.5 ? 1 : -1, axis: ax, lift: h * 1.5 };\n    };\n\n    const layout = () => {\n      W = box.clientWidth; H = W < 560 ? 330 : 480;\n      h = Math.min(60, (W - 28) / 10); ox = W / 2 - h * 0.3; oy = H - (W < 560 ? 56 : 78);\n      cv.width = Math.round(W * dpr); cv.height = Math.round(H * dpr); cv.style.height = `${H}px`;\n      soft.width = Math.ceil(W / SH); soft.height = Math.ceil(H / SH);\n    };\n    layout();\n    spell(clean(WORDS[0]), clock(), true);\n    api.current = { spell: (w, fromUser) => { if (fromUser) touched = clock() + 1e9; if (w === current) return; spell(w, clock(), reduce); if (reduce) draw(); else wake(); } };\n    if (reduce) draw(); else wake();\n\n    const onDown = (e: PointerEvent) => {\n      const r = cv.getBoundingClientRect(), px = e.clientX - r.left - ox, py = e.clientY - r.top - oy;\n      let pick = -1, depth = -1e9;\n      blocks.forEach((b, i) => { const c = view(b.p); if (b.on && Math.abs(c[0] - px) < h && Math.abs(c[1] - py) < h && c[2] > depth) { depth = c[2]; pick = i; } });\n      if (pick >= 0 && !reduce) { flip(pick, clock()); wake(); }\n    };\n    cv.addEventListener(\"pointerdown\", onDown);\n    let lastW = W;\n    const ro = new ResizeObserver(() => { if (box.clientWidth !== lastW) { lastW = box.clientWidth; layout(); spell(current, clock(), true); blocks.forEach((b, i) => { if (i >= current.length) b.on = false; }); draw(); } });\n    ro.observe(box);\n    const io = new IntersectionObserver(([entry]) => { seen = entry.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    return () => {\n      cancelAnimationFrame(raf); api.current = null;\n      cv.removeEventListener(\"pointerdown\", onDown);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const type = (raw: string) => { const w = clean(raw); setWord(w); api.current?.spell(w, true); };\n\n  return (\n    <div className=\"flex min-h-[620px] w-full flex-col items-center justify-center bg-[#ece2cf] bg-[radial-gradient(90%_70%_at_44%_38%,#f8f1e3_0%,#ece2cf_56%,#ddd0b8_100%)] px-3 py-10 sm:min-h-[720px] sm:px-8\">\n      <div ref={wrap} className=\"w-full max-w-[760px]\">\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"block w-full cursor-pointer touch-manipulation select-none\" />\n      </div>\n      <div className=\"mt-2 flex flex-col items-center\">\n        <label htmlFor=\"alphabet-blocks-word\" className=\"font-mono text-[11px] font-semibold uppercase tracking-[0.2em] text-stone-600\">\n          {LABEL}\n        </label>\n        <input\n          id=\"alphabet-blocks-word\"\n          type=\"text\"\n          value={word}\n          maxLength={MAX}\n          autoComplete=\"off\"\n          autoCapitalize=\"characters\"\n          spellCheck={false}\n          onChange={(e) => type(e.target.value)}\n          onFocus={() => api.current?.spell(word, true)}\n          placeholder=\"Up to 8 letters\"\n          className=\"mt-3 h-12 w-[264px] max-w-full rounded-full border border-stone-400/70 bg-[#fbf7ee] pl-[0.34em] text-center font-mono text-lg font-semibold uppercase tracking-[0.34em] text-stone-900 shadow-[inset_0_2px_4px_rgba(90,60,20,0.14)] outline-none placeholder:text-[11px] placeholder:font-medium placeholder:tracking-[0.18em] placeholder:text-stone-500 focus-visible:ring-2 focus-visible:ring-stone-900 focus-visible:ring-offset-2 focus-visible:ring-offset-[#ece2cf]\"\n        />\n        <p className=\"mt-3 font-mono text-[11px] uppercase tracking-[0.16em] text-stone-600\">Tap a block to toss it</p>\n      </div>\n    </div>\n  );\n}\n"}]}
