{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"nitro-cold-brew-tumbler-loader-that-pours-cascades-and-settles-under-a-cream-head","type":"registry:component","title":"Nitro Cold Brew Tumbler Loader That Pours, Cascades and Settles Under a Cream Head","description":"Loader on a canvas: a tumbler is poured full of nitro coffee, the surge cascades down the wall and settles under a cream head. Free React and HTML code.","dependencies":[],"files":[{"path":"components/we-inc/nitro-cold-brew-tumbler-loader-that-pours-cascades-and-settles-under-a-cream-head.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst DRINK = \"Nitro cold brew\";\nconst VALUE: number | null = null; // 0 to 1 shows real progress as how far the glass has settled; null plays the loop\nconst COFFEE = \"#1b0d06\"; // the settled drink\nconst SURGE = \"#c08c56\"; // the cloudy cascade straight after the pour\nconst CREAM = \"#f5ead3\"; // the head\nconst POUR = 2.3, SETTLE = 7.5, HOLD = 2.2, DRAIN = 1.5; // seconds for each part of the loop\n\nconst W = 560, H = 580, CX = 280, TOP = 84, BOT = 488, RT = 102, RB = 75, WALL = 5, BASE = 34;\nconst rgb = (hex: string) => [parseInt(hex.slice(1, 3), 16), parseInt(hex.slice(3, 5), 16), parseInt(hex.slice(5, 7), 16)];\nconst mix = (a: string, b: string, t: number, al = 1) => {\n  const p = rgb(a), q = rgb(b);\n  return `rgba(${p.map((v, i) => Math.round(v + (q[i] - v) * t)).join(\",\")},${al})`;\n};\nconst clamp = (v: number, a = 0, b = 1) => Math.max(a, Math.min(b, v));\ntype Ctx = CanvasRenderingContext2D;\n\nexport default function NitroColdBrewTumblerLoader() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [label, setLabel] = useState(\"Settling\");\n  const [pct, setPct] = useState(VALUE === null ? 60 : Math.round(VALUE * 100));\n  const api = useRef<{ again: () => void; knock: (d: number) => void } | null>(null);\n\n  useEffect(() => {\n    const cv = canvas.current;\n    if (!cv) return;\n    const ctx = cv.getContext(\"2d\");\n    if (!ctx) return;\n    const reduce = window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n    const yF = BOT - BASE, yFull = TOP + 30; // the floor of the glass and the top of a full pour\n    const rO = (y: number) => RT + ((RB - RT) * (y - TOP)) / (BOT - TOP);\n    const rI = (y: number) => rO(y) - WALL;\n    const ec = (y: number) => 0.17 + (0.07 * (y - TOP)) / (BOT - TOP); // lower rings are seen more from above\n    // The slice of a tapered round glass between two heights, as seen from the front.\n    const slab = (c: Ctx, y0: number, y1: number, rf: (y: number) => number) => {\n      c.beginPath(); c.moveTo(CX - rf(y0), y0); c.lineTo(CX - rf(y1), y1);\n      c.ellipse(CX, y1, rf(y1), rf(y1) * ec(y1), 0, Math.PI, 0, true);\n      c.lineTo(CX + rf(y0), y0); c.ellipse(CX, y0, rf(y0), rf(y0) * ec(y0), 0, 0, Math.PI, true); c.closePath();\n    };\n    let k = 1, seed = 11;\n    const rand = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; };\n\n    // A tile of the surge: wavy bands of tiny bubbles, made once and scrolled.\n    const tile = document.createElement(\"canvas\"); tile.width = tile.height = 256;\n    {\n      const c = tile.getContext(\"2d\");\n      if (c) {\n        const img = c.createImageData(256, 256), a = rgb(SURGE), hi = rgb(CREAM);\n        const hash = (x: number, y: number, o: number) => { const n = Math.sin(x * 127.1 + y * 311.7 + o * 74.7) * 43758.5453; return n - Math.floor(n); };\n        const vn = (x: number, y: number, nx: number, ny: number, o: number) => {\n          const xi = Math.floor(x), yi = Math.floor(y), fx = x - xi, fy = y - yi, sx = fx * fx * (3 - 2 * fx), sy = fy * fy * (3 - 2 * fy);\n          const p = hash(xi % nx, yi % ny, o), q = hash((xi + 1) % nx, yi % ny, o), r = hash(xi % nx, (yi + 1) % ny, o), t = hash((xi + 1) % nx, (yi + 1) % ny, o);\n          return p + (q - p) * sx + (r - p) * sy + (p - q - r + t) * sx * sy;\n        };\n        for (let y = 0; y < 256; y++) for (let x = 0; x < 256; x++) {\n          const u = x / 256, v = y / 256;\n          const n = vn(u * 3, v * 4, 3, 4, 1) * 0.5 + vn(u * 6, v * 9, 6, 9, 2) * 0.3 + vn(u * 14, v * 20, 14, 20, 3) * 0.2;\n          const band = 0.5 + 0.5 * Math.sin(v * Math.PI * 14 + n * 10 + Math.sin(u * Math.PI * 2) * 1.8);\n          const d = clamp(0.1 + 0.9 * band * (0.45 + n)), l = clamp(band * n * 1.1 - 0.3), i = (y * 256 + x) * 4;\n          img.data[i] = a[0] + (hi[0] - a[0]) * l; img.data[i + 1] = a[1] + (hi[1] - a[1]) * l; img.data[i + 2] = a[2] + (hi[2] - a[2]) * l; img.data[i + 3] = 255 * d;\n        }\n        c.putImageData(img, 0, 0);\n      }\n    }\n    const fx = document.createElement(\"canvas\"), lace = document.createElement(\"canvas\");\n    const fxX = CX - RT, fxY = yFull - 14, fxW = RT * 2, fxH = yF - yFull + 44;\n    let fctx: Ctx | null = null, pat: CanvasPattern | null = null, kf = 1;\n\n    const fit = () => {\n      const b = cv.getBoundingClientRect(), dpr = Math.min(2, window.devicePixelRatio || 1);\n      cv.width = Math.max(2, Math.round(b.width * dpr)); cv.height = Math.round((cv.width * H) / W); k = cv.width / W;\n      kf = Math.min(k, 1.2) * 0.8; /* the surge is soft, so it is painted small and scaled up */ fx.width = Math.ceil(fxW * kf); fx.height = Math.ceil(fxH * kf);\n      fctx = fx.getContext(\"2d\"); pat = fctx ? fctx.createPattern(tile, \"repeat\") : null;\n      // Foam left behind on the wall each time the level drops.\n      lace.width = cv.width; lace.height = cv.height;\n      const c = lace.getContext(\"2d\");\n      if (!c) return;\n      c.setTransform(k, 0, 0, k, 0, 0); c.lineCap = \"round\"; seed = 11;\n      for (let ring = 0; ring < 9; ring++) {\n        const y = yFull + 12 + ring * ((yF - yFull - 40) / 8) + rand() * 10, r = rI(y) - 1.2;\n        for (let j = 0; j < 26; j++) {\n          const a0 = rand() * Math.PI * 2, len = 0.05 + rand() * 0.3, front = Math.sin(a0 + len / 2) > 0;\n          c.beginPath(); c.ellipse(CX, y + (rand() - 0.5) * 7, r, r * ec(y), 0, a0, a0 + len);\n          c.lineWidth = 0.8 + rand() * rand() * 5; c.strokeStyle = mix(CREAM, \"#ffffff\", 0.3, (front ? 0.8 : 0.36) * (0.4 + rand() * 0.6)); c.stroke();\n        }\n      }\n    };\n\n    const s = { phase: \"settle\", pt: SETTLE * 0.03, level: 1, settle: 0.05, tilt: 0, tv: 0, lace: 0, scroll: 0, stream: 0, light: 0, lightTo: 0, t: 0 };\n\n    const draw = () => {\n      ctx.setTransform(k, 0, 0, k, 0, 0); ctx.clearRect(0, 0, W, H);\n      const ys = yF - s.level * (yF - yFull), has = s.level > 0.004;\n      const head = (7 + 27 * s.settle) * clamp(s.level * 4), yi = Math.min(yF - 2, ys + head);\n      const lx = s.light * 10;\n\n      // On the table: a soft pool of shade, a tight contact line and the warm light the drink throws.\n      const floorY = BOT + RB * ec(BOT) * 0.55;\n      ctx.save(); ctx.translate(CX + 34, floorY + 4); ctx.scale(1, 0.2);\n      let g = ctx.createRadialGradient(0, 0, 10, 0, 0, 210);\n      g.addColorStop(0, \"rgba(70,48,28,0.5)\"); g.addColorStop(0.45, \"rgba(70,48,28,0.2)\"); g.addColorStop(1, \"rgba(70,48,28,0)\");\n      ctx.fillStyle = g; ctx.beginPath(); ctx.arc(0, 0, 210, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n      ctx.save(); ctx.translate(CX + 4, floorY + 1); ctx.scale(1, 0.24);\n      g = ctx.createRadialGradient(0, 0, RB * 0.6, 0, 0, RB * 1.25);\n      g.addColorStop(0, \"rgba(40,24,12,0.75)\"); g.addColorStop(1, \"rgba(40,24,12,0)\");\n      ctx.fillStyle = g; ctx.beginPath(); ctx.arc(0, 0, RB * 1.25, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n      ctx.save(); ctx.translate(CX + 96, floorY + 9); ctx.rotate(-0.05); ctx.scale(1, 0.16);\n      g = ctx.createRadialGradient(0, 0, 0, 0, 0, 74);\n      g.addColorStop(0, `rgba(255,190,110,${0.5 * s.level})`); g.addColorStop(0.5, `rgba(240,150,70,${0.2 * s.level})`); g.addColorStop(1, \"rgba(240,150,70,0)\");\n      ctx.fillStyle = g; ctx.beginPath(); ctx.arc(0, 0, 74, 0, Math.PI * 2); ctx.fill(); ctx.restore();\n\n      // The far wall of the empty glass.\n      slab(ctx, TOP, BOT, rO);\n      g = ctx.createLinearGradient(CX - RT, 0, CX + RT, 0);\n      g.addColorStop(0, \"rgba(120,104,84,0.3)\"); g.addColorStop(0.16, \"rgba(255,255,255,0.42)\"); g.addColorStop(0.5, \"rgba(255,255,255,0.2)\"); g.addColorStop(0.84, \"rgba(236,226,210,0.3)\"); g.addColorStop(1, \"rgba(110,94,74,0.34)\");\n      ctx.fillStyle = g; ctx.fill();\n      ctx.beginPath(); ctx.ellipse(CX, yF, rI(yF), rI(yF) * ec(yF), 0, 0, Math.PI * 2);\n      ctx.fillStyle = \"rgba(255,255,255,0.22)\"; ctx.fill();\n      // Lacing above the drink.\n      if (s.lace > 0.01) {\n        ctx.save(); ctx.beginPath(); ctx.rect(0, TOP, W, Math.max(0, ys - TOP - 2)); ctx.clip();\n        ctx.globalAlpha = s.lace; ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.drawImage(lace, 0, 0); ctx.restore(); ctx.setTransform(k, 0, 0, k, 0, 0);\n      }\n\n      if (has) {\n        ctx.save(); slab(ctx, TOP + 4, yF, rI); ctx.clip();\n        ctx.translate(CX, ys); ctx.rotate(s.tilt); ctx.translate(-CX, -ys);\n        // The drink itself.\n        slab(ctx, ys, yF + 30, rI); ctx.fillStyle = COFFEE; ctx.fill();\n        // The surge: bands of bubbles falling down the wall while the dark drink clears from the floor up.\n        if (fctx && pat && s.settle < 0.995) {\n          const front = yF + 60 - s.settle * (yF + 60 - (yi - 34));\n          fctx.setTransform(kf, 0, 0, kf, 0, 0); fctx.globalCompositeOperation = \"source-over\"; fctx.clearRect(0, 0, fxW, fxH);\n          fctx.fillStyle = mix(SURGE, COFFEE, 0.42, 0.9); fctx.fillRect(0, 0, fxW, fxH);\n          for (const [sc, sp, al] of [[1, 1, 0.9], [0.62, 1.6, 0.5]]) {\n            const o = (s.scroll * sp) % (256 * sc);\n            fctx.save(); fctx.globalAlpha = al; fctx.translate(sp * 37, o - 256 * sc); fctx.scale(sc, sc); fctx.fillStyle = pat;\n            fctx.fillRect(-120, 0, fxW / sc + 240, (fxH + 256 * sc) / sc); fctx.restore();\n          }\n          fctx.globalCompositeOperation = \"destination-in\";\n          const m = fctx.createLinearGradient(0, front - fxY - 56, 0, front - fxY + 18);\n          m.addColorStop(0, \"rgba(0,0,0,1)\"); m.addColorStop(0.5, \"rgba(0,0,0,0.8)\"); m.addColorStop(1, \"rgba(0,0,0,0)\");\n          fctx.fillStyle = m; fctx.fillRect(0, 0, fxW, fxH);\n          ctx.drawImage(fx, fxX, fxY, fxW, fxH);\n        }\n        // A round glass of dark liquid: black through the middle, amber where the light gets through at the sides.\n        slab(ctx, ys, yF + 30, rI);\n        g = ctx.createLinearGradient(CX - RT, 0, CX + RT, 0);\n        g.addColorStop(0, \"rgba(255,150,60,0.34)\"); g.addColorStop(0.07, \"rgba(120,50,10,0.2)\"); g.addColorStop(0.2, \"rgba(10,4,0,0.3)\"); g.addColorStop(0.5, \"rgba(10,4,0,0.04)\");\n        g.addColorStop(0.8, \"rgba(10,4,0,0.34)\"); g.addColorStop(0.94, \"rgba(140,60,14,0.22)\"); g.addColorStop(1, \"rgba(255,160,70,0.4)\");\n        ctx.fillStyle = g; ctx.fill();\n        g = ctx.createRadialGradient(CX + 34, yF + 4, 0, CX + 34, yF + 4, 120);\n        g.addColorStop(0, \"rgba(214,112,36,0.5)\"); g.addColorStop(0.5, \"rgba(170,80,20,0.16)\"); g.addColorStop(1, \"rgba(170,80,20,0)\");\n        ctx.save(); ctx.translate(0, yF + 4); ctx.scale(1, 0.5); ctx.translate(0, -(yF + 4)); ctx.fillStyle = g; ctx.fillRect(CX - RT, yF - 140, RT * 2, 300); ctx.restore();\n\n        // Dark liquid mirrors the room: one broad soft window and a thin one beside it.\n        g = ctx.createLinearGradient(0, yi, 0, yF);\n        g.addColorStop(0, \"rgba(255,236,214,0)\"); g.addColorStop(0.12, \"rgba(255,236,214,0.13)\"); g.addColorStop(0.7, \"rgba(255,236,214,0.05)\"); g.addColorStop(1, \"rgba(255,236,214,0)\");\n        ctx.fillStyle = g;\n        ctx.beginPath(); ctx.moveTo(CX - rI(yi) + 46 + lx, yi); ctx.lineTo(CX - rI(yi) + 84 + lx, yi); ctx.lineTo(CX - rI(yF) + 70 + lx, yF); ctx.lineTo(CX - rI(yF) + 40 + lx, yF); ctx.closePath(); ctx.fill();\n\n        // The head: cream where it sits, tan where it is still feeding from the surge below.\n        for (let j = 5; j >= 1; j--) {\n          ctx.beginPath(); ctx.ellipse(CX, yi + j * 2.6, rI(yi), rI(yi) * ec(yi), 0, 0, Math.PI);\n          ctx.lineWidth = 5; ctx.strokeStyle = mix(CREAM, SURGE, 0.35 + j * 0.1, 0.5 - j * 0.085); ctx.stroke();\n        }\n        slab(ctx, ys, yi, rI);\n        g = ctx.createLinearGradient(0, ys, 0, yi + rI(yi) * ec(yi));\n        g.addColorStop(0, mix(CREAM, \"#ffffff\", 0.25)); g.addColorStop(0.6, CREAM); g.addColorStop(1, mix(CREAM, SURGE, 0.42));\n        ctx.fillStyle = g; ctx.fill();\n        g = ctx.createLinearGradient(CX - RT, 0, CX + RT, 0);\n        g.addColorStop(0, \"rgba(120,78,36,0.5)\"); g.addColorStop(0.14, \"rgba(120,78,36,0.08)\"); g.addColorStop(0.32, \"rgba(255,255,255,0.3)\"); g.addColorStop(0.52, \"rgba(255,255,255,0)\");\n        g.addColorStop(0.84, \"rgba(120,78,36,0.12)\"); g.addColorStop(1, \"rgba(104,64,28,0.55)\");\n        ctx.fillStyle = g; ctx.fill();\n        // Its top, seen a little from above.\n        const rs = rI(ys), ry = rs * ec(ys);\n        ctx.beginPath(); ctx.ellipse(CX, ys, rs, ry, 0, 0, Math.PI * 2);\n        g = ctx.createRadialGradient(CX - rs * 0.3, ys - ry * 0.3, 2, CX, ys, rs);\n        g.addColorStop(0, \"#fffaf0\"); g.addColorStop(0.6, mix(CREAM, \"#ffffff\", 0.2)); g.addColorStop(1, mix(CREAM, SURGE, 0.1));\n        ctx.fillStyle = g; ctx.fill();\n        ctx.beginPath(); ctx.ellipse(CX, ys, rs - 0.6, ry - 0.4, 0, 0.06 * Math.PI, 0.94 * Math.PI); ctx.lineWidth = 1.4; ctx.strokeStyle = \"rgba(255,255,255,0.85)\"; ctx.stroke();\n        seed = 5;\n        for (let j = 0; j < 46; j++) {\n          const a = rand() * Math.PI * 2, d = Math.sqrt(rand()) * 0.92, br = 0.5 + rand() * rand() * 2.6;\n          ctx.beginPath(); ctx.ellipse(CX + Math.cos(a) * d * rs, ys + Math.sin(a) * d * ry, br, br * 0.5, 0, 0, Math.PI * 2);\n          ctx.fillStyle = \"rgba(150,104,58,0.2)\"; ctx.fill();\n        }\n        if (s.stream > 0.02) {\n          ctx.beginPath(); ctx.ellipse(CX + 26, ys + 2, 26 * s.stream, 8 * s.stream, 0, 0, Math.PI * 2); ctx.fillStyle = mix(SURGE, CREAM, 0.45, 0.85); ctx.fill();\n          ctx.beginPath(); ctx.ellipse(CX + 26, ys + 2, (38 + 6 * Math.sin(s.t * 19)) * s.stream, 11 * s.stream, 0, 0, Math.PI * 2); ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(255,255,255,0.7)\"; ctx.stroke();\n        }\n        ctx.restore();\n      }\n\n      // The pour.\n      if (s.stream > 0.02) {\n        const w = 6.5 * s.stream, x = CX + 26, end = Math.min(yF, ys + 4);\n        ctx.beginPath(); ctx.moveTo(x - w, -6);\n        for (let y = 0; y <= end; y += 18) ctx.lineTo(x - w * (0.72 + 0.2 * Math.sin(y * 0.05 - s.t * 22)) + Math.sin(y * 0.03 + s.t * 9) * 0.8, y);\n        for (let y = end; y >= -6; y -= 18) ctx.lineTo(x + w * (0.72 + 0.2 * Math.sin(y * 0.05 - s.t * 22 + 2)) + Math.sin(y * 0.03 + s.t * 9) * 0.8, y);\n        ctx.closePath();\n        g = ctx.createLinearGradient(x - w, 0, x + w, 0);\n        g.addColorStop(0, mix(COFFEE, SURGE, 0.25)); g.addColorStop(0.3, mix(SURGE, CREAM, 0.5)); g.addColorStop(0.46, mix(COFFEE, SURGE, 0.6)); g.addColorStop(1, COFFEE);\n        ctx.fillStyle = g; ctx.fill();\n      }\n\n      // The heavy foot of the glass: a lens that carries the colour of the drink and one hard line of light.\n      const rF = rO(yF);\n      ctx.save();\n      ctx.beginPath(); ctx.moveTo(CX - rF, yF); ctx.lineTo(CX - RB, BOT); ctx.ellipse(CX, BOT, RB, RB * ec(BOT), 0, Math.PI, 0, true); ctx.lineTo(CX + rF, yF);\n      ctx.ellipse(CX, yF, rF, rF * ec(yF), 0, 0, Math.PI, false); ctx.closePath(); ctx.clip();\n      g = ctx.createLinearGradient(CX - RB, 0, CX + RB, 0);\n      const dk = mix(COFFEE, \"#7a3f16\", 0.5 * s.level, 0.25 + 0.6 * clamp(s.level * 5));\n      g.addColorStop(0, \"rgba(72,58,42,0.72)\"); g.addColorStop(0.06, \"rgba(255,255,255,0.86)\"); g.addColorStop(0.17, \"rgba(214,202,184,0.5)\"); g.addColorStop(0.3, dk);\n      g.addColorStop(0.62, dk); g.addColorStop(0.76, \"rgba(255,236,208,0.7)\"); g.addColorStop(0.9, \"rgba(255,255,255,0.86)\"); g.addColorStop(1, \"rgba(80,64,46,0.72)\");\n      ctx.fillStyle = g; ctx.fillRect(CX - RT, yF, RT * 2, BASE + 40);\n      g = ctx.createLinearGradient(0, yF + 8, 0, BOT + RB * ec(BOT));\n      g.addColorStop(0, \"rgba(40,24,10,0.36)\"); g.addColorStop(0.4, \"rgba(40,24,10,0)\"); g.addColorStop(0.78, \"rgba(255,255,255,0)\"); g.addColorStop(1, \"rgba(255,255,255,0.6)\");\n      ctx.fillStyle = g; ctx.fillRect(CX - RT, yF, RT * 2, BASE + 40);\n      ctx.beginPath(); ctx.ellipse(CX + 18 + lx, BOT - 7, RB * 0.5, RB * ec(BOT) * 0.5, 0, 0.1 * Math.PI, 0.9 * Math.PI); ctx.lineWidth = 3; ctx.strokeStyle = `rgba(255,214,160,${0.4 + 0.5 * s.level})`; ctx.stroke();\n      ctx.restore();\n      ctx.beginPath(); ctx.ellipse(CX, yF, rF - 1, (rF - 1) * ec(yF), 0, 0.04 * Math.PI, 0.96 * Math.PI); ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(255,255,255,0.5)\"; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(CX, BOT, RB - 0.8, (RB - 0.8) * ec(BOT), 0, 0.03 * Math.PI, 0.97 * Math.PI); ctx.lineWidth = 1.6; ctx.strokeStyle = \"rgba(255,255,255,0.8)\"; ctx.stroke();\n\n      // The wall: a dark line at each silhouette, a lit line inside it, and the studio window down the left.\n      const side = (d: number, y0: number, y1: number, w: number, col: string, dir: number) => {\n        ctx.beginPath(); ctx.moveTo(CX + dir * (rO(y0) - d), y0); ctx.lineTo(CX + dir * (rO(y1) - d), y1); ctx.lineWidth = w; ctx.strokeStyle = col; ctx.stroke();\n      };\n      ctx.lineCap = \"round\";\n      for (const dir of [-1, 1]) {\n        side(0.6, TOP, BOT, 1.3, \"rgba(70,54,36,0.62)\", dir); side(2.6, TOP + 3, BOT - 3, 1.6, \"rgba(255,255,255,0.78)\", dir); side(WALL + 0.8, TOP + 6, yF - 4, 1, \"rgba(60,44,28,0.26)\", dir);\n      }\n      const streak = (d0: number, d1: number, y0: number, y1: number, al: number) => {\n        ctx.beginPath(); ctx.moveTo(CX - rO(y0) + d0 + lx, y0); ctx.lineTo(CX - rO(y0) + d1 + lx, y0); ctx.lineTo(CX - rO(y1) + d1 * 0.8 + lx, y1); ctx.lineTo(CX - rO(y1) + d0 * 0.8 + lx, y1); ctx.closePath();\n        const q = ctx.createLinearGradient(0, y0, 0, y1);\n        q.addColorStop(0, \"rgba(255,255,255,0)\"); q.addColorStop(0.07, `rgba(255,255,255,${al})`); q.addColorStop(0.5, `rgba(255,255,255,${al * 0.7})`); q.addColorStop(0.93, `rgba(255,255,255,${al * 0.5})`); q.addColorStop(1, \"rgba(255,255,255,0)\");\n        ctx.fillStyle = q; ctx.fill();\n      };\n      streak(17, 31, TOP + 16, yF - 10, 0.74); streak(36, 39.5, TOP + 22, yF - 24, 0.42); streak(RT * 2 - 30, RT * 2 - 26, TOP + 18, yF - 14, 0.5);\n      streak(RT * 2 - 76, RT * 2 - 44, TOP + 28, TOP + 150, 0.13);\n      // The rim: cut glass has a flat top that catches light all the way round.\n      ctx.beginPath(); ctx.ellipse(CX, TOP, RT - 0.6, RT * ec(TOP), 0, 0, Math.PI * 2); ctx.lineWidth = 3.4; ctx.strokeStyle = \"rgba(246,240,230,0.85)\"; ctx.stroke();\n      ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(84,66,46,0.5)\"; ctx.beginPath(); ctx.ellipse(CX, TOP, RT + 1, RT * ec(TOP) + 1, 0, 0, Math.PI * 2); ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(CX, TOP, RT - 3.4, RT * ec(TOP) - 2.2, 0, 0, Math.PI * 2); ctx.strokeStyle = \"rgba(84,66,46,0.34)\"; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(CX, TOP, RT - 0.8, RT * ec(TOP) - 0.2, 0, 1.12 * Math.PI + s.light * 0.2, 1.52 * Math.PI + s.light * 0.2); ctx.lineWidth = 2.4; ctx.strokeStyle = \"#ffffff\"; ctx.stroke();\n      ctx.beginPath(); ctx.ellipse(CX, TOP, RT - 0.8, RT * ec(TOP) - 0.2, 0, 0.2 * Math.PI, 0.44 * Math.PI); ctx.lineWidth = 2; ctx.strokeStyle = \"rgba(255,255,255,0.92)\"; ctx.stroke();\n    };\n\n    let lastLabel = \"\", lastPct = -1;\n    const say = (text: string, p: number) => {\n      if (text !== lastLabel) { lastLabel = text; setLabel(text); }\n      if (p !== lastPct) { lastPct = p; setPct(p); }\n    };\n    const ease = (p: number) => 1 - Math.pow(1 - clamp(p), 1.25);\n    const go = (phase: string) => { s.phase = phase; s.pt = 0; };\n\n    let raf = 0, last = 0, seen = true;\n    const frame = (now: number) => {\n      raf = 0;\n      if (!last) last = now;\n      const dt = Math.min(0.04, (now - last) / 1000); last = now;\n      s.t += dt; s.pt += dt;\n      if (VALUE !== null) {\n        s.level = 1; s.settle += (clamp(VALUE) - s.settle) * Math.min(1, dt * 3); s.scroll += dt * 52 * (1 - s.settle * 0.6);\n        say(VALUE >= 1 ? \"Ready\" : \"Settling\", Math.round(clamp(VALUE) * 100));\n      } else if (s.phase === \"settle\") {\n        const p = s.pt / SETTLE; s.settle = ease(p); s.level = 1; s.stream += (0 - s.stream) * Math.min(1, dt * 9);\n        s.scroll += dt * (150 - 104 * clamp(p * 4)) * (1 - s.settle * 0.55); say(\"Settling\", Math.min(99, Math.round(s.settle * 100)));\n        if (p >= 1) go(\"hold\");\n      } else if (s.phase === \"hold\") {\n        s.settle = 1; say(\"Ready\", 100);\n        if (s.pt > HOLD) { go(\"drain\"); s.lace = 1; }\n      } else if (s.phase === \"drain\") {\n        const p = clamp(s.pt / DRAIN); s.level = 1 - p * p * (3 - 2 * p); say(\"Next glass\", 100);\n        if (p >= 1) go(\"pause\");\n      } else if (s.phase === \"pause\") {\n        s.level = 0; s.settle = 0; if (s.pt > 0.45) go(\"pour\");\n      } else {\n        const p = clamp((s.pt - 0.18) / POUR); s.level = p; s.settle = 0; s.scroll += dt * 170;\n        s.stream += ((s.pt < POUR + 0.05 ? 1 : 0) - s.stream) * Math.min(1, dt * 10); s.tv += (Math.random() - 0.5) * dt * 0.9; say(\"Pouring\", 0);\n        if (p >= 1) { go(\"settle\"); s.lace = 0; }\n      }\n      s.tv += (-90 * s.tilt - 5 * s.tv) * dt; s.tilt += s.tv * dt;\n      s.light += (s.lightTo - s.light) * Math.min(1, dt * 5);\n      draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    const still = () => { s.level = 1; s.settle = VALUE === null ? 1 : clamp(VALUE); s.stream = 0; s.tilt = 0; say(s.settle >= 1 ? \"Ready\" : \"Settling\", Math.round(s.settle * 100)); draw(); };\n    const wake = () => { if (reduce) still(); else if (!raf) { last = 0; raf = requestAnimationFrame(frame); } };\n    api.current = {\n      again: () => { if (VALUE === null && !reduce && (s.phase === \"settle\" || s.phase === \"hold\")) { go(\"drain\"); s.lace = 1; } wake(); },\n      knock: (d) => { s.tv += d; wake(); },\n    };\n    fit(); wake();\n\n    const host = cv.parentElement;\n    let px = 0;\n    const onMove = (e: PointerEvent) => {\n      const b = cv.getBoundingClientRect(), x = ((e.clientX - b.left) / b.width) * W;\n      s.lightTo = clamp((x / W) * 2 - 1, -1, 1);\n      for (const edge of [CX - RT, CX + RT]) if ((x - edge) * (px - edge) < 0) s.tv += clamp((x - px) * 0.012, -0.5, 0.5);\n      px = x;\n    };\n    const onLeave = () => { s.lightTo = 0; };\n    host?.addEventListener(\"pointermove\", onMove); host?.addEventListener(\"pointerleave\", onLeave);\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); host?.removeEventListener(\"pointerleave\", onLeave);\n      ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis);\n    };\n  }, []);\n\n  return (\n    <div className=\"flex min-h-[640px] w-full flex-col items-center justify-center bg-[#e9e1d3] bg-[radial-gradient(120%_85%_at_50%_34%,#fbf8f2_0%,#ece5d8_50%,#d3c8b5_100%)] px-4 py-8 sm:min-h-[760px]\">\n      <div className=\"relative w-full\" style={{ maxWidth: 640, aspectRatio: `${W} / ${H}` }}>\n        <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n        <button\n          type=\"button\"\n          aria-label={`Pour another ${DRINK.toLowerCase()}`}\n          onClick={() => { api.current?.knock(0.5); api.current?.again(); }}\n          className=\"absolute left-1/2 top-[14%] h-[72%] w-[42%] -translate-x-1/2 cursor-pointer rounded-[28px] outline-none focus-visible:ring-2 focus-visible:ring-stone-900/70 focus-visible:ring-offset-4 focus-visible:ring-offset-transparent\"\n        />\n      </div>\n      <div\n        role=\"progressbar\"\n        aria-label={`${DRINK}: ${label.toLowerCase()}`}\n        aria-valuemin={0}\n        aria-valuemax={100}\n        aria-valuenow={pct}\n        className=\"flex items-baseline gap-3 font-mono text-[11px] uppercase tracking-[0.2em] text-stone-600\"\n      >\n        <span className=\"text-stone-900\">{DRINK}</span>\n        <span aria-hidden=\"true\" className=\"text-stone-400\">/</span>\n        <span>{label}</span>\n        <span className=\"w-[4ch] text-right tabular-nums text-stone-900\">{pct}%</span>\n      </div>\n    </div>\n  );\n}\n"}]}
