{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"glass-rain-gauge-progress-meter-that-fills-drop-by-drop-with-a-meniscus-and-tips-out-when-full","type":"registry:component","title":"Glass Rain Gauge Progress Meter That Fills Drop by Drop with a Meniscus and Tips Out When Full","description":"Progress meter drawn as a glass rain gauge: drops land, the water rises against a printed scale and a tap pours it out. Free React code and HTML to copy.","dependencies":[],"files":[{"path":"components/we-inc/glass-rain-gauge-progress-meter-that-fills-drop-by-drop-with-a-meniscus-and-tips-out-when-full.tsx","type":"registry:component","content":"import { useEffect, useRef, useState } from \"react\";\n\n// Everything a visitor is likely to change sits here.\nconst gauge = { label: \"Rain collected\", unit: \"mm\" };\nconst CAPACITY = 50; // what the top line of the printed scale reads\nconst START = 0.6; // how full it is on load, from 0 to 1\nconst PER_DROP = 0.0034; // how much each caught drop adds\nconst EVERY = 0.15; // seconds between drops that land inside the glass\nconst LOOP = true; // tip itself out a few seconds after it reaches the top line\nconst WATER = [38, 132, 158]; // red, green and blue of the water\n\nconst W = 720, H = 700, CX = 360, GY = 596, HORIZON = 476; // canvas, centre line, table contact, back edge of the table\nconst HC = 452, R = 70, WALL = 5.5, RI = R - WALL, EL = 0.2, FOOT = 106; // tube height and radius, how flat the ellipses are\nconst Y0 = -36, Y100 = -404, FLOOR = -30, MID = -HC / 2;\nconst TAU = Math.PI * 2;\nconst clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, v));\nconst rgba = (c: number[], m: number, a: number) => `rgba(${(c[0] * m) | 0},${(c[1] * m) | 0},${(c[2] * m) | 0},${a})`;\n\nexport default function GlassRainGaugeProgressMeter() {\n  const canvas = useRef<HTMLCanvasElement>(null);\n  const [pct, setPct] = useState(Math.round(START * 100));\n  const api = useRef<{ tip: () => 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    let k = 1, ox = 0, VW = W, VH = H;\n    const fit = () => {\n      const cw = cv.clientWidth || W, ch = cv.clientHeight || H, sc = Math.min(cw / 560, (ch - 40) / H), dpr = Math.min(2, window.devicePixelRatio || 1);\n      k = sc * dpr; VW = cw / sc; VH = ch / sc; ox = (VW - W) / 2;\n      cv.width = Math.round(cw * dpr); cv.height = Math.round(ch * dpr);\n    };\n    const rnd = Math.random;\n    const s = { p: START, show: START, wob: 0, wobV: 0, th: 0, thV: 0, tipT: -1, area: 0, t: 0, next: 0, full: 0, said: -1 };\n    const rain: { x: number; y: number; gy: number; sp: number }[] = [];\n    const caught: { x: number; y: number }[] = [];\n    const ripples: { x: number; age: number }[] = [];\n    const beads: { x: number; y: number; vx: number; vy: number; life: number; r: number }[] = [];\n    const bubbles: { x: number; y: number; r: number; ph: number }[] = [];\n    const rings: { x: number; y: number; age: number }[] = [];\n    const clings = [[-38, -300, 3.4], [22, -352, 2.6], [41, -214, 3], [-14, -420, 2.2], [-46, -150, 2.4], [34, -96, 3.2], [8, -262, 2], [-30, -388, 1.8], [47, -330, 2], [-8, -122, 2.3]].map(([x, y, r]) => ({ x, y, r, v: 0 }));\n    let puddle = 0;\n    const sow = (d: { x: number; y: number; gy: number; sp: number }, fresh: boolean) => {\n      d.x = rnd() * (VW + 200) - 100 - ox; d.y = fresh ? rnd() * VH : -30 - rnd() * 90; d.sp = 760 + rnd() * 380;\n      d.gy = HORIZON + 24 + rnd() * rnd() * (VH - HORIZON - 30);\n    };\n    const pour = () => { for (let i = 0; i < 34 * clamp(VW / W, 1, 2.4); i++) { const d = { x: 0, y: 0, gy: 0, sp: 0 }; sow(d, true); rain.push(d); } };\n\n    const tilted = () => Math.abs(s.th) > 0.004 || s.tipT >= 0;\n    const lift = () => Math.sin(clamp(s.th / 2.25, 0, 1) * Math.PI * 0.5) * 78;\n    const world = (lx: number, ly: number): [number, number] => {\n      const c = Math.cos(s.th), n = Math.sin(s.th), dy = ly - MID;\n      return [CX + lx * c - dy * n, GY + MID - lift() + lx * n + dy * c];\n    };\n    // Area of the tube's inside that lies below a level line, used while the glass is tipped.\n    const inside = (): [number, number][] => [world(-RI, FLOOR), world(RI, FLOOR), world(RI, -HC), world(-RI, -HC)];\n    const below = (poly: [number, number][], level: number) => {\n      const out: [number, number][] = [];\n      for (let i = 0; i < poly.length; i++) {\n        const a = poly[i], b = poly[(i + 1) % poly.length], ia = a[1] >= level, ib = b[1] >= level;\n        if (ia) out.push(a);\n        if (ia !== ib) { const f = (level - a[1]) / (b[1] - a[1]); out.push([a[0] + (b[0] - a[0]) * f, level]); }\n      }\n      return out;\n    };\n    const areaOf = (poly: [number, number][]) => { let a = 0; for (let i = 0; i < poly.length; i++) { const p = poly[i], q = poly[(i + 1) % poly.length]; a += p[0] * q[1] - q[0] * p[1]; } return Math.abs(a) / 2; };\n    const levelFor = (poly: [number, number][], area: number) => {\n      let lo = Math.min(...poly.map((p) => p[1])), hi = Math.max(...poly.map((p) => p[1]));\n      for (let i = 0; i < 14; i++) { const m = (lo + hi) / 2; if (areaOf(below(poly, m)) > area) lo = m; else hi = m; }\n      return (lo + hi) / 2;\n    };\n    const surfaceY = () => Y0 + (Y100 - Y0) * clamp(s.show, 0, 1.02) + s.wob;\n\n    const stage = () => {\n      const wall = ctx.createLinearGradient(0, 0, 0, HORIZON);\n      wall.addColorStop(0, \"#f3f1ec\"); wall.addColorStop(1, \"#dcd8d0\");\n      ctx.fillStyle = wall; ctx.fillRect(-ox, 0, VW, HORIZON);\n      const table = ctx.createLinearGradient(0, HORIZON, 0, VH);\n      table.addColorStop(0, \"#c5c0b6\"); table.addColorStop(0.12, \"#d2cdc4\"); table.addColorStop(1, \"#b9b3a8\");\n      ctx.fillStyle = table; ctx.fillRect(-ox, HORIZON, VW, VH - HORIZON);\n      const lit = ctx.createRadialGradient(CX - 60, 250, 30, CX - 60, 250, 560);\n      lit.addColorStop(0, \"rgba(255,255,255,0.5)\"); lit.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = lit; ctx.fillRect(-ox, 0, VW, VH);\n      // Shadow across the table, with the bright streak a column of water focuses inside it.\n      const up = lift() / 78, wet = clamp(s.show, 0, 1) * (1 - up);\n      ctx.save(); ctx.translate(CX + 120 + up * 30, GY - 2); ctx.rotate(-0.05); ctx.scale(1, 0.17);\n      const sh = ctx.createRadialGradient(-60, 0, 6, 0, 0, 270);\n      sh.addColorStop(0, `rgba(52,46,38,${0.5 - 0.24 * up})`); sh.addColorStop(0.5, `rgba(52,46,38,${0.26 - 0.1 * up})`); sh.addColorStop(1, \"rgba(52,46,38,0)\");\n      ctx.fillStyle = sh; ctx.beginPath(); ctx.arc(0, 0, 270, 0, TAU); ctx.fill();\n      const ca = ctx.createRadialGradient(20, 0, 1, 20, 0, 130);\n      ca.addColorStop(0, `rgba(236,252,255,${0.85 * wet})`); ca.addColorStop(0.35, rgba([150, 214, 226], 1, 0.4 * wet)); ca.addColorStop(1, \"rgba(200,240,250,0)\");\n      ctx.save(); ctx.scale(1, 0.4); ctx.fillStyle = ca; ctx.beginPath(); ctx.arc(20, 0, 130, 0, TAU); ctx.fill(); ctx.restore();\n      ctx.restore();\n      if (puddle > 0.01) {\n        ctx.save(); ctx.translate(CX + 176, GY + 22); ctx.scale(1, 0.2);\n        const pg = ctx.createRadialGradient(0, 0, 2, 0, 0, 120 * Math.min(1, puddle + 0.3));\n        pg.addColorStop(0, rgba(WATER, 1.3, 0.2 * Math.min(1, puddle))); pg.addColorStop(0.8, rgba(WATER, 0.9, 0.26 * Math.min(1, puddle))); pg.addColorStop(1, rgba(WATER, 0.7, 0));\n        ctx.fillStyle = pg; ctx.beginPath(); ctx.arc(0, 0, 130, 0, TAU); ctx.fill();\n        ctx.beginPath(); ctx.arc(0, 0, 112 * Math.min(1, puddle + 0.3), Math.PI * 1.05, Math.PI * 1.7); ctx.lineWidth = 9; ctx.strokeStyle = `rgba(255,255,255,${0.3 * Math.min(1, puddle)})`; ctx.stroke(); ctx.restore();\n      }\n      ctx.lineWidth = 1;\n      for (const r of rings) { const a = 1 - r.age / 0.6, rad = 2 + r.age * 30; ctx.beginPath(); ctx.ellipse(r.x, r.y, rad, rad * 0.24, 0, 0, TAU); ctx.strokeStyle = `rgba(70,80,86,${0.34 * a})`; ctx.stroke(); }\n    };\n\n    const arc = (y: number, rx: number, a0: number, a1: number) => { ctx.beginPath(); ctx.ellipse(0, y, rx, rx * EL, 0, a0, a1); };\n    const glassBack = () => {\n      // The foot: a thick moulded disc.\n      const fy = -13;\n      ctx.beginPath(); ctx.ellipse(0, 0, FOOT, FOOT * EL, 0, 0, Math.PI); ctx.lineTo(-FOOT, fy); ctx.ellipse(0, fy, FOOT, FOOT * EL, 0, Math.PI, 0, true); ctx.closePath();\n      const side = ctx.createLinearGradient(-FOOT, 0, FOOT, 0);\n      side.addColorStop(0, \"rgba(70,96,92,0.75)\"); side.addColorStop(0.1, \"rgba(214,232,228,0.8)\"); side.addColorStop(0.22, \"rgba(120,150,145,0.5)\"); side.addColorStop(0.6, \"rgba(168,196,190,0.42)\"); side.addColorStop(0.86, \"rgba(236,248,246,0.85)\"); side.addColorStop(1, \"rgba(60,86,82,0.8)\");\n      ctx.fillStyle = side; ctx.fill();\n      arc(fy, FOOT, 0, TAU);\n      const topF = ctx.createLinearGradient(-FOOT, 0, FOOT, 0);\n      topF.addColorStop(0, \"rgba(150,176,170,0.7)\"); topF.addColorStop(0.3, \"rgba(236,246,243,0.85)\"); topF.addColorStop(0.7, \"rgba(196,216,211,0.7)\"); topF.addColorStop(1, \"rgba(120,148,142,0.75)\");\n      ctx.fillStyle = topF; ctx.fill(); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(50,74,70,0.6)\"; ctx.stroke();\n      arc(0, FOOT, 0.1, Math.PI - 0.1); ctx.lineWidth = 1.4; ctx.strokeStyle = \"rgba(34,52,50,0.7)\"; ctx.stroke();\n      arc(fy + 2.5, FOOT - 1.5, Math.PI * 0.56, Math.PI * 0.9); ctx.lineWidth = 2; ctx.strokeStyle = \"rgba(255,255,255,0.9)\"; ctx.stroke();\n      // The tube, seen through from the front: darker where the wall turns edge-on.\n      ctx.beginPath(); ctx.moveTo(-R, -HC); ctx.lineTo(-R, -12); ctx.ellipse(0, -12, R, R * EL, 0, Math.PI, 0, true); ctx.lineTo(R, -HC); ctx.closePath();\n      const body = ctx.createLinearGradient(-R, 0, R, 0);\n      body.addColorStop(0, \"rgba(58,84,80,0.62)\"); body.addColorStop(0.045, \"rgba(120,150,144,0.3)\"); body.addColorStop(0.12, \"rgba(255,255,255,0.2)\"); body.addColorStop(0.5, \"rgba(236,244,242,0.1)\"); body.addColorStop(0.9, \"rgba(170,196,190,0.2)\"); body.addColorStop(0.96, \"rgba(100,130,124,0.36)\"); body.addColorStop(1, \"rgba(50,76,72,0.66)\");\n      ctx.fillStyle = body; ctx.fill();\n      // The table shows faintly in the lower part of the glass.\n      const room = ctx.createLinearGradient(0, HORIZON - GY - 30, 0, HORIZON - GY + 6);\n      room.addColorStop(0, \"rgba(120,108,90,0)\"); room.addColorStop(1, \"rgba(120,108,90,0.13)\");\n      ctx.fillStyle = room; ctx.fill();\n      arc(-HC, R, Math.PI, TAU); ctx.lineWidth = 1.2; ctx.strokeStyle = \"rgba(70,96,92,0.55)\"; ctx.stroke();\n      arc(-HC, RI, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.22)\"; ctx.fill();\n    };\n\n    const waterBody = (path: () => void, x0: number, x1: number, yTop: number, yBot: number) => {\n      path();\n      const g = ctx.createLinearGradient(x0, 0, x1, 0);\n      g.addColorStop(0, rgba(WATER, 0.55, 0.86)); g.addColorStop(0.14, rgba(WATER, 0.9, 0.66)); g.addColorStop(0.42, rgba(WATER, 1.25, 0.46)); g.addColorStop(0.7, rgba(WATER, 1.5, 0.4)); g.addColorStop(0.88, rgba(WATER, 0.95, 0.62)); g.addColorStop(1, rgba(WATER, 0.5, 0.88));\n      ctx.fillStyle = g; ctx.fill();\n      path();\n      const d = ctx.createLinearGradient(0, yTop, 0, yBot);\n      d.addColorStop(0, \"rgba(255,255,255,0.1)\"); d.addColorStop(0.3, \"rgba(0,40,56,0)\"); d.addColorStop(1, \"rgba(0,40,56,0.3)\");\n      ctx.fillStyle = d; ctx.fill();\n    };\n\n    const waterUpright = () => {\n      const ys = surfaceY(), ry = RI * EL;\n      const path = () => { ctx.beginPath(); ctx.moveTo(-RI, ys); ctx.lineTo(-RI, FLOOR); ctx.ellipse(0, FLOOR, RI, ry, 0, Math.PI, 0, true); ctx.lineTo(RI, ys); ctx.ellipse(0, ys, RI, ry, 0, 0, Math.PI); ctx.closePath(); };\n      waterBody(path, -RI, RI, ys, FLOOR + ry);\n      ctx.save(); path(); ctx.clip();\n      // Light gathered into a soft column on the far side from the lamp.\n      const col = ctx.createLinearGradient(RI * 0.1, 0, RI * 0.8, 0);\n      col.addColorStop(0, \"rgba(255,255,255,0)\"); col.addColorStop(0.5, \"rgba(236,252,255,0.3)\"); col.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = col; ctx.fillRect(RI * 0.1, ys, RI * 0.7, FLOOR - ys + ry);\n      for (const b of bubbles) {\n        ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.16)\"; ctx.fill(); ctx.lineWidth = 0.8; ctx.strokeStyle = \"rgba(240,252,255,0.75)\"; ctx.stroke();\n        ctx.beginPath(); ctx.arc(b.x - b.r * 0.35, b.y - b.r * 0.35, b.r * 0.28, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.95)\"; ctx.fill();\n      }\n      ctx.restore();\n      // The surface, seen from a little above, and the meniscus where it climbs the glass.\n      arc(ys, RI, 0, TAU);\n      const top = ctx.createLinearGradient(0, ys - ry, 0, ys + ry);\n      top.addColorStop(0, \"rgba(244,250,250,0.97)\"); top.addColorStop(0.45, \"rgba(206,230,234,0.94)\"); top.addColorStop(1, rgba(WATER, 1.25, 0.95));\n      ctx.fillStyle = top; ctx.fill();\n      ctx.save(); arc(ys, RI, 0, TAU); ctx.clip();\n      for (const r of ripples) {\n        const a = 1 - r.age / 0.9, rad = 4 + r.age * 74;\n        ctx.beginPath(); ctx.ellipse(r.x, ys, rad, rad * EL, 0, 0, TAU); ctx.lineWidth = 1.6; ctx.strokeStyle = `rgba(255,255,255,${0.8 * a})`; ctx.stroke();\n        ctx.beginPath(); ctx.ellipse(r.x, ys + 1.6, rad, rad * EL, 0, 0, TAU); ctx.lineWidth = 1; ctx.strokeStyle = rgba(WATER, 0.5, 0.5 * a); ctx.stroke();\n      }\n      ctx.restore();\n      for (const sx of [-1, 1]) { ctx.beginPath(); ctx.moveTo(sx * RI, ys - 5.5); ctx.quadraticCurveTo(sx * (RI - 1.5), ys - 0.5, sx * (RI - 11), ys + 2.6); ctx.lineTo(sx * RI, ys + 3); ctx.closePath(); ctx.fillStyle = rgba(WATER, 0.8, 0.8); ctx.fill(); }\n      arc(ys + 1.4, RI - 0.6, 0.06, Math.PI - 0.06); ctx.lineWidth = 2.6; ctx.strokeStyle = rgba(WATER, 0.36, 0.72); ctx.stroke();\n      arc(ys - 0.6, RI - 1, 0.2, Math.PI - 0.2); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(255,255,255,0.8)\"; ctx.stroke();\n      arc(ys, RI - 1, Math.PI + 0.25, TAU - 0.25); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(255,255,255,0.9)\"; ctx.stroke();\n    };\n\n    const glassFront = () => {\n      // Solid glass under the water.\n      ctx.beginPath(); ctx.moveTo(-RI, FLOOR); ctx.ellipse(0, FLOOR, RI, RI * EL, 0, Math.PI, 0, true); ctx.lineTo(R - 1, -12); ctx.ellipse(0, -12, R - 1, (R - 1) * EL, 0, 0, Math.PI); ctx.closePath();\n      const slab = ctx.createLinearGradient(-R, 0, R, 0);\n      slab.addColorStop(0, \"rgba(40,70,66,0.6)\"); slab.addColorStop(0.2, \"rgba(226,244,240,0.75)\"); slab.addColorStop(0.36, \"rgba(120,160,152,0.42)\"); slab.addColorStop(0.74, \"rgba(176,206,200,0.38)\"); slab.addColorStop(0.9, \"rgba(240,252,250,0.7)\"); slab.addColorStop(1, \"rgba(40,70,66,0.6)\");\n      ctx.fillStyle = slab; ctx.fill();\n      arc(FLOOR, RI, 0.05, Math.PI - 0.05); ctx.lineWidth = 1.3; ctx.strokeStyle = \"rgba(30,56,54,0.6)\"; ctx.stroke();\n      arc(FLOOR + 2.4, RI - 3, 0.5, Math.PI - 0.9); ctx.lineWidth = 1.4; ctx.strokeStyle = \"rgba(255,255,255,0.7)\"; ctx.stroke();\n      // Edges of the tube.\n      for (const sx of [-1, 1]) {\n        ctx.beginPath(); ctx.moveTo(sx * R, -HC); ctx.lineTo(sx * R, -12); ctx.lineWidth = 1.5; ctx.strokeStyle = \"rgba(38,60,58,0.78)\"; ctx.stroke();\n        ctx.beginPath(); ctx.moveTo(sx * RI, -HC + 4); ctx.lineTo(sx * RI, FLOOR); ctx.lineWidth = 0.9; ctx.strokeStyle = \"rgba(60,86,82,0.34)\"; ctx.stroke();\n      }\n      // The printed scale wraps round the front of the tube, so every tick is a short arc.\n      ctx.strokeStyle = \"#17324f\"; ctx.fillStyle = \"#17324f\"; ctx.lineCap = \"butt\";\n      ctx.font = '600 15px \"Helvetica Neue\", Helvetica, Arial, sans-serif'; ctx.textAlign = \"left\"; ctx.textBaseline = \"middle\";\n      for (let v = 0; v <= CAPACITY; v++) {\n        const y = Y0 + ((Y100 - Y0) * v) / CAPACITY, ten = v % 10 === 0, five = v % 5 === 0, a0 = Math.PI / 2 + 0.12;\n        arc(y, R, a0, a0 + (ten ? 0.82 : five ? 0.58 : 0.36)); ctx.lineWidth = ten ? 1.9 : 1.1; ctx.stroke();\n        if (ten && v) ctx.fillText(String(v), R * 0.08, y + R * EL * 0.98);\n      }\n      ctx.font = '600 9px \"Helvetica Neue\", Helvetica, Arial, sans-serif';\n      ctx.fillText(gauge.unit.toUpperCase(), R * 0.1, Y100 - 16);\n      ctx.fillStyle = \"#b3261e\"; ctx.fillRect(R * 0.08, Y100 + R * EL - 13, 16, 2);\n      // Reflections of a tall window on the left and the room on the right.\n      const tall = ctx.createLinearGradient(0, -HC + 14, 0, -24);\n      tall.addColorStop(0, \"rgba(255,255,255,0)\"); tall.addColorStop(0.05, \"rgba(255,255,255,0.95)\"); tall.addColorStop(0.9, \"rgba(255,255,255,0.9)\"); tall.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = tall; ctx.fillRect(-R + 9, -HC + 14, 6.5, HC - 38); ctx.globalAlpha = 0.55; ctx.fillRect(-R + 19, -HC + 30, 1.6, HC - 70); ctx.globalAlpha = 1;\n      const soft = ctx.createLinearGradient(R - 30, 0, R - 8, 0);\n      soft.addColorStop(0, \"rgba(255,255,255,0)\"); soft.addColorStop(0.6, \"rgba(255,255,255,0.42)\"); soft.addColorStop(1, \"rgba(255,255,255,0)\");\n      ctx.fillStyle = soft; ctx.fillRect(R - 30, -HC + 12, 22, HC - 30);\n      // Drops clinging to the outside.\n      for (const c of clings) {\n        ctx.beginPath(); ctx.ellipse(c.x, c.y, c.r, c.r * 1.3, 0, 0, TAU);\n        const g = ctx.createRadialGradient(c.x - c.r * 0.3, c.y - c.r * 0.5, 0.2, c.x, c.y, c.r * 1.3);\n        g.addColorStop(0, \"rgba(255,255,255,0.75)\"); g.addColorStop(0.5, \"rgba(214,234,236,0.3)\"); g.addColorStop(1, \"rgba(40,70,74,0.5)\");\n        ctx.fillStyle = g; ctx.fill();\n        ctx.beginPath(); ctx.arc(c.x - c.r * 0.32, c.y - c.r * 0.5, c.r * 0.26, 0, TAU); ctx.fillStyle = \"#ffffff\"; ctx.fill();\n        ctx.beginPath(); ctx.ellipse(c.x + c.r * 0.2, c.y + c.r * 0.75, c.r * 0.45, c.r * 0.2, 0, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.6)\"; ctx.fill();\n      }\n      // The lip: a rolled rim with one hard glint.\n      arc(-HC, R + 2.5, 0, Math.PI); ctx.lineWidth = 6; ctx.strokeStyle = \"rgba(150,180,174,0.9)\"; ctx.stroke();\n      arc(-HC - 1.2, R + 2.5, 0, Math.PI); ctx.lineWidth = 2.2; ctx.strokeStyle = \"rgba(250,255,255,0.95)\"; ctx.stroke();\n      arc(-HC + 3, R + 2.5, 0.05, Math.PI - 0.05); ctx.lineWidth = 1; ctx.strokeStyle = \"rgba(34,56,54,0.7)\"; ctx.stroke();\n      arc(-HC, R + 2.5, Math.PI, TAU); ctx.lineWidth = 2.4; ctx.strokeStyle = \"rgba(196,216,212,0.9)\"; ctx.stroke();\n      arc(-HC - 0.8, R + 2.5, Math.PI * 1.1, Math.PI * 1.42); ctx.lineWidth = 1.6; ctx.strokeStyle = \"#ffffff\"; ctx.stroke();\n    };\n\n    const draw = () => {\n      ctx.setTransform(k, 0, 0, k, ox * k, 0);\n      stage();\n      ctx.beginPath();\n      for (const d of rain) { ctx.moveTo(d.x, d.y); ctx.lineTo(d.x + 2.4, d.y - d.sp * 0.02); }\n      ctx.lineWidth = 1; ctx.lineCap = \"round\"; ctx.strokeStyle = \"rgba(70,84,96,0.2)\"; ctx.stroke();\n      const tip = tilted();\n      const frame = () => { const [x, y] = world(0, MID); ctx.translate(x, y); ctx.rotate(s.th); ctx.translate(0, -MID); };\n      ctx.save(); frame(); glassBack(); if (!tip) waterUpright(); ctx.restore();\n      if (tip && s.area > 40) {\n        const poly = below(inside(), levelFor(inside(), s.area));\n        if (poly.length > 2) {\n          const xs = poly.map((p) => p[0]), ys = poly.map((p) => p[1]), top = Math.min(...ys);\n          waterBody(() => { ctx.beginPath(); poly.forEach((p, i) => (i ? ctx.lineTo(p[0], p[1]) : ctx.moveTo(p[0], p[1]))); ctx.closePath(); }, Math.min(...xs), Math.max(...xs), top, Math.max(...ys));\n          const edge = poly.filter((p) => Math.abs(p[1] - top) < 0.5).map((p) => p[0]);\n          if (edge.length > 1) { ctx.beginPath(); ctx.moveTo(Math.min(...edge), top); ctx.lineTo(Math.max(...edge), top); ctx.lineWidth = 2; ctx.strokeStyle = \"rgba(240,252,255,0.9)\"; ctx.stroke(); }\n        }\n      }\n      for (const b of beads) {\n        ctx.beginPath(); ctx.ellipse(b.x, b.y, b.r, b.r * (1 + Math.min(0.8, Math.abs(b.vy) * 0.002)), 0, 0, TAU); ctx.fillStyle = rgba(WATER, 1.15, 0.78); ctx.fill();\n        ctx.beginPath(); ctx.arc(b.x - b.r * 0.3, b.y - b.r * 0.3, b.r * 0.34, 0, TAU); ctx.fillStyle = \"rgba(255,255,255,0.9)\"; ctx.fill();\n      }\n      for (const c of caught) {\n        const x = CX + c.x;\n        ctx.beginPath(); ctx.moveTo(x, c.y - 17); ctx.quadraticCurveTo(x + 3.6, c.y - 2, x, c.y + 3); ctx.quadraticCurveTo(x - 3.6, c.y - 2, x, c.y - 17); ctx.fillStyle = rgba(WATER, 1.0, 0.7); ctx.fill();\n        ctx.beginPath(); ctx.arc(x - 0.9, c.y - 2, 0.9, 0, TAU); ctx.fillStyle = \"#ffffff\"; ctx.fill();\n      }\n      ctx.save(); frame(); glassFront(); ctx.restore();\n    };\n\n    const step = (dt: number) => {\n      s.t += dt;\n      for (const d of rain) { d.y += d.sp * dt; d.x += 26 * dt; if (d.y >= d.gy) { if (rings.length < 40 && d.gy > HORIZON + 30) rings.push({ x: d.x, y: d.gy, age: 0 }); sow(d, false); } }\n      for (let i = rings.length - 1; i >= 0; i--) { rings[i].age += dt; if (rings[i].age > 0.6) rings.splice(i, 1); }\n      // Tipping: a spring swings the glass over and back.\n      if (s.tipT >= 0) s.tipT += dt;\n      const aim = s.tipT >= 0 && s.tipT < 1.9 ? 2.25 : 0;\n      s.thV += (38 * (aim - s.th) - 9.6 * s.thV) * dt; s.th += s.thV * dt;\n      if (s.tipT > 1.9 && Math.abs(s.th) < 0.004 && Math.abs(s.thV) < 0.02) { s.tipT = -1; s.th = 0; s.thV = 0; s.p = Math.max(0, s.area / (2 * RI) - 6) / (Y0 - Y100); s.show = s.p; s.wobV = 90; s.full = 0; }\n      if (tilted()) {\n        const poly = inside(), lip = Math.max(poly[2][1], poly[3][1]), lv = levelFor(poly, s.area);\n        if (s.area > 0 && lv < lip - 0.5) {\n          const out = Math.min(s.area, 46000 * dt); s.area -= out; s.p = Math.max(0, s.area / (2 * RI) - 6) / (Y0 - Y100);\n          const at = poly[2][1] > poly[3][1] ? poly[2] : poly[3], ax = Math.sin(s.th), ay = -Math.cos(s.th);\n          for (let n = 0; n < 5; n++) beads.push({ x: at[0] - 3 - rnd() * 9, y: at[1] - 2 - rnd() * 7, vx: ax * (50 + rnd() * 90), vy: ay * (50 + rnd() * 90), life: 2, r: 3 + rnd() * 3.4 });\n        }\n      } else {\n        // Rain that lands inside the glass.\n        s.next -= dt;\n        if (s.next <= 0) { s.next = EVERY * (0.5 + rnd()); caught.push({ x: (rnd() * 2 - 1) * (RI - 10), y: -20 }); }\n        if (s.p >= 1) { s.full += dt; if (LOOP && s.full > 3.6) start(); }\n      }\n      const sy = GY + surfaceY();\n      for (let i = caught.length - 1; i >= 0; i--) {\n        const c = caught[i]; c.y += 980 * dt;\n        if (tilted()) { if (c.y > GY) caught.splice(i, 1); continue; }\n        if (c.y >= sy) {\n          caught.splice(i, 1);\n          s.p = Math.min(1, s.p + PER_DROP); s.wobV += 26;\n          ripples.push({ x: c.x, age: 0 });\n          for (let n = 0; n < 3; n++) beads.push({ x: CX + c.x + (rnd() - 0.5) * 6, y: sy - 2, vx: (rnd() - 0.5) * 70, vy: -110 - rnd() * 130, life: 0.5, r: 1.1 + rnd() * 1.1 });\n          if (bubbles.length < 14) for (let n = 0; n < 2; n++) bubbles.push({ x: c.x + (rnd() - 0.5) * 10, y: surfaceY() + 10 + rnd() * 26, r: 1.2 + rnd() * 1.8, ph: rnd() * 6 });\n        }\n      }\n      s.show += (s.p - s.show) * Math.min(1, dt * 5);\n      s.wobV += (-210 * s.wob - 9 * s.wobV) * dt; s.wob += s.wobV * dt;\n      for (let i = ripples.length - 1; i >= 0; i--) { ripples[i].age += dt; if (ripples[i].age > 0.9) ripples.splice(i, 1); }\n      for (let i = bubbles.length - 1; i >= 0; i--) { const b = bubbles[i]; b.y -= (38 + b.r * 16) * dt; b.x += Math.sin(s.t * 7 + b.ph) * 12 * dt; if (b.y < surfaceY() + b.r) bubbles.splice(i, 1); }\n      for (let i = beads.length - 1; i >= 0; i--) {\n        const b = beads[i]; b.vy += 1500 * dt; b.x += b.vx * dt; b.y += b.vy * dt; b.life -= dt;\n        if (b.r > 2.4 && b.y > GY + 16) { puddle = Math.min(1.2, puddle + 0.012); if (rings.length < 50) rings.push({ x: b.x, y: GY + 20, age: 0 }); beads.splice(i, 1); } else if (b.life <= 0 || (b.r <= 2.4 && b.vy > 0 && b.y > sy)) beads.splice(i, 1);\n      }\n      puddle = Math.max(0, puddle - dt * 0.09);\n      // One clinging drop at a time lets go and runs down the outside.\n      for (const c of clings) { if (c.v > 0) { c.v = Math.min(150, c.v + 160 * dt); c.y += c.v * dt; if (c.y > -40) { c.y = -HC + 30 + rnd() * 60; c.x = (rnd() * 2 - 1) * (R - 14); c.v = 0; } } else if (rnd() < dt * 0.09) c.v = 6; }\n      const now = Math.round(clamp(s.p, 0, 1) * 100);\n      if (now !== s.said) { s.said = now; setPct(now); }\n    };\n    const start = () => {\n      if (s.tipT >= 0) return;\n      if (reduce) { s.p = s.p > 0.02 ? 0 : START; s.show = s.p; s.full = 0; const now = Math.round(s.p * 100); s.said = now; setPct(now); draw(); return; }\n      s.area = 2 * RI * (6 + clamp(s.show, 0, 1) * (Y0 - Y100)); s.tipT = 0; caught.length = 0; ripples.length = 0; bubbles.length = 0; wake();\n    };\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.034, (now - last) / 1000); last = now;\n      step(dt); draw();\n      if (seen && !document.hidden) raf = requestAnimationFrame(frame);\n    };\n    function wake() { if (reduce) { draw(); return; } if (!raf) { last = 0; raf = requestAnimationFrame(frame); } }\n    api.current = { tip: start };\n    fit(); pour();\n    if (!reduce) { for (let i = 0; i < 70; i++) step(1 / 60); s.p = START; s.show = START; } else caught.push({ x: 12, y: 250 }, { x: -20, y: 90 });\n    wake();\n\n    const ro = new ResizeObserver(() => { fit(); if (reduce) draw(); });\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 () => { cancelAnimationFrame(raf); api.current = null; ro.disconnect(); io.disconnect(); document.removeEventListener(\"visibilitychange\", onVis); };\n  }, []);\n\n  const amount = Math.round((pct / 100) * CAPACITY);\n\n  return (\n    <div className=\"relative h-[500px] w-full overflow-hidden bg-[#dcd8d0] sm:h-[740px]\">\n      <canvas ref={canvas} aria-hidden=\"true\" className=\"absolute inset-0 h-full w-full\" />\n      <div role=\"progressbar\" aria-label={gauge.label} aria-valuemin={0} aria-valuemax={100} aria-valuenow={pct} aria-valuetext={`${amount} of ${CAPACITY} ${gauge.unit}`} className=\"sr-only\" />\n      <button\n        type=\"button\"\n        onClick={() => api.current?.tip()}\n        aria-label=\"Tip the gauge out and start again\"\n        className=\"absolute left-1/2 top-[14%] h-[72%] w-[210px] max-w-[52%] -translate-x-1/2 cursor-pointer rounded-[28px] outline-none focus-visible:ring-2 focus-visible:ring-[#0f6f86] focus-visible:ring-offset-2 focus-visible:ring-offset-transparent\"\n      />\n      <p className=\"absolute inset-x-0 bottom-5 px-4 text-center font-mono text-[11px] uppercase leading-5 tracking-[0.18em] text-[#57534b]\">\n        <span className=\"text-[#16242b]\">{gauge.label} {amount} {gauge.unit}</span>\n        <span className=\"mx-2 text-[#9a958b]\">/</span>\n        <span className=\"text-[#0b5f74]\">{pct >= 100 ? \"Full, tipping out\" : `${pct}% of the glass`}</span>\n      </p>\n    </div>\n  );\n}\n"}]}
