{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glass-test-tube-rack-bar-chart-with-dyed-liquid-that-rises-sloshes-and-settles-on-each-value","type":"registry:component","title":"Glass Test Tube Rack Bar Chart with Dyed Liquid That Rises, Sloshes and Settles on Each Value","description":"Bar chart drawn as glass test tubes in a rack: dyed liquid climbs to each value, sloshes, bubbles and settles. Copy the React code or the HTML free.","dependencies":[],"files":[{"path":"components/we-inc/glass-test-tube-rack-bar-chart-with-dyed-liquid-that-rises-sloshes-and-settles-on-each-value.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 = \"orders packed\";\nconst FULL = 100; // the reading at the top graduation\nconst days = [\"Mon\", \"Tue\", \"Wed\", \"Thu\", \"Fri\", \"Sat\", \"Sun\"];\nconst dayNames = [\"Monday\", \"Tuesday\", \"Wednesday\", \"Thursday\", \"Friday\", \"Saturday\", \"Sunday\"];\nconst sets = [\n  { label: \"This week\", values: [42, 58, 71, 64, 88, 96, 53] },\n  { label: \"Last week\", values: [35, 49, 62, 80, 74, 60, 28] },\n];\nconst dyes = [\"#e2336b\", \"#f05a28\", \"#f3a31c\", \"#8cc63f\", \"#1fb58f\", \"#2b8be3\", \"#6a55e0\"]; // one dye per tube\n\nconst layoutFor = (narrow: boolean) =>\n  narrow\n    ? { W: 380, H: 430, u: 0.7, tw: 31, gap: 48, x0: 46, top: 54, rail: 80, base: 346 }\n    : { W: 820, H: 540, u: 1, tw: 52, gap: 98, x0: 116, top: 60, rail: 98, base: 428 };\n\nconst rgb = (hex: string) => [parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16)];\nconst tone = (hex: string, t: number, a = 1) => {\n  const c = rgb(hex), to = t > 0 ? 255 : 0, m = Math.abs(t);\n  return `rgba(${c.map((v) => Math.round(v + (to - v) * m)).join(\",\")},${a})`;\n};\n\nexport default function GlassTestTubeRackBarChart() {\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 [active, setActive] = useState(-1);\n  const [touched, setTouched] = useState(false);\n  const live = useRef({ set: 0, active: -1, touched: false });\n  const api = useRef<{ poke: () => void; flick: (i: number) => 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), u = L.u, n = days.length, r = L.tw / 2;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const railH = 20 * u, railF = 13 * u, baseT = 30 * u, baseF = 38 * u; // top faces and front faces of the two slabs\n    const bot = L.base + baseT * 0.56; // where a tube rests\n    const yFull = L.rail + railH + railF + 16 * u, yZero = bot - r * 0.55; // travel of the liquid surface\n    const xl = L.x0 - 70 * u, xr = L.x0 + (n - 1) * L.gap + 70 * u;\n    const tx = (i: number) => L.x0 + i * L.gap;\n    let k = 1;\n\n    const back = document.createElement(\"canvas\"), front = document.createElement(\"canvas\");\n\n    const brushed = (c: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, a: string, b: string, seed: number) => {\n      const g = c.createLinearGradient(0, y, 0, y + h);\n      g.addColorStop(0, a); g.addColorStop(1, b);\n      c.fillStyle = g; c.fill();\n      c.save(); c.clip();\n      let s = seed;\n      const rand = () => { s = (s * 16807) % 2147483647; return s / 2147483647; };\n      for (let yy = y; yy < y + h; yy += 0.5) {\n        const v = rand(), a0 = x + rand() * w * 0.5, a1 = x + w - rand() * w * 0.5;\n        c.beginPath(); c.moveTo(a0, yy); c.lineTo(a1, yy); c.lineWidth = 0.5;\n        c.strokeStyle = v > 0.5 ? `rgba(255,255,255,${(v - 0.5) * 0.5})` : `rgba(40,48,60,${(0.5 - v) * 0.22})`; c.stroke();\n      }\n      // A long reflection of the studio window across the metal.\n      const sh = c.createLinearGradient(x, 0, x + w, 0);\n      sh.addColorStop(0, \"rgba(255,255,255,0)\"); sh.addColorStop(0.22, \"rgba(255,255,255,0.42)\"); sh.addColorStop(0.36, \"rgba(255,255,255,0)\");\n      sh.addColorStop(0.7, \"rgba(255,255,255,0)\"); sh.addColorStop(0.82, \"rgba(255,255,255,0.2)\"); sh.addColorStop(1, \"rgba(60,70,84,0.16)\");\n      c.fillStyle = sh; c.fillRect(x, y, w, h);\n      c.restore();\n    };\n    const trap = (c: CanvasRenderingContext2D, y: number, h: number, inset: number) => {\n      c.beginPath(); c.moveTo(xl + inset, y); c.lineTo(xr - inset, y); c.lineTo(xr, y + h); c.lineTo(xl, y + h); c.closePath();\n    };\n    const holes = (c: CanvasRenderingContext2D) => { for (let i = 0; i < n; i++) c.ellipse(tx(i), L.rail + railH * 0.5, r + 3.2 * u, railH * 0.3, 0, 0, Math.PI * 2); };\n\n    const build = () => {\n      for (const cnv of [back, front]) { cnv.width = cv.width; cnv.height = cv.height; }\n      const b = back.getContext(\"2d\"), f = front.getContext(\"2d\");\n      if (!b || !f) return;\n      b.setTransform(k, 0, 0, k, 0, 0); f.setTransform(k, 0, 0, k, 0, 0);\n      const inset = 9 * u, cx = (xl + xr) / 2, floor = L.base + baseT + baseF;\n\n      // Shadow of the rack on the sweep: one broad pool and a tight contact line.\n      const poolR = Math.min((xr - xl) * 0.6, L.W / 2 - 2);\n      b.save(); b.translate(cx, floor + 2 * u); b.scale(1, 0.1);\n      const pool = b.createRadialGradient(0, 0, 0, 0, 0, poolR);\n      pool.addColorStop(0, \"rgba(40,52,70,0.42)\"); pool.addColorStop(0.6, \"rgba(40,52,70,0.2)\"); pool.addColorStop(1, \"rgba(40,52,70,0)\");\n      b.fillStyle = pool; b.beginPath(); b.arc(0, 0, poolR, 0, Math.PI * 2); b.fill(); b.restore();\n      const contact = b.createLinearGradient(0, floor - 1, 0, floor + 7 * u);\n      contact.addColorStop(0, \"rgba(20,28,40,0.5)\"); contact.addColorStop(1, \"rgba(20,28,40,0)\");\n      b.fillStyle = contact; b.fillRect(xl + 3, floor - 1, xr - xl - 6, 8 * u);\n\n      // Two steel posts.\n      for (const px of [xl + 22 * u, xr - 22 * u]) {\n        const g = b.createLinearGradient(px - 5 * u, 0, px + 5 * u, 0);\n        g.addColorStop(0, \"#5d646e\"); g.addColorStop(0.28, \"#f6f8fa\"); g.addColorStop(0.5, \"#aab1ba\"); g.addColorStop(0.78, \"#69707a\"); g.addColorStop(1, \"#9aa1aa\");\n        b.fillStyle = g; b.fillRect(px - 5 * u, L.rail + railH, 10 * u, L.base - L.rail - railH + baseT * 0.5);\n      }\n\n      // Base slab: top face, then front face with its lit upper edge.\n      trap(b, L.base, baseT, inset); brushed(b, xl, L.base, xr - xl, baseT, \"#e9ecef\", \"#d2d7dd\", 7);\n      b.beginPath(); b.rect(xl, L.base + baseT, xr - xl, baseF); brushed(b, xl, L.base + baseT, xr - xl, baseF, \"#b4bac2\", \"#6f7782\", 23);\n      b.fillStyle = \"rgba(255,255,255,0.9)\"; b.fillRect(xl, L.base + baseT - 0.5, xr - xl, 1.1);\n      b.fillStyle = \"rgba(30,38,50,0.45)\"; b.fillRect(xl, floor - 1, xr - xl, 1);\n      // Dimples the tubes stand in, and the day stamped under each.\n      b.textAlign = \"center\"; b.textBaseline = \"middle\"; b.font = `700 ${Math.max(8.5, 10.5 * u)}px ui-monospace, SFMono-Regular, Menlo, monospace`;\n      for (let i = 0; i < n; i++) {\n        const d = b.createRadialGradient(tx(i), bot - 1, 1, tx(i), bot, r * 1.05);\n        d.addColorStop(0, \"rgba(40,50,64,0.5)\"); d.addColorStop(0.7, \"rgba(40,50,64,0.16)\"); d.addColorStop(1, \"rgba(40,50,64,0)\");\n        b.save(); b.translate(tx(i), bot); b.scale(1, 0.3); b.translate(-tx(i), -bot);\n        b.fillStyle = d; b.beginPath(); b.arc(tx(i), bot, r * 1.05, 0, Math.PI * 2); b.fill(); b.restore();\n        const ly = L.base + baseT + baseF * 0.52, label = days[i].toUpperCase();\n        b.fillStyle = \"rgba(255,255,255,0.75)\"; b.fillText(label, tx(i), ly + 0.9);\n        b.fillStyle = \"rgba(38,46,58,0.82)\"; b.fillText(label, tx(i), ly);\n      }\n      // Rail: the top face goes behind the glass, with the holes bored through it.\n      trap(b, L.rail, railH, inset * 0.6); brushed(b, xl, L.rail, xr - xl, railH, \"#e6e9ed\", \"#cdd2d9\", 41);\n      b.beginPath(); holes(b); b.fillStyle = \"rgba(24,30,40,0.72)\"; b.fill();\n      // Its shadow falling down the posts and across the air behind.\n      const under = b.createLinearGradient(0, L.rail + railH + railF, 0, L.rail + railH + railF + 16 * u);\n      under.addColorStop(0, \"rgba(30,40,56,0.22)\"); under.addColorStop(1, \"rgba(30,40,56,0)\");\n      b.fillStyle = under; b.fillRect(xl + 6, L.rail + railH + railF, xr - xl - 12, 16 * u);\n\n      // Front layer: the near half of the rail, drawn over the tubes so they pass through it.\n      const half = L.rail + railH * 0.5;\n      f.beginPath(); f.moveTo(xl + inset * 0.3, half); f.lineTo(xr - inset * 0.3, half); f.lineTo(xr, L.rail + railH); f.lineTo(xl, L.rail + railH); f.closePath(); holes(f);\n      f.save(); f.clip(\"evenodd\");\n      f.beginPath(); f.rect(xl, half, xr - xl, railH); brushed(f, xl, L.rail, xr - xl, railH, \"#e6e9ed\", \"#cdd2d9\", 41);\n      f.restore();\n      // Bright chamfer on the near lip of each hole.\n      for (let i = 0; i < n; i++) {\n        f.beginPath(); f.ellipse(tx(i), half, r + 3.2 * u, railH * 0.3, 0, 0.12 * Math.PI, 0.88 * Math.PI);\n        f.lineWidth = 1.1; f.strokeStyle = \"rgba(255,255,255,0.95)\"; f.stroke();\n      }\n      f.beginPath(); f.rect(xl, L.rail + railH, xr - xl, railF); brushed(f, xl, L.rail + railH, xr - xl, railF, \"#bcc2ca\", \"#7c848f\", 59);\n      f.fillStyle = \"rgba(255,255,255,0.95)\"; f.fillRect(xl, L.rail + railH - 0.5, xr - xl, 1.1);\n      f.fillStyle = \"rgba(30,38,50,0.4)\"; f.fillRect(xl, L.rail + railH + railF - 0.8, xr - xl, 0.8);\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    type Bubble = { x: number; y: number; r: number; v: number; w: number };\n    const s = {\n      lv: days.map(() => 0), vel: days.map(() => 0), tilt: days.map(() => 0), tv: days.map(() => 0),\n      lift: days.map(() => 0), liftV: days.map(() => 0), shown: days.map(() => 0),\n      bub: days.map(() => [] as Bubble[]), light: -0.2, lightTo: -0.2,\n    };\n    const target = (i: number) => sets[live.current.set].values[i] / FULL;\n\n    const tubePath = (c: CanvasRenderingContext2D, x: number, top: number, bottom: number, rad: number) => {\n      c.beginPath(); c.moveTo(x - rad, top); c.lineTo(x - rad, bottom - rad); c.arc(x, bottom - rad, rad, Math.PI, 0, true); c.lineTo(x + rad, top); c.closePath();\n    };\n\n    const tube = (i: number) => {\n      const x = tx(i), up = s.lift[i], top = L.top - up, bottom = bot - up, dye = dyes[i % dyes.length], lvl = Math.max(0, s.lv[i]);\n      const ys = yZero - up - lvl * (yZero - yFull), slope = Math.max(-0.5, Math.min(0.5, s.tilt[i])), wall = 2.2 * u, ri = r - wall;\n\n      // Coloured light the liquid throws onto the base, and the dark ring where glass meets metal.\n      ctx.save(); ctx.translate(x + 5 * u, bot + 2 * u); ctx.scale(1, 0.26);\n      const pool = ctx.createRadialGradient(0, 0, 0, 0, 0, r * 2.1);\n      pool.addColorStop(0, tone(dye, 0.1, 0.5 * Math.min(1, lvl * 3))); pool.addColorStop(0.5, tone(dye, 0.2, 0.22 * Math.min(1, lvl * 3))); pool.addColorStop(1, tone(dye, 0.2, 0));\n      ctx.fillStyle = pool; ctx.beginPath(); ctx.arc(0, 0, r * 2.1, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n      if (up > 0.5) {\n        ctx.save(); ctx.translate(x, bot); ctx.scale(1, 0.28); ctx.globalAlpha = Math.max(0, 1 - up / (26 * u));\n        ctx.fillStyle = \"rgba(30,40,56,0.35)\"; ctx.beginPath(); ctx.arc(0, 0, r * 0.9, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n      }\n\n      // A faint reflection of the dye in the glossy sweep under the rack.\n      const fl = L.base + baseT + baseF + 4 * u;\n      ctx.save(); ctx.translate(x, fl); ctx.scale(1, 0.5 + lvl * 0.7);\n      const mirror = ctx.createRadialGradient(0, 0, 0, 0, 0, r * 1.5);\n      mirror.addColorStop(0, tone(dye, 0.15, 0.3)); mirror.addColorStop(0.5, tone(dye, 0.2, 0.12)); mirror.addColorStop(1, tone(dye, 0.2, 0));\n      ctx.fillStyle = mirror; ctx.beginPath(); ctx.arc(0, 0, r * 1.5, 0, Math.PI); ctx.fill(); ctx.restore();\n\n      // The glass itself: darker where you look through more of it.\n      tubePath(ctx, x, top, bottom, r);\n      const glass = ctx.createLinearGradient(x - r, 0, x + r, 0);\n      glass.addColorStop(0, \"rgba(96,118,138,0.5)\"); glass.addColorStop(0.09, \"rgba(170,190,205,0.2)\"); glass.addColorStop(0.5, \"rgba(255,255,255,0.1)\"); glass.addColorStop(0.91, \"rgba(170,190,205,0.22)\"); glass.addColorStop(1, \"rgba(96,118,138,0.55)\");\n      ctx.fillStyle = glass; ctx.fill();\n\n      ctx.save(); tubePath(ctx, x, top, bottom - wall, ri); ctx.clip();\n      if (lvl > 0.004) {\n        // The body of the liquid, lit like a cylinder: dark at both walls, a bright core left of centre.\n        ctx.beginPath(); ctx.moveTo(x - ri, ys - slope * ri); ctx.lineTo(x + ri, ys + slope * ri); ctx.lineTo(x + ri, bottom + 2); ctx.lineTo(x - ri, bottom + 2); ctx.closePath();\n        const body = ctx.createLinearGradient(x - ri, 0, x + ri, 0);\n        body.addColorStop(0, tone(dye, -0.5, 0.96)); body.addColorStop(0.14, tone(dye, -0.12, 0.94)); body.addColorStop(0.36, tone(dye, 0.3, 0.9)); body.addColorStop(0.62, tone(dye, 0, 0.93)); body.addColorStop(0.88, tone(dye, -0.26, 0.95)); body.addColorStop(1, tone(dye, -0.56, 0.97));\n        ctx.fillStyle = body; ctx.fill();\n        const depth = ctx.createLinearGradient(0, ys, 0, bottom);\n        depth.addColorStop(0, \"rgba(255,255,255,0.1)\"); depth.addColorStop(0.12, \"rgba(255,255,255,0)\"); depth.addColorStop(1, \"rgba(10,0,30,0.26)\");\n        ctx.fillStyle = depth; ctx.fill();\n        // The round bottom works as a lens and gathers light.\n        const lens = ctx.createRadialGradient(x + r * 0.2, bottom - r * 0.62, 0, x + r * 0.2, bottom - r * 0.62, r * 0.75);\n        lens.addColorStop(0, tone(dye, 0.75, 0.75)); lens.addColorStop(0.5, tone(dye, 0.4, 0.3)); lens.addColorStop(1, tone(dye, 0.4, 0));\n        ctx.fillStyle = lens; ctx.fillRect(x - r, bottom - r * 1.5, L.tw, r * 1.5);\n        // Bubbles.\n        for (const q of s.bub[i]) {\n          if (q.y - up < ys + 3) continue;\n          ctx.beginPath(); ctx.arc(q.x + Math.sin(q.w) * 1.4, q.y - up, q.r, 0, Math.PI * 2);\n          ctx.fillStyle = \"rgba(255,255,255,0.2)\"; ctx.fill(); ctx.lineWidth = 0.7; ctx.strokeStyle = \"rgba(255,255,255,0.75)\"; ctx.stroke();\n          ctx.beginPath(); ctx.arc(q.x + Math.sin(q.w) * 1.4 - q.r * 0.35, q.y - up - q.r * 0.35, q.r * 0.28, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,255,255,0.95)\"; ctx.fill();\n        }\n        // The surface, seen a little from above, with the meniscus climbing the wall.\n        ctx.save(); ctx.translate(x, ys); ctx.rotate(Math.atan(slope));\n        const ry = 5.2 * u, top0 = ctx.createLinearGradient(0, -ry, 0, ry);\n        top0.addColorStop(0, tone(dye, -0.18, 0.96)); top0.addColorStop(0.5, tone(dye, 0.34, 0.96)); top0.addColorStop(1, tone(dye, 0.62, 0.96));\n        ctx.beginPath(); ctx.ellipse(0, 0, ri, ry, 0, 0, Math.PI * 2); ctx.fillStyle = top0; ctx.fill();\n        ctx.beginPath(); ctx.ellipse(0, 0, ri - 0.5, ry - 0.5, 0, 0.08 * Math.PI, 0.92 * Math.PI); ctx.lineWidth = 1.1; ctx.strokeStyle = \"rgba(255,255,255,0.85)\"; ctx.stroke();\n        ctx.beginPath(); ctx.ellipse(-ri * 0.3, -ry * 0.1, ri * 0.34, ry * 0.3, 0, 0, Math.PI * 2); ctx.fillStyle = \"rgba(255,255,255,0.4)\"; ctx.fill();\n        ctx.restore();\n      }\n      // Graduations fired onto the glass in blue enamel.\n      ctx.lineCap = \"butt\";\n      for (let g = 0; g <= 10; g++) {\n        const gy = yZero - up - (g / 10) * (yZero - yFull), long = g % 5 === 0;\n        ctx.beginPath(); ctx.moveTo(x + ri - (long ? 15 : 8.5) * u, gy); ctx.lineTo(x + ri - 1.5 * u, gy);\n        ctx.lineWidth = long ? 1.3 : 0.9; ctx.strokeStyle = long ? \"rgba(246,250,255,0.95)\" : \"rgba(246,250,255,0.72)\"; ctx.stroke();\n        ctx.beginPath(); ctx.moveTo(x + ri - (long ? 15 : 8.5) * u, gy + 0.9); ctx.lineTo(x + ri - 1.5 * u, gy + 0.9);\n        ctx.lineWidth = 0.6; ctx.strokeStyle = \"rgba(20,40,70,0.38)\"; ctx.stroke();\n      }\n      ctx.restore();\n\n      // Wall, reflections and the rolled lip.\n      const edge = ctx.createLinearGradient(x - r, 0, x + r, 0);\n      edge.addColorStop(0, \"rgba(58,76,94,0.78)\"); edge.addColorStop(0.5, \"rgba(58,76,94,0.3)\"); edge.addColorStop(1, \"rgba(58,76,94,0.82)\");\n      tubePath(ctx, x, top, bottom, r); ctx.lineWidth = 1.1; ctx.strokeStyle = edge; ctx.stroke();\n      ctx.beginPath(); ctx.arc(x, bottom - r, r - 0.9, 0.2 * Math.PI, 0.8 * Math.PI); ctx.lineWidth = 1.3; ctx.strokeStyle = \"rgba(255,255,255,0.55)\"; ctx.stroke();\n      const sx = x - r + L.tw * (0.17 + 0.07 * s.light), sw = L.tw * 0.13, y0 = top + 12 * u, y1 = bottom - r * 0.9;\n      const streak = ctx.createLinearGradient(0, y0, 0, y1);\n      streak.addColorStop(0, \"rgba(255,255,255,0)\"); streak.addColorStop(0.08, \"rgba(255,255,255,0.95)\"); streak.addColorStop(0.55, \"rgba(255,255,255,0.72)\"); streak.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = streak; ctx.beginPath(); ctx.ellipse(sx + sw / 2, (y0 + y1) / 2, sw / 2, (y1 - y0) / 2, 0, 0, Math.PI * 2); ctx.fill();\n      ctx.fillStyle = \"rgba(255,255,255,0.3)\"; ctx.beginPath(); ctx.ellipse(sx + sw * 1.75, (y0 + y1) / 2, sw * 0.2, (y1 - y0) * 0.46, 0, 0, Math.PI * 2); ctx.fill();\n      ctx.fillStyle = \"rgba(255,255,255,0.5)\"; ctx.beginPath(); ctx.ellipse(x + r - L.tw * (0.1 - 0.03 * s.light), (y0 + y1) / 2, 1.1 * u, (y1 - y0) * 0.44, 0, 0, Math.PI * 2); ctx.fill();\n      const lipR = r + 2.6 * u, lipY = 4.6 * u;\n      ctx.beginPath(); ctx.ellipse(x, top, lipR, lipY, 0, 0, Math.PI * 2); ctx.fillStyle = \"rgba(236,243,248,0.5)\"; ctx.fill();\n      ctx.lineWidth = 2.2 * u; ctx.strokeStyle = \"rgba(92,112,130,0.62)\"; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(x, top - 0.4, lipR, lipY, 0, 1.08 * Math.PI, 1.62 * Math.PI); ctx.lineWidth = 1.5 * u; ctx.strokeStyle = \"rgba(255,255,255,0.98)\"; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(x, top + 0.6, lipR - 1, lipY - 0.6, 0, 0.1 * Math.PI, 0.5 * Math.PI); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.85)\"; ctx.stroke();\n\n      // The reading above the lip.\n      const on = live.current.active === i;\n      ctx.textAlign = \"center\"; ctx.textBaseline = \"alphabetic\";\n      ctx.font = `${on ? 800 : 600} ${(on ? 17 : 14) * Math.max(0.8, u)}px ui-monospace, SFMono-Regular, Menlo, monospace`;\n      ctx.fillStyle = on ? \"#0f172a\" : \"#475569\"; ctx.fillText(String(Math.round(s.shown[i])), x, top - 15 * u);\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(back, 0, 0);\n      ctx.setTransform(k, 0, 0, k, 0, 0);\n      for (let i = 0; i < n; i++) tube(i);\n      ctx.setTransform(1, 0, 0, 1, 0, 0);\n      ctx.drawImage(front, 0, 0);\n    };\n\n    let raf = 0, last = 0, t0 = 0, seen = true, moved = -1e9, nextSwap = 6.2;\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 (!live.current.touched && t > nextSwap) { nextSwap = t + 5.4; live.current.set = (live.current.set + 1) % sets.length; setSet(live.current.set); }\n      if (now - moved > 2600) s.lightTo = 0.7 * Math.sin(t * 0.55);\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      for (let i = 0; i < n; i++) {\n        const goal = t > 0.12 + i * 0.075 ? target(i) : 0, before = s.vel[i];\n        s.vel[i] += (70 * (goal - s.lv[i]) - 9.5 * s.vel[i]) * dt; s.lv[i] += s.vel[i] * dt;\n        if (s.lv[i] < 0) { s.lv[i] = 0; s.vel[i] = 0; }\n        s.shown[i] = Math.max(0, Math.min(FULL, s.lv[i] * FULL));\n        if (Math.abs(goal - s.lv[i]) < 0.004 && Math.abs(s.vel[i]) < 0.02) s.shown[i] = goal * FULL;\n        // Lifting a tube out of the rack, and the slosh that any change of speed sets off.\n        const wantLift = live.current.active === i ? 20 * u : 0, lb = s.liftV[i];\n        s.liftV[i] += (260 * (wantLift - s.lift[i]) - 20 * s.liftV[i]) * dt; s.lift[i] += s.liftV[i] * dt;\n        s.tv[i] += ((s.vel[i] - before) * 0.4 * (i % 2 ? 1 : -1) + (s.liftV[i] - lb) * 0.006 - 120 * s.tilt[i] * dt - 3.4 * s.tv[i] * dt);\n        s.tilt[i] += s.tv[i] * dt;\n        const rising = s.vel[i] > 0.12;\n        if (rising && Math.random() < dt * 26) s.bub[i].push({ x: tx(i) + (Math.random() - 0.5) * (L.tw - 14 * u), y: bot - 6 - Math.random() * 10, r: (1 + Math.random() * 2.2) * Math.max(0.75, u), v: 60 + Math.random() * 80, w: Math.random() * 6 });\n        else if (Math.random() < dt * 0.22 && s.lv[i] > 0.15) s.bub[i].push({ x: tx(i) + (Math.random() - 0.5) * (L.tw - 16 * u), y: bot - 6, r: (0.8 + Math.random() * 1.2) * Math.max(0.75, u), v: 30 + Math.random() * 30, w: Math.random() * 6 });\n        const surface = yZero - s.lv[i] * (yZero - yFull);\n        s.bub[i] = s.bub[i].filter((q) => { q.y -= q.v * dt; q.w += dt * 7; return q.y > surface; });\n      }\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => {\n      for (let i = 0; i < n; i++) { s.lv[i] = target(i); s.shown[i] = s.lv[i] * FULL; s.vel[i] = 0; s.tilt[i] = 0; s.lift[i] = live.current.active === i ? 20 * u : 0; s.bub[i] = []; }\n      draw();\n    };\n    const wake = () => { if (reduce) still(); else if (!raf) { last = performance.now(); raf = requestAnimationFrame(frame); } };\n    api.current = { poke: wake, flick: (i) => { s.tv[i] += (Math.random() > 0.5 ? 1 : -1) * 2.2; wake(); } };\n    fit(); wake();\n\n    // Passing the pointer across a tube knocks its liquid; the reflections follow it too.\n    const host = cv.parentElement;\n    let px = 0, pt = 0;\n    const onMove = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect(), x = ((e.clientX - b.left) / b.width) * L.W, now = performance.now();\n      moved = now; s.lightTo = Math.max(-1, Math.min(1, (x / L.W) * 2 - 1));\n      const speed = pt ? (x - px) / Math.max(8, now - pt) : 0;\n      for (let i = 0; i < n; i++) if (Math.abs(x - tx(i)) < r + 4 && Math.abs(px - tx(i)) >= r + 4) s.tv[i] += Math.max(-3, Math.min(3, speed * 2.4));\n      px = x; pt = now;\n    };\n    host?.addEventListener(\"pointermove\", onMove);\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      host?.removeEventListener(\"pointermove\", onMove);\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 point = (i: number) => { live.current.active = i; setActive(i); api.current?.poke(); };\n  const values = sets[set].values, total = values.reduce((a, b) => a + b, 0);\n  const pct = (v: number, of: number) => `${(v / of) * 100}%`;\n\n  return (\n    <div ref={stage} className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#e6e9ed] bg-[radial-gradient(110%_80%_at_50%_30%,#fbfcfd_0%,#e9ecf0_52%,#cfd5dc_100%)] px-4 py-12 sm:min-h-[760px]\">\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\" />\n        <ul aria-label={`${sets[set].label}, ${UNIT} per day`} className=\"absolute inset-0\">\n          {days.map((d, i) => (\n            <li key={d}>\n              <button\n                type=\"button\"\n                aria-label={`${dayNames[i]}: ${values[i]} ${UNIT}`}\n                onPointerEnter={() => point(i)}\n                onPointerLeave={() => point(-1)}\n                onFocus={() => { touch(); point(i); }}\n                onBlur={() => point(-1)}\n                onClick={() => { touch(); point(i); api.current?.flick(i); }}\n                style={{ left: pct(L.x0 + i * L.gap - L.gap / 2, L.W), top: pct(L.top - 34 * L.u, L.H), width: pct(L.gap, L.W), height: pct(L.base + 68 * L.u - L.top + 34 * L.u, L.H) }}\n                className=\"absolute cursor-pointer rounded-xl outline-none focus-visible:ring-2 focus-visible:ring-slate-900/70\"\n              />\n            </li>\n          ))}\n        </ul>\n      </div>\n\n      <div className=\"mt-8 flex flex-col items-center gap-4\">\n        <div role=\"group\" aria-label=\"Data set\" className=\"flex rounded-full bg-white/70 p-1 shadow-[inset_0_1px_2px_rgba(15,23,42,0.16),0_1px_0_rgba(255,255,255,0.9)]\">\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-slate-900 motion-reduce:transition-none ${set === i ? \"bg-slate-900 text-white shadow-[0_2px_6px_rgba(15,23,42,0.35)]\" : \"text-slate-600 hover:text-slate-900\"}`}\n            >\n              {d.label}\n            </button>\n          ))}\n        </div>\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-600\">\n          <span className=\"text-slate-900\">{active >= 0 ? `${dayNames[active]} ${values[active]}` : `${sets[set].label} ${total}`}</span>\n          <span className=\"mx-2 text-slate-400\">/</span>\n          {UNIT}, demo data\n        </p>\n      </div>\n\n      <table className=\"sr-only\">\n        <caption>{`${sets[set].label}: ${UNIT} per day (demo data)`}</caption>\n        <thead>\n          <tr><th scope=\"col\">Day</th><th scope=\"col\">{UNIT}</th></tr>\n        </thead>\n        <tbody>\n          {days.map((d, i) => (\n            <tr key={d}><th scope=\"row\">{dayNames[i]}</th><td>{values[i]}</td></tr>\n          ))}\n        </tbody>\n      </table>\n    </div>\n  );\n}\n"}]}
