"use client"; /** * aquarium.tsx — a tiny glass fish tank. * Hover and the goldfish swim over to the cursor; click and they scatter in a burst of bubbles while one jumps out and back. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useImperativeHandle, useRef, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type ReactNode, type Ref } from "react"; type Palette = { water: [string, string, string]; fish: string; plant: string; rim: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; /** number of goldfish, 1 to 5 */ fishCount?: number; }; const W = 280; const H = 112; const PX = 16; // canvas padding left/right/bottom const PT = 70; // canvas padding on top (jumping fish, splash) const CW = W + 2 * PX; const CH = H + PT + PX; const SURF = 9; // water surface, px below the top of the tank const RESET_MS = 2800; const DEFAULT_PALETTE: Palette = { water: ["#4a98b4", "#1d5670", "#0d2a3b"], fish: "#ff8a1f", plant: "#4f8a3a", rim: "#7fd6f2", }; const FISH_START: [number, number, number][] = [[66, 40, 1], [225, 52, -1], [118, 78, 1], [190, 28, -1], [40, 74, 1]]; const STILL_POSE: [number, number, number][] = [[58, 42, 1], [92, 76, 1], [214, 54, -1], [196, 30, -1], [30, 66, 1]]; const PEBBLE_COLORS = ["#b8a588", "#93806a", "#6e604f", "#cfc0a4"]; const mixHex = (a: string, b: string, t: number) => { const ok = /^#[0-9a-f]{6}$/i; if (!ok.test(a) || !ok.test(b)) return a; const p = (c: string) => [1, 3, 5].map((i) => parseInt(c.slice(i, i + 2), 16)); const A = p(a), B = p(b); return "#" + A.map((v, i) => Math.round(v + (B[i] - v) * t).toString(16).padStart(2, "0")).join(""); }; const rnd = (a: number, b: number) => a + Math.random() * (b - a); const clamp = (n: number, lo: number, hi: number) => Math.max(lo, Math.min(hi, n)); const mulberry = (seed: number) => () => { seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; const PEBBLES = (() => { const r = mulberry(7); return Array.from({ length: 200 }, (_, i) => { let p = { x: 0, y: 0, r: 0, c: i % 4, ok: false }; for (let t = 0; t < 4; t++) { p = { x: 8 + r() * (W - 16), y: H - 9 + r() * 6, r: 1.1 + r() * 1.3, c: i % 4, ok: false }; const ex = Math.max(0, Math.max(H / 2 - p.x, p.x - (W - H / 2))); if (ex * ex + (p.y - H / 2) ** 2 <= (H / 2 - 4 - p.r) ** 2) { p.ok = true; break; } } return p; }).filter((p) => p.ok); })(); // two clumps of seven blades: base x, height, lean, phase const BLADES = [ ...[24, 29, 33, 38, 42, 47, 51].map((x, i) => ({ x, h: Math.min([44, 62, 52, 70, 48, 58, 38][i], H - SURF - 14), lean: [-10, -6, -3, 4, 8, 12, 6][i], ph: i * 0.9 })), ...[222, 226, 231, 235, 240, 244, 249].map((x, i) => ({ x, h: Math.min([40, 56, 66, 50, 60, 44, 34][i], H - SURF - 14), lean: [-8, -12, -4, 2, 6, 10, 5][i], ph: i * 0.9 + 2 })), ]; type Fish = { x: number; y: number; vx: number; vy: number; wa: number; ph: number; fx: number; dir: number; ang: number; flee: number; flash: number; air: boolean; out: boolean; drip: number; }; type Bubble = { x: number; y: number; x0: number; vx: number; vy: number; r: number; burst: boolean; ph: number; age: number }; type Drop = { x: number; y: number; vx: number; vy: number; r: number }; type Ripple = { x: number; y: number; t: number }; export function AquariumButton({ children = "feed", palette = DEFAULT_PALETTE, fishCount = 3, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); const wrapRef = useRef(null); const canvasRef = useRef(null); const play = useRef<(clientX: number, clientY: number, keyboard: boolean) => void>(() => {}); useImperativeHandle(ref, () => btnRef.current!, []); const count = clamp(Math.round(fishCount), 1, 5); useEffect(() => { const btn = btnRef.current; const wrap = wrapRef.current; const canvas = canvasRef.current; if (!btn || !wrap || !canvas) return; const ctx = canvas.getContext("2d"); if (!ctx) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let raf = 0; let last = 0; let visible = true; let hovering = false; let curX = 0; let curY = 0; let rect: DOMRect | null = null; let simT = 0; let animT = 0; let bubbleAcc = 0; let rippleAcc = 0; let jumpIdx = -1; let jumpAt = 0; let clickEnd = 0; let dentX = 0; let dentT = 9; let slosh = 0; let bodyGrad: CanvasGradient | null = null; let bubbles: Bubble[] = []; let drops: Drop[] = []; let ripples: Ripple[] = []; let pops: { x: number; t: number }[] = []; let rings: { x: number; t: number }[] = []; const timers = new Set(); let epoch = 0; const later = (fn: () => void, ms: number) => { const my = epoch; const id = window.setTimeout(() => { timers.delete(id); if (my === epoch) fn(); }, ms); timers.add(id); }; const fish: Fish[] = Array.from({ length: count }, (_, i) => { const [x, y, d] = FISH_START[i]; const wa = d > 0 ? 0 : Math.PI; return { x, y, vx: d * 14, vy: 0, wa, ph: i * 2, fx: d, dir: d, ang: 0, flee: 9, flash: 0, air: false, out: false, drip: 0 }; }); const allowed = () => !disabled && !btn.matches(":disabled") && visible && !document.hidden && !media.matches; const setup = () => { const r = btn.getBoundingClientRect(); const scale = r.width / (btn.offsetWidth || 1) || 1; const dpr = Math.min(2, (window.devicePixelRatio || 1) * scale); canvas.width = Math.round(CW * dpr); canvas.height = Math.round(CH * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); rect = null; }; const stadium = () => { const R = H / 2; ctx.beginPath(); ctx.moveTo(R, 0); ctx.lineTo(W - R, 0); ctx.arc(W - R, R, R, -Math.PI / 2, Math.PI / 2); ctx.lineTo(R, H); ctx.arc(R, R, R, Math.PI / 2, Math.PI * 1.5); ctx.closePath(); }; const surfY = (x: number) => SURF + Math.sin(x * 0.09 + animT * 1.6) * 0.8 + Math.sin(x * 0.23 - animT * 2.3) * 0.45 + slosh * Math.sin(simT * 13) * ((x - W / 2) / (W / 2)) + (dentT < 0.4 ? 3 * (1 - dentT / 0.4) * Math.exp(-((x - dentX) ** 2) / 128) : 0); let tailGrad: CanvasGradient | null = null; let finGrad: CanvasGradient | null = null; let waterGrad: CanvasGradient | null = null; let sheenGrad: CanvasGradient | null = null; let causGrad: CanvasGradient | null = null; let vigGrad: CanvasGradient | null = null; let gravelGrad: CanvasGradient | null = null; let airGrad: CanvasGradient | null = null; let bloomGrad: CanvasGradient | null = null; let shadeGrad: CanvasGradient | null = null; let bladeGrad: CanvasGradient | null = null; let stoneG1: CanvasGradient | null = null; let stoneG2: CanvasGradient | null = null; const drawFish = (f: Fish) => { const S = 1.6; if (!tailGrad) { tailGrad = ctx.createLinearGradient(2, 0, -12, 0); tailGrad.addColorStop(0, "rgba(255,140,40,.98)"); tailGrad.addColorStop(0.55, "rgba(255,172,84,.78)"); tailGrad.addColorStop(1, "rgba(255,228,196,.50)"); finGrad = ctx.createLinearGradient(0, -3, 0, -8); finGrad.addColorStop(0, "rgba(255,145,55,.95)"); finGrad.addColorStop(1, "rgba(255,222,186,.55)"); bodyGrad = ctx.createRadialGradient(1.5, -1.5, 0.5, 1.5, -1.5, 9); bodyGrad.addColorStop(0, "#ffb25a"); bodyGrad.addColorStop(0.45, palette.fish); bodyGrad.addColorStop(1, "#d4500a"); } ctx.save(); ctx.translate(f.x, f.y); ctx.rotate(f.ang); ctx.scale(f.fx * S, S); const bend = Math.sin(f.ph * 1) * 0.35; ctx.save(); ctx.translate(-6, 0); ctx.rotate(bend); ctx.beginPath(); ctx.moveTo(2, 0); ctx.quadraticCurveTo(-4, -5, -12, -8); ctx.quadraticCurveTo(-9, -3, -8, 0); ctx.quadraticCurveTo(-9, 3, -12, 8); ctx.quadraticCurveTo(-4, 5, 2, 0); ctx.fillStyle = tailGrad!; ctx.fill(); ctx.strokeStyle = "rgba(255,238,215,.40)"; ctx.lineWidth = 0.35; ctx.beginPath(); ctx.moveTo(1, 0); ctx.lineTo(-11, -6.5); ctx.moveTo(1, 0); ctx.lineTo(-9.5, -2.5); ctx.moveTo(1, 0); ctx.lineTo(-9.5, 2.5); ctx.moveTo(1, 0); ctx.lineTo(-11, 6.5); ctx.stroke(); ctx.restore(); ctx.fillStyle = finGrad!; ctx.beginPath(); ctx.moveTo(-1, -4); ctx.quadraticCurveTo(-3, -9.5, -7.5, -6.5 + Math.sin(f.ph * 0.8) * 0.8); ctx.lineTo(-5, -3); ctx.fill(); ctx.fillStyle = bodyGrad ?? palette.fish; ctx.beginPath(); ctx.ellipse(0, 0, 8, 4.5, 0, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = finGrad!; ctx.beginPath(); ctx.ellipse(1.2, 2.6, 2.8, 1.1, 0.55 + Math.sin(f.ph * 1.3) * 0.25, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "rgba(180,55,0,.30)"; ctx.beginPath(); ctx.ellipse(0, -2.7, 6.5, 1.5, 0, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "rgba(255,240,220,.6)"; ctx.beginPath(); ctx.ellipse(1.8, -2.5, 3.2, 0.75, -0.08, 0, Math.PI * 2); ctx.fill(); ctx.strokeStyle = "rgba(150,50,0,.45)"; ctx.lineWidth = 0.4; ctx.beginPath(); ctx.arc(3.1, 0, 3.1, -1.0, 1.0); ctx.stroke(); ctx.strokeStyle = "rgba(120,35,0,.38)"; ctx.lineWidth = 0.5; ctx.beginPath(); ctx.ellipse(0, 0, 8, 4.5, 0, 0, Math.PI * 2); ctx.stroke(); ctx.fillStyle = "#fff4e6"; ctx.beginPath(); ctx.arc(4.5, -1.2, 1.65, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "#111"; ctx.beginPath(); ctx.arc(4.5, -1.2, 1.1, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "#fff"; ctx.beginPath(); ctx.arc(4.85, -1.6, 0.45, 0, Math.PI * 2); ctx.fill(); if (f.air) { ctx.fillStyle = "rgba(255,255,255,.55)"; ctx.beginPath(); ctx.ellipse(0.5, -2.9, 4.6, 0.6, -0.05, 0, Math.PI * 2); ctx.fill(); } if (f.flash > 0) { ctx.fillStyle = `rgba(255,255,255,${0.45 * clamp(f.flash / 0.12, 0, 1)})`; ctx.beginPath(); ctx.ellipse(0, 0, 8, 4.5, 0, 0, Math.PI * 2); ctx.fill(); } ctx.restore(); }; const wavePath = (dy: number) => { ctx.moveTo(0, surfY(0) + dy); for (let x = 4; x <= W + 4; x += 4) ctx.lineTo(x, surfY(x) + dy); }; const draw = () => { ctx.clearRect(0, 0, CW, CH); ctx.save(); ctx.translate(PX, PT); if (!waterGrad) { waterGrad = ctx.createLinearGradient(0, SURF, 0, H); waterGrad.addColorStop(0, palette.water[0]); waterGrad.addColorStop(0.18, mixHex(palette.water[0], palette.water[1], 0.5)); waterGrad.addColorStop(0.6, palette.water[1]); waterGrad.addColorStop(1, palette.water[2]); sheenGrad = ctx.createLinearGradient(0, SURF, 0, SURF + 6); sheenGrad.addColorStop(0, "rgba(200,245,255,.18)"); sheenGrad.addColorStop(1, "rgba(200,245,255,0)"); causGrad = ctx.createLinearGradient(-24, 0, 30, 0); causGrad.addColorStop(0, "rgba(170,230,255,0)"); causGrad.addColorStop(0.5, "rgba(170,230,255,.12)"); causGrad.addColorStop(1, "rgba(170,230,255,0)"); vigGrad = ctx.createRadialGradient(W / 2, H * 0.35, 30, W / 2, H * 0.35, 160); vigGrad.addColorStop(0, "rgba(0,10,18,0)"); vigGrad.addColorStop(1, "rgba(0,10,18,.30)"); airGrad = ctx.createLinearGradient(0, 0, 0, SURF + 1); airGrad.addColorStop(0, "rgba(150,215,238,.62)"); airGrad.addColorStop(0.5, "rgba(96,170,198,.50)"); airGrad.addColorStop(1, "rgba(130,205,228,.58)"); bloomGrad = ctx.createRadialGradient(W * 0.42, SURF, 0, W * 0.42, SURF, 120); bloomGrad.addColorStop(0, "rgba(190,240,255,.16)"); bloomGrad.addColorStop(1, "rgba(190,240,255,0)"); shadeGrad = ctx.createLinearGradient(0, H - 12, 0, H - 6); shadeGrad.addColorStop(0, "rgba(10,20,26,.45)"); shadeGrad.addColorStop(1, "rgba(10,20,26,0)"); gravelGrad = ctx.createLinearGradient(0, H - 12, 0, H); gravelGrad.addColorStop(0, "#54473a"); gravelGrad.addColorStop(1, "#241e18"); bladeGrad = ctx.createLinearGradient(0, 0, 0, -70); bladeGrad.addColorStop(0, "#3a6a2b"); bladeGrad.addColorStop(1, "#7cc24f"); stoneG1 = ctx.createLinearGradient(0, -6, 0, 0); stoneG1.addColorStop(0, "#7d909c"); stoneG1.addColorStop(1, "#3e4c56"); stoneG2 = ctx.createLinearGradient(0, -5.5, 0, 0); stoneG2.addColorStop(0, "#62737f"); stoneG2.addColorStop(1, "#33404a"); } ctx.save(); stadium(); ctx.clip(); ctx.fillStyle = airGrad!; ctx.fillRect(0, 0, W, SURF + 1); ctx.strokeStyle = "rgba(255,255,255,.22)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(H * 0.45, 3.5); ctx.lineTo(W * 0.46, 3.5); ctx.moveTo(W * 0.58, 5.5); ctx.lineTo(W - H * 0.45, 5.5); ctx.stroke(); ctx.fillStyle = waterGrad!; ctx.beginPath(); wavePath(0); ctx.lineTo(W, H); ctx.lineTo(0, H); ctx.closePath(); ctx.fill(); // surface sheen ctx.fillStyle = sheenGrad!; ctx.beginPath(); wavePath(0); for (let x = W + 4; x >= 0; x -= 4) ctx.lineTo(x, surfY(x) + 6); ctx.closePath(); ctx.fill(); // soft caustic rays ctx.fillStyle = causGrad!; for (let k = 0; k < 3; k++) { const cx = -40 + (((animT / 8 + k / 3) % 1) * (W + 80)); ctx.translate(cx, 0); ctx.beginPath(); ctx.moveTo(0, SURF); ctx.lineTo(28, SURF); ctx.lineTo(6, H - 12); ctx.lineTo(-22, H - 12); ctx.closePath(); ctx.fill(); ctx.translate(-cx, 0); } ctx.fillStyle = bloomGrad!; ctx.fillRect(0, 0, W, H); // plants: back row then front row for (let row = 0; row < 2; row++) { for (let i = 0; i < BLADES.length; i++) { const b = BLADES[i]; const h = row === 0 ? b.h * 0.8 : b.h; const sway = Math.sin((animT / 2.8) * Math.PI * 2 + b.ph); const tipX = b.lean + sway * h * 0.13; const cs = sway * h * 0.04; ctx.save(); ctx.translate(b.x + (row === 0 ? 3 : 0), H - 8); const dir = i % 2 ? 1 : -1; const c1x = cs - dir * h * 0.10; const c2x = tipX * 0.55 + dir * h * 0.10 + cs; ctx.lineCap = "round"; ctx.beginPath(); ctx.moveTo(0, 2); ctx.bezierCurveTo(c1x, -h * 0.33, c2x, -h * 0.68, tipX, -h); if (row === 0) { ctx.lineWidth = 2.6; ctx.strokeStyle = "#2f5526"; ctx.stroke(); } else { const bw = i % 2 ? 3.4 : 2.8; ctx.lineWidth = bw + 1; ctx.strokeStyle = "rgba(14,40,18,.45)"; ctx.stroke(); ctx.lineWidth = bw; ctx.strokeStyle = bladeGrad!; ctx.stroke(); ctx.lineWidth = 0.6; ctx.strokeStyle = "rgba(215,255,170,.28)"; ctx.stroke(); } ctx.restore(); } } ctx.lineCap = "butt"; // gravel bed const gy = (x: number) => H - 11 + Math.sin(x * 0.07) - (Math.min(x, W - x) < 40 ? 3 * (1 - Math.min(x, W - x) / 40) : 0); ctx.beginPath(); ctx.moveTo(0, gy(0)); for (let x = 4; x <= W; x += 4) ctx.lineTo(x, gy(x)); ctx.lineTo(W, H); ctx.lineTo(0, H); ctx.closePath(); ctx.fillStyle = gravelGrad!; ctx.fill(); ctx.strokeStyle = "rgba(0,0,0,.28)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.moveTo(0, gy(0)); for (let x = 4; x <= W; x += 4) ctx.lineTo(x, gy(x)); ctx.stroke(); ctx.save(); ctx.translate(56, H - 9); ctx.fillStyle = stoneG1!; ctx.beginPath(); ctx.ellipse(0, 0, 9, 6, 0, Math.PI, 0); ctx.fill(); ctx.strokeStyle = "rgba(255,255,255,.25)"; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.ellipse(0, 0, 8, 5, 0, Math.PI * 1.1, Math.PI * 1.5); ctx.stroke(); ctx.restore(); ctx.save(); ctx.translate(214, H - 9); ctx.fillStyle = stoneG2!; ctx.beginPath(); ctx.ellipse(0, 0, 8, 5.5, 0, Math.PI, 0); ctx.fill(); ctx.strokeStyle = "rgba(255,255,255,.25)"; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.ellipse(0, 0, 7, 4.5, 0, Math.PI * 1.1, Math.PI * 1.5); ctx.stroke(); ctx.restore(); ctx.fillStyle = "rgba(0,0,0,.25)"; ctx.beginPath(); for (const p of PEBBLES) { ctx.moveTo(p.x + 0.2 * p.r + p.r, p.y + 0.35 * p.r); ctx.arc(p.x + 0.2 * p.r, p.y + 0.35 * p.r, p.r, 0, Math.PI * 2); } ctx.fill(); for (let c = 0; c < 4; c++) { ctx.fillStyle = PEBBLE_COLORS[c]; ctx.beginPath(); for (const p of PEBBLES) if (p.c === c) { ctx.moveTo(p.x + p.r, p.y); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); } ctx.fill(); } ctx.fillStyle = "rgba(255,255,255,.22)"; ctx.beginPath(); for (const p of PEBBLES) { const hx = p.x - 0.3 * p.r; const hy = p.y - 0.35 * p.r; ctx.moveTo(hx + 0.35 * p.r, hy); ctx.arc(hx, hy, 0.35 * p.r, 0, Math.PI * 2); } ctx.fill(); ctx.beginPath(); ctx.moveTo(0, gy(0)); for (let x = 4; x <= W; x += 4) ctx.lineTo(x, gy(x)); ctx.lineTo(W, H); ctx.lineTo(0, H); ctx.closePath(); ctx.fillStyle = shadeGrad!; ctx.fill(); // depth vignette ctx.fillStyle = vigGrad!; ctx.fillRect(0, 0, W, H); // floor shadows ctx.fillStyle = "rgba(0,0,0,.18)"; ctx.beginPath(); for (const f of fish) if (!f.air) { const rx = 9 * (0.6 + 0.4 * f.y / H); ctx.moveTo(f.x + rx, H - 11); ctx.ellipse(f.x, H - 11, rx, 1.6, 0, 0, Math.PI * 2); } ctx.fill(); for (const f of fish) if (!f.air) drawFish(f); // bubbles ctx.fillStyle = "rgba(200,240,255,.10)"; ctx.beginPath(); for (const b of bubbles) { ctx.moveTo(b.x + b.r, b.y); ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2); } ctx.fill(); ctx.strokeStyle = "rgba(220,245,255,.75)"; ctx.lineWidth = 0.7; ctx.beginPath(); for (const b of bubbles) { ctx.moveTo(b.x + b.r, b.y); ctx.arc(b.x, b.y, b.r, 0, Math.PI * 2); } ctx.stroke(); ctx.fillStyle = "rgba(255,255,255,.9)"; ctx.beginPath(); for (const b of bubbles) { const gr = Math.max(0.45, 0.3 * b.r); const gx = b.x - 0.35 * b.r; const gyy = b.y - 0.35 * b.r; ctx.moveTo(gx + gr, gyy); ctx.arc(gx, gyy, gr, 0, Math.PI * 2); } ctx.fill(); for (const p of pops) { const k = p.t / 0.18; ctx.strokeStyle = `rgba(220,245,255,${0.5 * (1 - k)})`; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.ellipse(p.x, surfY(p.x), 1 + 3 * k, 0.6 + 0.8 * k, 0, 0, Math.PI * 2); ctx.stroke(); } // ripples for (const r of ripples) { const k = r.t / 0.7; ctx.strokeStyle = `rgba(200,240,255,${0.5 * (1 - k)})`; ctx.lineWidth = 1.2; ctx.beginPath(); ctx.ellipse(r.x, r.y, 6 + 16 * k, (6 + 16 * k) * 0.8, 0, 0, Math.PI * 2); ctx.stroke(); if (r.t >= 0.12) { const k2 = (r.t - 0.12) / 0.58; ctx.strokeStyle = `rgba(200,240,255,${0.3 * (1 - k2)})`; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.ellipse(r.x, r.y, 3 + 11 * k2, (3 + 11 * k2) * 0.8, 0, 0, Math.PI * 2); ctx.stroke(); } } // surface line ctx.strokeStyle = "rgba(210,245,255,.7)"; ctx.lineWidth = 1; ctx.beginPath(); wavePath(0); ctx.stroke(); ctx.strokeStyle = "rgba(255,255,255,.12)"; ctx.beginPath(); wavePath(1.5); ctx.stroke(); for (const g of rings) { if (g.t < 0) continue; const k = g.t / 0.45; ctx.strokeStyle = `rgba(210,245,255,${0.55 * (1 - k)})`; ctx.lineWidth = 1; ctx.beginPath(); ctx.ellipse(g.x, SURF + 1, 4 + 18 * k, 1 + 2 * k, 0, 0, Math.PI * 2); ctx.stroke(); } // glass thickness crescents const R = H / 2; ctx.lineCap = "round"; ctx.strokeStyle = "rgba(255,255,255,.34)"; ctx.lineWidth = 3.5; ctx.beginPath(); ctx.arc(R, R, R - 5, 0.72 * Math.PI, 1.32 * Math.PI); ctx.stroke(); ctx.strokeStyle = "rgba(255,255,255,.22)"; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(R, R, R - 9, 0.80 * Math.PI, 1.22 * Math.PI); ctx.stroke(); ctx.strokeStyle = "rgba(255,255,255,.11)"; ctx.lineWidth = 2; ctx.beginPath(); ctx.arc(W - R, R, R - 5, -0.30 * Math.PI, 0.22 * Math.PI); ctx.stroke(); ctx.strokeStyle = "rgba(255,255,255,.55)"; ctx.lineWidth = 2.5; ctx.beginPath(); ctx.arc(W - R, R, R - 5, -0.36 * Math.PI, -0.22 * Math.PI); ctx.stroke(); ctx.lineCap = "butt"; ctx.restore(); for (const f of fish) if (f.air) drawFish(f); ctx.fillStyle = "rgba(225,246,255,.9)"; ctx.strokeStyle = "rgba(225,246,255,.9)"; ctx.lineCap = "round"; for (const d of drops) { if (Math.hypot(d.vx, d.vy) < 40) { ctx.beginPath(); ctx.arc(d.x, d.y, d.r, 0, Math.PI * 2); ctx.fill(); continue; } ctx.lineWidth = 2 * d.r; ctx.beginPath(); ctx.moveTo(d.x, d.y); ctx.lineTo(d.x - d.vx * 0.014, d.y - d.vy * 0.014); ctx.stroke(); } ctx.lineCap = "butt"; ctx.restore(); }; const spray = (x: number, n: number, up: [number, number]) => { for (let i = 0; i < n; i++) drops.push({ x: x + rnd(-3, 3), y: SURF, vx: rnd(-55, 55), vy: rnd(up[0], up[1]), r: rnd(1, 1.8) }); }; const stepAir = (f: Fish, dt: number) => { f.vy += 900 * dt; f.x += f.vx * dt; f.y += f.vy * dt; f.ph += dt * 30; f.dir = f.vx >= 0 ? 1 : -1; f.fx += clamp(f.dir - f.fx, -dt / 0.06, dt / 0.06); f.ang = Math.atan2(f.vy * f.dir, Math.abs(f.vx) + 0.001); f.ang = clamp(f.ang, -1.3, 1.3); if (f.out) { f.drip -= dt; if (f.drip <= 0) { f.drip = 0.055; const bx = f.x - Math.cos(f.ang) * f.dir * 14, by = f.y - Math.sin(f.ang) * f.dir * 14; drops.push({ x: bx, y: by, vx: f.vx * 0.25 + rnd(-15, 15), vy: f.vy * 0.25, r: rnd(0.8, 1.3) }); if (drops.length > 60) drops.splice(0, drops.length - 60); } } if (!f.out && f.y < SURF) { f.out = true; spray(f.x, 14, [-200, -80]); rings.push({ x: f.x, t: 0 }); } if (f.out && f.vy > 0 && f.y >= SURF) { f.air = false; f.out = false; spray(f.x, 6, [-150, -70]); dentX = f.x; dentT = 0; rings.push({ x: f.x, t: 0 }, { x: f.x, t: -0.09 }); f.vx *= 0.4; f.vy *= 0.15; f.y = SURF + 1; f.flee = 0.3; bubbles.push(...Array.from({ length: 4 }, () => ({ x: f.x, y: SURF + 6, x0: f.x, vx: rnd(-30, 30), vy: rnd(10, 50), r: rnd(1, 2.2), burst: true, ph: 0, age: 0 }))); } }; const stepFish = (f: Fish, i: number, dt: number) => { f.flash = Math.max(0, f.flash - dt); if (f.air) { stepAir(f, dt); return; } const fleeK = Math.max(0, 1 - f.flee / 0.9); f.flee += dt; let maxSp = 26 + (180 - 26) * fleeK; let ax = 0; let ay = 0; const seek = hovering && fleeK < 0.5; f.wa += (Math.random() - 0.5) * 6 * dt; const wk = seek ? 0.2 : 1; ax += Math.cos(f.wa) * 30 * wk; ay += Math.sin(f.wa) * 15 * wk; for (const o of fish) { if (o === f || o.air) continue; const dx = f.x - o.x; const dy = f.y - o.y; const d = Math.hypot(dx, dy); if (d < 22 && d > 0.01) { const k = (1 - d / 22) * 90; ax += (dx / d) * k; ay += (dy / d) * k; } } if (f.x < 18) ax += (18 - f.x) * 12; else if (f.x > 262) ax -= (f.x - 262) * 12; if (f.y < 18) ay += (18 - f.y) * 12; else if (f.y > 86) ay -= (f.y - 86) * 12; let drag = 0.5; if (seek) { const tx = clamp(curX, 18, 262); const ty = clamp(curY, 18, 86); const dx = tx - f.x; const dy = ty - f.y; const d = Math.hypot(dx, dy) || 1; const ring = 14 + ((i * 5) % 11); maxSp = Math.max(maxSp, 60); if (d > 30) { ax += (dx / d) * 70; ay += (dy / d) * 70; } else { const err = clamp((d - ring) * 6, -70, 70); ax += (dx / d) * err - (dy / d) * 45 * (i % 2 ? 1 : -1); ay += (dy / d) * err + (dx / d) * 45 * (i % 2 ? 1 : -1); drag = 1.2; } } const sp0 = Math.hypot(f.vx, f.vy); if (sp0 < 40 && Math.abs(f.x - 140) < 38 && Math.abs(f.y - 56) < 18) { const dx = f.x - 140; const dy = (f.y - 56) * 1.6; const d = Math.hypot(dx, dy) || 1; ax += (dx / d) * 120; ay += (dy / d) * 120; } f.vx += ax * dt; f.vy += ay * dt; const dk = Math.exp(-drag * dt); f.vx *= dk; f.vy *= dk; const sp = Math.hypot(f.vx, f.vy); if (sp > maxSp) { f.vx *= maxSp / sp; f.vy *= maxSp / sp; } f.x += f.vx * dt; f.y += f.vy * dt; if (f.x < 10) { f.x = 10; f.vx = Math.abs(f.vx); } else if (f.x > 270) { f.x = 270; f.vx = -Math.abs(f.vx); } if (f.y < 10) { f.y = 10; f.vy = Math.abs(f.vy); } else if (f.y > 98) { f.y = 98; f.vy = -Math.abs(f.vy); } if (Math.abs(f.vx) > 4 && Math.sign(f.vx) !== f.dir) f.dir = Math.sign(f.vx); f.fx += clamp(f.dir - f.fx, -dt / 0.06, dt / 0.06); const pitch = clamp(Math.atan2(f.vy * f.dir, Math.abs(f.vx) + 6), -0.9, 0.9); f.ang += (pitch - f.ang) * (1 - Math.exp(-dt / 0.6)); f.ph += dt * (9 + Math.hypot(f.vx, f.vy) * 0.1); }; const step = (dt: number) => { simT += dt; slosh *= Math.exp(-3.2 * dt); animT += dt; if (jumpIdx >= 0 && simT >= jumpAt) { const f = fish[jumpIdx]; jumpIdx = -1; if (f && !f.air && !fish.some((o) => o.air)) { f.air = true; f.out = false; f.drip = 0; f.vx = (f.x < 60 ? 1 : f.x > 220 ? -1 : Math.random() < 0.5 ? 1 : -1) * rnd(30, 60); f.vy = -Math.sqrt(300 * 300 + 2 * 900 * Math.max(0, f.y - SURF)); } } fish.forEach((f, i) => stepFish(f, i, dt)); bubbleAcc += dt; if (bubbleAcc >= 0.9) { bubbleAcc = 0; bubbles.push({ x: 72, y: H - 10, x0: 72, vx: 0, vy: -30, r: rnd(1, 2), burst: false, ph: rnd(0, 6), age: 0 }); } for (const b of bubbles) { b.age += dt; if (b.burst) { const k = Math.exp(-3.5 * dt); b.vx *= k; b.vy = b.vy * k - 90 * dt; b.x += b.vx * dt; b.y += b.vy * dt; } else { b.y += b.vy * dt; b.x = b.x0 + Math.sin(b.age * 4 + b.ph) * 2; } } for (const b of bubbles) if (b.y <= SURF + b.r) pops.push({ x: b.x, t: 0 }); if (pops.length > 20) pops.splice(0, pops.length - 20); for (const p of pops) p.t += dt; pops = pops.filter((p) => p.t < 0.18); for (const g of rings) g.t += dt; rings = rings.filter((g) => g.t < 0.45); bubbles = bubbles.filter((b) => b.y > SURF + b.r && b.x > -4 && b.x < W + 4); for (const d of drops) { d.vy += 900 * dt; d.x += d.vx * dt; d.y += d.vy * dt; } drops = drops.filter((d) => !(d.vy > 0 && d.y >= SURF)); if (dentT < 9) dentT += dt; if (hovering) { rippleAcc += dt; if (rippleAcc >= 0.9) { rippleAcc = 0; ripples.push({ x: curX, y: curY, t: 0 }); } } for (const r of ripples) r.t += dt; ripples = ripples.filter((r) => r.t < 0.7); const el = wrap.dataset.tbState; if (el === "playing" && simT >= clickEnd) wrap.dataset.tbState = "idle"; }; const tick = (now: number) => { raf = 0; if (!allowed()) return; const dt = last ? Math.min(0.048, (now - last) / 1000) : 0.016; last = now; step(dt); draw(); raf = requestAnimationFrame(tick); }; const wake = () => { if (!raf && allowed()) raf = requestAnimationFrame(tick); }; const poseStill = () => { fish.forEach((f, i) => { const [x, y, d] = STILL_POSE[i]; Object.assign(f, { x, y, vx: 0, vy: 0, fx: d, dir: d, ang: 0, air: false, out: false, flash: 0, ph: i }); }); bubbles = []; drops = []; ripples = []; pops = []; rings = []; dentT = 9; jumpIdx = -1; slosh = 0; }; const mode = () => { wrap.dataset.tbPaused = String(disabled || !visible || document.hidden); if (allowed()) { last = 0; wake(); return; } cancelAnimationFrame(raf); raf = 0; if (media.matches || disabled) { poseStill(); draw(); } }; const locate = (e: PointerEvent) => { if (!rect) rect = btn.getBoundingClientRect(); const scale = rect.width / (btn.offsetWidth || 1) || 1; curX = (e.clientX - rect.left) / scale; curY = (e.clientY - rect.top) / scale; }; const enter = (e: PointerEvent) => { rect = btn.getBoundingClientRect(); hovering = !disabled; locate(e); rippleAcc = 0.9; }; const move = (e: PointerEvent) => { if (hovering) locate(e); }; const leave = () => { hovering = false; }; const invalidate = () => { rect = null; }; const resize = () => { setup(); }; play.current = (cx, cy, keyboard) => { if (media.matches) { wrap.dataset.tbGlow = "true"; later(() => { delete wrap.dataset.tbGlow; }, 1200); return; } if (!allowed()) return; const r = btn.getBoundingClientRect(); const scale = r.width / (btn.offsetWidth || 1) || 1; const x = keyboard ? W / 2 : clamp((cx - r.left) / scale, 0, W); const y = keyboard ? H / 2 : clamp((cy - r.top) / scale, 0, H); slosh = 2.4; wrap.dataset.tbPress = "true"; later(() => { delete wrap.dataset.tbPress; }, 380); wrap.dataset.tbState = "playing"; clickEnd = simT + RESET_MS / 1000; for (let i = 0; i < 30; i++) { const a = rnd(0, Math.PI * 2); const sp = rnd(60, 160); bubbles.push({ x, y, x0: x, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp, r: Math.random() < 0.7 ? rnd(0.8, 1.8) : rnd(2.0, 3.4), burst: true, ph: 0, age: 0 }); } if (bubbles.length > 90) bubbles.splice(0, bubbles.length - 90); let near = -1; let nd = Infinity; fish.forEach((f, i) => { if (f.air) return; const dx = f.x - x; const dy = f.y - y; const d = Math.hypot(dx, dy); if (d < nd) { nd = d; near = i; } const m = d || 1; f.vx = (dx / m) * 180; f.vy = (dy / m) * 120; f.flee = 0; f.flash = 0.12; }); if (near >= 0 && jumpIdx < 0 && !fish.some((o) => o.air)) { jumpIdx = near; jumpAt = simT + 0.12; } }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; mode(); }); const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(invalidate); setup(); io?.observe(wrap); ro?.observe(btn); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointermove", move, { passive: true }); btn.addEventListener("pointerleave", leave); window.addEventListener("resize", resize); window.addEventListener("scroll", invalidate, true); document.addEventListener("visibilitychange", mode); media.addEventListener("change", mode); // prime a frame so it never paints empty step(0.001); draw(); mode(); return () => { epoch++; play.current = () => {}; cancelAnimationFrame(raf); timers.forEach((id) => window.clearTimeout(id)); timers.clear(); io?.disconnect(); ro?.disconnect(); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointermove", move); btn.removeEventListener("pointerleave", leave); window.removeEventListener("resize", resize); window.removeEventListener("scroll", invalidate, true); document.removeEventListener("visibilitychange", mode); media.removeEventListener("change", mode); }; }, [disabled, count, palette]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; play.current(e.clientX, e.clientY, e.detail === 0); }; return ( ); } export default AquariumButton; const css = ` .tb-aquarium-wrap{display:inline-flex;position:relative;vertical-align:middle;isolation:isolate} .tb-aquarium-wrap::before{content:"";position:absolute;left:22px;right:22px;bottom:-12px;height:16px;border-radius:50%;background:radial-gradient(closest-side,rgba(0,0,0,.5),rgba(0,0,0,0));z-index:-1;pointer-events:none;transition:transform 240ms,opacity 240ms} .tb-aquarium-wrap::after{content:"";position:absolute;left:40px;right:40px;bottom:-7px;height:9px;border-radius:50%;background:radial-gradient(closest-side,rgba(110,210,240,.22),rgba(110,210,240,0));z-index:-1;pointer-events:none} .tb-aquarium-wrap:has(.tb-aquarium-btn:hover:not(:disabled))::before{transform:scaleX(1.06);opacity:.75} .tb-aquarium-btn{position:relative;display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box; width:${W}px;height:${H}px;padding:0;border:0;border-radius:${H / 2}px;background:transparent;overflow:visible; color:#f1efe8;cursor:pointer;-webkit-tap-highlight-color:transparent; font:800 24px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;letter-spacing:.02em; transition:translate 240ms cubic-bezier(.2,.8,.2,1)} .tb-aquarium-btn:hover:not(:disabled){translate:0 -2px} .tb-aquarium-btn:focus-visible{outline:3px solid var(--tb-aquarium-rim,#7fd6f2);outline-offset:6px} .tb-aquarium-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-aquarium-wrap[data-tb-press=true] .tb-aquarium-btn{animation:tb-aquarium-squish 380ms cubic-bezier(.3,.7,.4,1)} @keyframes tb-aquarium-squish{0%{scale:1 1}18%{scale:.968 .952}55%{scale:1.012 1.008}100%{scale:1 1}} .tb-aquarium-canvas{position:absolute;left:-${PX}px;top:-${PT}px;width:${CW}px;height:${CH}px;pointer-events:none} .tb-aquarium-glass{position:absolute;inset:0;border-radius:${H / 2}px;pointer-events:none;box-sizing:border-box; border:2.5px solid color-mix(in srgb,var(--tb-aquarium-rim,#7fd6f2) 62%,transparent); box-shadow:0 0 0 1px rgba(8,24,32,.55),inset 0 0 0 1px rgba(255,255,255,.10),inset 0 2px 0 rgba(255,255,255,.22),inset 0 -3px 0 rgba(120,220,255,.30),inset 12px 0 14px -8px rgba(160,230,255,.22),inset -12px 0 14px -8px rgba(160,230,255,.16),inset 0 0 0 5px rgba(170,230,250,.07),inset 0 0 0 6px rgba(255,255,255,.12),inset 0 -8px 6px -5px rgba(150,225,255,.30); filter:drop-shadow(0 0 5px rgba(90,190,230,.34));transition:border-color 240ms,box-shadow 240ms,filter 240ms} .tb-aquarium-btn:hover:not(:disabled) .tb-aquarium-glass{border-color:color-mix(in srgb,var(--tb-aquarium-rim,#7fd6f2) 82%,transparent);filter:drop-shadow(0 0 8px rgba(90,190,230,.5)); box-shadow:0 0 0 1px rgba(8,24,32,.55),inset 0 0 0 1px rgba(255,255,255,.10),inset 0 2px 0 rgba(255,255,255,.22),inset 0 -3px 0 rgba(120,220,255,.30),inset 12px 0 14px -8px rgba(160,230,255,.22),inset -12px 0 14px -8px rgba(160,230,255,.16),inset 0 0 0 5px rgba(170,230,250,.07),inset 0 0 0 6px rgba(255,255,255,.12),inset 0 -8px 6px -5px rgba(150,225,255,.30)} .tb-aquarium-glass::before{content:"";position:absolute;top:2px;left:30px;right:30px;height:3px;border-radius:2px; background:linear-gradient(90deg,transparent 0%,rgba(255,255,255,.85) 6%,rgba(255,255,255,.35) 18%,transparent 30%,transparent 74%,rgba(255,255,255,.30) 86%,rgba(255,255,255,.70) 94%,transparent 100%)} .tb-aquarium-glass::after{content:"";position:absolute;bottom:3px;left:48px;right:48px;height:2px;border-radius:1px; background:linear-gradient(90deg,transparent,rgba(170,235,255,.55) 50%,transparent)} .tb-aquarium-plate{position:relative;display:flex;align-items:center;justify-content:center;width:100px;height:40px; box-sizing:border-box;border-radius:20px;background:linear-gradient(180deg,rgba(22,44,56,.80),rgba(6,16,22,.84));border:1px solid rgba(160,225,250,.28);pointer-events:none; box-shadow:inset 0 1px 0 rgba(255,255,255,.14),inset 0 -1px 0 rgba(0,0,0,.45),0 3px 8px rgba(0,0,0,.4)} .tb-aquarium-btn:hover:not(:disabled) .tb-aquarium-plate{border-color:rgba(160,225,250,.45)} .tb-aquarium-wrap[data-tb-glow=true] .tb-aquarium-plate{border-color:var(--tb-aquarium-rim,#7fd6f2);box-shadow:0 0 10px var(--tb-aquarium-rim,#7fd6f2)} .tb-aquarium-label{text-transform:uppercase;white-space:nowrap;font-size:23px;letter-spacing:.06em;color:#f4f1e8;text-shadow:0 1px 0 rgba(0,0,0,.6),0 0 10px rgba(127,214,242,.18)} .tb-aquarium-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-aquarium-wrap *,.tb-aquarium-wrap *::before,.tb-aquarium-wrap *::after{animation:none!important;transition:none!important}} `;