"use client"; /** * lava.tsx — warm wax rising and merging inside a glass pill, as a real metaball field. * Hover heats the lamp; click shoots a big blob out of the top, and it falls back in. * * 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 = { wax: string; core: string; rim: string; liquid: string; ink: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; }; const W = 260; const H = 84; const PADX = 40; const PADT = 200; const PADB = 40; const CW = W + PADX * 2; const CH = H + PADT + PADB; const GX0 = PADX + 24; // glass pill in canvas coordinates const GX1 = PADX + W - 24; const GY0 = PADT + 3; const GY1 = PADT + H - 3; const GR = 22; const CELL = 2; const GW = Math.ceil(CW / CELL) + 1; const GH = Math.ceil(CH / CELL) + 1; const HOT = "#ffd27a"; const FIRE = "#ff8a3d"; const DEFAULT_PALETTE: Palette = { wax: "#fc5901", core: "#ff7a12", rim: "#c23a03", liquid: "#3a1606", ink: "#ffc271" }; const BASE_BLOBS = 10; const MAX_BLOBS = 16; const GRAVITY = 900; type Blob = { x: number; y: number; vx: number; vy: number; r: number; T: number; ph: number; dying: boolean }; type Burst = { t: number; ax: number; x: number; y: number; vx: number; vy: number; r: number; r0: number; r1: number; launch: number; phase: 0 | 1 | 2; apex: boolean; snapped: boolean; snapT: number; neck: number }; type Drop = { x: number; y: number; vx: number; vy: number; r: number; age: number; delay: number; spark: boolean }; const clamp = (n: number, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, n)); const mix = (a: number, b: number, t: number) => a + (b - a) * t; const hexRgb = (hex: string): [number, number, number] => { const h = hex.replace("#", ""); const f = h.length === 3 ? h.split("").map((c) => c + c).join("") : h.padEnd(6, "0"); return [parseInt(f.slice(0, 2), 16) || 0, parseInt(f.slice(2, 4), 16) || 0, parseInt(f.slice(4, 6), 16) || 0]; }; const rng = (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; }; // Colour ramp for the field value f (index = f * 100). Alpha ramps in so the 3x upscale has soft edges. const buildLut = (p: Palette) => { const lut = new Uint32Array(256); const stops: { f: number; c: [number, number, number]; a: number }[] = [ { f: 0.80, c: hexRgb(p.rim), a: 1 }, { f: 1.00, c: hexRgb(p.rim), a: 1 }, { f: 1.15, c: hexRgb(p.wax), a: 1 }, { f: 1.90, c: hexRgb(p.core), a: 1 }, { f: 3.00, c: hexRgb(p.core), a: 1 }, ]; for (let i = 0; i < 256; i++) { const f = i / 70; let r = 0, g = 0, b = 0, a = 0; if (f >= stops[0].f) { let k = 0; while (k < stops.length - 2 && f > stops[k + 1].f) k++; const s0 = stops[k], s1 = stops[k + 1]; const t = clamp((f - s0.f) / (s1.f - s0.f)); r = mix(s0.c[0], s1.c[0], t); g = mix(s0.c[1], s1.c[1], t); b = mix(s0.c[2], s1.c[2], t); a = mix(s0.a, s1.a, t); } lut[i] = ((Math.round(a * 255) << 24) | (Math.round(b) << 16) | (Math.round(g) << 8) | Math.round(r)) >>> 0; } return lut; }; const pillPath = (ctx: CanvasRenderingContext2D, x0: number, y0: number, x1: number, y1: number, r: number) => { ctx.moveTo(x0 + r, y0); ctx.lineTo(x1 - r, y0); ctx.arcTo(x1, y0, x1, y0 + r, r); ctx.lineTo(x1, y1 - r); ctx.arcTo(x1, y1, x1 - r, y1, r); ctx.lineTo(x0 + r, y1); ctx.arcTo(x0, y1, x0, y1 - r, r); ctx.lineTo(x0, y0 + r); ctx.arcTo(x0, y0, x0 + r, y0, r); ctx.closePath(); }; export function LavaButton({ children = "glow", palette = DEFAULT_PALETTE, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); const wrapRef = useRef(null); const canvasRef = useRef(null); const plateRef = useRef(null); const playRef = useRef<() => void>(() => {}); useImperativeHandle(ref, () => btnRef.current!, []); const paletteKey = [palette.wax, palette.core, palette.rim, palette.liquid].join("|"); useEffect(() => { const btn = btnRef.current, wrap = wrapRef.current, cv = canvasRef.current, plate = plateRef.current; if (!btn || !wrap || !cv || !plate) return; const ctx = cv.getContext("2d"); if (!ctx) return; const pal: Palette = { wax: palette.wax, core: palette.core, rim: palette.rim, liquid: palette.liquid, ink: palette.ink }; const lut = buildLut(pal); const liquid = hexRgb(pal.liquid); const wax = hexRgb(pal.wax), core = hexRgb(pal.core), rimC = hexRgb(pal.rim); const hotC: [number, number, number] = [core[0] + (255 - core[0]) * 0.5, core[1] + (255 - core[1]) * 0.45, core[2] + (255 - core[2]) * 0.25]; const coreCss = `rgb(${core[0]},${core[1]},${core[2]})`; const off = document.createElement("canvas"); off.width = GW; off.height = GH; const octx = off.getContext("2d"); if (!octx) return; const img = octx.createImageData(GW, GH); const img32 = new Uint32Array(img.data.buffer); const field = new Float32Array(GW * GH); const rand = rng(7); const blobs: Blob[] = []; for (let i = 0; i < BASE_BLOBS; i++) { const r = 6 + rand() * 10; blobs.push({ x: GX0 + 24 + rand() * (GX1 - GX0 - 48), y: GY0 + r + 6 + rand() * (GY1 - GY0 - r * 2 - 30), vx: 0, vy: 0, r, T: 0.2 + rand() * 0.6, ph: rand() * 6.28, dying: false, }); } const poolBase = [ { x: GX0 + 26, r: 22 }, { x: GX0 + 66, r: 17 }, { x: (GX0 + GX1) / 2, r: 23 }, { x: GX1 - 64, r: 18 }, { x: GX1 - 26, r: 24 }, ]; const pool = poolBase.map((p) => ({ x: p.x, y: GY1 - 2, r: p.r })); const bursts: Burst[] = []; const drops: Drop[] = []; let frame = 0, last = 0, simT = 0; let visible = true, hovering = false; let heat = 0, kick = 0, kickHold = 0, flash = 0; let spawnT = 0, ripT = -1, glowX = (GX0 + GX1) / 2; let cursorX = -9999; let rect: DOMRect | undefined; let rectDirty = true; let overrideHeat = -1; // reduced-motion click state const timers = new Set(); const anims = new Set(); const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let reduced = media.matches; const running = () => !disabled && !btn.matches(":disabled") && visible && !document.hidden && !reduced; const setup = () => { const scale = btn.getBoundingClientRect().width / (btn.offsetWidth || 1) || 1; const dpr = Math.min(2, (window.devicePixelRatio || 1) * scale); cv.width = Math.round(CW * dpr); cv.height = Math.round(CH * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); rectDirty = true; }; const splat = (x: number, y: number, r: number, amp: number) => { const R = r * 2.2, R2 = R * R; const i0 = Math.max(0, Math.floor((x - R) / CELL)), i1 = Math.min(GW - 1, Math.ceil((x + R) / CELL)); const j0 = Math.max(0, Math.floor((y - R) / CELL)), j1 = Math.min(GH - 1, Math.ceil((y + R) / CELL)); for (let j = j0; j <= j1; j++) { const dy = j * CELL + CELL / 2 - y; const dy2 = dy * dy; for (let i = i0; i <= i1; i++) { const dx = i * CELL + CELL / 2 - x; const d2 = dx * dx + dy2; if (d2 < R2) { const u = 1 - d2 / R2; field[j * GW + i] += amp * u * u; } } } }; const AMP = 1.588; const neckPoints = (b: Burst, fn: (x: number, y: number, r: number, amp: number) => void) => { const ay = GY1 - 4; const dist = ay - b.y; if (dist < 12) return; const n = Math.min(60, Math.ceil(dist / 6)); const sp = dist / n; for (let i = 1; i < n; i++) { const s = i / n; const rr = (4 + 13 * Math.pow(1 - s, 2.5) + 5 * Math.pow(s, 5)) * b.neck; if (rr < 1) continue; const wob = Math.sin(s * 6 + b.t * 9) * 1.5 * s; fn(mix(b.ax, b.x, s) + wob, mix(ay, b.y, s), rr, (AMP * sp) / (1.067 * rr * 2.2) * 1.15); } }; const drawField = (h: number) => { field.fill(0); for (const b of blobs) splat(b.x, b.y, b.r, AMP); for (const p of pool) splat(p.x, p.y, p.r, AMP); for (const b of bursts) { let rr = b.r; if (b.snapped) rr *= 1 + 0.08 * Math.exp(-(b.t - b.snapT) / 0.5) * Math.sin((b.t - b.snapT) * 30); splat(b.x, b.y, rr, AMP); if (b.phase < 2 && !b.snapped) neckPoints(b, splat); } for (const d of drops) { if (d.spark || d.delay > 0) continue; const rr = d.r * clamp(1 - (d.age - 0.55) / 0.5); if (rr < 0.9) continue; splat(d.x, d.y, rr, AMP); splat(d.x - d.vx * 0.014, d.y - d.vy * 0.014, rr * 0.62, AMP); } const sc = 70; const LX = -0.42, LY = -0.91; img32.fill(0); for (let j = 1; j < GH - 1; j++) { for (let i = 1, k = j * GW + 1; i < GW - 1; i++, k++) { const f = field[k]; if (f < 0.80) continue; const gx = field[k + 1] - field[k - 1], gy = field[k + GW] - field[k - GW]; const gl = Math.sqrt(gx * gx + gy * gy) + 1e-4; const aa = clamp(0.5 + ((f - 1) / (gl * 0.5)) * 0.85); if (aa <= 0) continue; const c = lut[Math.min(255, (f * sc) | 0)]; let r = c & 255, g = (c >>> 8) & 255, b = (c >>> 16) & 255; const a = (aa * 255) | 0; const hot = clamp((f - 1.9) / 1.4) * 0.5; r += (hotC[0] - r) * hot; g += (hotC[1] - g) * hot; b += (hotC[2] - b) * hot; const occ = clamp((j * CELL + 1 - (GY1 - 14)) / 14) * 0.4; r += (rimC[0] - r) * occ; g += (rimC[1] - g) * occ; b += (rimC[2] - b) * occ; const ndl = -(gx * LX + gy * LY) / gl; if (ndl > 0) { const q = (f - 1.3) / 0.12; const s = ndl * ndl * ndl * ndl * ndl * ndl * Math.exp(-q * q) * 0.85; r += (255 - r) * s; g += (241 - g) * s; b += (214 - b) * s; } else { const d = -ndl * clamp((1.7 - f) / 0.7) * 0.35; r += (rimC[0] - r) * d; g += (rimC[1] - g) * d; b += (rimC[2] - b) * d; } img32[k] = ((a << 24) | ((b | 0) << 16) | ((g | 0) << 8) | (r | 0)) >>> 0; } } octx.putImageData(img, 0, 0); }; const draw = (h: number) => { ctx.clearRect(0, 0, CW, CH); const glowA = 0.35 + 0.45 * h + 0.2 * flash; const rimA = Math.min(1, 0.45 + 0.35 * h + 0.2 * flash); ctx.save(); ctx.translate(CW / 2, PADT + H + 2 + 3 * heat); ctx.scale(1 - 0.06 * heat, 7 / 120); const sg = ctx.createRadialGradient(0, 0, 0, 0, 0, 120); sg.addColorStop(0, `rgba(0,0,0,${0.42 * (1 - 0.35 * heat)})`); sg.addColorStop(0.6, "rgba(0,0,0,.18)"); sg.addColorStop(1, "rgba(0,0,0,0)"); ctx.fillStyle = sg; ctx.fillRect(-120, -120, 240, 240); ctx.restore(); // warm glow around the body and on the floor ctx.save(); ctx.translate(CW / 2, PADT + H / 2); ctx.scale(1, 0.5); let g = ctx.createRadialGradient(0, 0, 20, 0, 0, 150); g.addColorStop(0, `rgba(252,89,1,${glowA * 0.7})`); g.addColorStop(0.55, `rgba(252,89,1,${glowA * 0.28})`); g.addColorStop(1, "rgba(252,89,1,0)"); ctx.fillStyle = g; ctx.fillRect(-150, -150, 300, 300); ctx.restore(); ctx.save(); ctx.translate(glowX, PADT + H + 8); ctx.scale(1, 12 / 110); g = ctx.createRadialGradient(0, 0, 0, 0, 0, 110); g.addColorStop(0, `rgba(252,89,1,${0.24 + 0.3 * (glowA - 0.35)})`); g.addColorStop(1, "rgba(252,89,1,0)"); ctx.fillStyle = g; ctx.fillRect(-110, -110, 220, 220); ctx.restore(); // glass back ctx.beginPath(); pillPath(ctx, GX0, GY0, GX1, GY1, GR); const lg = ctx.createLinearGradient(0, GY0, 0, GY1); lg.addColorStop(0, `rgb(${liquid[0]},${liquid[1]},${liquid[2]})`); lg.addColorStop(1, `rgb(${liquid[0] >> 1},${liquid[1] >> 1},${liquid[2] >> 1})`); ctx.fillStyle = lg; ctx.fill(); const lit = ctx.createLinearGradient(0, GY1, 0, GY0); lit.addColorStop(0, `rgba(255,120,30,${0.30 + 0.30 * h})`); lit.addColorStop(0.55, `rgba(255,120,30,${0.06 + 0.12 * h})`); lit.addColorStop(1, "rgba(255,120,30,0)"); ctx.fillStyle = lit; ctx.fill(); // wax: clipped to the glass, plus the open air above it while a blob is out drawField(h); const out = bursts.length > 0 || drops.length > 0; ctx.save(); ctx.beginPath(); pillPath(ctx, GX0, GY0, GX1, GY1, GR); if (out) ctx.rect(0, 0, CW, GY0); ctx.clip(); ctx.imageSmoothingEnabled = true; ctx.imageSmoothingQuality = "high"; ctx.drawImage(off, 0, 0, GW * CELL, GH * CELL); ctx.restore(); ctx.fillStyle = "rgba(255,246,228,.78)"; ctx.beginPath(); for (const b of bursts) { if (b.phase !== 1 || b.y > GY0 - b.r * 0.5) continue; ctx.moveTo(b.x - b.r * 0.32 + b.r * 0.2, b.y - b.r * 0.42); ctx.ellipse(b.x - b.r * 0.32, b.y - b.r * 0.42, b.r * 0.2, b.r * 0.11, -0.6, 0, 6.2832); } ctx.fill(); // droplets if (drops.length) { ctx.save(); ctx.beginPath(); ctx.rect(0, 0, CW, GY0); ctx.clip(); ctx.globalCompositeOperation = "lighter"; ctx.strokeStyle = HOT; ctx.lineWidth = 1.4; ctx.lineCap = "round"; ctx.beginPath(); let sa = 0; for (const d of drops) { if (!d.spark || d.delay > 0) continue; sa = Math.max(sa, clamp(1 - d.age / 0.45)); ctx.moveTo(d.x, d.y); ctx.lineTo(d.x - d.vx * 0.018, d.y - d.vy * 0.018); } ctx.globalAlpha = sa; ctx.stroke(); ctx.restore(); } // glass rim over the wax, with the wax redrawn across it where a neck passes through ctx.save(); ctx.globalAlpha = rimA; ctx.strokeStyle = FIRE; ctx.lineWidth = 1.5; ctx.shadowColor = `rgba(255,110,30,${0.55 + 0.4 * h})`; ctx.shadowBlur = 10 + 14 * h + 10 * flash; ctx.beginPath(); pillPath(ctx, GX0 + 0.75, GY0 + 0.75, GX1 - 0.75, GY1 - 0.75, GR); ctx.stroke(); ctx.restore(); for (const b of bursts) { if (b.phase === 2 || b.snapped) continue; ctx.save(); ctx.beginPath(); ctx.ellipse(b.ax, GY0 + 1, 22, 7, 0, 0, 6.2832); ctx.clip(); ctx.drawImage(off, 0, 0, GW * CELL, GH * CELL); ctx.restore(); } // breach flare for (const b of bursts) { if (b.phase > 1 || b.snapped) continue; const k = b.phase === 0 ? clamp(b.t / 0.18) : Math.exp(-(b.t - 0.18) / 0.25); ctx.save(); ctx.beginPath(); ctx.rect(0, 0, CW, GY0 + 6); ctx.clip(); ctx.globalCompositeOperation = "lighter"; const fg = ctx.createRadialGradient(b.ax, GY0, 0, b.ax, GY0, 32); fg.addColorStop(0, `rgba(255,200,110,${0.5 * k})`); fg.addColorStop(0.4, `rgba(255,120,30,${0.28 * k})`); fg.addColorStop(1, "rgba(255,90,0,0)"); ctx.fillStyle = fg; ctx.fillRect(b.ax - 32, GY0 - 32, 64, 64); ctx.restore(); } // glass curvature ctx.save(); ctx.beginPath(); pillPath(ctx, GX0, GY0, GX1, GY1, GR); ctx.clip(); const cg = ctx.createLinearGradient(0, GY0, 0, GY1); cg.addColorStop(0, "rgba(0,0,0,.30)"); cg.addColorStop(0.09, "rgba(255,236,210,.20)"); cg.addColorStop(0.26, "rgba(255,236,210,.05)"); cg.addColorStop(0.34, "rgba(255,236,210,0)"); cg.addColorStop(0.78, "rgba(0,0,0,0)"); cg.addColorStop(0.93, "rgba(0,0,0,.24)"); cg.addColorStop(1, "rgba(255,170,90,.20)"); ctx.fillStyle = cg; ctx.fillRect(GX0, GY0, GX1 - GX0, GY1 - GY0); const hg = ctx.createLinearGradient(GX0, 0, GX1, 0); hg.addColorStop(0, "rgba(0,0,0,.45)"); hg.addColorStop(0.05, "rgba(0,0,0,0)"); hg.addColorStop(0.95, "rgba(0,0,0,0)"); hg.addColorStop(1, "rgba(0,0,0,.45)"); ctx.fillStyle = hg; ctx.fillRect(GX0, GY0, GX1 - GX0, GY1 - GY0); const eg2 = ctx.createLinearGradient(0, GY0 + 16, 0, GY1 - 16); eg2.addColorStop(0, "rgba(255,236,210,0)"); eg2.addColorStop(0.3, "rgba(255,236,210,.14)"); eg2.addColorStop(0.7, "rgba(255,236,210,.14)"); eg2.addColorStop(1, "rgba(255,236,210,0)"); ctx.fillStyle = eg2; ctx.beginPath(); ctx.roundRect(GX0 + 9, GY0 + 16, 2, GY1 - GY0 - 32, 1); ctx.roundRect(GX1 - 11, GY0 + 16, 2, GY1 - GY0 - 32, 1); ctx.fill(); ctx.restore(); // inner heat on the glass edge ctx.save(); ctx.beginPath(); pillPath(ctx, GX0 + 2, GY0 + 2, GX1 - 2, GY1 - 2, GR - 2); ctx.clip(); ctx.globalAlpha = 0.18 + 0.2 * h; const eg = ctx.createLinearGradient(0, GY0, 0, GY1); eg.addColorStop(0, "rgba(255,138,61,0.5)"); eg.addColorStop(0.2, "rgba(255,138,61,0)"); eg.addColorStop(0.8, "rgba(255,138,61,0)"); eg.addColorStop(1, "rgba(255,138,61,0.55)"); ctx.strokeStyle = eg; ctx.lineWidth = 5; ctx.beginPath(); pillPath(ctx, GX0 + 2, GY0 + 2, GX1 - 2, GY1 - 2, GR - 2); ctx.stroke(); ctx.restore(); }; const poolLayout = (t: number, h: number) => { pool.forEach((p, i) => { const b = poolBase[i]; p.x = b.x + Math.sin(t * 0.7 + i * 1.7) * 4; p.r = b.r * (1 + 0.05 * Math.sin(t * 1.1 + i)) * (1 + 0.14 * h); p.y = GY1 - 2 - h * 4; }); if (ripT >= 0) { const k = simT - ripT; if (k > 0.4) ripT = -1; else pool[2].y += 5 * Math.exp(-k / 0.12) * Math.cos(k * 41.9); } }; const step = (dt: number) => { simT += dt; const hTarget = hovering ? 1 : 0; heat += (hTarget - heat) * (1 - Math.exp(-dt / (hTarget > heat ? 0.22 : 0.45))); if (simT > kickHold) kick *= Math.exp(-dt / 0.9); flash *= Math.exp(-dt / 0.25); const h = Math.max(heat, kick); const buoy = 60 * (1 + 1.2 * h); const vmax = 18 + 22 * h; poolLayout(simT, h); // spawn from the pool while hot spawnT += dt; if (h > 0.5 && spawnT > 0.45 && blobs.length < MAX_BLOBS) { spawnT = 0; const p = pool[(Math.random() * pool.length) | 0]; blobs.push({ x: p.x, y: GY1 - 18, vx: 0, vy: -20, r: 8 + Math.random() * 5, T: 0.95, ph: Math.random() * 6.28, dying: false }); } for (let i = blobs.length - 1; i >= 0; i--) { const b = blobs[i]; let T = b.T; if (b.y > GY1 - 28) T += 0.25 * dt; else if (b.y < GY0 + 28) T -= 0.3 * dt; else T += (0.5 - T) * 0.05 * dt; b.T = clamp(T, 0, 1.2); let heatBoost = 1; if (hovering && Math.abs(b.x - cursorX) < 50) heatBoost = 1.2; b.vy += -(b.T - 0.5) * buoy * heatBoost * dt; b.vx += Math.sin(simT * 0.6 + b.ph) * 8 * dt; const drag = Math.exp(-0.9 * dt); b.vx *= drag; b.vy *= drag; const sp = Math.hypot(b.vx, b.vy); if (sp > vmax) { b.vx *= vmax / sp; b.vy *= vmax / sp; } b.x += b.vx * dt; b.y += b.vy * dt; const xl = GX0 + b.r * 0.8 + 4, xr = GX1 - b.r * 0.8 - 4; if (b.x < xl) { b.x = xl; b.vx = Math.abs(b.vx); } else if (b.x > xr) { b.x = xr; b.vx = -Math.abs(b.vx); } const yt = GY0 + b.r + 3, yb = GY1 - b.r * 0.3; if (b.y < yt) { b.y = yt; b.vy = Math.abs(b.vy) * 0.3; } else if (b.y > yb) { b.y = yb; b.vy = -Math.abs(b.vy) * 0.3; } if (!b.dying && blobs.length > BASE_BLOBS && b.r < 16 && b.y > GY1 - b.r - 10 && b.T < 0.55 && i >= BASE_BLOBS) b.dying = true; if (b.dying) { b.r -= 9 * dt; if (b.r < 2) blobs.splice(i, 1); } } // bursts for (let i = bursts.length - 1; i >= 0; i--) { const b = bursts[i]; b.t += dt; if (b.phase === 0) { const p = clamp(b.t / 0.18); const e = p * p * (3 - 2 * p); b.y = mix(GY1 - 10, GY0, e); b.r = mix(b.r0, b.r1, e); if (p >= 1) { b.phase = 1; b.vy = -b.launch; b.vx = (Math.random() - 0.5) * 10; const n = i === 0 && bursts.length === 1 ? 14 : 8; for (let k = 0; k < n; k++) { const ang = (-90 + (Math.random() * 2 - 1) * 55) * (Math.PI / 180); const spd = 180 + Math.random() * 200; drops.push({ x: b.ax + (Math.random() - 0.5) * 14, y: GY0, vx: Math.cos(ang) * spd, vy: Math.sin(ang) * spd, r: 2 + Math.random() * 3.5, age: 0, delay: Math.random() * 0.24, spark: false }); } for (let k = 0; k < 6; k++) { const ang = (-90 + (Math.random() * 2 - 1) * 55) * (Math.PI / 180); const spd = 260 + Math.random() * 220; drops.push({ x: b.ax + (Math.random() - 0.5) * 14, y: GY0, vx: Math.cos(ang) * spd, vy: Math.sin(ang) * spd, r: 0.8 + Math.random() * 0.6, age: 0, delay: Math.random() * 0.08, spark: true }); } } } else if (b.phase === 1) { b.vy += GRAVITY * dt; const d = Math.exp(-0.6 * dt); b.vx *= d; b.vy *= d; b.x += b.vx * dt; b.y += b.vy * dt; if (!b.apex && b.vy >= 0) b.apex = true; if (b.apex && !b.snapped) { b.neck *= Math.pow(0.86, (dt * 1000) / 16); if (b.neck < 0.3) { b.snapped = true; b.snapT = b.t; ripT = simT; } } if (b.apex && b.y > GY0 - b.r * 0.4) { b.phase = 2; flash = 1; b.snapped = true; } } else { b.vy = b.vy * Math.exp(-5 * dt) + 160 * dt; b.y += b.vy * dt; b.r = Math.max(14, b.r - 22 * dt); b.y = Math.max(b.y, GY0 + 2); if (b.r <= 14.5) { const dying = blobs.length >= BASE_BLOBS; blobs.push({ x: b.x, y: Math.max(b.y, GY0 + b.r + 8), vx: 0, vy: 10, r: b.r, T: 0.85, ph: Math.random() * 6.28, dying }); bursts.splice(i, 1); } } } for (let i = drops.length - 1; i >= 0; i--) { const d = drops[i]; if (d.delay > 0) { d.delay -= dt; continue; } d.age += dt; d.vy += GRAVITY * dt; const dr = Math.exp(-0.6 * dt); d.vx *= dr; d.vy *= dr; d.x += d.vx * dt; d.y += d.vy * dt; if (d.age > 1.05 || (d.vy > 0 && d.y > GY0 - 1) || (d.spark && d.age > 0.45)) drops.splice(i, 1); } // floor glow follows the wax mass let mx = 0, mw = 0; for (const b of blobs) { mx += b.x * b.r * b.r; mw += b.r * b.r; } for (const p of pool) { mx += p.x * p.r * p.r; mw += p.r * p.r; } glowX += ((mw ? mx / mw : glowX) - glowX) * (1 - Math.exp(-dt / 0.4)); return h; }; const tick = (now: number) => { frame = 0; if (!running()) { last = 0; return; } const dt = last ? Math.min(0.048, (now - last) / 1000) : 0.016; last = now; const h = step(dt); draw(h); frame = requestAnimationFrame(tick); }; const wake = () => { if (!frame && running()) { last = 0; frame = requestAnimationFrame(tick); } }; const drawStill = (h: number) => { poolLayout(0, h); glowX = (GX0 + GX1) / 2; draw(h); }; const stillFade = (from: number, to: number, ms: number, done?: () => void) => { const steps = 4; for (let s = 1; s <= steps; s++) { const id = window.setTimeout(() => { timers.delete(id); overrideHeat = mix(from, to, s / steps); drawStill(overrideHeat); if (s === steps) { done?.(); } }, (ms * s) / steps); timers.add(id); } }; const refresh = () => { wrap.dataset.tbPaused = String(disabled || !visible || document.hidden); if (running()) { hovering = hovering && btn.matches(":hover"); wake(); } else { cancelAnimationFrame(frame); frame = 0; last = 0; if (!reduced || overrideHeat < 0) { heat = kick = 0; drawStill(0); } } }; const onMove = (e: PointerEvent) => { if (rectDirty || !rect) { rect = btn.getBoundingClientRect(); rectDirty = false; } const scale = rect.width / (btn.offsetWidth || 1) || 1; cursorX = (e.clientX - rect.left) / scale + PADX; }; const enter = (e: PointerEvent) => { hovering = true; rectDirty = true; onMove(e); wake(); }; const leave = () => { hovering = false; cursorX = -9999; wake(); }; const dirty = () => { rectDirty = true; }; const onResize = () => { setup(); refresh(); }; const mediaChange = () => { reduced = media.matches; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); overrideHeat = -1; refresh(); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; refresh(); }); const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => { rectDirty = true; setup(); if (!running()) drawStill(Math.max(0, overrideHeat)); }); setup(); io?.observe(wrap); ro?.observe(btn); window.addEventListener("pointermove", onMove, { passive: true }); window.addEventListener("resize", onResize); window.addEventListener("scroll", dirty, true); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointerleave", leave); document.addEventListener("visibilitychange", refresh); media.addEventListener("change", mediaChange); playRef.current = () => { if (disabled || btn.matches(":disabled")) return; if (reduced) { timers.forEach((id) => window.clearTimeout(id)); timers.clear(); stillFade(Math.max(0, overrideHeat), 1, 150, () => { const id = window.setTimeout(() => { timers.delete(id); stillFade(1, 0, 150, () => { overrideHeat = -1; }); }, 600); timers.add(id); }); return; } const a = plate.animate( [{ transform: "scale(1,1)" }, { transform: "scale(1.04,.94)", offset: 0.45 }, { transform: "scale(1,1)" }], { duration: 200, easing: "ease-out" }, ); anims.add(a); a.onfinish = () => anims.delete(a); const ba = btn.animate([ { transform: "translateY(1px) scale(.975,.96)", offset: 0 }, { transform: "translateY(-6px) scale(1.015,1.035)", offset: 0.32 }, { transform: "translateY(-2px) scale(.996,1)", offset: 0.68 }, ], { duration: 460, easing: "cubic-bezier(.2,.8,.3,1)" }); anims.add(ba); ba.onfinish = () => anims.delete(ba); for (const b of blobs) b.T = Math.min(1.2, b.T + 0.4); kick = 1; kickHold = simT + 0.8; if (bursts.length < 2) { const second = bursts.length > 0; const p = pool[second ? 3 : 2]; bursts.push({ t: 0, ax: p.x, x: p.x, y: GY1 - 10, vx: 0, vy: 0, r: p.r, r0: second ? 18 : 22, r1: second ? 22 : 30, launch: second ? 420 : 520, phase: 0, apex: false, snapped: false, snapT: 0, neck: 1, }); } wake(); }; refresh(); return () => { playRef.current = () => {}; cancelAnimationFrame(frame); timers.forEach((id) => window.clearTimeout(id)); timers.clear(); anims.forEach((a) => a.cancel()); anims.clear(); io?.disconnect(); ro?.disconnect(); window.removeEventListener("pointermove", onMove); window.removeEventListener("resize", onResize); window.removeEventListener("scroll", dirty, true); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointerleave", leave); document.removeEventListener("visibilitychange", refresh); media.removeEventListener("change", mediaChange); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [disabled, paletteKey, palette.ink]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; playRef.current(); }; return ( ); } export default LavaButton; const css = ` .tb-lava-wrap{position:relative;display:inline-block;width:${W}px;height:${H}px;vertical-align:middle} .tb-lava-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;border:0;margin:0; border-radius:42px;background:transparent;overflow:visible;cursor:pointer;-webkit-tap-highlight-color:transparent;color:var(--tb-lava-ink,#ffc271); font:600 20px ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif} .tb-lava-btn:focus-visible{outline:3px solid #ff8a3d;outline-offset:6px} .tb-lava-btn{transition:transform 260ms cubic-bezier(.34,1.56,.64,1)} .tb-lava-btn:hover{transform:translateY(-3px)} .tb-lava-btn:active{transform:translateY(1px) scale(.975,.96);transition:transform 70ms cubic-bezier(.3,0,.6,1)} @media (prefers-reduced-motion:reduce){.tb-lava-btn,.tb-lava-btn:hover,.tb-lava-btn:active{transform:none}} .tb-lava-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-lava-canvas{position:absolute;left:-${PADX}px;top:-${PADT}px;width:${CW}px;height:${CH}px;pointer-events:none} .tb-lava-spec{position:absolute;left:46px;top:6px;width:168px;height:8px;border-radius:8px;pointer-events:none; background:linear-gradient(180deg,rgba(255,248,236,.62) 0,rgba(255,236,210,.16) 65%,rgba(255,236,210,0) 100%); -webkit-mask-image:linear-gradient(90deg,transparent 0,#000 12%,#000 72%,transparent 100%); mask-image:linear-gradient(90deg,transparent 0,#000 12%,#000 72%,transparent 100%)} .tb-lava-spec::after{content:"";position:absolute;left:10px;top:1px;width:28px;height:2px;border-radius:2px;background:rgba(255,255,255,.85);filter:blur(.4px)} .tb-lava-cap{position:absolute;top:0;width:28px;height:${H}px;box-sizing:border-box;pointer-events:none;border:0; background: radial-gradient(55% 22% at 50% 12%,rgba(255,255,255,.26),rgba(255,255,255,0) 75%), linear-gradient(180deg,#3a3a3e 0,#1d1d20 16%,#0b0b0c 52%,#121214 84%,#2a2a2e 100%); box-shadow:inset 0 1px 0 rgba(255,255,255,.16),inset 0 -2px 3px rgba(0,0,0,.7),inset 0 0 0 1px #3a3a3d} .tb-lava-cap::after{content:"";position:absolute;inset:0;border-radius:inherit;opacity:.35;transition:opacity 600ms ease-out} .tb-lava-cap::before{content:"";position:absolute;top:4px;bottom:8px;width:14px;pointer-events:none;box-sizing:border-box; -webkit-mask-image:linear-gradient(180deg,#000 0,#000 30%,transparent 78%);mask-image:linear-gradient(180deg,#000 0,#000 30%,transparent 78%)} .tb-lava-cap-l::before{left:3px;border-left:2px solid rgba(255,255,255,.38);border-radius:20px 0 0 20px / 38px 0 0 38px} .tb-lava-cap-r::before{right:3px;border-right:2px solid rgba(255,255,255,.18);border-radius:0 20px 20px 0 / 0 38px 38px 0} .tb-lava-cap-l{left:0;box-shadow:inset 0 1px 0 rgba(255,255,255,.16),inset 0 -2px 3px rgba(0,0,0,.7),inset 0 0 0 1px #3a3a3d,3px 0 0 -1px #0b0b0c,5px 0 4px -1px rgba(0,0,0,.55);border-radius:24px 6px 6px 24px / 42px 8px 8px 42px} .tb-lava-cap-l::after{box-shadow:inset -3px 0 4px rgba(255,120,40,.55);background:linear-gradient(90deg,rgba(255,255,255,.08) 0,rgba(255,255,255,0) 45%,rgba(0,0,0,.25) 100%)} .tb-lava-cap-r{right:0;box-shadow:inset 0 1px 0 rgba(255,255,255,.16),inset 0 -2px 3px rgba(0,0,0,.7),inset 0 0 0 1px #3a3a3d,-3px 0 0 -1px #0b0b0c,-5px 0 4px -1px rgba(0,0,0,.55);border-radius:6px 24px 24px 6px / 8px 42px 42px 8px} .tb-lava-cap-r::after{box-shadow:inset 3px 0 4px rgba(255,120,40,.55);background:linear-gradient(270deg,rgba(255,255,255,.08) 0,rgba(255,255,255,0) 45%,rgba(0,0,0,.25) 100%)} .tb-lava-btn:hover .tb-lava-cap::after{opacity:1} .tb-lava-plate{position:absolute;left:84px;top:24px;width:92px;height:36px;box-sizing:border-box;display:flex;align-items:center;justify-content:center; border-radius:18px;background:linear-gradient(180deg,#221c18 0,#120f0d 55%,#0c0a09 100%);border:1px solid #3d2a1e; box-shadow:inset 0 1px 0 rgba(255,220,190,.10),inset 0 3px 6px rgba(0,0,0,.75),0 1px 0 rgba(255,140,60,.28),0 4px 10px rgba(0,0,0,.45);pointer-events:none;transform-origin:50% 50%} .tb-lava-label{font-weight:700;letter-spacing:.08em;text-transform:uppercase;white-space:nowrap;text-shadow:0 0 6px rgba(255,150,50,.6),0 0 14px rgba(255,110,20,.32);line-height:1} .tb-lava-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-lava-wrap *,.tb-lava-wrap *::before,.tb-lava-wrap *::after{animation:none!important;transition:none!important}} `;