{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"walnut-dice-tray-lined-in-green-baize-where-two-dice-tumble-with-rigid-body-physics-and-settle-on-true-faces","type":"registry:component","title":"Walnut Dice Tray Lined in Green Baize Where Two Dice Tumble with Rigid Body Physics and Settle on True Faces","description":"Dice roller on one canvas: the dice tumble round a walnut tray, knock each other and settle on the faces the physics chose. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/walnut-dice-tray-lined-in-green-baize-where-two-dice-tumble-with-rigid-body-physics-and-settle-on-true-faces.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// One entry per die: body colour as r, g, b, then the pip paint and the paint of the single pip on the one.\nconst dice = [\n  { body: [246, 238, 222], pip: \"#19130f\", ace: \"#b3261e\" },\n  { body: [158, 30, 38], pip: \"#f7eeda\", ace: \"#f7eeda\" },\n];\nconst FELT = [30, 112, 74]; // baize colour, r, g, b\nconst GRAVITY = 34; // die widths per second squared: lower it for a slower, floatier throw\nconst BOUNCE = 0.5; // how much speed survives a landing on the cloth\nconst GRIP = 0.42; // friction of the cloth\nconst W = 720, H = 500; // drawing size in CSS pixels\nconst NUMBER = [\"one\", \"two\", \"three\", \"four\", \"five\", \"six\"];\n\ntype Die = { p: number[]; v: number[]; q: number[]; w: number[]; m: number[]; sleep: boolean };\nconst cross = (a: number[], b: number[]) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];\nconst dot = (a: number[], b: number[]) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];\nconst CORNERS = [0, 1, 2, 3, 4, 5, 6, 7].map((i) => [i & 1 ? 0.5 : -0.5, i & 2 ? 0.5 : -0.5, i & 4 ? 0.5 : -0.5]);\nconst A = 0.25;\nconst PIPS: number[][][] = [[[0, 0]], [[-A, -A], [A, A]], [[-A, -A], [0, 0], [A, A]], [[-A, -A], [A, -A], [-A, A], [A, A]], [[-A, -A], [A, -A], [0, 0], [-A, A], [A, A]], [[-A, -A], [-A, 0], [-A, A], [A, -A], [A, 0], [A, A]]];\n// Opposite faces add up to seven, as on a real die.\nconst faceValue = (axis: number, sign: number) => (sign > 0 ? [2, 1, 3][axis] : [5, 6, 4][axis]);\n\nexport default function WalnutDiceTray() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const pad = useRef<HTMLDivElement>(null);\n  const api = useRef<{ roll: () => void } | null>(null);\n  const [faces, setFaces] = useState<number[] | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current, hit = pad.current;\n    if (!cv || !hit) 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    cv.width = W * dpr; cv.height = H * dpr;\n\n    // The camera looks down at sixty degrees with no perspective, so every face is a plain parallelogram.\n    const S = 56, SP = Math.sin(Math.PI / 3), CP = Math.cos(Math.PI / 3), CX = W / 2, CY = 248;\n    const HX = 4.5, HZ = 2.9; // half the floor of the tray, in die widths\n    const sx = (x: number) => CX + x * S, sy = (y: number, z: number) => CY + (z * SP - y * CP) * S;\n    const norm = (a: number[]) => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };\n    const LIGHT = norm([-0.52, 0.8, -0.22]), FILL = norm([0.25, 0.35, 0.9]), VIEW = [0, SP, CP], HALF = norm([LIGHT[0] + VIEW[0], LIGHT[1] + VIEW[1], LIGHT[2] + VIEW[2]]);\n    const rr = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, r: number) => { c.beginPath(); c.moveTo(x + r, y); c.arcTo(x + w, y, x + w, y + h, r); c.arcTo(x + w, y + h, x, y + h, r); c.arcTo(x, y + h, x, y, r); c.arcTo(x, y, x + w, y, r); c.closePath(); };\n    let seed = 11;\n    const rnd = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    // Tray, cloth and wood are painted once and reused every frame.\n    const FX = sx(-HX), FW = 2 * HX * S, FY = sy(0, -HZ), FH = 2 * HZ * S * SP;\n    const LIFT = 17, RIM = 30, RIMZ = 27, SIDE = 22;\n    const OX = FX - RIM, OY = FY - LIFT - RIMZ, OW = FW + RIM * 2, OH = FH + RIMZ * 2;\n    const tray = document.createElement(\"canvas\");\n    tray.width = cv.width; tray.height = cv.height;\n    const t = tray.getContext(\"2d\");\n    if (t) {\n      t.scale(dpr, dpr);\n      t.save(); t.shadowColor = \"rgba(0,0,0,0.7)\"; t.shadowBlur = 46 * dpr; t.shadowOffsetY = 26 * dpr; rr(t, OX + 6, OY + SIDE, OW - 12, OH - 6, 34); t.fillStyle = \"#0a0705\"; t.fill(); t.restore();\n      const side = t.createLinearGradient(0, OY + OH - 20, 0, OY + OH + SIDE);\n      side.addColorStop(0, \"#3a2414\"); side.addColorStop(0.5, \"#2a190d\"); side.addColorStop(1, \"#150c06\");\n      rr(t, OX, OY + SIDE, OW, OH, 34); t.fillStyle = side; t.fill();\n      // Rim: walnut with long wandering grain.\n      t.save(); rr(t, OX, OY, OW, OH, 34); t.clip();\n      const wood = t.createLinearGradient(OX, OY, OX + OW, OY + OH);\n      wood.addColorStop(0, \"#7a4f2e\"); wood.addColorStop(0.45, \"#5e3a20\"); wood.addColorStop(1, \"#472a16\");\n      t.fillStyle = wood; t.fillRect(OX, OY, OW, OH);\n      for (let i = 0; i < 260; i++) {\n        const y0 = OY + rnd() * OH, amp = 1 + rnd() * 3, ph = rnd() * 6, len = 0.004 + rnd() * 0.008, x0 = OX + rnd() * OW * 0.7 - 40, span = 120 + rnd() * 380;\n        t.beginPath();\n        for (let x = 0; x <= span; x += 8) { const y = y0 + Math.sin(ph + (x0 + x) * len * 3) * amp; if (x === 0) t.moveTo(x0 + x, y); else t.lineTo(x0 + x, y); }\n        t.lineWidth = 0.5 + rnd() * 1.1; t.strokeStyle = rnd() > 0.3 ? `rgba(28,14,6,${0.08 + rnd() * 0.2})` : `rgba(214,160,104,${0.05 + rnd() * 0.1})`; t.stroke();\n      }\n      const lit = t.createLinearGradient(OX, OY, OX + OW * 0.6, OY + OH);\n      lit.addColorStop(0, \"rgba(255,214,160,0.2)\"); lit.addColorStop(0.5, \"rgba(255,214,160,0)\"); lit.addColorStop(1, \"rgba(0,0,0,0.24)\");\n      t.fillStyle = lit; t.fillRect(OX, OY, OW, OH);\n      t.restore();\n      const edge = t.createLinearGradient(0, OY, 0, OY + OH);\n      edge.addColorStop(0, \"rgba(255,224,180,0.6)\"); edge.addColorStop(0.3, \"rgba(255,224,180,0.08)\"); edge.addColorStop(1, \"rgba(0,0,0,0.55)\");\n      rr(t, OX + 0.75, OY + 0.75, OW - 1.5, OH - 1.5, 33.5); t.lineWidth = 1.5; t.strokeStyle = edge; t.stroke();\n      // A brass line let into the rim.\n      const brass = t.createLinearGradient(OX, OY, OX + OW, OY + OH);\n      brass.addColorStop(0, \"#f6dc97\"); brass.addColorStop(0.3, \"#a47a2c\"); brass.addColorStop(0.55, \"#e8c877\"); brass.addColorStop(0.8, \"#8a6220\"); brass.addColorStop(1, \"#d9b765\");\n      rr(t, OX + 12, OY + 11, OW - 24, OH - 22, 25); t.lineWidth = 2; t.strokeStyle = \"rgba(0,0,0,0.45)\"; t.stroke();\n      rr(t, OX + 12, OY + 10.2, OW - 24, OH - 22, 25); t.lineWidth = 1.4; t.strokeStyle = brass; t.stroke();\n      // The well: lined walls, then the cloth floor seen below the far wall.\n      rr(t, FX, FY - LIFT, FW, FH, 20);\n      const wall = t.createLinearGradient(0, FY - LIFT, 0, FY + 6);\n      wall.addColorStop(0, `rgb(${FELT[0] * 0.3 | 0},${FELT[1] * 0.3 | 0},${FELT[2] * 0.3 | 0})`); wall.addColorStop(1, `rgb(${FELT[0] * 0.62 | 0},${FELT[1] * 0.62 | 0},${FELT[2] * 0.62 | 0})`);\n      t.fillStyle = wall; t.fill();\n      t.save(); rr(t, FX, FY - LIFT, FW, FH, 20); t.clip(); rr(t, FX, FY, FW, FH, 20); t.clip();\n      const cloth = t.createRadialGradient(CX - 110, CY - 60, 30, CX, CY, 360);\n      cloth.addColorStop(0, `rgb(${FELT[0] * 1.3 | 0},${FELT[1] * 1.2 | 0},${FELT[2] * 1.22 | 0})`); cloth.addColorStop(0.6, `rgb(${FELT[0]},${FELT[1]},${FELT[2]})`); cloth.addColorStop(1, `rgb(${FELT[0] * 0.6 | 0},${FELT[1] * 0.66 | 0},${FELT[2] * 0.66 | 0})`);\n      t.fillStyle = cloth; t.fillRect(FX, FY, FW, FH);\n      for (let i = 0; i < 26000; i++) { // the nap: thousands of short fibres\n        const x = FX + rnd() * FW, y = FY + rnd() * FH, a = rnd() * Math.PI, l = 1.2 + rnd() * 2.6;\n        t.beginPath(); t.moveTo(x, y); t.lineTo(x + Math.cos(a) * l, y + Math.sin(a) * l * SP);\n        t.lineWidth = 0.6; t.strokeStyle = rnd() > 0.5 ? `rgba(190,255,214,${0.03 + rnd() * 0.07})` : `rgba(0,22,10,${0.05 + rnd() * 0.12})`; t.stroke();\n      }\n      const far = t.createLinearGradient(0, FY, 0, FY + 46);\n      far.addColorStop(0, \"rgba(0,14,6,0.6)\"); far.addColorStop(1, \"rgba(0,14,6,0)\");\n      t.fillStyle = far; t.fillRect(FX, FY, FW, 46);\n      const left = t.createLinearGradient(FX, 0, FX + 40, 0);\n      left.addColorStop(0, \"rgba(0,14,6,0.55)\"); left.addColorStop(1, \"rgba(0,14,6,0)\");\n      t.fillStyle = left; t.fillRect(FX, FY, 40, FH);\n      t.restore();\n      rr(t, FX, FY - LIFT, FW, FH, 20); t.lineWidth = 1.2; t.strokeStyle = \"rgba(0,0,0,0.5)\"; t.stroke();\n      const lip = t.createLinearGradient(0, FY - LIFT, 0, FY - LIFT + FH);\n      lip.addColorStop(0, \"rgba(255,226,180,0)\"); lip.addColorStop(0.8, \"rgba(255,226,180,0)\"); lip.addColorStop(1, \"rgba(255,226,180,0.5)\");\n      rr(t, FX - 1, FY - LIFT - 1, FW + 2, FH + 2, 21); t.lineWidth = 1; t.strokeStyle = lip; t.stroke();\n    }\n\n    const mat = (d: Die) => {\n      const [w, x, y, z] = d.q;\n      d.m = [1 - 2 * (y * y + z * z), 2 * (x * y - w * z), 2 * (x * z + w * y), 2 * (x * y + w * z), 1 - 2 * (x * x + z * z), 2 * (y * z - w * x), 2 * (x * z - w * y), 2 * (y * z + w * x), 1 - 2 * (x * x + y * y)];\n    };\n    const axis = (d: Die, k: number) => [d.m[k], d.m[3 + k], d.m[6 + k]];\n    const top = (d: Die) => { let k = 0; for (let i = 1; i < 3; i++) if (Math.abs(d.m[3 + i]) > Math.abs(d.m[3 + k])) k = i; return { k, s: d.m[3 + k] > 0 ? 1 : -1 }; };\n    const spin = (d: Die, dt: number) => {\n      const [w, x, y, z] = d.q, [a, b, c] = d.w, h = dt * 0.5;\n      const q = [w - h * (a * x + b * y + c * z), x + h * (a * w + b * z - c * y), y + h * (b * w + c * x - a * z), z + h * (c * w + a * y - b * x)];\n      const l = Math.hypot(q[0], q[1], q[2], q[3]);\n      d.q = [q[0] / l, q[1] / l, q[2] / l, q[3] / l]; mat(d);\n    };\n    const all: Die[] = dice.map((_, i) => ({ p: [i * 1.6 - 0.8, 0.5, 0], v: [0, 0, 0], q: [1, 0, 0, 0], w: [0, 0, 0], m: [1, 0, 0, 0, 1, 0, 0, 0, 1], sleep: true }));\n    const PLANES = [\n      { n: [0, 1, 0], d: 0, e: BOUNCE, mu: GRIP }, { n: [1, 0, 0], d: -HX, e: 0.5, mu: 0.25 }, { n: [-1, 0, 0], d: -HX, e: 0.5, mu: 0.25 },\n      { n: [0, 0, 1], d: -HZ, e: 0.5, mu: 0.25 }, { n: [0, 0, -1], d: -HZ, e: 0.5, mu: 0.25 },\n    ];\n    let airtime = 0, held = false, rolling = false;\n    const hand = { x: 0, z: 0, vx: 0, vz: 0, t: 0 };\n\n    // One small step of a real rigid body: gravity, then an impulse at every corner that is through a surface.\n    const step = (d: Die, dt: number) => {\n      if (d.sleep) return;\n      d.v[1] -= GRAVITY * dt;\n      for (let i = 0; i < 3; i++) d.p[i] += d.v[i] * dt;\n      spin(d, dt);\n      let onFloor = false;\n      PLANES.forEach((pl, pi) => {\n        let deep = 0;\n        for (const c of CORNERS) {\n          const r = [d.m[0] * c[0] + d.m[1] * c[1] + d.m[2] * c[2], d.m[3] * c[0] + d.m[4] * c[1] + d.m[5] * c[2], d.m[6] * c[0] + d.m[7] * c[1] + d.m[8] * c[2]];\n          const dist = pl.n[0] * (d.p[0] + r[0]) + pl.n[1] * (d.p[1] + r[1]) + pl.n[2] * (d.p[2] + r[2]) - pl.d;\n          if (dist >= 0) continue;\n          if (pi === 0) onFloor = true;\n          deep = Math.max(deep, -dist);\n          let wr = cross(d.w, r);\n          const vn = (d.v[0] + wr[0]) * pl.n[0] + (d.v[1] + wr[1]) * pl.n[1] + (d.v[2] + wr[2]) * pl.n[2];\n          if (vn >= 0) continue;\n          const rn = cross(r, pl.n);\n          const jn = (-(1 + (vn < -0.7 ? pl.e : 0)) * vn) / (1 + 6 * dot(rn, rn)); // a cube's inertia is its mass over six\n          for (let i = 0; i < 3; i++) { d.v[i] += jn * pl.n[i]; d.w[i] += 6 * rn[i] * jn; }\n          wr = cross(d.w, r);\n          const vc = [d.v[0] + wr[0], d.v[1] + wr[1], d.v[2] + wr[2]], along = dot(vc, pl.n);\n          const vt = [vc[0] - along * pl.n[0], vc[1] - along * pl.n[1], vc[2] - along * pl.n[2]], sl = Math.hypot(vt[0], vt[1], vt[2]);\n          if (sl > 1e-4) {\n            const tg = [vt[0] / sl, vt[1] / sl, vt[2] / sl], rt = cross(r, tg), jt = Math.min(pl.mu * jn, sl / (1 + 6 * dot(rt, rt)));\n            for (let i = 0; i < 3; i++) { d.v[i] -= jt * tg[i]; d.w[i] -= 6 * rt[i] * jt; }\n          }\n        }\n        for (let i = 0; i < 3; i++) d.p[i] += pl.n[i] * deep;\n      });\n      if (onFloor) {\n        // The cloth soaks up spin, and a die that has dithered too long is eased onto its nearest face.\n        const { k, s } = top(d), up = axis(d, k).map((v) => v * s), lean = cross(up, [0, 1, 0]), push = airtime > 3 ? 260 : up[1] > 0.9 ? 14 : 0;\n        const drag = 1 - (airtime > 3 ? 6 : 0.5) * dt;\n        for (let i = 0; i < 3; i++) d.w[i] = (d.w[i] + lean[i] * push * dt) * drag;\n        if (up[1] > 0.9995 && Math.hypot(d.v[0], d.v[1], d.v[2]) < 0.7 && Math.hypot(d.w[0], d.w[1], d.w[2]) < 1.1) {\n          const ang = Math.asin(Math.min(1, Math.hypot(lean[0], lean[1], lean[2])));\n          if (ang > 1e-6) { const ax = norm(lean), sh = Math.sin(ang / 2), ch = Math.cos(ang / 2), [w, x, y, z] = d.q, [a, b, c] = [ax[0] * sh, ax[1] * sh, ax[2] * sh]; d.q = [ch * w - a * x - b * y - c * z, ch * x + a * w + b * z - c * y, ch * y + b * w + c * x - a * z, ch * z + c * w + a * y - b * x]; mat(d); }\n          d.p[1] = 0.5; d.v = [0, 0, 0]; d.w = [0, 0, 0]; d.sleep = true;\n        }\n      }\n    };\n    const knock = () => {\n      for (let i = 0; i < all.length; i++) for (let j = i + 1; j < all.length; j++) {\n        const a = all[i], b = all[j], dl = [b.p[0] - a.p[0], b.p[1] - a.p[1], b.p[2] - a.p[2]], dist = Math.hypot(dl[0], dl[1], dl[2]) || 1;\n        if (dist >= 1.42) continue;\n        const n = [dl[0] / dist, dl[1] / dist, dl[2] / dist], rel = (b.v[0] - a.v[0]) * n[0] + (b.v[1] - a.v[1]) * n[1] + (b.v[2] - a.v[2]) * n[2], gap = (1.42 - dist) / 2;\n        if (a.sleep && b.sleep) continue;\n        a.sleep = false; b.sleep = false;\n        for (let k = 0; k < 3; k++) { a.p[k] -= n[k] * gap; b.p[k] += n[k] * gap; if (rel < 0) { a.v[k] += n[k] * rel * 0.7; b.v[k] -= n[k] * rel * 0.7; } }\n        if (rel < -1) { a.w[1] += rel * 0.6; b.w[1] -= rel * 0.6; }\n      }\n    };\n    const report = () => { rolling = false; setFaces(all.map((d) => { const { k, s } = top(d); return faceValue(k, s); })); };\n    const advance = (dt: number) => {\n      const n = 8;\n      for (let i = 0; i < n; i++) { all.forEach((d) => step(d, dt / n)); knock(); }\n      airtime += dt;\n      if (rolling && all.every((d) => d.sleep)) report();\n    };\n    const fling = (from: number[], vel: number[], low: boolean) => {\n      all.forEach((d, i) => {\n        d.sleep = false;\n        d.p = [from[0] + (i - 0.5) * 0.3 + Math.random() * 0.2, low ? 1.5 : 3 + i * 0.5, from[2] + (i - 0.5) * 1.5];\n        d.v = [vel[0] * (0.85 + Math.random() * 0.3), 1 + Math.random() * 2, vel[2] + (i - 0.5) * -2.4 + Math.random() * 2 - 1];\n        const along = norm([d.v[2], 0, -d.v[0]]), sp = 14 + Math.random() * 10;\n        d.w = [along[0] * sp + Math.random() * 8 - 4, Math.random() * 12 - 6, along[2] * sp + Math.random() * 8 - 4];\n        const q = [Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5], l = Math.hypot(q[0], q[1], q[2], q[3]) || 1;\n        d.q = q.map((v) => v / l); mat(d);\n      });\n      airtime = 0; rolling = true; setFaces(null);\n    };\n\n    const hull = (pts: number[][]) => {\n      const p = pts.slice().sort((a, b) => a[0] - b[0] || a[1] - b[1]), turn = (o: number[], a: number[], b: number[]) => (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0]);\n      const lo: number[][] = [], hi: number[][] = [];\n      for (const q of p) { while (lo.length > 1 && turn(lo[lo.length - 2], lo[lo.length - 1], q) <= 0) lo.pop(); lo.push(q); }\n      for (let i = p.length - 1; i >= 0; i--) { const q = p[i]; while (hi.length > 1 && turn(hi[hi.length - 2], hi[hi.length - 1], q) <= 0) hi.pop(); hi.push(q); }\n      return lo.slice(0, -1).concat(hi.slice(0, -1));\n    };\n    const shade = (rgb: number[], k: number) => `rgb(${Math.min(255, rgb[0] * k) | 0},${Math.min(255, rgb[1] * (k * 0.98 + 0.005)) | 0},${Math.min(255, rgb[2] * (k * 0.94 + 0.03)) | 0})`;\n\n    const drawDie = (d: Die, look: (typeof dice)[number]) => {\n      const seen: { n: number[]; u: number[]; w: number[]; val: number }[] = [];\n      for (let k = 0; k < 3; k++) for (const s of [1, -1]) {\n        const n = axis(d, k).map((v) => v * s);\n        if (dot(n, VIEW) > 0.012) seen.push({ n, u: axis(d, (k + 1) % 3), w: axis(d, (k + 2) % 3), val: faceValue(k, s) });\n      }\n      const mid = seen.length === 3 ? [0, 1, 2].map((i) => (seen[0].n[i] + seen[1].n[i] + seen[2].n[i]) * 0.5) : null;\n      for (const f of seen) {\n        const c = [d.p[0] + f.n[0] * 0.5, d.p[1] + f.n[1] * 0.5, d.p[2] + f.n[2] * 0.5];\n        ctx.setTransform(dpr * f.u[0] * S, dpr * (f.u[2] * SP - f.u[1] * CP) * S, dpr * f.w[0] * S, dpr * (f.w[2] * SP - f.w[1] * CP) * S, dpr * sx(c[0]), dpr * sy(c[1], c[2]));\n        const k = 0.42 + 0.6 * Math.max(0, dot(f.n, LIGHT)) + 0.2 * Math.max(0, dot(f.n, FILL));\n        if (mid) { // fill the notch where three rounded faces meet\n          const mu = dot(mid, f.u) - dot(f.n, f.u) * 0.5, mw = dot(mid, f.w) - dot(f.n, f.w) * 0.5;\n          ctx.fillStyle = shade(look.body, 0.98); ctx.fillRect(mu > 0 ? 0.3 : -0.5, mw > 0 ? 0.3 : -0.5, 0.2, 0.2);\n        }\n        rr(ctx, -0.5, -0.5, 1, 1, 0.15); ctx.fillStyle = shade(look.body, k * 0.9); ctx.fill();\n        const lu = dot(LIGHT, f.u), lw = dot(LIGHT, f.w);\n        const g = ctx.createLinearGradient(-lu * 0.6, -lw * 0.6, lu * 0.6, lw * 0.6);\n        g.addColorStop(0, shade(look.body, k * 0.9)); g.addColorStop(1, shade(look.body, k * 1.07));\n        rr(ctx, -0.455, -0.455, 0.91, 0.91, 0.125); ctx.fillStyle = g; ctx.fill();\n        const gl = Math.pow(Math.max(0, dot(f.n, HALF)), 14);\n        if (gl > 0.02) {\n          const hi = ctx.createRadialGradient(lu * 0.5, lw * 0.5, 0, lu * 0.5, lw * 0.5, 0.75);\n          hi.addColorStop(0, `rgba(255,255,255,${0.55 * gl})`); hi.addColorStop(1, \"rgba(255,255,255,0)\");\n          ctx.fillStyle = hi; ctx.fill();\n        }\n        // Drilled pips: paint in the pit, a shadow under the lit lip and a glint on the wall that faces the lamp.\n        const la = Math.atan2(lw, lu);\n        for (const [pu, pw] of PIPS[f.val - 1]) {\n          const r = f.val === 1 ? 0.135 : 0.092;\n          ctx.beginPath(); ctx.arc(pu, pw, r, 0, Math.PI * 2); ctx.fillStyle = f.val === 1 ? look.ace : look.pip; ctx.fill();\n          ctx.lineWidth = 0.026; ctx.beginPath(); ctx.arc(pu, pw, r - 0.013, la - 1.1, la + 1.1); ctx.strokeStyle = \"rgba(0,0,0,0.42)\"; ctx.stroke();\n          ctx.lineWidth = 0.02; ctx.beginPath(); ctx.arc(pu, pw, r - 0.012, la + Math.PI - 0.9, la + Math.PI + 0.9); ctx.strokeStyle = `rgba(255,255,255,${0.16 + 0.3 * k * 0.6})`; ctx.stroke();\n        }\n      }\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, cv.width, cv.height); ctx.drawImage(tray, 0, 0);\n      ctx.setTransform(dpr, 0, 0, dpr, 0, 0);\n      ctx.save(); ctx.beginPath(); ctx.rect(FX, FY, FW, FH - LIFT); ctx.clip();\n      for (const d of all) {\n        const pts = CORNERS.map((c) => {\n          const r = [d.m[0] * c[0] + d.m[1] * c[1] + d.m[2] * c[2], d.m[3] * c[0] + d.m[4] * c[1] + d.m[5] * c[2], d.m[6] * c[0] + d.m[7] * c[1] + d.m[8] * c[2]];\n          const y = d.p[1] + r[1];\n          return [sx(d.p[0] + r[0] - (y * LIGHT[0]) / LIGHT[1]), sy(0, d.p[2] + r[2] - (y * LIGHT[2]) / LIGHT[1])];\n        });\n        const lift = Math.max(0, d.p[1] - 0.5);\n        for (const [blur, alpha] of [[5 + lift * 9, 0.55 / (1 + lift * 0.7)], [1.5 + lift * 4, 0.3 / (1 + lift * 2)]]) {\n          ctx.save(); ctx.shadowColor = `rgba(0,10,4,${alpha})`; ctx.shadowBlur = blur * dpr; ctx.shadowOffsetX = 3000 * dpr;\n          ctx.beginPath(); hull(pts).forEach((p, i) => (i ? ctx.lineTo(p[0] - 3000, p[1]) : ctx.moveTo(p[0] - 3000, p[1]))); ctx.closePath(); ctx.fillStyle = \"#000\"; ctx.fill(); ctx.restore();\n        }\n      }\n      ctx.restore();\n      all.map((d, i) => ({ d, i })).sort((a, b) => a.d.p[2] * CP + a.d.p[1] * SP - (b.d.p[2] * CP + b.d.p[1] * SP)).forEach(({ d, i }) => drawDie(d, dice[i]));\n      // The near rim stands in front of anything low in the tray.\n      const cut = (FY + FH - LIFT) * dpr;\n      ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(tray, 0, cut, cv.width, cv.height - cut, 0, cut, cv.width, cv.height - cut);\n    };\n\n    let raf = 0, last = 0, seen = true, touched = -1e9, rest = 0;\n    const roll = () => { const side = Math.random() < 0.5 ? -1 : 1; fling([3.3 * side, 0, 1.1], [-8.6 * side, 0, -2.2], false); }; // from the left or the right\n    const frame = (now: number) => {\n      raf = 0;\n      const dt = Math.min(0.033, (now - last) / 1000); last = now;\n      if (held) {\n        hand.t += dt;\n        all.forEach((d, i) => {\n          const goal = [hand.x + Math.cos(hand.t * 9 + i * 3) * 0.5, 2.1 + Math.sin(hand.t * 13 + i) * 0.25, hand.z + Math.sin(hand.t * 9 + i * 3) * 0.5];\n          for (let k = 0; k < 3; k++) { d.v[k] = (goal[k] - d.p[k]) * 16; d.p[k] += d.v[k] * dt; }\n          d.w = [7 + i * 3, 9 - i * 4, 5]; spin(d, dt);\n        });\n      } else advance(dt);\n      rest = all.every((d) => d.sleep) ? rest + dt : 0;\n      if (rest > 3.4 && now - touched > 7000) roll(); // nobody is playing: throw again\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const wake = () => { if (!raf && !reduce) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    // With motion reduced the whole throw is worked out at once and only the result is shown.\n    const settle = () => { for (let i = 0; i < 900 && !all.every((d) => d.sleep); i++) advance(1 / 60); draw(); };\n    api.current = { roll: () => { touched = performance.now(); held = false; roll(); if (reduce) settle(); else wake(); } };\n    fling([-2.9, 0, 0.8], [6.6, 0, -1.6], true);\n    if (reduce) settle(); else wake();\n\n    const where = (e: PointerEvent) => {\n      const b = hit.getBoundingClientRect(), x = ((e.clientX - b.left) / b.width) * W, y = ((e.clientY - b.top) / b.height) * H;\n      return [Math.max(-HX + 1.1, Math.min(HX - 1.1, (x - CX) / S)), Math.max(-HZ + 1.1, Math.min(HZ - 1.1, ((y - CY) / S + 2.1 * CP) / SP))];\n    };\n    let stamp = 0;\n    const onDown = (e: PointerEvent) => {\n      if (reduce) { api.current?.roll(); return; }\n      const [x, z] = where(e);\n      held = true; hand.x = x; hand.z = z; hand.vx = 0; hand.vz = 0; stamp = performance.now(); touched = stamp; rolling = true; setFaces(null);\n      all.forEach((d) => { d.sleep = false; });\n      hit.setPointerCapture(e.pointerId); e.preventDefault(); wake();\n    };\n    const onMove = (e: PointerEvent) => {\n      if (!held) return;\n      const [x, z] = where(e), now = performance.now(), dt = Math.max(0.008, (now - stamp) / 1000);\n      hand.vx += ((x - hand.x) / dt - hand.vx) * 0.5; hand.vz += ((z - hand.z) / dt - hand.vz) * 0.5;\n      hand.x = x; hand.z = z; stamp = now; touched = now;\n    };\n    const onUp = () => {\n      if (!held) return;\n      held = false; touched = performance.now();\n      let vx = hand.vx, vz = hand.vz;\n      if (performance.now() - stamp > 120) { vx = 0; vz = 0; }\n      const sp = Math.hypot(vx, vz);\n      if (sp < 5) { const a = Math.atan2(-hand.z, -hand.x) + Math.random() - 0.5; vx = Math.cos(a) * 9; vz = Math.sin(a) * 6; } else if (sp > 22) { vx *= 22 / sp; vz *= 22 / sp; }\n      fling([hand.x, 0, hand.z], [vx, 0, vz], false);\n    };\n    hit.addEventListener(\"pointerdown\", onDown); hit.addEventListener(\"pointermove\", onMove);\n    hit.addEventListener(\"pointerup\", onUp); hit.addEventListener(\"pointercancel\", onUp);\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      hit.removeEventListener(\"pointerdown\", onDown); hit.removeEventListener(\"pointermove\", onMove);\n      hit.removeEventListener(\"pointerup\", onUp); hit.removeEventListener(\"pointercancel\", onUp);\n      io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  const total = faces ? faces.reduce((a, b) => a + b, 0) : 0;\n  return (\n    <div className=\"flex min-h-[520px] w-full flex-col items-center justify-center bg-[#0d0b0a] bg-[radial-gradient(110%_80%_at_50%_34%,#221c17_0%,#0d0b0a_64%)] px-4 py-10 sm:min-h-[720px]\">\n      <div className=\"relative w-full max-w-[720px]\" style={{ aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <div ref={pad} aria-hidden=\"true\" className=\"absolute inset-[12%] cursor-grab touch-none active:cursor-grabbing\" />\n      </div>\n      <p aria-live=\"polite\" className=\"mt-2 min-h-[20px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-stone-400\">\n        {faces ? (\n          <>\n            <span className=\"text-stone-100\">{faces.map((f) => NUMBER[f - 1]).join(\" and \")}</span>\n            <span className=\"mx-2 text-stone-600\">/</span>\n            total {total}\n          </>\n        ) : (\n          \"Rolling\"\n        )}\n      </p>\n      <button\n        type=\"button\"\n        onClick={() => api.current?.roll()}\n        className=\"mt-5 h-11 rounded-full border border-[#c9a45c]/50 bg-[#c9a45c]/10 px-6 font-mono text-[11px] font-semibold uppercase tracking-[0.2em] text-[#ecdcae] outline-none transition-colors duration-200 hover:bg-[#c9a45c]/20 focus-visible:ring-2 focus-visible:ring-[#e8c877] focus-visible:ring-offset-2 focus-visible:ring-offset-[#0d0b0a] motion-reduce:transition-none\"\n      >\n        Throw the dice\n      </button>\n    </div>\n  );\n}\n"}]}
