{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"velvet-ring-box-with-a-sprung-hinged-lid-and-a-diamond-solitaire-that-flashes-under-a-moving-light","type":"registry:block","title":"Velvet Ring Box with a Sprung Hinged Lid and a Diamond Solitaire That Flashes Under a Moving Light","description":"Ring box on a canvas: a velvet case whose sprung lid snaps open on a solitaire, with a diamond that flashes as the light moves. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/velvet-ring-box-with-a-sprung-hinged-lid-and-a-diamond-solitaire-that-flashes-under-a-moving-light.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// The piece in the box. Change the words here; the foil stamp inside the lid uses `maker`.\nconst piece = {\n  name: \"Halcyon solitaire\",\n  detail: \"1.2 ct round brilliant on 18k yellow gold\",\n  price: \"£4,850\",\n  maker: \"ORREL & VANE\",\n  makerLine: \"GOLDSMITHS\",\n};\nconst VELVET: [number, number, number] = [15, 58, 49]; // the flock on the outside of the box\nconst OPEN_ANGLE = 1.76; // radians the lid rests at when open (about 100 degrees)\nconst VW = 520, VH = 580; // drawing space; the canvas scales this to its width\n\ntype V3 = [number, number, number];\n\nexport default function VelvetRingBox() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const hit = useRef<HTMLButtonElement>(null);\n  const [open, setOpen] = useState(true);\n  const openRef = useRef(true);\n  const api = useRef<{ toggle: () => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, pad = hit.current;\n    if (!cv || !pad) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    let scale = 1;\n\n    // Box measurements in millimetre-like units.\n    const A = 100, RAD = 20, SEG = 6, HB = 64, HL = 48, WALL = 9;\n    const PHI = 0.53, CP = Math.cos(PHI), SP = Math.sin(PHI), K = 1.3, F = 1500, OX = VW / 2 + 6, OY = 452;\n    const TAU = 0.42; // how far the ring leans toward you on its cushion\n    const RING = 37, TUBE = 4.3, GEM = 26;\n\n    const foot: { x: number; z: number; nx: number; nz: number }[] = [];\n    for (let q = 0; q < 4; q++) for (let i = 0; i <= SEG; i++) {\n      const a = (q * 90 + (i * 90) / SEG) * (Math.PI / 180), mid = (q * 90 + 45) * (Math.PI / 180);\n      const nx = Math.cos(a), nz = Math.sin(a);\n      foot.push({ x: Math.sign(Math.cos(mid)) * (A - RAD) + nx * RAD, z: Math.sign(Math.sin(mid)) * (A - RAD) + nz * RAD, nx, nz });\n    }\n    const N = foot.length;\n\n    // Flock grain, drawn once.\n    const grainCv = document.createElement(\"canvas\"); grainCv.width = grainCv.height = 128;\n    const gx = grainCv.getContext(\"2d\");\n    if (gx) {\n      let seed = 11; const rnd = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n      for (let i = 0; i < 9000; i++) { const v = rnd(); gx.fillStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.3})` : `rgba(0,0,0,${(0.5 - v) * 0.46})`; gx.fillRect(rnd() * 128, rnd() * 128, 0.6 + rnd() * 0.7, 0.6 + rnd() * 0.7); }\n    }\n    const grain = ctx.createPattern(grainCv, \"repeat\");\n\n    // One seeded number per facet so the stone sparkles the same way on every load.\n    const fac: { k: number; p: number; fire: number }[] = [];\n    { let seed = 5; const rnd = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; }; for (let i = 0; i < 64; i++) fac.push({ k: 5 + rnd() * 9, p: rnd() * 6.28, fire: rnd() }); }\n\n    const s = { th: OPEN_ANGLE, v: 0, drag: false, lx: -0.35, ly: 0.1, lxTo: -0.35, lyTo: 0.1, yaw: -0.22, thud: 0 };\n    let Lx = 0, Ly = 1, Lz = 0, Vx = 0, Vy = 0, Vz = 1, cyw = 1, syw = 0, cth = 1, sth = 0;\n\n    const P = (p: V3): V3 => {\n      const xr = p[0] * cyw + p[2] * syw, zr = -p[0] * syw + p[2] * cyw;\n      const d = zr * CP + p[1] * SP, k = (K * F) / (F - d);\n      return [OX + xr * k, OY - (p[1] * CP - zr * SP) * k, d];\n    };\n    // Swing a point of the lid about the hinge along the back edge.\n    const LID = (p: V3): V3 => [p[0], HB + (p[1] - HB) * cth + (p[2] + A) * sth, -A + (p[2] + A) * cth - (p[1] - HB) * sth];\n    const LIDN = (n: V3): V3 => [n[0], n[1] * cth + n[2] * sth, n[2] * cth - n[1] * sth];\n    const ID = (p: V3): V3 => p;\n    const facing = (n: V3) => n[0] * Vx + n[1] * Vy + n[2] * Vz;\n    const clamp = (v: number) => Math.max(0, Math.min(255, v)) | 0;\n    // Velvet goes pale where you see it edge on and stays dark where you look straight into the pile.\n    const velvet = (n: V3, add = 0) => {\n      const ndl = Math.max(0, n[0] * Lx + n[1] * Ly + n[2] * Lz), edge = Math.pow(1 - Math.abs(facing(n)), 2.4);\n      return 0.5 + 0.62 * ndl + 1.5 * edge + add;\n    };\n    const tone = (v: number) => `rgb(${clamp(VELVET[0] * v + Math.max(0, v - 1) * 34)},${clamp(VELVET[1] * v + Math.max(0, v - 1) * 40)},${clamp(VELVET[2] * v + Math.max(0, v - 1) * 40)})`;\n    const path = (pts: V3[]) => { ctx.beginPath(); pts.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.closePath(); };\n    const fill = (pts: V3[], style: string | CanvasGradient, flock = true) => {\n      path(pts); ctx.fillStyle = style; ctx.fill(); ctx.lineWidth = 0.7; ctx.strokeStyle = style; ctx.stroke();\n      if (flock && grain) { ctx.save(); ctx.globalAlpha = 0.55; ctx.fillStyle = grain; ctx.fill(); ctx.restore(); }\n    };\n    const lin = (a: V3, b: V3, stops: [number, string][]) => { const g = ctx.createLinearGradient(a[0], a[1], b[0], b[1]); for (const [o, c] of stops) g.addColorStop(o, c); return g; };\n    const ring = (y: number, inset: number, T: (p: V3) => V3) => foot.map((f) => P(T([f.x - f.nx * inset, y, f.z - f.nz * inset])));\n    const gold = (a: V3, b: V3) => lin(a, b, [[0, \"#6d4710\"], [0.22, \"#e9c46a\"], [0.4, \"#fff6d2\"], [0.55, \"#c9953a\"], [0.8, \"#7a5215\"], [1, \"#e2b85c\"]]);\n\n    // The upright walls of either half: one strip per edge of the footprint, lit by its own normal.\n    const walls = (y0: number, y1: number, T: (p: V3) => V3, TN: (n: V3) => V3, bevel: number) => {\n      for (let i = 0; i < N; i++) {\n        const a = foot[i], b = foot[(i + 1) % N];\n        const n = TN([(a.nx + b.nx) / 2, 0, (a.nz + b.nz) / 2]);\n        if (facing(n) <= 0.001) continue;\n        const p0 = P(T([a.x, y0, a.z])), p1 = P(T([b.x, y0, b.z])), p2 = P(T([b.x, y1, b.z])), p3 = P(T([a.x, y1, a.z]));\n        const v = velvet(n);\n        fill([p0, p1, p2, p3], lin(p3, p0, [[0, tone(v + 0.2)], [0.12, tone(v)], [0.8, tone(v - 0.1)], [1, tone(v - 0.3)]]));\n        if (bevel) {\n          const nb = TN([n[0] * 0.7, 0.7, n[2] * 0.7]);\n          const q2 = P(T([b.x - b.nx * bevel, y1 + bevel, b.z - b.nz * bevel])), q3 = P(T([a.x - a.nx * bevel, y1 + bevel, a.z - a.nz * bevel]));\n          fill([p3, p2, q2, q3], tone(velvet(nb, 0.16)));\n        }\n      }\n    };\n\n    const drawLid = () => {\n      const up = LIDN([0, 1, 0]), down: V3 = [-up[0], -up[1], -up[2]];\n      // outside\n      walls(HB, HB + HL - 7, LID, LIDN, 7);\n      if (facing(up) > 0.001) {\n        const top = ring(HB + HL, 7, LID), v = velvet(up);\n        const b = P(LID([0, HB + HL, -A])), f = P(LID([0, HB + HL, A]));\n        fill(top, lin(b, f, [[0, tone(v + 0.26)], [0.5, tone(v)], [1, tone(v - 0.12)]]));\n        // a soft bloom where the lamp lands on the pile\n        const c = P(LID([Lx * 60, HB + HL, -Lz * 30]));\n        const g = ctx.createRadialGradient(c[0], c[1], 0, c[0], c[1], 150);\n        g.addColorStop(0, \"rgba(190,255,235,0.16)\"); g.addColorStop(1, \"rgba(190,255,235,0)\");\n        path(top); ctx.fillStyle = g; ctx.fill();\n      }\n      if (facing(down) <= 0.001) return;\n      // inside, seen through the mouth of the lid\n      const mouthIn = ring(HB, WALL, LID), mouthOut = ring(HB, 0, LID);\n      const yT = HB + HL - 8;\n      ctx.save(); path(mouthIn); ctx.clip();\n      const inner = ring(yT, WALL, LID);\n      const b = P(LID([0, yT, -A])), f = P(LID([0, yT, A]));\n      fill(inner, lin(b, f, [[0, \"#cfc4b0\"], [0.2, \"#ece4d5\"], [0.55, \"#fbf6ec\"], [0.86, \"#e6dccb\"], [1, \"#c2b6a1\"]]), false);\n      // satin: a broad sheen that slides as the lamp moves, and a pillowed middle\n      const c = P(LID([Lx * 70, yT, 10 + Ly * 40]));\n      const sh = ctx.createRadialGradient(c[0], c[1], 0, c[0], c[1], 170);\n      sh.addColorStop(0, \"rgba(255,255,255,0.75)\"); sh.addColorStop(0.5, \"rgba(255,255,255,0.12)\"); sh.addColorStop(1, \"rgba(120,100,70,0.2)\");\n      path(inner); ctx.fillStyle = sh; ctx.fill();\n      for (let i = 0; i < N; i++) {\n        const a0 = foot[i], b0 = foot[(i + 1) % N];\n        const n = LIDN([-(a0.nx + b0.nx) / 2, 0, -(a0.nz + b0.nz) / 2]);\n        if (facing(n) <= 0.001) continue;\n        const lit = 0.55 + 0.45 * Math.max(0, n[0] * Lx + n[1] * Ly + n[2] * Lz);\n        const p0 = P(LID([a0.x - a0.nx * WALL, HB, a0.z - a0.nz * WALL])), p1 = P(LID([b0.x - b0.nx * WALL, HB, b0.z - b0.nz * WALL]));\n        const p2 = P(LID([b0.x - b0.nx * WALL, yT, b0.z - b0.nz * WALL])), p3 = P(LID([a0.x - a0.nx * WALL, yT, a0.z - a0.nz * WALL]));\n        fill([p0, p1, p2, p3], lin(p0, p3, [[0, `rgb(${clamp(236 * lit)},${clamp(228 * lit)},${clamp(212 * lit)})`], [1, `rgb(${clamp(176 * lit)},${clamp(166 * lit)},${clamp(148 * lit)})`]]), false);\n      }\n      // foil stamp, set in the plane of the lining\n      const o = P(LID([0, yT, 0])), ux = P(LID([1, yT, 0])), uz = P(LID([0, yT, 1]));\n      ctx.save();\n      ctx.transform(ux[0] - o[0], ux[1] - o[1], -(uz[0] - o[0]), -(uz[1] - o[1]), o[0], o[1]);\n      ctx.textAlign = \"center\"; ctx.textBaseline = \"middle\";\n      const spaced = (text: string, y: number, size: number, gap: number, weight: number) => {\n        ctx.font = `${weight} ${size}px \"Didot\",\"Bodoni MT\",\"Playfair Display\",Georgia,\"Times New Roman\",serif`;\n        const w = [...text].reduce((t, ch) => t + ctx.measureText(ch).width + gap, -gap);\n        let x = -w / 2;\n        const foil = ctx.createLinearGradient(-w / 2 + Lx * 60, 0, w / 2 + Lx * 60, 0);\n        foil.addColorStop(0, \"#8a6420\"); foil.addColorStop(0.35, \"#c9a04a\"); foil.addColorStop(0.5, \"#f1dc9a\"); foil.addColorStop(0.65, \"#b58a34\"); foil.addColorStop(1, \"#8a6420\");\n        for (const ch of text) { const cw = ctx.measureText(ch).width; ctx.fillStyle = \"rgba(255,255,255,0.7)\"; ctx.fillText(ch, x + cw / 2, y + 0.5); ctx.fillStyle = foil; ctx.fillText(ch, x + cw / 2, y); x += cw + gap; }\n      };\n      spaced(piece.maker, -8, 13.5, 4.2, 500);\n      spaced(piece.makerLine, 11, 6.5, 3.4, 600);\n      ctx.fillStyle = \"#b58a34\"; ctx.fillRect(-13, 1.6, 26, 0.6);\n      ctx.restore();\n      ctx.restore();\n      // the velvet lip round the mouth and the thin brass frame\n      const v = velvet(down, 0.1);\n      path(mouthOut); path2(mouthIn); ctx.fillStyle = tone(v); ctx.fill(\"evenodd\");\n      if (grain) { ctx.save(); ctx.globalAlpha = 0.5; ctx.fillStyle = grain; ctx.fill(\"evenodd\"); ctx.restore(); }\n      ctx.lineWidth = 1.1; ctx.strokeStyle = \"rgba(226,190,110,0.85)\"; path(mouthIn); ctx.stroke();\n    };\n    const path2 = (pts: V3[]) => { pts.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.closePath(); };\n\n    const drawBase = () => {\n      walls(0, HB, ID, ID, 0);\n      // brass press stud on the front\n      const stud = P([0, HB - 15, A]);\n      if (Vz > 0) {\n        const r = 5.2 * K;\n        ctx.beginPath(); ctx.ellipse(stud[0], stud[1] + 1.4, r + 1, r + 1, 0, 0, 6.3); ctx.fillStyle = \"rgba(0,0,0,0.45)\"; ctx.fill();\n        const g = ctx.createRadialGradient(stud[0] - r * 0.35 + Lx * 2, stud[1] - r * 0.4, 0.5, stud[0], stud[1], r);\n        g.addColorStop(0, \"#fff8dc\"); g.addColorStop(0.35, \"#e3bb5e\"); g.addColorStop(0.8, \"#8b5f17\"); g.addColorStop(1, \"#5a3b0b\");\n        ctx.beginPath(); ctx.arc(stud[0], stud[1], r, 0, 6.3); ctx.fillStyle = g; ctx.fill();\n      }\n      const out = ring(HB, 0, ID), inn = ring(HB, WALL, ID);\n      const v = velvet([0, 1, 0]);\n      path(out); path2(inn); ctx.fillStyle = tone(v + 0.1); ctx.fill(\"evenodd\");\n      if (grain) { ctx.save(); ctx.globalAlpha = 0.5; ctx.fillStyle = grain; ctx.fill(\"evenodd\"); ctx.restore(); }\n      // cushion: two rolls of suede that meet at the slot\n      const yP = HB - 3;\n      ctx.save(); path(inn); ctx.clip();\n      const mid = P([0, yP, 0]), fr = P([0, yP, A - WALL]), bk = P([0, yP, -A + WALL]);\n      const roll: [number, string][] = [[0, \"#a59a86\"], [0.04, \"#d9d0bf\"], [0.16, \"#f3ede1\"], [0.45, \"#fbf8f0\"], [0.82, \"#ece4d6\"], [1, \"#c4b9a5\"]];\n      const cut = [P([-A, yP, 0]), P([A, yP, 0])];\n      ctx.save(); path([cut[0], cut[1], P([A, yP, A]), P([-A, yP, A])]); ctx.clip(); path(inn); ctx.fillStyle = lin(mid, fr, roll); ctx.fill(); ctx.restore();\n      ctx.save(); path([cut[0], cut[1], P([A, yP, -A]), P([-A, yP, -A])]); ctx.clip(); path(inn); ctx.fillStyle = lin(mid, bk, roll); ctx.fill(); ctx.restore();\n      const l = P([-A + WALL, yP, 0]), r = P([A - WALL, yP, 0]);\n      path(inn); ctx.fillStyle = lin(l, r, [[0, \"rgba(80,66,44,0.42)\"], [0.14, \"rgba(80,66,44,0)\"], [0.86, \"rgba(80,66,44,0)\"], [1, \"rgba(80,66,44,0.42)\"]]); ctx.fill();\n      // the lamp pools on the suede\n      const pool = P([Lx * 55, yP, 30 - Lz * 20]);\n      const pg = ctx.createRadialGradient(pool[0], pool[1], 0, pool[0], pool[1], 120);\n      pg.addColorStop(0, \"rgba(255,255,255,0.42)\"); pg.addColorStop(1, \"rgba(255,255,255,0)\");\n      path(inn); ctx.fillStyle = pg; ctx.fill();\n      if (grain) { ctx.globalAlpha = 0.14; ctx.fillStyle = grain; ctx.fill(); ctx.globalAlpha = 1; }\n      ctx.lineCap = \"round\"; ctx.lineWidth = 2.2; ctx.strokeStyle = \"rgba(52,42,26,0.78)\";\n      ctx.beginPath(); ctx.moveTo(P([-64, yP, 0])[0], P([-64, yP, 0])[1]); ctx.lineTo(P([64, yP, 0])[0], P([64, yP, 0])[1]); ctx.stroke();\n      // shade from the lid when it hangs over the cushion\n      const cover = Math.max(0, 1 - s.th / 1.1);\n      if (cover > 0) { path(inn); ctx.fillStyle = `rgba(20,24,20,${cover * 0.72})`; ctx.fill(); }\n      ctx.restore();\n      ctx.lineWidth = 1.1; ctx.strokeStyle = \"rgba(226,190,110,0.9)\"; path(inn); ctx.stroke();\n    };\n\n    const drawRing = () => {\n      const yP = HB - 3, cB = RING * 0.36;\n      const cT = Math.cos(TAU), sT = Math.sin(TAU);\n      const W = (a: number, b: number, c = 0): V3 => [a, yP + b * cT - c * sT, b * sT + c * cT]; // c runs toward you, square to the ring\n      const lightDim = Math.min(1, 0.25 + s.th / 0.9);\n      // shadow of the ring on the cushion\n      const sp = P([-Lx * 26, yP, 12 - Lz * 8]);\n      ctx.save(); ctx.translate(sp[0], sp[1]); ctx.scale(1, 0.34);\n      const sg = ctx.createRadialGradient(0, 0, 4, 0, 0, 62);\n      sg.addColorStop(0, \"rgba(46,36,22,0.5)\"); sg.addColorStop(1, \"rgba(46,36,22,0)\");\n      ctx.fillStyle = sg; ctx.beginPath(); ctx.arc(0, 0, 62, 0, 6.3); ctx.fill(); ctx.restore();\n\n      const centre = P(W(0, cB));\n      const arc = (rad: number, from = -0.1) => {\n        ctx.beginPath(); let first = true;\n        for (let i = 0; i <= 64; i++) {\n          const t = (i / 64) * Math.PI * 2, b = cB + Math.sin(t) * rad;\n          if (b < from) { first = true; continue; }\n          const p = P(W(Math.cos(t) * rad, b));\n          if (first) { ctx.moveTo(p[0], p[1]); first = false; } else ctx.lineTo(p[0], p[1]);\n        }\n      };\n      const la = Math.atan2(-Ly * 0.6 - 0.6, Lx);\n      ctx.lineCap = \"butt\";\n      const w = TUBE * 2 * K;\n      arc(RING); ctx.lineWidth = w + 1.6; ctx.strokeStyle = \"#4a2f07\"; ctx.stroke();\n      if (typeof ctx.createConicGradient === \"function\") {\n        const cg = ctx.createConicGradient(la, centre[0], centre[1]);\n        const stops: [number, string][] = [[0, \"#fff7d6\"], [0.05, \"#f0cf7c\"], [0.13, \"#b9842a\"], [0.2, \"#7b5010\"], [0.3, \"#d8ac50\"], [0.38, \"#fbe9ae\"], [0.46, \"#a8741f\"], [0.56, \"#6e470d\"], [0.66, \"#c99b40\"], [0.74, \"#f6dc92\"], [0.82, \"#9a6a1b\"], [0.9, \"#d2a548\"], [1, \"#fff7d6\"]];\n        for (const [o, c] of stops) cg.addColorStop(o, c);\n        ctx.strokeStyle = cg;\n      } else ctx.strokeStyle = \"#d8ac50\";\n      arc(RING); ctx.lineWidth = w; ctx.stroke();\n      arc(RING - TUBE * 0.62); ctx.lineWidth = w * 0.26; ctx.strokeStyle = \"rgba(70,40,4,0.55)\"; ctx.stroke();\n      arc(RING + TUBE * 0.36); ctx.lineWidth = w * 0.17; ctx.strokeStyle = \"rgba(255,252,236,0.82)\"; ctx.stroke();\n      if (lightDim < 1) { arc(RING); ctx.lineWidth = w + 1.6; ctx.strokeStyle = `rgba(14,20,16,${(1 - lightDim) * 0.7})`; ctx.stroke(); }\n\n      // the head: a tapered collet and four claws\n      const gb = cB + RING + TUBE + 10.5; // height of the girdle above the slot\n      const G = (u: number, v2: number, h: number): V3 => W(u, gb + h, v2);\n      const c0 = P(G(-5, 0, -10.6)), c1 = P(G(5, 0, -10.6)), c2 = P(G(GEM * 0.82, 0, -0.5)), c3 = P(G(-GEM * 0.82, 0, -0.5));\n      path([c0, c1, c2, c3]); ctx.fillStyle = gold(c3, c2); ctx.fill();\n      ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(60,36,4,0.7)\"; ctx.stroke();\n      const claws = [45, 135, 225, 315].map((d) => { const a = (d * Math.PI) / 180; return { u: Math.cos(a), v: Math.sin(a) }; });\n      const claw = (c: { u: number; v: number }) => {\n        const a = P(G(c.u * GEM * 0.62, c.v * GEM * 0.62, -6.5)), b = P(G(c.u * GEM * 1.02, c.v * GEM * 1.02, -0.4)), t = P(G(c.u * GEM * 0.9, c.v * GEM * 0.9, 3.6));\n        ctx.lineCap = \"round\"; ctx.lineJoin = \"round\";\n        ctx.beginPath(); ctx.moveTo(a[0], a[1]); ctx.lineTo(b[0], b[1]); ctx.lineTo(t[0], t[1]);\n        ctx.lineWidth = 3.4 * K; ctx.strokeStyle = \"#5a3b0b\"; ctx.stroke();\n        ctx.lineWidth = 2.3 * K; ctx.strokeStyle = \"#d9ae54\"; ctx.stroke();\n        ctx.lineWidth = 0.7 * K; ctx.strokeStyle = \"rgba(255,250,225,0.85)\"; ctx.stroke();\n      };\n      claws.filter((c) => c.v < 0).forEach(claw);\n\n      // the stone: 33 crown facets, each flashing on its own schedule as the lamp moves\n      const pt = (ang: number, rad: number, h: number) => G(Math.cos(ang) * rad, Math.sin(ang) * rad, h);\n      const T: V3[] = [], S: V3[] = [], Gd: V3[] = [];\n      for (let i = 0; i < 8; i++) { T.push(pt((i * Math.PI) / 4, GEM * 0.56, 7.4)); S.push(pt(((i + 0.5) * Math.PI) / 4, GEM * 0.8, 4)); }\n      for (let i = 0; i < 16; i++) Gd.push(pt((i * Math.PI) / 8, GEM, 0));\n      const facets: V3[][] = [];\n      for (let i = 0; i < 8; i++) {\n        const j = (i + 1) % 8;\n        facets.push([T[i], T[j], S[i]]);\n        facets.push([T[i], S[i], Gd[(2 * i + 1) % 16], Gd[2 * i]]);\n        facets.push([T[i], Gd[2 * i], Gd[(2 * i + 15) % 16], S[(i + 7) % 8]]);\n        facets.push([S[i], T[j], Gd[(2 * i + 2) % 16], Gd[(2 * i + 1) % 16]]);\n      }\n      const tc = G(0, 0, 7.4);\n      for (let i = 0; i < 8; i++) {\n        const j = (i + 1) % 8, m: V3 = [(T[i][0] + T[j][0]) / 2, (T[i][1] + T[j][1]) / 2, (T[i][2] + T[j][2]) / 2];\n        facets.push([tc, T[i], m]); facets.push([tc, m, T[j]]);\n      }\n      const girdle = Gd.map(P);\n      path(girdle); ctx.fillStyle = \"#10151f\"; ctx.fill(); ctx.lineWidth = 2.4; ctx.strokeStyle = \"rgba(150,170,200,0.9)\"; ctx.stroke();\n      const drive = Lx * 2.3 + Ly * 1.7 + s.yaw * 4;\n      let best = 0, bx = 0, by = 0;\n      facets.forEach((f, i) => {\n        const q = f.map(P), fc = fac[i];\n        const wave = 0.5 + 0.5 * Math.sin(fc.k * drive + fc.p);\n        const bright = Math.pow(wave, 2.1) * lightDim;\n        let r = 44 + 211 * bright, g = 54 + 201 * bright, b = 74 + 181 * bright;\n        if (fc.fire < 0.34 && wave > 0.35) {\n          const hue = fc.p + wave * 7, mix = 0.24 * Math.sin(Math.min(1, (wave - 0.35) * 1.6) * Math.PI);\n          r += (128 + 127 * Math.sin(hue) - r) * mix; g += (128 + 127 * Math.sin(hue + 2.1) - g) * mix; b += (128 + 127 * Math.sin(hue + 4.2) - b) * mix;\n        }\n        const col = `rgb(${clamp(r)},${clamp(g)},${clamp(b)})`;\n        path(q); ctx.fillStyle = col; ctx.fill(); ctx.lineWidth = 0.45; ctx.strokeStyle = \"rgba(235,244,255,0.42)\"; ctx.stroke();\n        if (bright > best) { best = bright; bx = q.reduce((t, p) => t + p[0], 0) / q.length; by = q.reduce((t, p) => t + p[1], 0) / q.length; }\n      });\n      // the table also mirrors the lamp as one soft pane\n      const tab = T.map(P), tcp = P(tc);\n      ctx.save(); path(tab); ctx.clip();\n      const pane = ctx.createLinearGradient(tcp[0] - 16 + Lx * 14, tcp[1] - 12, tcp[0] + 10 + Lx * 14, tcp[1] + 12);\n      pane.addColorStop(0, \"rgba(255,255,255,0)\"); pane.addColorStop(0.45, `rgba(255,255,255,${0.5 * lightDim})`); pane.addColorStop(0.55, `rgba(255,255,255,${0.1 * lightDim})`); pane.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = pane; ctx.fillRect(tcp[0] - 40, tcp[1] - 40, 80, 80); ctx.restore();\n      path(girdle); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(210,225,245,0.75)\"; ctx.stroke();\n      claws.filter((c) => c.v >= 0).forEach(claw);\n      claws.forEach((c) => {\n        const t = P(G(c.u * GEM * 0.9, c.v * GEM * 0.9, 3.9)), r = 2.5 * K;\n        const g = ctx.createRadialGradient(t[0] - r * 0.35, t[1] - r * 0.4, 0.3, t[0], t[1], r);\n        g.addColorStop(0, \"#fffbe6\"); g.addColorStop(0.4, \"#e2b95c\"); g.addColorStop(1, \"#6a450e\");\n        ctx.beginPath(); ctx.arc(t[0], t[1], r, 0, 6.3); ctx.fillStyle = g; ctx.fill();\n      });\n      // the flash itself spills past the stone\n      if (best > 0.55) {\n        const a = (best - 0.55) / 0.45;\n        ctx.save(); ctx.globalCompositeOperation = \"lighter\";\n        const bl = ctx.createRadialGradient(bx, by, 0, bx, by, 34 * K);\n        bl.addColorStop(0, `rgba(255,255,255,${0.95 * a})`); bl.addColorStop(0.14, `rgba(235,244,255,${0.42 * a})`); bl.addColorStop(1, \"rgba(220,235,255,0)\");\n        ctx.fillStyle = bl; ctx.beginPath(); ctx.arc(bx, by, 34 * K, 0, 6.3); ctx.fill();\n        ctx.translate(bx, by); ctx.scale(1, 0.035);\n        const st = ctx.createRadialGradient(0, 0, 0, 0, 0, 60 * K);\n        st.addColorStop(0, `rgba(255,255,255,${0.5 * a})`); st.addColorStop(1, \"rgba(255,255,255,0)\");\n        ctx.fillStyle = st; ctx.beginPath(); ctx.arc(0, 0, 60 * K, 0, 6.3); ctx.fill();\n        ctx.restore();\n      }\n    };\n\n    const drawShadow = () => {\n      // The box and its lid thrown onto the table along the lamp's direction.\n      const drop = (p: V3): V3 => { const k = p[1] / Math.max(0.35, Ly); return P([p[0] - Lx * k, 0, p[2] - Lz * k]); };\n      const shade = (pts: V3[], blur: number, alpha: number) => {\n        ctx.save(); ctx.shadowColor = `rgba(46,34,20,${alpha})`; ctx.shadowBlur = blur * scale; ctx.shadowOffsetX = 3000 * scale; ctx.translate(-3000, 0);\n        path(pts); ctx.fillStyle = \"#000\"; ctx.fill(); ctx.restore();\n      };\n      shade(foot.map((f) => P([f.x * 1.04 - Lx * 8, 0, f.z * 1.03 + 7])), 13, 0.85);\n      shade(foot.map((f) => P([f.x * 1.2 - Lx * 20, 0, f.z * 1.16 + 6])), 46, 0.3);\n      shade(foot.map((f) => drop([f.x, HB, f.z])), 26, 0.46);\n      shade(foot.map((f) => drop(LID([f.x, HB + HL, f.z]))), 34, 0.3);\n      shade(foot.map((f) => drop(LID([f.x, HB, f.z]))), 34, 0.22);\n    };\n\n    const draw = () => {\n      ctx.setTransform(scale, 0, 0, scale, 0, 0);\n      ctx.clearRect(0, 0, VW, VH);\n      cyw = Math.cos(s.yaw); syw = Math.sin(s.yaw); cth = Math.cos(s.th); sth = Math.sin(s.th);\n      Vx = -syw * CP; Vy = SP; Vz = cyw * CP;\n      const lx = -0.5 + s.lx * 0.75, ly = 0.95 + s.ly * 0.2, lz = 0.62, ll = Math.hypot(lx, ly, lz);\n      Lx = lx / ll; Ly = ly / ll; Lz = lz / ll;\n      ctx.save(); ctx.translate(0, s.thud);\n      drawShadow();\n      if (s.th > 0.86) { drawLid(); drawBase(); drawRing(); }\n      else { drawBase(); if (s.th > 0.02) drawRing(); drawLid(); }\n      ctx.restore();\n    };\n\n    const fit = () => {\n      const w = cv.clientWidth; if (!w) return;\n      const dpr = Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(w * dpr); cv.height = Math.round(((w * VH) / VW) * dpr); scale = cv.width / VW; draw();\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, touched = -1e9, nextDemo = 4200, dragY = 0, dragTh = 0, moved = 0;\n    const want = () => (openRef.current ? OPEN_ANGLE : 0);\n    const setWant = (o: boolean) => { if (openRef.current !== o) { openRef.current = o; setOpen(o); } };\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      const t = now - t0;\n      if (!s.drag) {\n        // A sprung hinge: it throws the lid open, and pulls it shut onto a hard stop.\n        const target = want();\n        s.v += ((target > 0 ? 150 : 210) * (target - s.th) - (target > 0 ? 9.5 : 13) * s.v) * dt; s.th += s.v * dt;\n        if (s.th < 0) { if (s.v < -1.2) s.thud = 2.2; s.th = 0; s.v = -s.v * 0.24; }\n      }\n      s.thud *= Math.pow(0.0005, dt);\n      if (now - touched > 6000) {\n        s.lxTo = Math.sin(t / 1300) * 0.85; s.lyTo = Math.cos(t / 1900) * 0.5;\n        if (t > nextDemo && !s.drag) { const o = !openRef.current; setWant(o); nextDemo = t + (o ? 5200 : 1500); }\n      }\n      s.lx += (s.lxTo - s.lx) * Math.min(1, dt * 5); s.ly += (s.lyTo - s.ly) * Math.min(1, dt * 5);\n      s.yaw = -0.22 + s.lx * 0.09;\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    const still = () => { s.th = want(); s.v = 0; s.lx = s.lxTo; s.ly = s.lyTo; s.yaw = -0.22 + s.lx * 0.09; draw(); };\n\n    api.current = { toggle: () => { touched = performance.now(); setWant(!openRef.current); if (reduce) still(); else wake(); } };\n\n    const onDown = (e: PointerEvent) => { touched = performance.now(); s.drag = true; dragY = e.clientY; dragTh = s.th; moved = 0; pad.setPointerCapture(e.pointerId); wake(); };\n    const onMove = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect();\n      s.lxTo = Math.max(-1, Math.min(1, ((e.clientX - b.left) / b.width) * 2 - 1)); s.lyTo = Math.max(-1, Math.min(1, ((e.clientY - b.top) / b.height) * 2 - 1));\n      touched = Math.max(touched, performance.now() - 2500);\n      if (s.drag) {\n        touched = performance.now();\n        const dy = dragY - e.clientY; moved = Math.max(moved, Math.abs(dy));\n        const next = Math.max(0, Math.min(OPEN_ANGLE + 0.12, dragTh + (dy / Math.max(120, b.height * 0.36)) * OPEN_ANGLE));\n        s.v = (next - s.th) * 30; s.th = next;\n      }\n      if (reduce) { if (s.drag) { s.lx = s.lxTo; s.ly = s.lyTo; draw(); } else still(); }\n    };\n    const onUp = () => {\n      if (!s.drag) return;\n      s.drag = false; touched = performance.now();\n      if (moved < 5) setWant(!openRef.current); // a plain press works the catch\n      else setWant(s.th + s.v * 0.08 > 0.62);\n      if (reduce) still();\n    };\n    pad.addEventListener(\"pointerdown\", onDown);\n    pad.addEventListener(\"pointermove\", onMove);\n    pad.addEventListener(\"pointerup\", onUp);\n    pad.addEventListener(\"pointercancel\", onUp);\n    const ro = new ResizeObserver(fit); ro.observe(cv);\n    const io = new IntersectionObserver(([en]) => { seen = en.isIntersecting; if (seen) wake(); });\n    io.observe(cv);\n    const onVis = () => { if (!document.hidden) wake(); };\n    document.addEventListener(\"visibilitychange\", onVis);\n    fit();\n    if (reduce) still(); else wake();\n    return () => {\n      cancelAnimationFrame(raf); api.current = null; ro.disconnect(); io.disconnect();\n      pad.removeEventListener(\"pointerdown\", onDown); pad.removeEventListener(\"pointermove\", onMove);\n      pad.removeEventListener(\"pointerup\", onUp); pad.removeEventListener(\"pointercancel\", onUp);\n      document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  return (\n    <section className=\"relative flex min-h-[640px] w-full flex-col items-center justify-center overflow-hidden bg-[#e7e1d7] bg-[radial-gradient(90%_70%_at_50%_42%,#f6f2ea_0%,#e7e1d7_58%,#d6cec1_100%)] px-4 py-10 sm:min-h-[720px]\">\n      <div className=\"relative w-full max-w-[570px]\">\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"block w-full\" style={{ aspectRatio: `${VW} / ${VH}` }} />\n        <button\n          ref={hit}\n          type=\"button\"\n          aria-pressed={open}\n          aria-label={`Ring box holding the ${piece.name}. ${open ? \"Open. Press to close the lid.\" : \"Closed. Press to open the lid.\"}`}\n          onClick={(e) => { if (e.detail === 0) api.current?.toggle(); }}\n          className=\"absolute inset-[6%] cursor-pointer touch-none rounded-[32px] outline-none transition-shadow duration-200 focus-visible:ring-2 focus-visible:ring-[#0f3a31] focus-visible:ring-offset-4 focus-visible:ring-offset-[#e7e1d7] motion-reduce:transition-none\"\n        />\n      </div>\n      <p className=\"pointer-events-none absolute bottom-5 left-5 text-[#3b352b] sm:bottom-8 sm:left-8\">\n        <span className=\"block font-serif text-[17px] italic leading-tight text-[#1d1a14]\">{piece.name}</span>\n        <span className=\"mt-1 block font-mono text-[10.5px] uppercase tracking-[0.18em]\">{piece.detail}</span>\n        <span className=\"mt-1 block font-mono text-[10.5px] uppercase tracking-[0.18em]\">\n          <span className=\"text-[#1d1a14]\">{piece.price}</span>\n          <span className=\"mx-2 text-[#8d8474]\">/</span>\n          {open ? \"Press the box to shut it\" : \"Press the box to open it\"}\n        </span>\n      </p>\n    </section>\n  );\n}\n"}]}
