"use client"; /** * plasma.tsx — a plasma globe set into the end of a dark glass button. * Hover and the tendrils reach for the cursor, one arc leaving the glass; click and it flashes, the label crackles with charge, then it drains. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type ReactNode, type Ref } from "react"; type Palette = { core: string; glow: string; glass: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; }; const W = 260; const H = 100; const PAD = 120; const CW = W + PAD * 2; const CH = H + PAD * 2; const GX = 50; const GY = 50; const GR = 45.5; // tendril reach inside the glass const LABEL_X = 182; const LABEL_Y = 50; const DEFAULT_PALETTE: Palette = { core: "#8b3fd5", glow: "#a769d8", glass: "#2a1450" }; const N = 7; const NEXTRA = 7; const SEGS = 12; const TOTAL = 1800; const REACH = 220; const clamp = (n: number, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, n)); const rnd = (a: number, b: number) => a + Math.random() * (b - a); const easeOut = (t: number) => 1 - Math.pow(1 - clamp(t), 3); const wrap = (a: number) => { while (a > Math.PI) a -= Math.PI * 2; while (a < -Math.PI) a += Math.PI * 2; return a; }; const rgb = (c: string, a: number) => { const h = c.length === 4 ? `#${c[1]}${c[1]}${c[2]}${c[2]}${c[3]}${c[3]}` : c; return `rgba(${parseInt(h.slice(1, 3), 16)},${parseInt(h.slice(3, 5), 16)},${parseInt(h.slice(5, 7), 16)},${a})`; }; type Tendril = { th: number; w: number; coarse: number[]; fine: number[]; bend: number; extra: boolean; seed: number; nextCoarse: number; br: number[]; brA: number; }; type Streak = { a: number; r0: number; v: number; age: number; life: number; d: number; w: number; hot: boolean }; type Ray = { a: number; len: number; w: number }; type Micro = { x0: number; y0: number; x1: number; y1: number; age: number; pts: number[] }; export function PlasmaButton({ children = "zap", 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 rimRef = useRef(null); const rimHotRef = useRef(null); const play = useRef<() => void>(() => {}); const pal = useRef(palette); pal.current = palette; const uid = `tb-plasma-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const plain = typeof children === "string" || typeof children === "number" ? String(children) : ""; const text = useRef(plain); text.current = plain; useImperativeHandle(ref, () => btnRef.current!, []); useEffect(() => { const el = btnRef.current; const host = wrapRef.current; const cv = canvasRef.current; const rim = rimRef.current; const rimHot = rimHotRef.current; if (!el || !host || !cv || !rim || !rimHot) return; const ctx = cv.getContext("2d"); if (!ctx) return; let raf = 0; let last = 0; let visible = true; let hovering = false; let hov = 0; let lastHot = 0; let kick: Animation | null = null; let began = 0; let gain = 1; let curX = 0; let curY = 0; let cx = -1e4; let cy = -1e4; let rect: DOMRect | undefined; let dirty = true; let fork: { i: number; ang: number; born: number } | null = null; let nextFork = 0; let arcFine = 0; let arcCoarseAt = 0; const arcCoarse = Array.from({ length: 6 }, () => rnd(-1, 1)); const arcFineV = Array.from({ length: 31 }, () => rnd(-1, 1)); const arcFineV2 = Array.from({ length: 31 }, () => rnd(-1, 1)); let epoch = 0; const timers = new Set(); const streaks: Streak[] = []; let rays: Ray[] = []; const micros: Micro[] = []; let microAcc = 0; let lastTwoArc = 0; let twoFine: number[][] = [0, 1].map(() => Array.from({ length: 23 }, () => rnd(-1, 1))); const twoCoarse: number[][] = [0, 1].map(() => Array.from({ length: 5 }, () => rnd(-1, 1))); let lastCoarse = 0; let dashOff = 0; let dashAt = 0; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const allowed = () => !disabled && visible && !document.hidden && !media.matches; const tend: Tendril[] = Array.from({ length: N + NEXTRA }, (_, i) => ({ th: (i / N) * Math.PI * 2 + rnd(-0.2, 0.2) + (i >= N ? 0.4 : 0), w: (rnd(0.3, 0.7)) * (Math.random() < 0.5 ? -1 : 1), coarse: Array.from({ length: 5 }, () => rnd(-1, 1)), fine: Array.from({ length: SEGS + 1 }, () => rnd(-1, 1)), bend: 0, extra: i >= N, seed: Math.random() * 100, nextCoarse: 0, br: Array.from({ length: 6 }, () => rnd(-1, 1)), brA: rnd(0.45, 0.75) * (Math.random() < 0.5 ? -1 : 1), })); const mkSprite = (size: number, stops: [number, string][]) => { const c = document.createElement("canvas"); c.width = c.height = size; const x = c.getContext("2d"); if (x) { const g = x.createRadialGradient(size / 2, size / 2, 0, size / 2, size / 2, size / 2); stops.forEach(([o, col]) => g.addColorStop(o, col)); x.fillStyle = g; x.fillRect(0, 0, size, size); } return c; }; const p0 = pal.current; const sprCore = mkSprite(64, [[0, "#ffffff"], [0.18, "#e3bbf5"], [0.45, p0.core], [0.7, "#521b9d"], [1, "rgba(82,27,157,0)"]]); const sprHalo = mkSprite(96, [[0, rgb(p0.glow, 0.6)], [0.5, rgb(p0.glow, 0.2)], [1, rgb(p0.glow, 0)]]); const sprBlob = mkSprite(32, [[0, "rgba(240,215,255,.95)"], [0.4, rgb(p0.glow, 0.5)], [1, rgb(p0.glow, 0)]]); const setup = () => { const scale = el.getBoundingClientRect().width / (el.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); dirty = true; }; // ---- stroke helpers (three additive passes, no shadowBlur) ---- const strokePts = (pts: number[], core: number, alpha: number) => { if (alpha <= 0.01) return; const trace = () => { ctx.beginPath(); ctx.moveTo(pts[0], pts[1]); for (let i = 2; i < pts.length; i += 2) ctx.lineTo(pts[i], pts[i + 1]); }; ctx.lineCap = "round"; ctx.lineJoin = "round"; trace(); ctx.lineWidth = 5 * core; ctx.strokeStyle = `rgba(106,43,191,${0.18 * alpha})`; ctx.stroke(); trace(); ctx.lineWidth = 2.4 * core; ctx.strokeStyle = rgb(pal.current.glow, 0.45 * alpha); ctx.stroke(); trace(); ctx.lineWidth = Math.max(0.9, core * 0.9); ctx.strokeStyle = `rgba(244,220,255,${alpha})`; ctx.stroke(); }; const blob = (x: number, y: number, r: number, a: number) => { ctx.globalAlpha = clamp(a); ctx.drawImage(sprBlob, x - r, y - r, r * 2, r * 2); ctx.globalAlpha = 1; }; const tendrilPts = (t: Tendril, ang: number, amp: number, len: number): number[] => { const ex = GX + Math.cos(ang) * len; const ey = GY + Math.sin(ang) * len; const nx = -Math.sin(ang), ny = Math.cos(ang); const out: number[] = []; for (let j = 0; j <= SEGS; j++) { const u = j / SEGS; const cu = u * (t.coarse.length - 1); const ci = Math.min(t.coarse.length - 2, Math.floor(cu)); const cval = t.coarse[ci] + (t.coarse[ci + 1] - t.coarse[ci]) * (cu - ci); const env = Math.sin(Math.PI * u) ** 0.7; const off = amp * (cval * 0.8 + t.fine[j] * 0.35) * env; out.push(GX + (ex - GX) * u + nx * off, GY + (ey - GY) * u + ny * off); } return out; }; const zigzag = (x0: number, y0: number, x1: number, y1: number, n: number, amp: number, vals: number[]) => { const dx = x1 - x0, dy = y1 - y0; const l = Math.hypot(dx, dy) || 1; const nx = -dy / l, ny = dx / l; const out: number[] = []; for (let j = 0; j <= n; j++) { const u = j / n; const env = Math.sin(Math.PI * u) ** 0.6; const off = amp * (vals[j % vals.length] ?? 0) * env; out.push(x0 + dx * u + nx * off, y0 + dy * u + ny * off); } return out; }; const frame = (now: number, dt: number, animate: boolean) => { if (animate) hov += ((hovering ? 1 : 0) - hov) * (1 - Math.exp(-dt / 140)); const t = began ? now - began : 0; // charge envelope: 1 until 900 ms, then drains ease-out by 1600 const charge = began ? (t < 120 ? clamp(t / 120) : t < 900 ? 1 : 1 - easeOut((t - 900) / 700)) : 0; if (began && t >= TOTAL) { began = 0; host.dataset.tbState = "idle"; host.style.setProperty("--tb-plasma-charge", "0"); } const chargeNow = began ? charge : 0; if (animate) host.style.setProperty("--tb-plasma-charge", chargeNow.toFixed(3)); // ---- pointer targets ---- let phi = 0; let cdist = 0; let haveCursor = false; if (hovering && animate) { if (dirty || !rect) { rect = el.getBoundingClientRect(); dirty = false; } const sc = rect.width / (el.offsetWidth || 1) || 1; const lx = (cx - rect.left) / sc; const ly = (cy - rect.top) / sc; phi = Math.atan2(ly - GY, lx - GX); cdist = Math.hypot(lx - GX, ly - GY); haveCursor = cx > -1e3; curX = lx; curY = ly; } ctx.clearRect(0, 0, CW, CH); ctx.save(); ctx.translate(PAD, PAD); ctx.globalCompositeOperation = "lighter"; // globe inner glow const breathe = 1 + 0.08 * Math.sin((2 * Math.PI * (animate ? now : 0)) / 2200); ctx.globalAlpha = 0.85 + 0.15 * hov; const hs = 76 * breathe * (1 + 0.3 * hov); ctx.drawImage(sprHalo, GX - hs / 2, GY - hs / 2, hs, hs); ctx.globalAlpha = 1; // bending: the 3 tendrils nearest the cursor const bendSet = new Set(); if (haveCursor && cdist > 4) { tend.filter((q) => !q.extra).map((q) => ({ q, d: Math.abs(wrap(phi - q.th)) })).filter((o) => o.d < (70 * Math.PI) / 180) .sort((a, b) => a.d - b.d).slice(0, 3).forEach((o) => bendSet.add(o.q)); } const nowS = animate ? now : 0; const flickSlot = Math.floor((nowS * 12) / 1000); const ends: number[] = []; tend.forEach((q, i) => { if (animate) { q.th += q.w * (dt / 1000) * (1 + 0.4 * Math.sin(nowS * 0.0007 + q.seed)); q.bend += ((bendSet.has(q) ? 1 : 0) - q.bend) * (1 - Math.exp(-dt / 130)); // fine segments re-jitter each frame; the coarse shape drifts a few segments at a time for (let j = 0; j < q.fine.length; j++) q.fine[j] = rnd(-1, 1); if (nowS > q.nextCoarse) { q.nextCoarse = nowS + rnd(40, 80); const k = Math.floor(Math.random() * q.coarse.length); q.coarse[k] = rnd(-1, 1); q.br[k % 6] = rnd(-1, 1); if (Math.random() < 0.1) q.brA = -q.brA; } } const vis = q.extra ? clamp(chargeNow * 1.2) : 1; if (vis <= 0.02) return; const ang = q.th + wrap(phi - q.th) * q.bend; const amp = (animate ? 3 + 3 * chargeNow : 3) * (q.extra ? 1.2 : 1); const pts = tendrilPts(q, ang, amp, GR); const h = Math.sin(flickSlot * 12.9898 + i * 78.233) * 43758.5453; const flick = animate ? 0.6 + 0.4 * (h - Math.floor(h)) : 0.85; const core = 1 + 1.1 * q.bend + 0.3 * chargeNow; strokePts(pts, core, vis * flick); { const bx = pts[14], by = pts[15]; const a1 = ang + q.brA, L1 = 0.38 * GR; strokePts(zigzag(bx, by, bx + Math.cos(a1) * L1, by + Math.sin(a1) * L1, 6, 2.5, q.br), 0.7, vis * flick * 0.55); if (q.bend > 0.3) { const b2x = pts[8], b2y = pts[9], a2 = ang - q.brA, L2 = 0.25 * GR; strokePts(zigzag(b2x, b2y, b2x + Math.cos(a2) * L2, b2y + Math.sin(a2) * L2, 6, 2.5, q.br), 0.7, vis * flick * 0.4); } } blob(pts[pts.length - 2], pts[pts.length - 1], 6 + 2 * chargeNow, vis * flick); ends.push(pts[pts.length - 2], pts[pts.length - 1]); }); if (ends.length) { ctx.beginPath(); for (let i = 0; i < ends.length; i += 2) { ctx.moveTo(ends[i] + 1.2, ends[i + 1]); ctx.arc(ends[i], ends[i + 1], 1.2, 0, Math.PI * 2); } ctx.fillStyle = "rgba(255,255,255,0.9)"; ctx.fill(); } // cursor arc if (haveCursor && cdist > GR + 3) { const lx = curX, ly = curY; const dirx = Math.cos(phi), diry = Math.sin(phi); const ex = GX + dirx * GR, ey = GY + diry * GR; const far = cdist > REACH; const tx = far ? ex + dirx * 40 : lx; const ty = far ? ey + diry * 40 : ly; if (now - arcCoarseAt > 80) { arcCoarseAt = now; arcCoarse[Math.floor(Math.random() * 6)] = rnd(-1, 1); } if (now - arcFine > 40) { arcFine = now; for (let j = 0; j < arcFineV.length; j++) { arcFineV[j] = rnd(-1, 1); arcFineV2[j] = rnd(-1, 1); } } const vals: number[] = []; const vals2: number[] = []; for (let j = 0; j <= 30; j++) { const u = (j / 30) * 5; const i0 = Math.min(4, Math.floor(u)); const cv0 = arcCoarse[i0] + (arcCoarse[i0 + 1] - arcCoarse[i0]) * (u - i0); vals.push(cv0 * 0.8 + arcFineV[j] * 0.22); vals2.push(-cv0 * 0.55 + arcFineV2[j] * 0.3); } const pts = zigzag(ex, ey, tx, ty, 30, 8, vals); const fz = far ? 0.5 + 0.5 * Math.random() : 1; strokePts(pts, 1.15, fz); if (!far) strokePts(zigzag(ex, ey + 2, tx, ty, 30, 6, vals2), 0.6, 0.42 * fz); blob(ex, ey, 13, 0.8); ctx.globalAlpha = 0.6; ctx.drawImage(sprHalo, ex - 26, ey - 26, 52, 52); ctx.globalAlpha = 1; ctx.beginPath(); ctx.arc(GX, GY, 46, phi - 0.42, phi + 0.42); ctx.lineWidth = 2; ctx.strokeStyle = "rgba(225,195,255,.55)"; ctx.stroke(); if (now > nextFork) { nextFork = now + 200; fork = { i: 10 + Math.floor(Math.random() * 10), ang: rnd(0.4, 0.8) * (Math.random() < 0.5 ? -1 : 1), born: now }; } if (fork && now - fork.born < 200) { const bx = pts[fork.i * 2], by = pts[fork.i * 2 + 1]; const dl = Math.hypot(tx - ex, ty - ey) * 0.3; const a = Math.atan2(ty - ey, tx - ex) + fork.ang; const fp = zigzag(bx, by, bx + Math.cos(a) * dl, by + Math.sin(a) * dl, 6, 5, vals); strokePts(fp, 0.8, 0.8 * fz); } if (!far) blob(tx, ty, 7, 0.55); } // core const coreR = (12 * breathe + 10 * chargeNow * (t < 400 ? 1 : 0.6)) * (began && t < 120 ? 1.3 : 1) * (1 + 0.15 * hov) * (began && t < 60 ? 0.78 + 0.22 * (t / 60) : 1); ctx.drawImage(sprCore, GX - coreR * 1.3, GY - coreR * 1.3, coreR * 2.6, coreR * 2.6); ctx.globalAlpha = 1; // ---- click effects ---- if (began) { const flash = t < 220 ? (1 - t / 220) : 0; if (flash > 0) { ctx.globalAlpha = clamp(flash * flash * gain); const g = ctx.createRadialGradient(GX, GY, 0, GX, GY, 130); g.addColorStop(0, "rgba(240,215,255,.85)"); g.addColorStop(0.15, "rgba(190,120,255,.5)"); g.addColorStop(0.45, "rgba(130,55,225,.16)"); g.addColorStop(1, "rgba(90,30,190,0)"); ctx.fillStyle = g; ctx.fillRect(GX - 130, GY - 130, 260, 260); ctx.globalAlpha = 1; } if (t < 320) { const gf = (1 - t / 320) ** 2 * gain; ctx.save(); ctx.beginPath(); ctx.arc(GX, GY, 46.5, 0, Math.PI * 2); ctx.clip(); ctx.globalAlpha = clamp(0.85 * gf); ctx.drawImage(sprHalo, GX - 62, GY - 62, 124, 124); ctx.restore(); ctx.globalAlpha = 1; } if (t < 160) { const ra = (1 - t / 160) ** 2 * Math.min(1.3, gain); const rl = easeOut(t / 70); const wedge = (k: number) => { ctx.beginPath(); for (const r of rays) { const c = Math.cos(r.a), sn = Math.sin(r.a), r1 = 4 + r.len * rl, hw = r.w * k; ctx.moveTo(GX + c * 4 - sn * hw, GY + sn * 4 + c * hw); ctx.lineTo(GX + c * r1, GY + sn * r1); ctx.lineTo(GX + c * 4 + sn * hw, GY + sn * 4 - c * hw); ctx.closePath(); } }; wedge(4); ctx.fillStyle = `rgba(167,105,216,${0.18 * ra})`; ctx.fill(); wedge(1); ctx.fillStyle = `rgba(255,238,255,${0.8 * ra})`; ctx.fill(); } if (t < 80) { const wr = 22 * (1 - t / 160) * gain; ctx.globalAlpha = 1; ctx.drawImage(sprCore, GX - wr * 1.6, GY - wr * 1.6, wr * 3.2, wr * 3.2); } // curved sparks, each with its own delay and fade ctx.lineCap = "round"; ctx.lineJoin = "round"; for (const s of streaks) { if (s.age < s.d) continue; const u = s.age - s.d, k = u / s.life; if (k >= 1) continue; const pos = (uu: number) => { const m = Math.max(0, uu); const rad = s.r0 + s.v * (m / 1000) * (1 - 0.5 * m / s.life); const an = s.a + s.w * (m / 1000); return [GX + Math.cos(an) * rad, GY + Math.sin(an) * rad]; }; const P = [pos(u), pos(u - 10), pos(u - 22), pos(u - 38)]; const tr = () => { ctx.beginPath(); ctx.moveTo(P[0][0], P[0][1]); for (let j = 1; j < 4; j++) ctx.lineTo(P[j][0], P[j][1]); }; tr(); ctx.lineWidth = s.hot ? 3.6 : 2.4; ctx.strokeStyle = `rgba(167,105,216,${0.4 * (1 - k)})`; ctx.stroke(); for (let j = 0; j < 3; j++) { ctx.beginPath(); ctx.moveTo(P[j][0], P[j][1]); ctx.lineTo(P[j + 1][0], P[j + 1][1]); ctx.lineWidth = (s.hot ? 1.5 : 1) * (1 - j * 0.3); ctx.strokeStyle = `rgba(255,240,255,${(1 - k) * (1 - j * 0.25)})`; ctx.stroke(); } } // two arcs skim along the pill to the label if (t >= 60 && t < 900) { if (now - lastTwoArc > 30) { lastTwoArc = now; twoFine = [0, 1].map(() => Array.from({ length: 23 }, () => rnd(-1, 1))); } if (now - lastCoarse > 60) { lastCoarse = now; twoCoarse.forEach((c) => { c[Math.floor(Math.random() * 5)] = rnd(-1, 1); }); } const a = clamp(1 - (t - 60) / 840) * Math.min(1.2, gain); [0, 1].forEach((k) => { if (k === 1 && t < 85) return; const vals: number[] = []; for (let j = 0; j < 23; j++) { const uu = (j / 22) * 4, i0 = Math.min(3, Math.floor(uu)); const cvv = twoCoarse[k][i0] + (twoCoarse[k][i0 + 1] - twoCoarse[k][i0]) * (uu - i0); vals.push(cvv * 0.75 + twoFine[k][j] * 0.3); } const ey = LABEL_Y + (k ? 4 : -4); strokePts(zigzag(GX + GR - 2, GY + (k ? 7 : -7), LABEL_X - 44, ey, 22, 7, vals), 1, a); blob(LABEL_X - 44, ey, 6, a); }); } // crackle: micro arcs between the label and the globe, decaying if (t >= 120 && t < 900) { microAcc += (dt / 100) * 6 * (1 - (t - 120) / 780); while (microAcc >= 1) { microAcc -= 1; const a = rnd(-0.9, 0.9); micros.push({ x0: LABEL_X - 44 + rnd(-3, 0), y0: LABEL_Y + rnd(-12, 12), x1: GX + Math.cos(a) * GR, y1: GY + Math.sin(a) * GR, age: 0, pts: Array.from({ length: 11 }, () => rnd(-1, 1)), }); } } } for (let i = micros.length - 1; i >= 0; i--) { const m = micros[i]; m.age += dt; if (m.age > 70) { micros.splice(i, 1); continue; } strokePts(zigzag(m.x0, m.y0, m.x1, m.y1, 10, 5, m.pts), 0.9, 1 - m.age / 70); } for (let i = streaks.length - 1; i >= 0; i--) { streaks[i].age += dt; if (streaks[i].age >= streaks[i].d + streaks[i].life) streaks.splice(i, 1); } ctx.globalCompositeOperation = "source-over"; ctx.restore(); if (animate && hovering) rim.style.opacity = String(0.9 + (Math.random() - 0.5) * 0.1); else if (animate) rim.style.opacity = "0.9"; const hot = Math.max(hov * 0.6, chargeNow * 0.95); if (Math.abs(hot - lastHot) > 0.01) { lastHot = hot; rimHot.style.opacity = hot.toFixed(3); } }; const tick = (now: number) => { raf = 0; if (!allowed()) return; const dt = last ? Math.min(48, now - last) : 16; last = now; frame(now, dt, true); raf = requestAnimationFrame(tick); }; const wake = () => { if (!raf && allowed()) raf = requestAnimationFrame(tick); }; 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 clearTimers = () => { epoch++; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); }; const sync = () => { cancelAnimationFrame(raf); raf = 0; last = 0; clearTimers(); began = 0; kick?.cancel(); kick = null; streaks.length = micros.length = 0; rays = []; host.dataset.tbPaused = String(disabled || !visible || document.hidden); host.dataset.tbState = "idle"; host.removeAttribute("data-rm"); host.style.setProperty("--tb-plasma-charge", "0"); hovering = allowed() && el.matches(":hover"); hov = hovering ? 1 : 0; frame(performance.now(), 16, false); wake(); }; play.current = () => { if (disabled) return; if (media.matches) { if (host.dataset.rm) return; host.dataset.rm = "on"; host.dataset.tbState = "playing"; later(() => { host.dataset.rm = "off"; }, 950); later(() => { host.removeAttribute("data-rm"); host.dataset.tbState = "idle"; }, 1100); return; } if (!allowed()) return; kick?.cancel(); kick = el.animate([ { transform: "translateY(0) scale(1)" }, { transform: "translateY(2px) scale(.978)", offset: 0.2 }, { transform: "translateY(-0.8px) scale(1.014)", offset: 0.55 }, { transform: "translateY(0) scale(1)" }], { duration: 420, easing: "cubic-bezier(.3,.7,.4,1)" }); const now = performance.now(); gain = began && now - began < 300 ? Math.min(1.5, gain + 0.25) : 1; began = now; host.dataset.tbState = "playing"; for (let i = 0; i < 16; i++) streaks.push({ a: Math.random() < 0.7 ? Math.PI + rnd(-1.9, 1.9) : rnd(0, Math.PI * 2), r0: rnd(GR - 2, GR + 3), v: rnd(240, 560), age: 0, life: rnd(160, 320), d: rnd(0, 30), w: rnd(-4, 4), hot: i < 3 }); rays = Array.from({ length: 7 }, (_, i) => ({ a: Math.PI + (i / 6 - 0.5) * 4.4 + rnd(-0.2, 0.2), len: rnd(28, 60), w: rnd(0.8, 1.4) })); rays.push({ a: Math.PI, len: 70, w: 0.6 }, { a: -Math.PI / 2 + rnd(-0.15, 0.15), len: 54, w: 0.6 }); wake(); }; const move = (e: PointerEvent) => { cx = e.clientX; cy = e.clientY; }; const enter = (e: PointerEvent) => { dirty = true; cx = e.clientX; cy = e.clientY; hovering = allowed(); wake(); }; const leave = () => { hovering = false; cx = cy = -1e4; }; const docLeave = () => { hovering = false; cx = cy = -1e4; }; const inval = () => { dirty = true; }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; sync(); }); const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => { setup(); frame(performance.now(), 16, false); }); setup(); io?.observe(host); ro?.observe(el); el.addEventListener("pointerenter", enter); el.addEventListener("pointerleave", leave); window.addEventListener("pointermove", move, { passive: true }); window.addEventListener("resize", inval); window.addEventListener("scroll", inval, true); document.documentElement.addEventListener("pointerleave", docLeave); document.addEventListener("visibilitychange", sync); media.addEventListener("change", sync); sync(); return () => { play.current = () => {}; cancelAnimationFrame(raf); clearTimers(); kick?.cancel(); kick = null; io?.disconnect(); ro?.disconnect(); el.removeEventListener("pointerenter", enter); el.removeEventListener("pointerleave", leave); window.removeEventListener("pointermove", move); window.removeEventListener("resize", inval); window.removeEventListener("scroll", inval, true); document.documentElement.removeEventListener("pointerleave", docLeave); document.removeEventListener("visibilitychange", sync); media.removeEventListener("change", sync); }; }, [disabled]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; play.current(); }; const pill = `M50 1H210A49 49 0 0 1 210 99H50A49 49 0 0 1 50 1Z`; return ( ); } export default PlasmaButton; const css = ` .tb-plasma-wrap{display:inline-flex;position:relative;vertical-align:middle;width:${W}px;height:${H}px;--tb-plasma-charge:0} .tb-plasma-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;border:0;border-radius:${H / 2}px; background:transparent;color:#e9d4ff;cursor:pointer;overflow:visible;-webkit-tap-highlight-color:transparent; font:600 26px/1 ui-sans-serif,system-ui,"Segoe UI",sans-serif;letter-spacing:.14em;text-transform:uppercase} .tb-plasma-btn{transition:transform 320ms cubic-bezier(.34,1.56,.64,1)} .tb-plasma-btn:active:not(:disabled){transform:translateY(1.5px) scale(.985);transition-duration:80ms;transition-timing-function:cubic-bezier(.3,.7,.4,1)} .tb-plasma-btn:active:not(:disabled) .tb-plasma-glow{opacity:.6;transform:scaleX(.92)} .tb-plasma-btn:focus-visible{outline:2px solid #c29bf7;outline-offset:4px} .tb-plasma-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-plasma-glow{position:absolute;left:24px;right:24px;bottom:-14px;height:28px;border-radius:50%;background:radial-gradient(closest-side,rgba(139,63,213,.45),rgba(139,63,213,.14) 60%,rgba(139,63,213,0));pointer-events:none;opacity:.8;transition:opacity 160ms,transform 160ms} .tb-plasma-shell{filter:drop-shadow(0 0 8px rgba(139,63,213,.28)) drop-shadow(0 2px 2px rgba(0,0,0,.55)) drop-shadow(0 12px 10px rgba(0,0,0,.5))} .tb-plasma-wrap[data-tb-state=playing] .tb-plasma-glow{opacity:1;transform:scaleX(1.06)} .tb-plasma-btn:focus-visible .tb-plasma-glow{opacity:1} .tb-plasma-shell,.tb-plasma-glass{position:absolute;left:0;top:0;width:${W}px;height:${H}px;pointer-events:none} .tb-plasma-canvas{position:absolute;left:-${PAD}px;top:-${PAD}px;width:${W + PAD * 2}px;height:${H + PAD * 2}px;max-width:none;pointer-events:none} .tb-plasma-label{position:absolute;left:104px;top:0;width:156px;height:${H}px;display:flex;align-items:center;justify-content:center;pointer-events:none; padding-left:.14em;-webkit-font-smoothing:antialiased; color:color-mix(in srgb,#fff calc(var(--tb-plasma-charge) * 100%),#e9d4ff); text-shadow:0 1px 0 rgba(10,4,20,.85),0 0 calc(4px + 8px * var(--tb-plasma-charge)) rgba(205,160,255,calc(.55 + .45 * var(--tb-plasma-charge))),0 0 calc(14px + 22px * var(--tb-plasma-charge)) rgba(139,63,213,calc(.45 + .5 * var(--tb-plasma-charge))),0 0 calc(1px * var(--tb-plasma-charge)) #fff} .tb-plasma-btn:hover:not(:disabled) .tb-plasma-label{color:color-mix(in srgb,#fff calc(var(--tb-plasma-charge) * 100%),#f2e3ff); text-shadow:0 1px 0 rgba(10,4,20,.85),0 0 calc(7px + 8px * var(--tb-plasma-charge)) rgba(205,160,255,.9),0 0 calc(22px + 12px * var(--tb-plasma-charge)) rgba(139,63,213,.7)} .tb-plasma-wrap[data-rm=on] .tb-plasma-label{color:#fff;text-shadow:0 1px 0 rgba(10,4,20,.85),0 0 10px rgba(205,160,255,1),0 0 26px rgba(139,63,213,.85)} .tb-plasma-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-plasma-wrap *,.tb-plasma-wrap *::before,.tb-plasma-wrap *::after{animation:none!important;transition:none!important}} `;