{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"cheese-wheel-pie-chart-on-a-marble-board-with-wedges-that-slide-out-and-toothpick-flag-labels","type":"registry:component","title":"Cheese Wheel Pie Chart on a Marble Board with Wedges That Slide Out and Toothpick Flag Labels","description":"Pie chart drawn as a wheel of cheeses on a marble board: wedges slide out on a spring and paper flags carry the percentages. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/cheese-wheel-pie-chart-on-a-marble-board-with-wedges-that-slide-out-and-toothpick-flag-labels.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here. The figures are invented demo data.\nconst UNIT = \"share of cheese sold\";\nconst cheeses = [\n  { name: \"Red wax gouda\", rind: \"#c7261c\", paste: \"#f2b632\", gloss: 1, kind: \"crystal\" },\n  { name: \"Alpine\", rind: \"#c99442\", paste: \"#f6e39a\", gloss: 0.22, kind: \"holes\" },\n  { name: \"Blue\", rind: \"#e6dfd0\", paste: \"#f4edd8\", gloss: 0.1, kind: \"veins\" },\n  { name: \"Black wax cheddar\", rind: \"#222226\", paste: \"#f0922b\", gloss: 1, kind: \"crumb\" },\n  { name: \"Sage\", rind: \"#4c7a37\", paste: \"#ece5b2\", gloss: 0.8, kind: \"flecks\" },\n];\nconst sets = [\n  { label: \"Market stall\", values: [34, 24, 18, 14, 10] },\n  { label: \"Online shop\", values: [20, 15, 12, 30, 23] },\n];\n\nconst layoutFor = (narrow: boolean) =>\n  narrow\n    ? { W: 380, H: 340, cx: 172, cy: 150, R: 112, h: 42, board: 176, pull: 26, u: 0.7 }\n    : { W: 860, H: 520, cx: 404, cy: 232, R: 198, h: 70, board: 322, pull: 62, u: 1 };\nconst E = 0.52; // how far the circle is squashed by the camera angle\n\nconst rgb = (hex: string) => [parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16)];\nconst shade = (hex: string, f: number, a = 1) => {\n  const c = rgb(hex).map((v) => (f <= 1 ? v * f : v + (255 - v) * Math.min(1, f - 1)));\n  return `rgba(${c.map((v) => Math.round(Math.max(0, Math.min(255, v)))).join(\",\")},${a})`;\n};\nconst spans = (values: number[]) => {\n  const total = values.reduce((a, b) => a + b, 0);\n  let a = -0.3 - (values[0] / total) * Math.PI; // the first wedge points to the right and a little away, so its cut face shows when it slides out\n  return values.map((v) => { const a0 = a; a += (v / total) * Math.PI * 2; return [a0, a]; });\n};\n\nexport default function CheeseWheelPieChart() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [narrow, setNarrow] = useState(false);\n  const [set, setSet] = useState(0);\n  const [sel, setSel] = useState(-1);\n  const [touched, setTouched] = useState(false);\n  const live = useRef({ set: 0, sel: -1, hover: -1, touched: false });\n  const api = useRef<{ poke: () => void } | null>(null);\n\n  useEffect(() => {\n    const el = stage.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => setNarrow(el.clientWidth < 700));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  useEffect(() => {\n    const cv = canvas.current;\n    if (!cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const L = layoutFor(narrow), n = cheeses.length, { cx, cy, R, h, u } = L;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const MONO = \"ui-monospace, SFMono-Regular, Menlo, monospace\";\n    let k = 1;\n    const rnd = (seed: number) => { let s = seed; return () => { s = (s * 16807) % 2147483647; return s / 2147483647; }; };\n\n    // The marble board never changes, so it is painted once.\n    const base = document.createElement(\"canvas\");\n    const build = () => {\n      base.width = cv.width; base.height = cv.height;\n      const b = base.getContext(\"2d\");\n      if (!b) return;\n      b.setTransform(k, 0, 0, k, 0, 0);\n      const bx = cx + 26 * u, by = cy + h + 6 * u, br = L.board, th = 15 * u;\n      // Shadow of the board on the table.\n      b.save(); b.translate(bx + 10 * u, by + th + 16 * u); b.scale(1, E);\n      const sh = b.createRadialGradient(0, 0, br * 0.6, 0, 0, br * 1.22);\n      sh.addColorStop(0, \"rgba(2,6,14,0.75)\"); sh.addColorStop(0.55, \"rgba(2,6,14,0.4)\"); sh.addColorStop(1, \"rgba(2,6,14,0)\");\n      b.fillStyle = sh; b.beginPath(); b.arc(0, 0, br * 1.22, 0, Math.PI * 2); b.fill(); b.restore();\n      // Edge of the slab.\n      const side = b.createLinearGradient(bx - br, 0, bx + br, 0);\n      side.addColorStop(0, \"#b9bcc2\"); side.addColorStop(0.3, \"#eceef0\"); side.addColorStop(0.62, \"#b3b6bd\"); side.addColorStop(1, \"#7c8088\");\n      b.beginPath(); b.ellipse(bx, by + th, br, br * E, 0, 0, Math.PI); b.lineTo(bx - br, by); b.ellipse(bx, by, br, br * E, 0, Math.PI, 0, true); b.closePath(); b.fillStyle = side; b.fill();\n      // Top of the slab with veining.\n      const top = b.createRadialGradient(bx - br * 0.35, by - br * E * 0.5, 10, bx, by, br * 1.1);\n      top.addColorStop(0, \"#ffffff\"); top.addColorStop(0.6, \"#f1f1ee\"); top.addColorStop(1, \"#dcdcd8\");\n      b.beginPath(); b.ellipse(bx, by, br, br * E, 0, 0, Math.PI * 2); b.fillStyle = top; b.fill();\n      b.save(); b.clip();\n      const r = rnd(31);\n      for (let v = 0; v < 7; v++) {\n        let x = bx - br + r() * br * 2, y = by - br * E + r() * br * E * 2, dir = 0.5 + (r() - 0.5) * 0.9;\n        const width = 0.4 + r() * 0.9;\n        b.beginPath(); b.moveTo(x, y);\n        for (let sgm = 0; sgm < 9; sgm++) {\n          dir += (r() - 0.5) * 0.7;\n          const mx = x + Math.cos(dir + (r() - 0.5)) * 30 * u, my = y + Math.sin(dir + (r() - 0.5)) * 15 * u;\n          x += Math.cos(dir) * 62 * u; y += Math.sin(dir) * 31 * u; b.quadraticCurveTo(mx, my, x, y);\n        }\n        b.lineJoin = \"round\"; b.lineCap = \"round\";\n        b.lineWidth = width * 16; b.strokeStyle = \"rgba(128,134,150,0.03)\"; b.stroke();\n        b.lineWidth = width * 6; b.strokeStyle = \"rgba(128,134,150,0.05)\"; b.stroke();\n        b.lineWidth = width; b.strokeStyle = `rgba(110,116,132,${0.1 + r() * 0.12})`; b.stroke();\n      }\n      b.restore();\n      b.beginPath(); b.ellipse(bx, by, br - 0.8, br * E - 0.8, 0, Math.PI * 0.9, Math.PI * 1.9); b.lineWidth = 1.4; b.strokeStyle = \"rgba(255,255,255,0.95)\"; b.stroke();\n      b.beginPath(); b.ellipse(bx, by, br - 0.5, br * E - 0.5, 0, 0, Math.PI); b.lineWidth = 1; b.strokeStyle = \"rgba(255,255,255,0.7)\"; b.stroke();\n    };\n\n    const fit = () => {\n      const w = cv.clientWidth || L.W;\n      k = (w / L.W) * Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.round(L.W * k); cv.height = Math.round(L.H * k);\n      build();\n    };\n\n    const s = {\n      share: sets[live.current.set].values.slice() as number[], sv: cheeses.map(() => 0),\n      out: cheeses.map(() => 0), ov: cheeses.map(() => 0), sway: cheeses.map(() => 0), swv: cheeses.map(() => 0),\n      light: -0.2, lightTo: -0.2,\n    };\n\n    type G = { i: number; a0: number; a1: number; am: number; ox: number; oy: number };\n    const geom = (): G[] => spans(s.share).map(([a0, a1], i) => { const am = (a0 + a1) / 2; return { i, a0, a1, am, ox: Math.cos(am) * s.out[i], oy: Math.sin(am) * s.out[i] * E }; });\n    const pt = (g: G, a: number, rad = 1): [number, number] => [cx + g.ox + Math.cos(a) * R * rad, cy + g.oy + Math.sin(a) * R * rad * E];\n    const sector = (g: G, dy = 0, rad = 1) => {\n      ctx.beginPath(); ctx.moveTo(cx + g.ox, cy + g.oy + dy); ctx.ellipse(cx + g.ox, cy + g.oy + dy, R * rad, R * rad * E, 0, g.a0, g.a1); ctx.closePath();\n    };\n\n    // One cut face: the paste, what is in it, and the rind seen edge-on.\n    const face = (g: G, a: number, seed: number, nx: number) => {\n      const c = cheeses[g.i], [bxp, byp] = pt(g, a), ax = cx + g.ox, ay = cy + g.oy;\n      ctx.save(); ctx.transform(bxp - ax, byp - ay, 0, h, ax, ay);\n      ctx.beginPath(); ctx.rect(0, 0, 1, 1); ctx.clip();\n      const lit = 0.9 - nx * 0.14;\n      const paste = ctx.createLinearGradient(0, 0, 0, 1);\n      paste.addColorStop(0, shade(c.paste, lit * 1.06)); paste.addColorStop(0.7, shade(c.paste, lit * 0.97)); paste.addColorStop(1, shade(c.paste, lit * 0.78));\n      ctx.fillStyle = paste; ctx.fillRect(0, 0, 1, 1);\n      const r = rnd(seed), ex = 1 / R, ey = 1 / h;\n      if (c.kind === \"holes\") for (let q = 0; q < 9; q++) {\n        const x = 0.08 + r() * 0.8, y = 0.16 + r() * 0.66, rad = (3 + r() * 6.5) * u;\n        const hole = ctx.createLinearGradient(0, y - rad * ey, 0, y + rad * ey);\n        hole.addColorStop(0, shade(c.paste, lit * 0.56)); hole.addColorStop(0.6, shade(c.paste, lit * 0.84)); hole.addColorStop(1, shade(c.paste, lit * 1.02));\n        ctx.beginPath(); ctx.ellipse(x, y, rad * ex, rad * ey, 0, 0, Math.PI * 2); ctx.fillStyle = hole; ctx.fill();\n        ctx.beginPath(); ctx.ellipse(x, y, rad * ex, rad * ey, 0, 0.1 * Math.PI, 0.9 * Math.PI); ctx.lineWidth = 0.02; ctx.strokeStyle = \"rgba(255,255,255,0.75)\"; ctx.stroke();\n      }\n      if (c.kind === \"veins\") for (let q = 0; q < 16; q++) {\n        let x = 0.05 + r() * 0.85, y = 0.12 + r() * 0.76;\n        ctx.beginPath(); ctx.moveTo(x, y);\n        for (let j = 0; j < 4; j++) { x += (r() - 0.5) * 16 * ex * u; y += (r() - 0.5) * 16 * ey * u; ctx.lineTo(x, y); }\n        ctx.lineJoin = \"round\"; ctx.lineCap = \"round\"; ctx.lineWidth = (1.6 + r() * 2.6) * ey * u * 0.5; ctx.strokeStyle = `rgba(54,104,118,${0.4 + r() * 0.4})`; ctx.stroke();\n        ctx.lineWidth *= 2.6; ctx.strokeStyle = \"rgba(74,124,128,0.14)\"; ctx.stroke();\n      }\n      if (c.kind === \"flecks\") for (let q = 0; q < 46; q++) {\n        ctx.beginPath(); ctx.ellipse(0.03 + r() * 0.9, 0.1 + r() * 0.82, (0.8 + r() * 1.8) * ex * u, (0.6 + r() * 1.2) * ey * u, 0, 0, Math.PI * 2); ctx.fillStyle = `rgba(74,118,52,${0.45 + r() * 0.5})`; ctx.fill();\n      }\n      if (c.kind === \"crystal\") for (let q = 0; q < 26; q++) {\n        ctx.beginPath(); ctx.ellipse(0.04 + r() * 0.88, 0.1 + r() * 0.8, (0.6 + r()) * ex * u, (0.6 + r()) * ey * u, 0, 0, Math.PI * 2); ctx.fillStyle = `rgba(255,250,225,${0.5 + r() * 0.45})`; ctx.fill();\n      }\n      if (c.kind === \"crumb\") for (let q = 0; q < 18; q++) {\n        const x = 0.05 + r() * 0.82, y = 0.12 + r() * 0.76, len = (4 + r() * 12) * ex * u;\n        ctx.beginPath(); ctx.moveTo(x, y); ctx.quadraticCurveTo(x + len / 2, y + (r() - 0.5) * 5 * ey, x + len, y + (r() - 0.5) * 3 * ey);\n        ctx.lineWidth = 0.9 * ey; ctx.strokeStyle = `rgba(150,70,8,${0.2 + r() * 0.24})`; ctx.stroke();\n      }\n      // Rind: along the top, down the outer end and along the foot.\n      const rw = (c.gloss > 0.5 ? 4 : 5.5) * u;\n      ctx.fillStyle = shade(c.rind, lit * 0.95); ctx.fillRect(1 - rw * ex, 0, rw * ex, 1); ctx.fillRect(0, 0, 1, rw * 0.7 * ey); ctx.fillRect(0, 1 - rw * 0.6 * ey, 1, rw * 0.6 * ey);\n      const blend = ctx.createLinearGradient(1 - rw * 2.6 * ex, 0, 1 - rw * ex, 0);\n      blend.addColorStop(0, shade(c.paste, lit * 0.8, 0)); blend.addColorStop(1, shade(c.paste, lit * 0.7, 0.6));\n      ctx.fillStyle = blend; ctx.fillRect(1 - rw * 2.6 * ex, 0, rw * 1.6 * ex, 1);\n      // Contact shade where the wedge meets the stone, and a little light along the top arris.\n      const foot = ctx.createLinearGradient(0, 0.62, 0, 1);\n      foot.addColorStop(0, \"rgba(40,20,0,0)\"); foot.addColorStop(1, \"rgba(40,20,0,0.3)\");\n      ctx.fillStyle = foot; ctx.fillRect(0, 0.62, 1, 0.38);\n      ctx.fillStyle = \"rgba(255,255,255,0.4)\"; ctx.fillRect(0, 0, 1, 1.1 * ey);\n      ctx.restore();\n    };\n\n    const wedge = (g: G) => {\n      const c = cheeses[g.i], la = 2.25 - s.light * 0.5;\n      // Cut faces that look towards the camera.\n      if (Math.cos(g.a0) < -0.02) face(g, g.a0, 100 + g.i * 7, Math.sin(g.a0));\n      if (Math.cos(g.a1) > 0.02) face(g, g.a1, 200 + g.i * 7, -Math.sin(g.a1));\n      // Outer wall, painted in thin staves so the light goes round it.\n      const from = Math.max(g.a0, 0), to = Math.min(g.a1, Math.PI);\n      if (to > from) {\n        const steps = Math.max(1, Math.ceil((to - from) / 0.035));\n        for (let q = 0; q < steps; q++) {\n          const s0 = from + ((to - from) * q) / steps, s1 = Math.min(to, from + ((to - from) * (q + 1)) / steps + 0.014), mid = (s0 + s1) / 2;\n          const lit = 0.56 + 0.44 * Math.max(0, Math.cos(mid - la)), spec = Math.pow(Math.max(0, Math.cos(mid - la)), 22) * c.gloss;\n          const [x0, y0] = pt(g, s0), [x1, y1] = pt(g, s1), ym = Math.min(y0, y1);\n          const wall = ctx.createLinearGradient(0, ym, 0, ym + h);\n          wall.addColorStop(0, shade(c.rind, lit * 1.1)); wall.addColorStop(0.07, shade(c.rind, lit * 1.02)); wall.addColorStop(0.16, shade(c.rind, lit * 0.96 + spec * 0.7)); wall.addColorStop(0.3, shade(c.rind, lit * 0.94 + spec * 0.25)); wall.addColorStop(0.8, shade(c.rind, lit * 0.82)); wall.addColorStop(1, shade(c.rind, lit * 0.46));\n          ctx.beginPath(); ctx.moveTo(x0, y0); ctx.lineTo(x1, y1); ctx.lineTo(x1, y1 + h); ctx.lineTo(x0, y0 + h); ctx.closePath(); ctx.fillStyle = wall; ctx.fill();\n        }\n      }\n      if (to > from) {\n        // Light catching the shoulder where wall meets top, and the dark seam where it meets the stone.\n        ctx.beginPath(); ctx.ellipse(cx + g.ox, cy + g.oy + 1.2 * u, R, R * E, 0, from, to); ctx.lineWidth = 1.6 * u; ctx.strokeStyle = `rgba(255,255,255,${0.22 + 0.36 * c.gloss})`; ctx.stroke();\n        ctx.beginPath(); ctx.ellipse(cx + g.ox, cy + g.oy + h - 1, R, R * E, 0, from, to); ctx.lineWidth = 2 * u; ctx.strokeStyle = \"rgba(10,6,14,0.4)\"; ctx.stroke();\n      }\n      // Top of the wedge.\n      sector(g);\n      const top = ctx.createRadialGradient(cx - R * 0.5, cy - R * E * 0.7, R * 0.1, cx, cy, R * 1.25);\n      top.addColorStop(0, shade(c.rind, 1.16 + 0.1 * c.gloss)); top.addColorStop(0.55, shade(c.rind, 1)); top.addColorStop(1, shade(c.rind, 0.8));\n      ctx.fillStyle = top; ctx.fill();\n      ctx.save(); sector(g); ctx.clip();\n      // Texture of the rind: pores and mould marks, placed relative to the wedge so they travel with it.\n      const r = rnd(900 + g.i * 13), dark = c.rind === \"#222226\";\n      for (let q = 0; q < (c.gloss > 0.5 ? 26 : 150); q++) {\n        const a = g.a0 + r() * (g.a1 - g.a0), rad = Math.sqrt(r()), [x, y] = pt(g, a, rad), sz = (0.5 + r() * (c.gloss > 0.5 ? 1.2 : 2.4)) * u;\n        ctx.beginPath(); ctx.ellipse(x, y, sz, sz * E, 0, 0, Math.PI * 2);\n        ctx.fillStyle = r() > 0.5 ? `rgba(255,255,255,${c.gloss > 0.5 ? 0.1 : 0.22})` : `rgba(${dark ? \"0,0,0\" : \"70,40,10\"},${c.gloss > 0.5 ? 0.12 : 0.14})`; ctx.fill();\n      }\n      for (let ring = 0.2; ring < 1; ring += 0.2) { ctx.beginPath(); ctx.ellipse(cx + g.ox, cy + g.oy, R * ring, R * ring * E, 0, 0, Math.PI * 2); ctx.lineWidth = 0.8; ctx.strokeStyle = `rgba(${dark ? \"255,255,255\" : \"40,20,0\"},${dark ? 0.05 : 0.07})`; ctx.stroke(); }\n      // Sheen of the wax: a broad window reflection and a tighter hot spot, both sliding with the light.\n      const [mx, my] = pt(g, g.am, 0.56), gx = mx - R * (0.1 + 0.14 * s.light), gy = my - R * E * 0.1;\n      ctx.save(); ctx.translate(gx, gy); ctx.rotate(-0.3); ctx.scale(1, 0.3);\n      const sheen = ctx.createRadialGradient(0, 0, 0, 0, 0, R * 0.5);\n      sheen.addColorStop(0, `rgba(255,255,255,${0.62 * c.gloss + 0.06})`); sheen.addColorStop(0.35, `rgba(255,255,255,${0.26 * c.gloss + 0.03})`); sheen.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = sheen; ctx.beginPath(); ctx.arc(0, 0, R * 0.5, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n      // The rounded shoulder: bright on the far rim, dark on the near one.\n      ctx.beginPath(); ctx.ellipse(cx + g.ox, cy + g.oy, R - 2.2 * u, (R - 2.2 * u) * E, 0, Math.PI, Math.PI * 2); ctx.lineWidth = 3 * u; ctx.strokeStyle = `rgba(255,255,255,${0.12 + 0.3 * c.gloss})`; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(cx + g.ox, cy + g.oy, R - 1.6 * u, (R - 1.6 * u) * E, 0, 0, Math.PI); ctx.lineWidth = 3.4 * u; ctx.strokeStyle = \"rgba(0,0,0,0.16)\"; ctx.stroke();\n      ctx.restore();\n      // Knife-cut arrises catch the light.\n      const [ex0, ey0] = pt(g, g.a0), [ex1, ey1] = pt(g, g.a1);\n      ctx.beginPath(); ctx.moveTo(ex0, ey0); ctx.lineTo(cx + g.ox, cy + g.oy); ctx.lineTo(ex1, ey1); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(255,255,255,0.4)\"; ctx.stroke();\n    };\n\n    // The little paper flag on a cocktail stick that carries the figure.\n    const flag = (g: G) => {\n      const [x, y] = pt(g, g.am, 0.6), tall = 52 * u, sw = s.sway[g.i], fw = 48 * u, fh = 24 * u;\n      ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + 22 * u, y + 9 * u); ctx.lineWidth = 2 * u; ctx.strokeStyle = \"rgba(0,0,0,0.22)\"; ctx.lineCap = \"round\"; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(x, y, 2.4 * u, 1.3 * u, 0, 0, Math.PI * 2); ctx.fillStyle = \"rgba(0,0,0,0.4)\"; ctx.fill();\n      ctx.save(); ctx.translate(x, y); ctx.rotate(sw);\n      const stick = ctx.createLinearGradient(-1.4 * u, 0, 1.4 * u, 0);\n      stick.addColorStop(0, \"#f3d9a4\"); stick.addColorStop(0.5, \"#d9b06a\"); stick.addColorStop(1, \"#8f6a30\");\n      ctx.fillStyle = stick; ctx.fillRect(-1.3 * u, -tall, 2.6 * u, tall);\n      ctx.beginPath(); ctx.moveTo(-1.3 * u, -tall); ctx.lineTo(0, -tall - 5 * u); ctx.lineTo(1.3 * u, -tall); ctx.fillStyle = \"#d9b06a\"; ctx.fill();\n      // Paper, folded round the stick, with the far half a touch darker.\n      ctx.save(); ctx.shadowColor = \"rgba(0,0,0,0.35)\"; ctx.shadowBlur = 5 * k; ctx.shadowOffsetX = 3 * k; ctx.shadowOffsetY = 4 * k;\n      ctx.beginPath(); ctx.moveTo(0, -tall + 3 * u); ctx.lineTo(fw, -tall + 1 * u); ctx.lineTo(fw - 7 * u, -tall + 3 * u + fh / 2); ctx.lineTo(fw, -tall + 5 * u + fh); ctx.lineTo(0, -tall + 3 * u + fh); ctx.closePath();\n      const sheet = ctx.createLinearGradient(0, 0, fw, 0);\n      sheet.addColorStop(0, \"#fffdf6\"); sheet.addColorStop(0.55, \"#f7f1e2\"); sheet.addColorStop(1, \"#e3dac4\");\n      ctx.fillStyle = sheet; ctx.fill(); ctx.restore();\n      ctx.fillStyle = \"rgba(0,0,0,0.1)\"; ctx.fillRect(0, -tall + 3 * u, 2.6 * u, fh);\n      ctx.fillStyle = cheeses[g.i].rind === \"#e6dfd0\" ? \"#8f8672\" : cheeses[g.i].rind; ctx.fillRect(4.5 * u, -tall + 3 * u + fh - 5 * u, fw - 16 * u, 2.2 * u);\n      ctx.font = `800 ${Math.max(10, 13.5 * u)}px ${MONO}`; ctx.textAlign = \"left\"; ctx.textBaseline = \"middle\"; ctx.fillStyle = \"#1c1917\";\n      ctx.fillText(`${Math.round(s.share[g.i])}%`, 5 * u, -tall + 3 * u + fh * 0.4);\n      ctx.restore();\n    };\n\n    const draw = () => {\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.clearRect(0, 0, cv.width, cv.height);\n      ctx.drawImage(base, 0, 0);\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      const gs = geom();\n      // Shadows on the stone: a soft cast shadow and a tight one under each foot.\n      for (const g of gs) {\n        ctx.save(); ctx.shadowColor = \"rgba(30,26,44,0.5)\"; ctx.shadowBlur = 26 * k * u; ctx.shadowOffsetX = 22 * k * u; ctx.shadowOffsetY = 20 * k * u; sector(g, h, 0.98); ctx.fillStyle = \"#3a3440\"; ctx.fill(); ctx.restore();\n        ctx.save(); ctx.shadowColor = \"rgba(20,14,24,0.75)\"; ctx.shadowBlur = 9 * k * u; ctx.shadowOffsetX = 3 * k * u; ctx.shadowOffsetY = 4 * k * u; sector(g, h, 0.99); ctx.fillStyle = \"#2a2430\"; ctx.fill(); ctx.restore();\n      }\n      const order = gs.slice().sort((p, q) => Math.sin(p.am) * (R + s.out[p.i]) - Math.sin(q.am) * (R + s.out[q.i]));\n      for (const g of order) wedge(g);\n      for (const g of order) flag(g);\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, moved = -1e9, step = 0, nextStep = 0.35;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!t0) { t0 = now; last = now; }\n      const dt = Math.min(0.04, (now - last) / 1000); last = now;\n      const t = (now - t0) / 1000;\n      // The self-running demo: pull each wedge in turn, then change the data set.\n      if (!live.current.touched && t > nextStep) {\n        if (step < n) { live.current.sel = step; setSel(step); step++; nextStep = t + (step === 1 ? 3.4 : 2.3); }\n        else { step = 0; nextStep = t + 1.5; live.current.sel = -1; setSel(-1); live.current.set = (live.current.set + 1) % sets.length; setSet(live.current.set); }\n      }\n      if (now - moved > 2600) s.lightTo = 0.7 * Math.sin(t * 0.5);\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      const goal = sets[live.current.set].values;\n      for (let i = 0; i < n; i++) {\n        s.sv[i] += (60 * (goal[i] - s.share[i]) - 13 * s.sv[i]) * dt; s.share[i] = Math.max(1, s.share[i] + s.sv[i] * dt);\n        const want = live.current.sel === i ? L.pull : live.current.hover === i ? L.pull * 0.2 : 0, before = s.ov[i];\n        s.ov[i] += (170 * (want - s.out[i]) - 17 * s.ov[i]) * dt; s.out[i] = Math.max(0, s.out[i] + s.ov[i] * dt);\n        // The flag lags behind its wedge and wags as it stops.\n        s.swv[i] += (-(s.ov[i] - before) * 0.012 * Math.cos((spans(s.share)[i][0] + spans(s.share)[i][1]) / 2) - 90 * s.sway[i] * dt - 5 * s.swv[i] * dt);\n        s.sway[i] += s.swv[i] * dt;\n      }\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => {\n      const goal = sets[live.current.set].values;\n      for (let i = 0; i < n; i++) { s.share[i] = goal[i]; s.out[i] = live.current.sel === i ? L.pull : 0; s.sway[i] = 0; }\n      draw();\n    };\n    const wake = () => { if (reduce) still(); else if (!raf) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    api.current = { poke: wake };\n    if (reduce && live.current.sel < 0 && !live.current.touched) { live.current.sel = 0; setSel(0); }\n    fit(); wake();\n\n    // Which wedge is under the pointer: test the top of each one, then its wall.\n    const hit = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect(), x = ((e.clientX - b.left) / b.width) * L.W, y = ((e.clientY - b.top) / b.height) * L.H;\n      for (const g of geom()) for (const drop of [0, h * 0.5, h]) {\n        const dx = (x - cx - g.ox) / R, dy = (y - drop - cy - g.oy) / (R * E);\n        if (dx * dx + dy * dy > 1) continue;\n        let a = Math.atan2(dy, dx); while (a < g.a0) a += Math.PI * 2;\n        if (a <= g.a1) return g.i;\n      }\n      return -1;\n    };\n    const onMove = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect(); moved = performance.now();\n      s.lightTo = Math.max(-1, Math.min(1, ((e.clientX - b.left) / b.width) * 2 - 1));\n      const i = hit(e); live.current.hover = i; cv.style.cursor = i >= 0 ? \"pointer\" : \"default\";\n    };\n    const onLeave = () => { live.current.hover = -1; };\n    const onDown = (e: PointerEvent) => {\n      const i = hit(e); if (i < 0) return;\n      live.current.touched = true; setTouched(true);\n      live.current.sel = live.current.sel === i ? -1 : i; setSel(live.current.sel); wake();\n    };\n    cv.addEventListener(\"pointermove\", onMove); cv.addEventListener(\"pointerleave\", onLeave); cv.addEventListener(\"pointerdown\", onDown);\n    const ro = new ResizeObserver(() => { fit(); if (reduce) still(); });\n    ro.observe(cv);\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(\"pointermove\", onMove); cv.removeEventListener(\"pointerleave\", onLeave); cv.removeEventListener(\"pointerdown\", onDown);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, [narrow]);\n\n  const L = layoutFor(narrow);\n  const touch = () => { if (!live.current.touched) { live.current.touched = true; setTouched(true); } };\n  const choose = (i: number) => { touch(); live.current.set = i; setSet(i); api.current?.poke(); };\n  const pick = (i: number) => { touch(); live.current.sel = live.current.sel === i ? -1 : i; setSel(live.current.sel); api.current?.poke(); };\n  const values = sets[set].values;\n\n  return (\n    <div ref={stage} className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#121a2b] sm:min-h-[760px] bg-[radial-gradient(100%_70%_at_50%_36%,#2a3c5c_0%,#121a2b_66%)] px-4 py-10\">\n      <div className=\"relative w-full\" style={{ maxWidth: L.W, aspectRatio: `${L.W} / ${L.H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full touch-manipulation\" />\n      </div>\n\n      <div className=\"mt-4 flex w-full max-w-[900px] flex-col items-center gap-4\">\n        <div role=\"group\" aria-label=\"Data set\" className=\"flex rounded-full bg-black/30 p-1 shadow-[inset_0_1px_3px_rgba(0,0,0,0.6),0_1px_0_rgba(255,255,255,0.08)]\">\n          {sets.map((d, i) => (\n            <button\n              key={d.label}\n              type=\"button\"\n              aria-pressed={set === i}\n              onClick={() => choose(i)}\n              className={`h-10 rounded-full px-5 font-mono text-[11px] font-semibold uppercase tracking-[0.16em] outline-none transition-colors duration-300 focus-visible:ring-2 focus-visible:ring-amber-300 motion-reduce:transition-none ${set === i ? \"bg-[#f6efe0] text-stone-900 shadow-[0_2px_6px_rgba(0,0,0,0.5)]\" : \"text-slate-300 hover:text-white\"}`}\n            >\n              {d.label}\n            </button>\n          ))}\n        </div>\n        <ul aria-label={`${sets[set].label}: ${UNIT}, demo data`} className=\"flex flex-wrap justify-center gap-x-1 gap-y-1\">\n          {cheeses.map((c, i) => (\n            <li key={c.name}>\n              <button\n                type=\"button\"\n                aria-pressed={sel === i}\n                onClick={() => pick(i)}\n                className={`flex h-10 items-center gap-2 rounded-full px-3 font-mono text-[11px] uppercase tracking-[0.12em] outline-none transition-colors duration-300 focus-visible:ring-2 focus-visible:ring-amber-300 motion-reduce:transition-none ${sel === i ? \"bg-white/15 text-white\" : \"text-slate-300 hover:text-white\"}`}\n              >\n                <span aria-hidden=\"true\" className=\"h-3 w-3 rounded-full ring-1 ring-white/40\" style={{ background: c.rind }} />\n                {c.name}\n                <span className=\"font-semibold text-white\">{values[i]}%</span>\n              </button>\n            </li>\n          ))}\n        </ul>\n        <p aria-live={touched ? \"polite\" : \"off\"} className=\"min-h-[20px] text-center font-mono text-[11px] uppercase leading-5 tracking-[0.16em] text-slate-400\">\n          <span className=\"text-slate-100\">{sel >= 0 ? `${cheeses[sel].name} ${values[sel]}%` : sets[set].label}</span>\n          <span className=\"mx-2 text-slate-600\">/</span>\n          {UNIT}, demo data\n        </p>\n      </div>\n    </div>\n  );\n}\n"}]}
