"use client"; /** * jelly.tsx — a glowing jellyfish bell with trailing verlet tentacles. * Hover leans the bell toward the cursor and the tentacles stream after it; click squeezes, jets upward, then drifts back down. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type MouseEvent, type Ref, type ReactNode } from "react"; type Palette = { bellTop: string; bellMid: string; bellRim: string; arm: string; armTip: string; glow: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; }; const W = 210; const H = 76; const PAD_X = 60; const PAD_T = 110; // room above for the jet and the chevrons const PAD_B = 150; // tentacles const CW = W + PAD_X * 2; const CH = PAD_T + H + PAD_B; const BELL_CX = CW / 2; const BELL_CY = PAD_T + H / 2; const ACCENT = "#9fc4ff"; const SIL = "M40,71 C19,71 2,58 2,41 C2,24 18,11 38,11 C42,11 45,11.5 48,12.5 C53,7 60,4 68,3 L142,3 C150,4 157,7 162,12.5 C165,11.5 168,11 172,11 C192,11 208,24 208,41 C208,58 191,71 170,71 Z"; const DEFAULT_PALETTE: Palette = { bellTop: "#8fb3f6", bellMid: "#446fdb", bellRim: "#2f4eb5", arm: "#6b76e2", armTip: "#b48cf0", glow: "rgba(116,147,242,.9)", }; const ARMS = 6; const THREADS = 10; const POINTS = 12; const SUB = 0.008; // fixed physics step, seconds const DRAG = Math.pow(0.94, 0.5); // 0.94 per 16 ms const GRAVITY = 700; // stronger than the brief's 260 so trails hang straight during the jet const SCALLOPS = 14; const LIFT = 52; const MAX_LIFT = 70; type Chain = { rx: number; ry: number; len: number; iters: number; ribbon: boolean; phase: number; x: Float32Array; y: Float32Array; px: Float32Array; py: Float32Array; }; const ease = (u: number) => (u < 0 ? 0 : u > 1 ? 1 : u); const outCubic = (u: number) => 1 - Math.pow(1 - ease(u), 3); const inOut = (u: number) => { const v = ease(u); return v * v * (3 - 2 * v); }; const lerp = (a: number, b: number, u: number) => a + (b - a) * u; export function JellyButton({ children = "swim", palette = DEFAULT_PALETTE, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); useImperativeHandle(ref, () => btnRef.current!, []); const wrapRef = useRef(null); const canvasRef = useRef(null); const visRef = useRef(null); const bodyRef = useRef(null); const glowRef = useRef(null); const flashRef = useRef(null); const labelRef = useRef(null); const api = useRef<{ play: () => void }>({ play: () => {} }); const uid = `tb-jelly-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const { arm, armTip } = palette; useEffect(() => { const btn = btnRef.current; const wrap = wrapRef.current; const canvas = canvasRef.current; const vis = visRef.current; const body = bodyRef.current; const glowEl = glowRef.current; const flashEl = flashRef.current; const label = labelRef.current; if (!btn || !wrap || !canvas || !vis || !body || !glowEl || !flashEl || !label) return; const ctx = canvas.getContext("2d"); if (!ctx) return; // ---- chains ---- const chains: Chain[] = []; const mk = (rx: number, ry: number, len: number, ribbon: boolean, i: number) => { const c: Chain = { rx, ry, len, ribbon, iters: ribbon ? 3 : 2, phase: i * 1.3, x: new Float32Array(POINTS), y: new Float32Array(POINTS), px: new Float32Array(POINTS), py: new Float32Array(POINTS), }; for (let k = 0; k < POINTS; k++) { const sway = Math.sin(k * 0.55 + i * 1.7) * k * 0.9; c.x[k] = c.px[k] = BELL_CX + rx + sway; c.y[k] = c.py[k] = BELL_CY + ry + k * len; } chains.push(c); }; const ARM_RX = [-34, -20, -7, 7, 20, 34]; for (let i = 0; i < ARMS; i++) mk(ARM_RX[i], 30 + (i % 2) * 3, 10.4 + (i % 3) * 0.5, true, i); for (let i = 0; i < THREADS; i++) mk(-86 + i * (172 / (THREADS - 1)), 28, 8.5 + ((i * 7) % 4) * 0.8, false, i + ARMS); // ---- state ---- const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let reduced = media.matches; let visible = true; let raf = 0; let last = 0; let acc = 0; let time = 0; let hover = btn.matches(":hover"); let hov = 0; let rect: DOMRect | undefined; let dirty = true; let scale = 1; let curX = -1e4, curY = -1e4, curT = 0, cvx = 0, cvy = 0; // bell spring let bx = 0, bvx = 0, brot = 0, bvrot = 0; let glow = 0.55; let dpr = 1; let grad: CanvasGradient | null = null; let frillGrad: CanvasGradient | null = null; let click: { t0: number; y0: number; yT: number; ringLift?: number } | null = null; let lift = 0; let bubbles: { ts: number; x: number; y: number; r: number; ph: number; l0: number | null }[] = []; const timers = new Set(); const later = (fn: () => void, ms: number) => { const id = window.setTimeout(() => { timers.delete(id); fn(); }, ms); timers.add(id); }; const allowed = () => !disabled && !btn.matches(":disabled") && visible && !document.hidden; const fit = () => { rect = btn.getBoundingClientRect(); scale = rect.width / (btn.offsetWidth || 1) || 1; dpr = Math.min(2, (window.devicePixelRatio || 1) * scale); const w = Math.round(CW * dpr), h = Math.round(CH * dpr); if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h; } ctx.setTransform(dpr, 0, 0, dpr, 0, 0); grad = ctx.createLinearGradient(0, BELL_CY + 30, 0, BELL_CY + 30 + 130); grad.addColorStop(0, arm); grad.addColorStop(1, armTip); frillGrad = ctx.createLinearGradient(0, BELL_CY + H / 2 - 6, 0, BELL_CY + H / 2 + 9); frillGrad.addColorStop(0, "rgba(160,180,255,.15)"); frillGrad.addColorStop(0.5, "rgba(170,190,255,.32)"); frillGrad.addColorStop(1, "rgba(205,220,255,.55)"); dirty = false; }; // ---- per frame: bell pose + click timeline ---- let sx = 1, sy = 1, flash = 0, clickGlow = 0; const timeline = (now: number) => { let tx = 1, ty = 1, jetting = false; flash = 0; clickGlow = 0; if (click) { const ct = now - click.t0; if (ct < 140) { const u = Math.pow(ct / 140, 2.2); tx = 1 - 0.14 * u; ty = 1 + 0.1 * u; lift = click.y0; clickGlow = 1; flash = 0.85 * u; } else if (ct < 520) { const u = (ct - 140) / 380; lift = lerp(click.y0, click.yT, outCubic(u)); const g = outCubic(u / 0.3); tx = u < 0.3 ? lerp(0.86, 1.06, g) : lerp(1.06, 1, inOut((u - 0.3) / 0.7)); ty = u < 0.3 ? lerp(1.1, 0.94, g) : lerp(0.94, 1, inOut((u - 0.3) / 0.7)); clickGlow = 1; jetting = true; flash = 0.85 * (1 - ease(u * 1.4)); } else if (ct < 1900) { const u = (ct - 520) / 1380; lift = click.yT * (1 - inOut(u)); if (u > 0.8) { const w = (u - 0.8) / 0.2; ty = 1 + 0.025 * Math.sin(w * Math.PI * 2) * (1 - w * 0.5); tx = 1 - 0.015 * Math.sin(w * Math.PI * 2) * (1 - w * 0.5); } clickGlow = 1; } else if (ct < 2600) { lift = 0; clickGlow = 1 - inOut((ct - 1900) / 700); } else { click = null; lift = 0; wrap.dataset.tbState = hover ? "hover" : "rest"; } } else lift = 0; lift -= 8 * hov; // ambient breath on top of the click pose const ph = 0.5 - 0.5 * Math.cos((time * Math.PI * 2) / 3.2); sx = tx * (1 + 0.03 * ph); sy = ty * (1 - 0.04 * ph); sx *= 1 + 0.025 * hov; sy *= 1 - 0.02 * hov; return jetting; }; const rootWorld = (c: Chain, out: number[]) => { const cs = Math.cos(brot), sn = Math.sin(brot); const lx = c.rx * sx, ly = c.ry * sy; out[0] = BELL_CX + bx + lx * cs - ly * sn; out[1] = BELL_CY + lift + lx * sn + ly * cs; }; const rt = [0, 0]; const physics = (dt: number) => { acc = Math.min(acc + dt, SUB * 4); const lean = hover && rect ? Math.max(-1, Math.min(1, (curX - (rect.left + rect.width / 2)) / (rect.width / 2 * scale))) : 0; const pct = click ? performance.now() - click.t0 : 1e9; const settle = pct > 140 && pct < 1100; const jetHold = pct < 900; while (acc >= SUB) { acc -= SUB; const h2 = SUB * SUB; for (let ci = 0; ci < chains.length; ci++) { const c = chains[ci]; rootWorld(c, rt); c.x[0] = c.px[0] = rt[0]; c.y[0] = c.py[0] = rt[1]; const wind = settle || jetHold ? 0 : hov * lean * 1700 + Math.sin(time / 1.7 + c.phase) * 140; const drag = settle ? Math.pow(0.86, 0.5) : DRAG; const stf = (settle ? 3.2 : 1) * (1 - 0.8 * hov); const cur = (curT > 0 && cur_active) ? 1 : 0; for (let k = 1; k < POINTS; k++) { const w = k / (POINTS - 1); let vx = (c.x[k] - c.px[k]) * drag, vy = (c.y[k] - c.py[k]) * drag; c.px[k] = c.x[k]; c.py[k] = c.y[k]; let nx = c.x[k] + vx + wind * w * h2; let ny = c.y[k] + vy + GRAVITY * h2; if (cur) { const dx = (c.x[k] - cwx), dy = (c.y[k] - cwy); const d = Math.hypot(dx, dy); if (d < 70) { const f = (1 - d / 70) * 0.35 * SUB; nx += Math.max(-1600, Math.min(1600, cvx)) * f; ny += Math.max(-1600, Math.min(1600, cvy)) * f; } } // soft stiffness: each point prefers to sit below its parent nx += (c.x[k - 1] + c.rx * (c.ribbon ? 0.035 : 0.03) - nx) * 0.012 * (c.ribbon ? 1.4 : 0.7) * stf; ny += (c.y[k - 1] + c.len - ny) * 0.012 * (c.ribbon ? 1.4 : 0.7) * stf; if (jetHold) nx += (c.x[0] + c.rx * (c.ribbon ? 0.035 : 0.03) * k - nx) * 0.04; c.x[k] = nx; c.y[k] = ny; } const tip = POINTS - 1; if (!(Math.abs(c.x[tip] - rt[0]) < 200 && Math.abs(c.y[tip] - rt[1]) < 200)) { for (let k = 1; k < POINTS; k++) { c.x[k] = c.px[k] = rt[0]; c.y[k] = c.py[k] = rt[1] + k * c.len; } } for (let it = 0; it < c.iters; it++) { for (let k = 1; k < POINTS; k++) { const dx = c.x[k] - c.x[k - 1], dy = c.y[k] - c.y[k - 1]; const d = Math.hypot(dx, dy) || 1e-4; const diff = (d - c.len) / d; if (k === 1) { c.x[k] -= dx * diff; c.y[k] -= dy * diff; } else { const hx = dx * diff * 0.5, hy = dy * diff * 0.5; c.x[k] -= hx; c.y[k] -= hy; c.x[k - 1] += hx; c.y[k - 1] += hy; } } } for (let k = 1; k < POINTS; k++) { if (c.y[k] < c.y[0] + 2) c.y[k] = c.y[0] + 2; c.x[k] = Math.max(10, Math.min(CW - 10, c.x[k])); } } } }; let cur_active = false, cwx = 0, cwy = 0; const draw = (now: number) => { ctx.clearRect(0, 0, CW, CH); const ct = click ? now - click.t0 : 1e9; const cs = Math.cos(brot), sn = Math.sin(brot); const ringHalf = (rot: number, a0: number, a1: number, m: number) => { const rl = click && click.ringLift !== undefined ? click.ringLift : lift; const u = (ct - 140) / 480, e = outCubic(u); const cx = BELL_CX + bx, cy = BELL_CY + rl + H / 2 + 2, rx = lerp(84, 140, e), ry = lerp(8, 22, e); ctx.lineCap = "round"; ctx.beginPath(); ctx.ellipse(cx, cy, rx, ry, rot, a0, a1); ctx.lineWidth = lerp(6, 2, u); ctx.strokeStyle = `rgba(110,195,255,${(0.14 * (1 - u) * m * 2).toFixed(3)})`; ctx.stroke(); ctx.beginPath(); ctx.ellipse(cx, cy, rx, ry, rot, a0, a1); ctx.lineWidth = lerp(1.6, 0.6, u); ctx.strokeStyle = `rgba(190,235,255,${(0.6 * (1 - u) * m * 2).toFixed(3)})`; ctx.stroke(); }; if (click) { // pulse ring, back half if (ct > 140 && ct < 620) { if (click.ringLift === undefined) click.ringLift = lift; ringHalf(0, Math.PI, 2 * Math.PI, 0.5); } // upward arrows above the bell const a = ct < 140 ? 0 : ct < 260 ? (ct - 140) / 120 : Math.max(0, 1 - (ct - 260) / 300); if (a > 0) { const base0 = BELL_CY + lift - H / 2 - 8 - (ct - 140) * 0.05; const arrows = [[0, 30, 8], [-46, 18, 6], [46, 18, 6]]; ctx.lineCap = "round"; ctx.lineJoin = "round"; const passes: [number, string, number][] = [[12, "60,150,255", 0.16], [5, "80,190,255", 0.55], [2.4, "200,240,255", 1]]; for (const [lw, rgb, A] of passes) { const aa = A * a; const g = ctx.createLinearGradient(0, base0 + 6, 0, base0 - 32); g.addColorStop(0, `rgba(${rgb},0)`); g.addColorStop(0.35, `rgba(${rgb},${(aa * 0.6).toFixed(3)})`); g.addColorStop(1, `rgba(${rgb},${aa.toFixed(3)})`); ctx.strokeStyle = g; ctx.lineWidth = lw; ctx.beginPath(); for (const [dx, sh, hd] of arrows) { const x = BELL_CX + bx + dx + (ct - 140) * 0.06 * Math.sign(dx); const base = base0 + (dx ? 6 : 0); ctx.moveTo(x, base); ctx.lineTo(x, base - sh); ctx.moveTo(x - hd, base - sh + hd); ctx.lineTo(x, base - sh); ctx.lineTo(x + hd, base - sh + hd); } ctx.stroke(); } } // jet streaks if (ct > 140 && ct < 620) { const u = (ct - 140) / 480; const rimY = BELL_CY + lift + H / 2; const mul = [0.7, 1, 0.85, 0.85, 1, 0.7]; ctx.lineCap = "round"; for (const [lw, rgb, A] of [[3.5, "100,190,255", 0.22], [1, "200,240,255", 0.8]] as [number, string, number][]) { const aa = A * (1 - u); const g = ctx.createLinearGradient(0, rimY - 14, 0, rimY + 100); g.addColorStop(0, `rgba(${rgb},${aa.toFixed(3)})`); g.addColorStop(0.6, `rgba(${rgb},${(aa * 0.35).toFixed(3)})`); g.addColorStop(1, `rgba(${rgb},0)`); ctx.strokeStyle = g; ctx.lineWidth = lw; ctx.beginPath(); [-100, -90, -78, 78, 90, 100].forEach((dx, n) => { ctx.moveTo(BELL_CX + bx + dx, rimY - 14); ctx.lineTo(BELL_CX + bx + dx, rimY - 14 + lerp(30, 110, u) * mul[n]); }); ctx.stroke(); } } } // ribbons: two depth layers, spine const lp: number[] = [], rp: number[] = []; const armPath = (idx: number[]) => { for (const ci of idx) { const c = chains[ci]; lp.length = 0; rp.length = 0; for (let k = 0; k < POINTS; k++) { const a = c.y[Math.min(POINTS - 1, k + 1)] - c.y[Math.max(0, k - 1)]; const b = c.x[Math.min(POINTS - 1, k + 1)] - c.x[Math.max(0, k - 1)]; const l = Math.hypot(a, b) || 1; const nx = -a / l, ny = b / l; const t = k / (POINTS - 1); const flare = t < 0.15 ? lerp(0.5, 1, t / 0.15) : 1; const base = lerp(9, 1.2, Math.pow(t, 0.9)) * flare; const twist = 0.4 + 0.6 * Math.abs(Math.cos(k * 0.62 + c.phase * 0.7 + time * 0.9)); const wl = base * twist * (1 + 0.38 * Math.sin(k * 1.15 + c.phase + time * 1.8)); const wr = base * twist * (1 + 0.38 * Math.sin(k * 1.15 + c.phase + 1.9 + time * 1.8)); lp.push(c.x[k] + nx * wl, c.y[k] + ny * wl); rp.push(c.x[k] - nx * wr, c.y[k] - ny * wr); } ctx.moveTo(lp[0], lp[1]); for (let k = 1; k < POINTS - 1; k++) ctx.quadraticCurveTo(lp[k * 2], lp[k * 2 + 1], (lp[k * 2] + lp[k * 2 + 2]) / 2, (lp[k * 2 + 1] + lp[k * 2 + 3]) / 2); ctx.lineTo(lp[(POINTS - 1) * 2], lp[(POINTS - 1) * 2 + 1]); ctx.lineTo(rp[(POINTS - 1) * 2], rp[(POINTS - 1) * 2 + 1]); for (let k = POINTS - 2; k > 0; k--) ctx.quadraticCurveTo(rp[k * 2], rp[k * 2 + 1], (rp[k * 2] + rp[k * 2 - 2]) / 2, (rp[k * 2 + 1] + rp[k * 2 - 1]) / 2); ctx.lineTo(rp[0], rp[1]); ctx.closePath(); } }; ctx.lineCap = "round"; ctx.lineJoin = "round"; for (let layer = 0; layer < 2; layer++) { ctx.globalAlpha = layer ? 0.58 : 0.36; ctx.fillStyle = grad ?? arm; ctx.beginPath(); armPath(layer ? [0, 2, 4] : [1, 3, 5]); ctx.fill(); ctx.globalAlpha = 1; ctx.strokeStyle = layer ? "rgba(215,205,255,.6)" : "rgba(150,160,240,.35)"; ctx.lineWidth = layer ? 0.9 : 0.8; ctx.stroke(); } ctx.strokeStyle = "rgba(225,230,255,.28)"; ctx.lineWidth = 1.6; ctx.beginPath(); for (let ci = 0; ci < ARMS; ci++) { const c = chains[ci]; ctx.moveTo(c.x[0], c.y[0]); for (let k = 1; k < POINTS - 1; k++) ctx.quadraticCurveTo(c.x[k], c.y[k], (c.x[k] + c.x[k + 1]) / 2, (c.y[k] + c.y[k + 1]) / 2); ctx.lineTo(c.x[POINTS - 1], c.y[POINTS - 1]); } ctx.stroke(); // threads (one stroke) ctx.strokeStyle = "rgba(165,198,255,.62)"; ctx.lineWidth = 0.7; ctx.beginPath(); for (let ci = ARMS; ci < chains.length; ci++) { const c = chains[ci]; ctx.moveTo(c.x[0], c.y[0]); for (let k = 1; k < POINTS - 1; k++) ctx.quadraticCurveTo(c.x[k], c.y[k], (c.x[k] + c.x[k + 1]) / 2, (c.y[k] + c.y[k + 1]) / 2); ctx.lineTo(c.x[POINTS - 1], c.y[POINTS - 1]); } ctx.stroke(); // tip halo + dots for (let pass = 0; pass < 2; pass++) { ctx.fillStyle = pass ? "#eef5ff" : "rgba(150,205,255,.3)"; ctx.beginPath(); for (let ci = ARMS; ci < chains.length; ci++) { const c = chains[ci]; const tr = pass ? 1.1 : 2.8; ctx.moveTo(c.x[POINTS - 1] + tr, c.y[POINTS - 1]); ctx.arc(c.x[POINTS - 1], c.y[POINTS - 1], tr, 0, Math.PI * 2); } ctx.fill(); } // velum frill: translucent scalloped skirt const jet = click && ct > 100 && ct < 700; const period = jet ? 0.8 : 3.2; const tw = (lx: number, ly: number, out: number[], o: number) => { const x = lx * sx, y = ly * sy; out[o] = BELL_CX + bx + x * cs - y * sn; out[o + 1] = BELL_CY + lift + x * sn + y * cs; }; const q: number[] = [0, 0, 0, 0, 0, 0]; const sw = 160 / SCALLOPS, yb = H / 2 + 1; const scallops = () => { for (let i = SCALLOPS - 1; i >= 0; i--) { const x0 = -80 + i * sw, x1 = x0 + sw; const rip = 1.2 * Math.sin((time / period) * Math.PI * 2 - i * 0.7); tw((x0 + x1) / 2, yb + (3 + 4 * Math.sin(Math.PI * (i + 0.5) / SCALLOPS)) + rip, q, 0); tw(x0, yb + 1.5 * rip, q, 2); ctx.quadraticCurveTo(q[0], q[1], q[2], q[3]); } }; tw(-78, H / 2 - 6, q, 4); ctx.beginPath(); ctx.moveTo(q[4], q[5]); tw(78, H / 2 - 6, q, 4); ctx.lineTo(q[4], q[5]); tw(80, yb, q, 4); ctx.lineTo(q[4], q[5]); scallops(); ctx.closePath(); ctx.fillStyle = frillGrad ?? "rgba(130,140,240,.4)"; ctx.fill(); ctx.strokeStyle = "rgba(215,230,255,.75)"; ctx.lineWidth = 1.2; ctx.lineJoin = "round"; tw(80, yb, q, 4); ctx.beginPath(); ctx.moveTo(q[4], q[5]); scallops(); ctx.stroke(); // marginal fringe ctx.strokeStyle = "rgba(190,210,255,.3)"; ctx.lineWidth = 0.5; ctx.lineCap = "round"; ctx.beginPath(); for (let j = 0; j < 22; j++) { const lx = -76 + j * (152 / 21); tw(lx, yb + 2, q, 4); const len = 5 + 4 * ((j * 5) % 3) + 2 * Math.sin(time * 1.3 + j); const swy = Math.max(-6, Math.min(6, 2 * Math.sin(time * 1.1 + j * 0.8) - bvx * 0.03)); ctx.moveTo(q[4], q[5]); ctx.quadraticCurveTo(q[4] + swy * 0.4, q[5] + len * 0.5, q[4] + swy, q[5] + len); } ctx.stroke(); if (click && ct > 140 && ct < 620) ringHalf(0, 0, Math.PI, 0.5); // bubbles if (bubbles.length && click) { const act: { x: number; y: number; r: number }[] = []; for (const b of bubbles) { const age = ct - b.ts; if (age < 0 || age > 900) continue; if (b.l0 === null) b.l0 = lift; act.push({ x: BELL_CX + bx + b.x + Math.sin(age / 120 + b.ph) * 6, y: BELL_CY + H / 2 + b.l0 + b.y - (age / 1000) * 60, r: b.r }); } if (act.length) { ctx.lineWidth = 0.9; ctx.fillStyle = "rgba(180,215,255,.12)"; ctx.beginPath(); for (const p of act) { ctx.moveTo(p.x + p.r, p.y); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); } ctx.fill(); ctx.strokeStyle = "rgba(215,235,255,.75)"; ctx.beginPath(); for (const p of act) { ctx.moveTo(p.x + p.r, p.y); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); } ctx.stroke(); ctx.fillStyle = "rgba(235,245,255,.8)"; ctx.beginPath(); for (const p of act) { const hx = p.x - p.r * 0.35, hy = p.y - p.r * 0.35; ctx.moveTo(hx + 0.8, hy); ctx.arc(hx, hy, 0.8, 0, Math.PI * 2); } ctx.fill(); } } }; const apply = () => { vis.style.transform = `translate(${bx.toFixed(2)}px,${lift.toFixed(2)}px) rotate(${((brot * 180) / Math.PI).toFixed(2)}deg)`; body.style.transform = `scale(${sx.toFixed(4)},${sy.toFixed(4)})`; glowEl.style.opacity = glow.toFixed(3); flashEl.style.opacity = flash.toFixed(3); label.style.setProperty("--tb-jelly-lit", String(Math.max(0, (glow - 0.55) / 0.45))); }; const tick = (now: number) => { raf = 0; if (!allowed() || reduced) return; if (dirty) fit(); const dt = last ? Math.min(0.048, (now - last) / 1000) : 0.016; last = now; time += dt; hov += (((hover ? 1 : 0) - hov) * (1 - Math.exp(-dt / 0.2))); timeline(now); // bell spring toward the cursor const lean = hover && rect ? Math.max(-1, Math.min(1, (curX - (rect.left + rect.width / 2)) / ((rect.width / 2) || 1))) : 0; const tgtX = lean * 6 * (click ? 0.25 : 1), tgtR = (lean * 4 * (click ? 0.25 : 1) * Math.PI) / 180; bvx += (-120 * (bx - tgtX) - 14 * bvx) * dt; bx += bvx * dt; bvrot += (-120 * (brot - tgtR) - 14 * bvrot) * dt; brot += bvrot * dt; // glow const gT = Math.max(lerp(0.5, 0.95, hov), clickGlow); glow += (gT - glow) * (1 - Math.exp(-dt / (click ? 0.05 : 0.09))); // cursor world coords for the wake if (rect) { cur_active = curT > 0 && now - curT < 120 && curX > rect.left - 90 && curX < rect.right + 90 && curY > rect.top - 90 && curY < rect.bottom + 190; cwx = (curX - rect.left) / scale + PAD_X; cwy = (curY - rect.top) / scale + PAD_T; } if (!cur_active) { cvx = 0; cvy = 0; } physics(dt); apply(); draw(now); raf = requestAnimationFrame(tick); }; const start = () => { if (!raf && allowed() && !reduced) { last = 0; raf = requestAnimationFrame(tick); } }; const staticDraw = () => { if (dirty) fit(); sx = 1; sy = 1; flash = 0; glow = reduced && wrap.dataset.tbState === "lit" ? 1 : hover && !reduced ? 0.8 : 0.55; bx = 0; brot = 0; lift = 0; apply(); draw(performance.now()); }; const settleReduced = () => { // let the chains find a calm hanging pose once, no wind for (let n = 0; n < 40; n++) { acc = SUB; const hv = hov; hov = 0; const t = time; time = 0; physics(0); hov = hv; time = t; } acc = 0; }; const pause = () => { wrap.dataset.tbPaused = String(disabled || !visible || document.hidden); if (!allowed() || reduced) { cancelAnimationFrame(raf); raf = 0; } if (reduced || !allowed()) staticDraw(); else { dirty = true; start(); } }; const invalidate = () => { dirty = true; if (reduced) staticDraw(); }; const enter = () => { if (disabled) return; hover = true; dirty = true; wrap.dataset.tbState = click ? "click" : "hover"; if (reduced) staticDraw(); else start(); }; const leave = () => { hover = false; wrap.dataset.tbState = click ? "click" : "rest"; if (reduced) staticDraw(); }; const move = (e: PointerEvent) => { const now = performance.now(); if (curT > 0 && now > curT) { const k = Math.min(1, 0.3); const dtt = Math.max(8, now - curT) / 1000; cvx += ((e.clientX - curX) / scale / dtt - cvx) * k; cvy += ((e.clientY - curY) / scale / dtt - cvy) * k; } curX = e.clientX; curY = e.clientY; curT = now; if (!raf) start(); }; const gone = () => { curT = 0; curX = curY = -1e4; }; api.current.play = () => { if (disabled) return; if (reduced) { wrap.dataset.tbState = "lit"; staticDraw(); later(() => { wrap.dataset.tbState = hover ? "hover" : "rest"; staticDraw(); }, 1200); return; } const now = performance.now(); const y0 = click ? lift : 0; const yT = Math.max(-MAX_LIFT, y0 - LIFT); click = { t0: now, y0, yT }; wrap.dataset.tbState = "click"; bubbles = []; for (let i = 0; i < 18; i++) { bubbles.push({ ts: 140 + Math.random() * 380, x: (Math.random() * 2 - 1) * 90, y: Math.random() * 6, r: 1.6 + Math.random() * 2, ph: Math.random() * 6, l0: null }); } start(); }; const onVis = () => pause(); const onMedia = () => { reduced = media.matches; click = null; wrap.dataset.tbState = hover ? "hover" : "rest"; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); pause(); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; pause(); }); io?.observe(wrap); const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(invalidate); ro?.observe(btn); window.addEventListener("pointermove", move, { passive: true }); window.addEventListener("resize", invalidate); window.addEventListener("scroll", invalidate, true); document.documentElement.addEventListener("pointerleave", gone); document.addEventListener("visibilitychange", onVis); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointerleave", leave); media.addEventListener("change", onMedia); wrap.dataset.tbState = hover && !disabled ? "hover" : "rest"; fit(); if (reduced) settleReduced(); pause(); return () => { api.current.play = () => {}; cancelAnimationFrame(raf); timers.forEach((id) => window.clearTimeout(id)); timers.clear(); io?.disconnect(); ro?.disconnect(); window.removeEventListener("pointermove", move); window.removeEventListener("resize", invalidate); window.removeEventListener("scroll", invalidate, true); document.documentElement.removeEventListener("pointerleave", gone); document.removeEventListener("visibilitychange", onVis); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointerleave", leave); media.removeEventListener("change", onMedia); }; }, [disabled, arm, armTip]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; api.current.play(); }; return ( ); } export default JellyButton; const css = ` .tb-jelly-wrap{display:inline-block;position:relative;width:${W}px;height:${H}px;vertical-align:middle} .tb-jelly-btn{position:relative;display:block;width:${W}px;height:${H}px;padding:0;margin:0;border:0;border-radius:${H / 2}px;background:none;color:#eaf2ff;cursor:pointer;overflow:visible;-webkit-tap-highlight-color:transparent;font:inherit} .tb-jelly-btn:focus-visible{outline:3px solid ${ACCENT};outline-offset:6px} .tb-jelly-canvas{position:absolute;left:-${PAD_X}px;top:-${PAD_T}px;width:${CW}px;height:${CH}px;max-width:none;pointer-events:none} .tb-jelly-vis{position:absolute;inset:0;display:block;transform-origin:50% 50%;will-change:transform;pointer-events:none} .tb-jelly-body{position:absolute;inset:0;display:block;transform-origin:50% 50%;will-change:transform} .tb-jelly-glow{position:absolute;left:14px;right:14px;top:14px;bottom:12px;border-radius:${H / 2}px;background:rgba(68,111,219,.5);opacity:.55;pointer-events:none} .tb-jelly-svg{position:absolute;left:0;top:0;display:block;overflow:visible;pointer-events:none} .tb-jelly-flash{position:absolute;left:20px;right:20px;top:4px;height:44px;border-radius:50%;background:radial-gradient(closest-side,rgba(255,255,255,.6),rgba(255,255,255,0));opacity:0;pointer-events:none} .tb-jelly-label{--tb-jelly-lit:0;position:absolute;inset:0;display:flex;align-items:center;justify-content:center;text-transform:uppercase;letter-spacing:.12em;padding-left:.12em;transform:translateY(-1px); font:700 20px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;color:#f3f8ff; text-shadow:0 1px 0 rgba(25,40,130,.45),0 0 calc(5px + 5px * var(--tb-jelly-lit)) rgba(175,212,255,.85),0 0 calc(14px + 10px * var(--tb-jelly-lit)) rgba(120,165,255,calc(.35 + .4 * var(--tb-jelly-lit)));pointer-events:none} .tb-jelly-wrap[data-tb-state=lit] .tb-jelly-label{color:#fff} .tb-jelly-wrap[data-tb-state=lit] .tb-jelly-glow{opacity:1!important} .tb-jelly-btn:disabled{cursor:not-allowed;opacity:.6;filter:saturate(.5)} .tb-jelly-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-jelly-wrap *,.tb-jelly-wrap *::before,.tb-jelly-wrap *::after{animation:none!important;transition:none!important}} `;