{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"spice-jar-rack-filter-with-glass-jars-that-turn-behind-a-brass-rail-to-show-their-labels","type":"registry:component","title":"Spice Jar Rack Filter with Glass Jars That Turn Behind a Brass Rail to Show Their Labels","description":"Filter where each chip is a glass spice jar on a shelf: press a jar and it turns behind a brass rail so its label faces you. Free React and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/spice-jar-rack-filter-with-glass-jars-that-turn-behind-a-brass-rail-to-show-their-labels.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\n// kind: \"powder\" | \"balls\" | \"flakes\" | \"crystals\" decides how the contents are painted.\nconst spices = [\n  { name: \"Smoked paprika\", label: \"PAPRIKA\", note: \"SMOKED / 45 G\", kind: \"powder\", color: \"#a2331b\", grain: 0 },\n  { name: \"Turmeric\", label: \"TURMERIC\", note: \"GROUND / 40 G\", kind: \"powder\", color: \"#d6931a\", grain: 0 },\n  { name: \"Black pepper\", label: \"PEPPER\", note: \"WHOLE / 50 G\", kind: \"balls\", color: \"#2b2523\", grain: 3.9 },\n  { name: \"Oregano\", label: \"OREGANO\", note: \"DRIED / 12 G\", kind: \"flakes\", color: \"#6d7a3c\", grain: 0 },\n  { name: \"Pink salt\", label: \"PINK SALT\", note: \"COARSE / 90 G\", kind: \"crystals\", color: \"#e3aa9c\", grain: 0 },\n  { name: \"Mustard seed\", label: \"MUSTARD\", note: \"YELLOW / 60 G\", kind: \"balls\", color: \"#c08a2a\", grain: 2.3 },\n];\nconst START = [0, 2]; // jars that already face the room when the page opens\nconst R = 46; // jar radius in design pixels\nconst BH = 160; // height of the glass\nconst LID = 22; // height of the steel lid\nconst TILT = 0.17; // how far we look down on the shelf: 0 is dead level\nconst FILL = 30; // empty glass above the spice\nconst ACCENT = \"#f0c060\"; // caption accent (the focus rings use the same value in their class names)\n\nconst layoutFor = (narrow: boolean) =>\n  narrow\n    ? { W: 380, H: 570, rows: [{ y: 222, x0: 8, x1: 372 }, { y: 490, x0: 8, x1: 372 }], jars: [0, 1, 2, 3, 4, 5].map((i) => ({ x: 70 + (i % 3) * 120, y: i < 3 ? 222 : 490 })) }\n    : { W: 860, H: 330, rows: [{ y: 232, x0: 30, x1: 830 }], jars: [0, 1, 2, 3, 4, 5].map((i) => ({ x: 105 + i * 130, y: 232 })) };\n\nconst tone = (hex: string, amt: number) => {\n  const n = parseInt(hex.slice(1), 16);\n  const ch = (v: number) => Math.max(0, Math.min(255, Math.round(amt >= 0 ? v + (255 - v) * amt : v * (1 + amt))));\n  return `rgb(${ch(n >> 16)},${ch((n >> 8) & 255)},${ch(n & 255)})`;\n};\n\nexport default function SpiceJarRackFilter() {\n  const stage = useRef<HTMLDivElement>(null);\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [narrow, setNarrow] = useState(false);\n  const [picked, setPicked] = useState<boolean[]>(() => spices.map((_, i) => START.includes(i)));\n  const live = useRef<boolean[]>(picked);\n  const api = useRef<{ turn: (i: number) => void; stopDemo: () => 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    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const mk = (w: number, h: number) => { const c = document.createElement(\"canvas\"); c.width = Math.max(1, Math.round(w)); c.height = Math.max(1, Math.round(h)); return c; };\n    let seed = 7;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    // 1. The unrolled surface of every jar: what you would get by slitting the glass and laying it flat.\n    const TS = 3, TW = Math.round(2 * Math.PI * R), SPICE_END = BH - 9;\n    const LW = 120, LX = TW / 2 - LW / 2, LY = 42, LH = 78;\n    const textures = spices.map((sp, idx) => {\n      seed = 31 + idx * 977;\n      const inner = mk(TW * TS, BH * TS), label = mk(TW * TS, BH * TS);\n      const c = inner.getContext(\"2d\"), l = label.getContext(\"2d\");\n      if (!c || !l) return { inner, label };\n      c.scale(TS, TS); l.scale(TS, TS);\n      const tiled = (x: number, fn: (x: number) => void) => { fn(x); fn(x - TW); fn(x + TW); };\n      const body = c.createLinearGradient(0, FILL, 0, SPICE_END);\n      body.addColorStop(0, tone(sp.color, sp.kind === \"balls\" ? -0.5 : 0.1)); body.addColorStop(1, tone(sp.color, sp.kind === \"balls\" ? -0.7 : -0.22));\n      c.fillStyle = body; c.fillRect(0, FILL, TW, SPICE_END - FILL);\n      const span = SPICE_END - FILL;\n      if (sp.kind === \"powder\") {\n        for (let i = 0; i < 5200; i++) {\n          const v = rand(), y = FILL + rand() * span;\n          c.fillStyle = tone(sp.color, (v - 0.5) * 0.55); c.globalAlpha = 0.5 + rand() * 0.5;\n          c.fillRect(rand() * TW, y, 0.5 + rand() * 1.1, 0.5 + rand() * 0.9);\n        }\n        // Soft tide lines where the powder settled after the last shake.\n        for (let i = 0; i < 5; i++) {\n          const y = FILL + 8 + rand() * (span - 12), ph = rand() * 6;\n          c.beginPath(); for (let x = 0; x <= TW; x += 4) c.lineTo(x, y + Math.sin((x / TW) * Math.PI * 2 * (1 + (i % 2)) + ph) * 3);\n          c.globalAlpha = 0.16; c.lineWidth = 2.2; c.strokeStyle = tone(sp.color, i % 2 ? 0.35 : -0.4); c.stroke();\n        }\n        c.globalAlpha = 1;\n      } else if (sp.kind === \"balls\") {\n        const g = sp.grain, count = Math.round((TW * span) / (g * g * 1.9));\n        for (let i = 0; i < count; i++) {\n          const x = rand() * TW, y = FILL + g * 0.6 + rand() * (span - g), r = g * (0.82 + rand() * 0.36), v = rand();\n          tiled(x, (px) => {\n            c.beginPath(); c.arc(px + 0.5, y + 0.9, r, 0, Math.PI * 2); c.fillStyle = \"rgba(0,0,0,0.45)\"; c.fill();\n            const ball = c.createRadialGradient(px - r * 0.35, y - r * 0.4, r * 0.1, px, y, r);\n            ball.addColorStop(0, tone(sp.color, 0.42 + v * 0.2)); ball.addColorStop(0.45, tone(sp.color, (v - 0.5) * 0.4)); ball.addColorStop(1, tone(sp.color, -0.55));\n            c.beginPath(); c.arc(px, y, r, 0, Math.PI * 2); c.fillStyle = ball; c.fill();\n          });\n        }\n      } else if (sp.kind === \"flakes\") {\n        const greens = [\"#56672c\", \"#7d8a45\", \"#8f9652\", \"#4a5627\", \"#a39a5c\", \"#6b5a36\"];\n        for (let i = 0; i < 2400; i++) {\n          const x = rand() * TW, y = FILL + rand() * span, w = 1.6 + rand() * 4.2, h = 0.8 + rand() * 1.9, a = rand() * Math.PI;\n          const col = tone(greens[(rand() * greens.length) | 0], (rand() - 0.6) * 0.5);\n          tiled(x, (px) => { c.save(); c.translate(px, y); c.rotate(a); c.fillStyle = \"rgba(0,0,0,0.35)\"; c.fillRect(-w / 2 + 0.4, -h / 2 + 0.6, w, h); c.fillStyle = col; c.fillRect(-w / 2, -h / 2, w, h); c.restore(); });\n        }\n      } else {\n        for (let i = 0; i < 1500; i++) {\n          const x = rand() * TW, y = FILL + rand() * span, r = 1.4 + rand() * 3.4, a0 = rand() * 6, v = rand();\n          tiled(x, (px) => {\n            c.beginPath(); for (let k2 = 0; k2 < 5; k2++) { const a = a0 + (k2 / 5) * Math.PI * 2, rr2 = r * (0.7 + ((k2 * 37 + i) % 5) * 0.09); c.lineTo(px + Math.cos(a) * rr2, y + Math.sin(a) * rr2); }\n            c.closePath(); c.fillStyle = tone(sp.color, (v - 0.35) * 0.9); c.fill();\n            c.lineWidth = 0.45; c.strokeStyle = v > 0.5 ? \"rgba(255,255,255,0.6)\" : \"rgba(120,60,50,0.4)\"; c.stroke();\n          });\n        }\n      }\n      // Dust that clings to the empty glass above the fill line.\n      for (let i = 0; i < 130; i++) { const d = rand(); c.globalAlpha = 0.5 * (1 - d); c.fillStyle = tone(sp.color, 0.1); c.fillRect(rand() * TW, FILL - d * 16, 0.7, 0.7); }\n      c.globalAlpha = 1;\n      // The thick glass foot.\n      const foot = c.createLinearGradient(0, SPICE_END, 0, BH);\n      foot.addColorStop(0, \"rgba(16,24,20,0.9)\"); foot.addColorStop(0.25, \"rgba(150,185,170,0.5)\"); foot.addColorStop(0.6, \"rgba(60,82,74,0.75)\"); foot.addColorStop(1, \"rgba(190,220,208,0.6)\");\n      c.fillStyle = foot; c.fillRect(0, SPICE_END, TW, BH - SPICE_END);\n\n      // The paper label.\n      l.fillStyle = \"#efe6d1\"; l.fillRect(LX, LY, LW, LH);\n      for (let i = 0; i < 900; i++) { l.fillStyle = rand() > 0.5 ? \"rgba(255,255,255,0.35)\" : \"rgba(120,96,60,0.09)\"; l.fillRect(LX + rand() * LW, LY + rand() * LH, 0.8, 0.8); }\n      l.fillStyle = sp.color; l.fillRect(LX, LY, LW, 7);\n      l.strokeStyle = \"rgba(70,54,36,0.75)\"; l.lineWidth = 0.7; l.strokeRect(LX + 5, LY + 12, LW - 10, LH - 17);\n      l.strokeStyle = \"rgba(70,54,36,0.35)\"; l.lineWidth = 0.4; l.strokeRect(LX + 7, LY + 14, LW - 14, LH - 21);\n      l.textAlign = \"center\"; l.textBaseline = \"middle\"; l.fillStyle = \"#3a2c1e\";\n      const spaced = (text: string, y: number, font: string, gap: number, max: number) => {\n        l.font = font;\n        const widths = Array.from(text).map((ch) => l.measureText(ch).width + gap), total = widths.reduce((a, b) => a + b, 0) - gap, sc = Math.min(1, max / total);\n        l.save(); l.translate(TW / 2, y); l.scale(sc, 1); let x = -total / 2;\n        Array.from(text).forEach((ch, i) => { l.fillText(ch, x + (widths[i] - gap) / 2, 0); x += widths[i]; });\n        l.restore();\n      };\n      spaced(`No. 0${idx + 1}`, LY + 23, \"600 6.5px ui-monospace, SFMono-Regular, Menlo, monospace\", 1.2, LW - 30);\n      spaced(sp.label, LY + 41, \"700 17px Georgia, 'Times New Roman', serif\", 1.6, LW - 28);\n      l.fillRect(TW / 2 - 14, LY + 53.5, 28, 0.7);\n      spaced(sp.note, LY + 63, \"600 6.5px ui-monospace, SFMono-Regular, Menlo, monospace\", 1, LW - 30);\n      return { inner, label };\n    });\n\n    const body = (c: CanvasRenderingContext2D, cx: number, y0: number, r: number, top: number) => {\n      c.beginPath(); c.moveTo(cx - r, top); c.lineTo(cx - r, y0 + BH); c.ellipse(cx, y0 + BH, r, r * TILT, 0, Math.PI, 0, true); c.lineTo(cx + r, top); c.closePath();\n    };\n\n    // 2. Layers that never change: wall and shelf behind, glass shine, rail in front.\n    let k = 1;\n    let base = mk(1, 1), front = mk(1, 1), glass = mk(1, 1);\n    const GP = 6; // padding around the glass sprite\n    const build = () => {\n      base = mk(cv.width, cv.height); front = mk(cv.width, cv.height); glass = mk((R * 2 + GP * 2) * k, (BH + R * TILT + GP * 2) * k);\n      const b = base.getContext(\"2d\"), f = front.getContext(\"2d\"), g = glass.getContext(\"2d\");\n      if (!b || !f || !g) return;\n      b.setTransform(k, 0, 0, k, 0, 0); f.setTransform(k, 0, 0, k, 0, 0); g.setTransform(k, 0, 0, k, 0, 0);\n      seed = 5;\n      // Wall: painted plaster, lit from the upper left.\n      for (const row of L.rows) {\n        const top = row.y - R * TILT - 9, lip = row.y + R * TILT + 11, w = row.x1 - row.x0;\n        // Shadow the shelf throws down the wall.\n        b.save(); b.shadowColor = \"rgba(0,0,0,0.6)\"; b.shadowBlur = 26 * k; b.shadowOffsetX = 3000 * k; b.shadowOffsetY = 26 * k; b.fillStyle = \"#000\"; b.fillRect(row.x0 + 10 - 3000, lip, w - 20, 16); b.restore();\n        // Shadows the jars throw on the wall.\n        b.save(); b.shadowColor = \"rgba(0,0,0,0.42)\"; b.shadowBlur = 20 * k; b.shadowOffsetX = (22 + 2000) * k; b.shadowOffsetY = 8 * k; b.fillStyle = \"#000\";\n        L.jars.filter((j) => j.y === row.y).forEach((j) => b.fillRect(j.x - R - 2000, j.y - BH - LID + 6, R * 2, BH + LID - 18));\n        b.restore();\n        // Oak plank: top face, then front edge.\n        const face = b.createLinearGradient(0, top, 0, lip);\n        face.addColorStop(0, \"#7a5230\"); face.addColorStop(1, \"#b98651\");\n        b.fillStyle = face; b.fillRect(row.x0, top, w, lip - top);\n        const edge = b.createLinearGradient(0, lip, 0, lip + 17);\n        edge.addColorStop(0, \"#8d6238\"); edge.addColorStop(1, \"#553519\");\n        b.fillStyle = edge; b.fillRect(row.x0, lip, w, 17);\n        b.save(); b.beginPath(); b.rect(row.x0, top, w, lip - top + 17); b.clip();\n        for (let i = 0; i < 150; i++) {\n          const y = top + rand() * (lip - top + 17), x = row.x0 + rand() * w, len = 40 + rand() * 260, v = rand();\n          b.beginPath(); b.moveTo(x, y); b.bezierCurveTo(x + len * 0.3, y + (rand() - 0.5) * 2.4, x + len * 0.7, y + (rand() - 0.5) * 2.4, x + len, y + (rand() - 0.5) * 1.6);\n          b.lineWidth = 0.5 + rand() * 0.6; b.strokeStyle = v > 0.6 ? \"rgba(255,226,180,0.13)\" : \"rgba(52,28,10,0.22)\"; b.stroke();\n        }\n        b.restore();\n        b.fillStyle = \"rgba(255,232,196,0.5)\"; b.fillRect(row.x0, lip - 0.6, w, 1.1);\n        b.fillStyle = \"rgba(0,0,0,0.5)\"; b.fillRect(row.x0, lip + 16.2, w, 0.9);\n        b.fillStyle = \"rgba(0,0,0,0.3)\"; b.fillRect(row.x0, top, w, 2.5);\n        // Where each jar touches the wood.\n        for (const j of L.jars.filter((q) => q.y === row.y)) {\n          const sh = b.createRadialGradient(j.x + 5, j.y + 3, R * 0.5, j.x + 5, j.y + 3, R * 1.45);\n          sh.addColorStop(0, \"rgba(22,10,2,0.75)\"); sh.addColorStop(1, \"rgba(22,10,2,0)\");\n          b.save(); b.translate(j.x + 5, j.y + 3); b.scale(1, TILT * 1.5); b.translate(-(j.x + 5), -(j.y + 3)); b.fillStyle = sh; b.beginPath(); b.arc(j.x + 5, j.y + 3, R * 1.45, 0, Math.PI * 2); b.fill(); b.restore();\n        }\n        // Light that came through the contents lands on the wood as a faint stain of colour.\n        L.jars.forEach((j, i) => {\n          if (j.y !== row.y) return;\n          b.save(); b.beginPath(); b.rect(row.x0, top, w, lip - top); b.clip(); b.globalAlpha = 0.34; b.globalCompositeOperation = \"lighter\";\n          const glow = b.createRadialGradient(j.x + 30, j.y + 6, 2, j.x + 30, j.y + 6, R * 0.9);\n          glow.addColorStop(0, tone(spices[i].color, 0.25)); glow.addColorStop(1, \"rgba(0,0,0,0)\");\n          b.translate(j.x + 30, j.y + 6); b.scale(1, 0.3); b.translate(-(j.x + 30), -(j.y + 6)); b.fillStyle = glow; b.beginPath(); b.arc(j.x + 30, j.y + 6, R * 0.9, 0, Math.PI * 2); b.fill(); b.restore();\n        });\n        // Brass gallery rail on two turned posts.\n        const railY = row.y - 24, px0 = row.x0 + 16, px1 = row.x1 - 16, foot = lip - 3;\n        f.save(); f.shadowColor = \"rgba(0,0,0,0.5)\"; f.shadowBlur = 5 * k; f.shadowOffsetY = 3 * k;\n        const rod = f.createLinearGradient(0, railY - 3, 0, railY + 3);\n        rod.addColorStop(0, \"#8a6218\"); rod.addColorStop(0.28, \"#fff0b8\"); rod.addColorStop(0.5, \"#dba63f\"); rod.addColorStop(0.8, \"#7c5514\"); rod.addColorStop(1, \"#b98a30\");\n        f.fillStyle = rod; f.fillRect(px0, railY - 3, px1 - px0, 6); f.restore();\n        for (const px of [px0, px1]) {\n          const post = f.createLinearGradient(px - 3, 0, px + 3, 0);\n          post.addColorStop(0, \"#6f4c12\"); post.addColorStop(0.35, \"#ffe9a6\"); post.addColorStop(0.6, \"#cf9a36\"); post.addColorStop(1, \"#5e3f0d\");\n          f.fillStyle = \"rgba(0,0,0,0.4)\"; f.beginPath(); f.ellipse(px + 3, foot + 1, 9, 2.6, 0, 0, Math.PI * 2); f.fill();\n          f.fillStyle = post; f.fillRect(px - 2.6, railY, 5.2, foot - railY);\n          f.beginPath(); f.ellipse(px, foot, 6.5, 2.4, 0, 0, Math.PI * 2); f.fill();\n          const ball = f.createRadialGradient(px - 2.2, railY - 2.6, 0.5, px, railY, 7);\n          ball.addColorStop(0, \"#fff6d2\"); ball.addColorStop(0.35, \"#e2ad45\"); ball.addColorStop(1, \"#5c3d0c\");\n          f.beginPath(); f.arc(px, railY, 6.6, 0, Math.PI * 2); f.fillStyle = ball; f.fill();\n        }\n      }\n      // The shine and the dark refracting edges of one jar, stamped over each in turn.\n      const cx = R + GP, y0 = GP;\n      g.save(); body(g, cx, y0, R, y0); g.clip();\n      const rim = g.createLinearGradient(cx - R, 0, cx + R, 0);\n      rim.addColorStop(0, \"rgba(0,0,0,0.7)\"); rim.addColorStop(0.035, \"rgba(0,0,0,0.38)\"); rim.addColorStop(0.06, \"rgba(255,255,255,0.3)\"); rim.addColorStop(0.1, \"rgba(255,255,255,0.04)\"); rim.addColorStop(0.3, \"rgba(0,0,0,0)\");\n      rim.addColorStop(0.62, \"rgba(0,0,0,0.05)\"); rim.addColorStop(0.9, \"rgba(0,0,0,0.32)\"); rim.addColorStop(0.95, \"rgba(255,255,255,0.16)\"); rim.addColorStop(0.975, \"rgba(0,0,0,0.45)\"); rim.addColorStop(1, \"rgba(0,0,0,0.75)\");\n      g.fillStyle = rim; g.fillRect(cx - R, y0, R * 2, BH + R * TILT);\n      const under = g.createLinearGradient(0, y0, 0, y0 + 16);\n      under.addColorStop(0, \"rgba(0,0,0,0.6)\"); under.addColorStop(1, \"rgba(0,0,0,0)\");\n      g.fillStyle = under; g.fillRect(cx - R, y0, R * 2, 16);\n      const streak = (x: number, w: number, a: number, y1: number, y2: number) => {\n        const v = g.createLinearGradient(0, y1, 0, y2);\n        v.addColorStop(0, \"rgba(255,255,255,0)\"); v.addColorStop(0.12, `rgba(255,255,255,${a})`); v.addColorStop(0.7, `rgba(255,255,255,${a * 0.7})`); v.addColorStop(1, \"rgba(255,255,255,0)\");\n        g.save(); g.filter = \"blur(1.4px)\"; g.fillStyle = v; g.beginPath(); g.ellipse(x + w / 2, (y1 + y2) / 2, w / 2, (y2 - y1) / 2, 0, 0, Math.PI * 2); g.fill(); g.restore();\n      };\n      streak(cx - R * 0.74, 10, 0.62, y0 + 6, y0 + BH - 14);\n      // A window somewhere behind the camera, bent by the curve of the glass.\n      g.save(); g.filter = \"blur(0.6px)\"; g.fillStyle = \"rgba(255,255,255,0.16)\";\n      for (const [wx, ww] of [[-0.46, 0.2]]) { g.beginPath(); g.moveTo(cx + R * wx, y0 + 15); g.lineTo(cx + R * (wx + ww), y0 + 17.5); g.lineTo(cx + R * (wx + ww), y0 + 28); g.lineTo(cx + R * wx, y0 + 26.5); g.closePath(); g.fill(); }\n      g.restore();\n      streak(cx - R * 0.47, 2.4, 0.3, y0 + 14, y0 + BH - 40);\n      streak(cx + R * 0.66, 4, 0.22, y0 + 20, y0 + BH - 20);\n      g.lineWidth = 1; g.strokeStyle = \"rgba(255,255,255,0.5)\"; g.beginPath(); g.ellipse(cx, y0 + BH - 1.2, R - 2, R * TILT, 0, Math.PI * 0.95, Math.PI * 0.45, true); g.stroke();\n      g.lineWidth = 0.8; g.strokeStyle = \"rgba(255,255,255,0.22)\"; g.beginPath(); g.ellipse(cx, y0 + BH - 9, R - 3, R * TILT, 0, Math.PI * 0.9, Math.PI * 0.1, true); g.stroke();\n      g.restore();\n    };\n\n    const fit = () => {\n      const dpr = Math.min(2, window.devicePixelRatio || 1), w = cv.clientWidth || L.W;\n      cv.width = Math.round(w * dpr); cv.height = Math.round((w * dpr * L.H) / L.W); k = cv.width / L.W;\n      build();\n    };\n\n    const s = {\n      a: live.current.map((on) => (on ? 0 : Math.PI)), v: spices.map(() => 0), to: live.current.map((on) => (on ? 0 : Math.PI)),\n      peek: spices.map(() => 0), peekTo: spices.map(() => 0),\n    };\n\n    const jar = (i: number) => {\n      const { x: cx, y: by } = L.jars[i], y0 = by - BH, a = s.a[i] + s.peek[i], tex = textures[i], sp = spices[i];\n      // Back wall of the glass, seen through the empty top.\n      body(ctx, cx, y0, R, y0 - 2); ctx.fillStyle = \"rgba(196,224,212,0.07)\"; ctx.fill();\n      ctx.beginPath(); ctx.ellipse(cx, y0 + FILL, R - 1.4, R * TILT, 0, 0, Math.PI * 2);\n      const topFace = ctx.createLinearGradient(cx - R, 0, cx + R, 0);\n      topFace.addColorStop(0, tone(sp.color, sp.kind === \"balls\" ? -0.45 : -0.3)); topFace.addColorStop(0.4, tone(sp.color, sp.kind === \"balls\" ? -0.1 : 0.16)); topFace.addColorStop(1, tone(sp.color, -0.5));\n      ctx.fillStyle = topFace; ctx.fill();\n      // Wrap the flat surface round the cylinder, one thin strip at a time.\n      for (let pass = 0; pass < 2; pass++) {\n        const img = pass === 0 ? tex.inner : tex.label, step = pass === 0 ? 1 : 0.5;\n        if (pass === 1) ctx.drawImage(glass, (cx - R - GP), (y0 - GP), glass.width / k, glass.height / k);\n        for (let x = -R + (pass === 0 ? 0.6 : 0); x < R - 0.4; x += step) {\n          const xm = Math.min(R - 0.01, x + step / 2), th = Math.asin(xm / R), cos = Math.cos(th);\n          let u = (TW / 2 + (th - a) * R) % TW; if (u < 0) u += TW;\n          if (pass === 1 && (u < LX - 1 || u > LX + LW + 1)) continue;\n          const du = Math.min(step / Math.max(0.08, cos), TW - u), dy = R * TILT * cos;\n          ctx.drawImage(img, u * TS, 0, Math.max(1, du * TS), BH * TS, cx + x, y0 + dy, step + 0.35, BH);\n          if (pass === 1) {\n            // Paper is matte: it falls off towards the sides and catches the lamp on the left.\n            const lit = Math.max(0, Math.cos(th + 0.55)), dark = Math.pow(1 - cos, 1.25) * 0.72 + (th > 0 ? th * 0.1 : 0);\n            ctx.fillStyle = `rgba(30,18,6,${Math.min(0.85, dark)})`; ctx.fillRect(cx + x, y0 + dy + LY, step + 0.35, LH);\n            ctx.fillStyle = `rgba(255,250,235,${Math.pow(lit, 6) * 0.2})`; ctx.fillRect(cx + x, y0 + dy + LY, step + 0.35, LH);\n          }\n        }\n      }\n      // Shadow of the rail across the glass.\n      const sh = ctx.createLinearGradient(0, by - 20, 0, by - 8);\n      sh.addColorStop(0, \"rgba(0,0,0,0)\"); sh.addColorStop(0.4, \"rgba(0,0,0,0.3)\"); sh.addColorStop(1, \"rgba(0,0,0,0)\");\n      ctx.fillStyle = sh; ctx.fillRect(cx - R + 1, by - 20, R * 2 - 2, 12);\n      // Steel lid: a ridged skirt that turns with the jar and a brushed top.\n      const RL = R + 1.6, yT = y0 - LID + 5, yB = y0 + 5, et = RL * TILT;\n      ctx.beginPath(); ctx.moveTo(cx - RL, yT); ctx.lineTo(cx - RL, yB); ctx.ellipse(cx, yB, RL, et, 0, Math.PI, 0, true); ctx.lineTo(cx + RL, yT); ctx.ellipse(cx, yT, RL, et, 0, 0, Math.PI, false); ctx.closePath();\n      const skirt = ctx.createLinearGradient(cx - RL, 0, cx + RL, 0);\n      skirt.addColorStop(0, \"#2c2f32\"); skirt.addColorStop(0.1, \"#8d9196\"); skirt.addColorStop(0.2, \"#f3f4f5\"); skirt.addColorStop(0.3, \"#a1a5aa\"); skirt.addColorStop(0.58, \"#5f6367\"); skirt.addColorStop(0.84, \"#a4a8ad\"); skirt.addColorStop(1, \"#26292c\");\n      ctx.fillStyle = skirt; ctx.fill();\n      ctx.save(); ctx.clip();\n      for (let n = 0; n < 60; n++) {\n        const th = a + (n / 60) * Math.PI * 2, cos = Math.cos(th);\n        if (cos <= 0.04) continue;\n        const x = cx + Math.sin(th) * RL, dy = et * cos;\n        ctx.fillStyle = `rgba(0,0,0,${0.3 * cos})`; ctx.fillRect(x, yT + dy + 3, 0.9, LID - 5);\n        ctx.fillStyle = `rgba(255,255,255,${0.26 * cos})`; ctx.fillRect(x + 0.9, yT + dy + 3, 0.7, LID - 5);\n      }\n      const hem = ctx.createLinearGradient(0, yB - 2, 0, yB + et + 1);\n      hem.addColorStop(0, \"rgba(0,0,0,0)\"); hem.addColorStop(1, \"rgba(0,0,0,0.5)\");\n      ctx.fillStyle = hem; ctx.fillRect(cx - RL, yB - 2, RL * 2, et + 3);\n      ctx.restore();\n      ctx.beginPath(); ctx.ellipse(cx, yT, RL, et, 0, 0, Math.PI * 2);\n      const cap = ctx.createLinearGradient(cx - RL, yT - et, cx + RL, yT + et);\n      cap.addColorStop(0, \"#f4f5f6\"); cap.addColorStop(0.4, \"#b9bcc0\"); cap.addColorStop(0.62, \"#dcdee0\"); cap.addColorStop(1, \"#7b7f84\");\n      ctx.fillStyle = cap; ctx.fill();\n      ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(255,255,255,0.85)\"; ctx.beginPath(); ctx.ellipse(cx, yT, RL - 0.5, et - 0.4, 0, Math.PI * 0.05, Math.PI * 0.95); ctx.stroke();\n      ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(0,0,0,0.22)\"; ctx.beginPath(); ctx.ellipse(cx, yT, RL - 5, et - 1.4, 0, 0, Math.PI * 2); ctx.stroke();\n      // A stamped dot on the lid rim marks the label side, so the turn reads from above.\n      const mx = cx + Math.sin(a) * (RL - 9), my = yT + Math.cos(a) * (et - 2.4);\n      ctx.beginPath(); ctx.ellipse(mx, my, 2.2, 1, 0, 0, Math.PI * 2); ctx.fillStyle = sp.color; ctx.fill();\n      ctx.lineWidth = 0.5; ctx.strokeStyle = \"rgba(0,0,0,0.4)\"; ctx.stroke();\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      for (let i = 0; i < spices.length; i++) jar(i);\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.drawImage(front, 0, 0);\n    };\n\n    // 3. Springs, and a hand that turns a jar now and then until somebody else does.\n    let raf = 0, last = 0, t0 = 0, seen = true, demo = !reduce, nextAt = 0.9, step = 0, first = true;\n    const order = [4, 0, 3, 2, 5, 4, 1, 3, 0, 5, 2, 1];\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      if (demo && t > nextAt) {\n        const i = order[step % order.length]; step++; nextAt = t + (step === 1 ? 3.4 : 2.3);\n        const next = live.current.map((on, j) => (j === i ? !on : on));\n        live.current = next; setPicked(next); s.to[i] += Math.PI;\n      }\n      let moving = first; first = false;\n      for (let i = 0; i < spices.length; i++) {\n        s.v[i] += (70 * (s.to[i] - s.a[i]) - 10.5 * s.v[i]) * dt; s.a[i] += s.v[i] * dt;\n        s.peek[i] += (s.peekTo[i] - s.peek[i]) * Math.min(1, dt * 9);\n        if (Math.abs(s.v[i]) > 0.002 || Math.abs(s.to[i] - s.a[i]) > 0.002 || Math.abs(s.peekTo[i] - s.peek[i]) > 0.002) moving = true;\n      }\n      if (moving) draw();\n      if ((moving || demo) && seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => { for (let i = 0; i < spices.length; i++) { s.a[i] = s.to[i]; s.v[i] = 0; s.peek[i] = 0; } draw(); };\n    const wake = () => { first = true; if (reduce) still(); else if (!raf) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    api.current = { turn: (i) => { s.to[i] += Math.PI; wake(); }, stopDemo: () => { demo = false; } };\n    fit(); wake();\n\n    // Running a pointer over a jar nudges it round, enough to see the edge of the label.\n    const host = cv.parentElement;\n    const onMove = (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      L.jars.forEach((j, i) => { const inside = Math.abs(x - j.x) < R + 8 && y > j.y - BH - LID && y < j.y + 14; s.peekTo[i] = inside ? -Math.max(-1, Math.min(1, (x - j.x) / R)) * 0.42 : 0; });\n      wake();\n    };\n    const onLeave = () => { s.peekTo = spices.map(() => 0); wake(); };\n    host?.addEventListener(\"pointermove\", onMove); host?.addEventListener(\"pointerleave\", onLeave);\n    const ro = new ResizeObserver(() => { fit(); if (reduce) still(); else { draw(); wake(); } });\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      host?.removeEventListener(\"pointermove\", onMove); host?.removeEventListener(\"pointerleave\", onLeave);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, [narrow]);\n\n  const L = layoutFor(narrow);\n  const toggle = (i: number) => {\n    api.current?.stopDemo();\n    const next = live.current.map((on, j) => (j === i ? !on : on));\n    live.current = next; setPicked(next); api.current?.turn(i);\n  };\n  const clear = () => {\n    api.current?.stopDemo();\n    live.current.forEach((on, i) => { if (on) api.current?.turn(i); });\n    const next = spices.map(() => false); live.current = next; setPicked(next);\n  };\n  const pct = (v: number, of: number) => `${(v / of) * 100}%`;\n  const chosen = spices.filter((_, i) => picked[i]);\n\n  return (\n    <div ref={stage} className=\"flex min-h-[720px] w-full flex-col items-center justify-center bg-[#121714] bg-[radial-gradient(75%_65%_at_34%_30%,#435443_0%,#263129_45%,#121714_100%)] px-3 py-12 sm:px-6\">\n      <div role=\"group\" aria-label=\"Filter by spice\" className=\"relative w-full\" style={{ maxWidth: narrow ? 420 : 1080, aspectRatio: `${L.W} / ${L.H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        {spices.map((sp, i) => (\n          <button\n            key={sp.name}\n            type=\"button\"\n            aria-pressed={picked[i]}\n            aria-label={sp.name}\n            onClick={() => toggle(i)}\n            style={{ left: pct(L.jars[i].x - R - 4, L.W), top: pct(L.jars[i].y - BH - LID, L.H), width: pct(R * 2 + 8, L.W), height: pct(BH + LID + 14, L.H) }}\n            className=\"absolute cursor-pointer rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-[#f0c060]\"\n          />\n        ))}\n      </div>\n      <div className=\"mt-8 flex min-h-[44px] flex-wrap items-center justify-center gap-x-4 gap-y-2 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-stone-400\">\n        <p aria-live=\"polite\">\n          <span style={{ color: ACCENT }}>{chosen.length ? \"Cooking with\" : \"Turn a jar to filter\"}</span>\n          {chosen.length > 0 && <span className=\"mx-2 text-stone-600\">/</span>}\n          <span className=\"text-stone-200\">{chosen.map((sp) => sp.name).join(\" + \")}</span>\n        </p>\n        {chosen.length > 0 && (\n          <button type=\"button\" onClick={clear} className=\"min-h-[40px] rounded-full border border-stone-500 px-4 font-mono text-[11px] uppercase tracking-[0.18em] text-stone-200 outline-none transition-colors hover:border-stone-300 focus-visible:ring-2 focus-visible:ring-[#f0c060]\">\n            Put all back\n          </button>\n        )}\n      </div>\n    </div>\n  );\n}\n"}]}
