"use client"; /** * blackhole.tsx — a little gravitational catastrophe in a button. * Approach to bend space; click to let it swallow itself and come back. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, useState, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type ReactNode, type Ref, } from "react"; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; }; const WIDTH = 570; const HEIGHT = 320; const DURATION = 2400; const clamp = (n: number, low = 0, high = 1) => Math.max(low, Math.min(high, n)); const easeOut = (n: number) => 1 - Math.pow(1 - clamp(n), 3); // A small set of purposeful infalling traces, kept close to the event horizon. const infall = [ { angle: -2.55, radius: 216 }, { angle: -2.05, radius: 195 }, { angle: -1.47, radius: 204 }, { angle: -0.83, radius: 208 }, { angle: -0.35, radius: 212 }, { angle: 0.48, radius: 207 }, { angle: 1.14, radius: 194 }, { angle: 2.09, radius: 202 }, ]; export function BlackHoleButton({ children = "delete", onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const button = useRef(null); const wrap = useRef(null); const canvas = useRef(null); const uid = `tbh-${useId().replace(/:/g, "")}`; const start = useRef<() => void>(() => {}); const [playing, setPlaying] = useState(false); const plainLabel = typeof children === "string" || typeof children === "number" ? String(children) : null; const label = useRef(plainLabel || ""); label.current = plainLabel || ""; useImperativeHandle(ref, () => button.current!, []); useEffect(() => { if (disabled) setPlaying(false); const el = button.current; const host = wrap.current; const field = canvas.current; if (!el || !host || !field) return; const ctx = field.getContext("2d"); if (!ctx) return; let frame = 0; let lastFrame = 0; let began = 0; let visible = true; let hovering = false; let strength = 0; let target = 0; let pointerX = -10000; let pointerY = -10000; let rect: DOMRect | undefined; let dirty = true; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const allowed = () => !disabled && !el.matches(":disabled") && visible && !document.hidden && !media.matches; const setup = () => { // Account for the demo's CSS zoom as well as the device pixel ratio. const scale = el.getBoundingClientRect().width / (el.offsetWidth || 1) || 1; const dpr = Math.min((window.devicePixelRatio || 1) * scale, 3); field.width = Math.round(WIDTH * dpr); field.height = Math.round(HEIGHT * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); dirty = true; }; const clear = () => ctx.clearRect(0, 0, WIDTH, HEIGHT); const locate = () => { if (dirty || !rect) { rect = el.getBoundingClientRect(); dirty = false; } const scale = rect.width / (el.offsetWidth || 1) || 1; const x = (pointerX - rect.left - rect.width / 2) / scale; const y = (pointerY - rect.top - rect.height / 2) / scale; // The field strengthens as a hovering cursor approaches the centre. target = hovering ? 0.47 + 0.53 * clamp(1 - Math.hypot(x * 0.9, y * 1.2) / 150) : 0; }; const draw = (_now: number, progress: number) => { clear(); const elapsed = began ? progress * DURATION : 0; const collapsing = Boolean(began && elapsed < 800); const returning = Boolean(began && elapsed >= 1000); const snap = collapsing ? Math.pow(clamp((elapsed - 150) / 600), 2) : 0; const wave = returning ? clamp((elapsed - 1030) / 950) : 0; const rise = returning ? easeOut((elapsed - 1000) / 200) : 0; let force = strength; let alpha = strength * 0.92; if (collapsing) { const surge = easeOut(elapsed / 180); force = strength + (1 - strength) * surge; alpha = (alpha + (0.92 - alpha) * surge) * (1 - clamp((elapsed - 640) / 140)); } else if (began && !returning) { alpha = 0; } else if (returning) { force = strength * rise; alpha = rise * Math.max(strength * 0.92, 0.88 * (1 - wave)); } ctx.save(); ctx.translate(WIDTH / 2, HEIGHT / 2); if (alpha > 0.003) { ctx.strokeStyle = "#9797a5"; ctx.lineWidth = 1.05; ctx.globalAlpha = alpha; const bend = (x: number, y: number) => { const influence = Math.exp(-(x * x / 57600 + y * y / 22500)); const scale = (1 - 0.76 * force * influence) * (1 - 0.83 * snap); let px = x * scale; let py = y * scale; if (returning && wave < 1) { // The outward-moving crest displaces each grid point with the ring. const radius = Math.hypot(x, y * 1.65) || 1; const front = 15 + wave * 330; const crest = Math.exp(-Math.pow((radius - front) / 34, 2)); const push = 34 * crest * (1 - wave * 0.65) / radius; px += x * push; py += y * push; } return [px, py]; }; for (let x = -300; x <= 300; x += 30) { ctx.beginPath(); for (let y = -180; y <= 180; y += 6) { const [px, py] = bend(x, y); if (y === -180) ctx.moveTo(px, py); else ctx.lineTo(px, py); } ctx.stroke(); } for (let y = -180; y <= 180; y += 30) { ctx.beginPath(); for (let x = -300; x <= 300; x += 6) { const [px, py] = bend(x, y); if (x === -300) ctx.moveTo(px, py); else ctx.lineTo(px, py); } ctx.stroke(); } if (returning && wave < 1) { const radius = 15 + wave * 330; ctx.globalAlpha = Math.sin(Math.PI * wave) * 0.9; ctx.strokeStyle = "#dc8e49"; ctx.lineWidth = 2; ctx.shadowColor = "rgba(255,150,55,.65)"; ctx.shadowBlur = 10; ctx.beginPath(); ctx.ellipse(0, 0, radius, radius / 1.65, 0, 0, Math.PI * 2); ctx.stroke(); ctx.shadowBlur = 0; } // Fade the whole field to transparent at an elliptical boundary, never a box. ctx.save(); ctx.globalAlpha = 1; ctx.globalCompositeOperation = "destination-in"; const maskScale = 1 - 0.83 * snap; ctx.scale(maskScale, maskScale * HEIGHT / WIDTH); const mask = ctx.createRadialGradient(0, 0, 0, 0, 0, WIDTH / 2); mask.addColorStop(0, "rgba(0,0,0,1)"); mask.addColorStop(0.58, "rgba(0,0,0,1)"); mask.addColorStop(0.88, "rgba(0,0,0,.36)"); mask.addColorStop(1, "rgba(0,0,0,0)"); ctx.fillStyle = mask; ctx.fillRect(-WIDTH / maskScale, -WIDTH / maskScale, 2 * WIDTH / maskScale, 2 * WIDTH / maskScale); ctx.restore(); } if (collapsing && elapsed > 30 && elapsed < 740) { const t = clamp((elapsed - 30) / 710); const opacity = Math.min(1, elapsed / 120) * (1 - clamp((elapsed - 580) / 160)); const chars = Array.from(label.current.replace(/\s/g, "")); const position = (p: typeof infall[number], at: number) => { const a = p.angle + at * 1.85; const r = p.radius * (1 - Math.pow(at, 1.35)) + 2; return [Math.cos(a) * r, Math.sin(a) * r * 0.66]; }; infall.forEach((p, i) => { const head = position(p, t); const tail = position(p, Math.max(0, t - 0.24)); const mid = position(p, Math.max(0, t - 0.12)); const ink = ctx.createLinearGradient(head[0], head[1], tail[0], tail[1]); ink.addColorStop(0, i % 2 ? "#f68c3c" : "#5e4b46"); ink.addColorStop(1, "rgba(250,142,62,0)"); ctx.globalAlpha = opacity * 0.9; ctx.strokeStyle = ink; ctx.lineWidth = i % 2 ? 2 : 1.5; ctx.lineCap = "round"; ctx.beginPath(); ctx.moveTo(head[0], head[1]); ctx.quadraticCurveTo(mid[0], mid[1], tail[0], tail[1]); ctx.stroke(); if (i % 2 === 0 && chars.length) { ctx.save(); ctx.translate(head[0], head[1]); ctx.rotate(Math.atan2(head[1] - tail[1], head[0] - tail[0])); ctx.scale(1.5 + t * 0.8, 1 - t * 0.35); ctx.globalAlpha = opacity * 0.8; ctx.fillStyle = "#4d3e3c"; ctx.font = "600 17px system-ui, sans-serif"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText(chars[(i / 2) % chars.length], 0, 0); ctx.restore(); } }); } ctx.restore(); }; const tick = (now: number) => { frame = 0; if (!allowed()) return; const dt = lastFrame ? Math.min(48, now - lastFrame) : 16; lastFrame = now; strength += (target - strength) * (1 - Math.exp(-dt / 150)); const progress = began ? (now - began) / DURATION : 0; if (began && progress >= 1) { began = 0; setPlaying(false); } draw(now, progress); if (began || Math.abs(target - strength) > 0.002) frame = requestAnimationFrame(tick); else { strength = target; lastFrame = 0; if (target <= 0.002) { strength = 0; clear(); } } }; const wake = () => { if (!frame && allowed()) frame = requestAnimationFrame(tick); }; const pause = () => { host.dataset.tbhPaused = String(disabled || !visible || document.hidden); if (!allowed()) { cancelAnimationFrame(frame); frame = 0; lastFrame = 0; strength = target = 0; clear(); if (began) { began = 0; setPlaying(false); } } else { hovering = el.matches(":hover"); dirty = true; locate(); wake(); } }; const move = (event: PointerEvent) => { pointerX = event.clientX; pointerY = event.clientY; if (!allowed()) return; locate(); if (target > 0 || strength > 0) wake(); }; const enter = (event: PointerEvent) => { hovering = true; move(event); }; const exit = () => { hovering = false; target = 0; wake(); }; const leave = () => { hovering = false; pointerX = pointerY = -10000; target = 0; wake(); }; const invalidate = () => { dirty = true; if (allowed()) { locate(); wake(); } }; const resize = () => { setup(); invalidate(); }; const observer = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; pause(); }); const sizeObserver = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(invalidate); setup(); observer?.observe(host); sizeObserver?.observe(el); window.addEventListener("pointermove", move, { passive: true }); window.addEventListener("resize", resize); window.addEventListener("scroll", invalidate, true); el.addEventListener("pointerenter", enter); el.addEventListener("pointerleave", exit); document.documentElement.addEventListener("pointerleave", leave); document.addEventListener("visibilitychange", pause); media.addEventListener("change", pause); start.current = () => { if (!allowed() || began) return; began = performance.now(); setPlaying(true); wake(); }; pause(); return () => { start.current = () => {}; cancelAnimationFrame(frame); observer?.disconnect(); sizeObserver?.disconnect(); window.removeEventListener("pointermove", move); window.removeEventListener("resize", resize); window.removeEventListener("scroll", invalidate, true); el.removeEventListener("pointerenter", enter); el.removeEventListener("pointerleave", exit); document.documentElement.removeEventListener("pointerleave", leave); document.removeEventListener("visibilitychange", pause); media.removeEventListener("change", pause); }; }, [disabled]); const activate = (event: MouseEvent) => { if (disabled || event.currentTarget.matches(":disabled")) return; onClick?.(event); if (!event.defaultPrevented) start.current(); }; const characters = plainLabel === null ? null : Array.from(plainLabel); return ( ); } export default BlackHoleButton; const css = ` .tbh-wrap{display:inline-flex;position:relative;vertical-align:middle} .tbh-btn{position:relative;display:inline-flex;align-items:center;justify-content:center;box-sizing:border-box; width:300px;height:112px;padding:0;border:0;border-radius:999px;overflow:visible;background:transparent; color:#fff5e8;cursor:pointer;-webkit-tap-highlight-color:transparent; font:600 27px/1 ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;letter-spacing:-.035em} .tbh-btn:focus-visible{outline:3px solid #e87e33;outline-offset:6px} .tbh-btn:disabled{cursor:default} .tbh-field{position:absolute;left:50%;top:50%;width:570px;max-width:calc(100vw - 16px);height:320px;transform:translate(-50%,-50%);pointer-events:none} .tbh-shell{position:absolute;inset:0;border-radius:inherit;pointer-events:none; background:#080808;box-shadow:0 19px 24px -15px rgba(36,19,10,.43),0 6px 9px -5px rgba(0,0,0,.38), 0 12px 24px -12px rgba(239,100,20,.42),0 1px 2px rgba(0,0,0,.42),inset 0 0 0 1px #292723;transform-origin:50% 50%} .tbh-skin{position:absolute;inset:0;overflow:hidden;border-radius:inherit; background:radial-gradient(ellipse at 50% 56%,#21110b 0%,#100c0b 36%,transparent 68%), linear-gradient(180deg,#1c1d1c 0%,#101110 17%,#080808 52%,#0b0a09 88%,#171410 100%); box-shadow:inset 0 1px 0 rgba(255,255,255,.15),inset 0 0 0 2px #080808, inset 0 0 0 3px rgba(255,255,255,.055),inset 0 -7px 12px rgba(0,0,0,.65)} .tbh-core-glow{position:absolute;left:50%;top:54%;width:220px;height:90px;transform:translate(-50%,-50%); background:radial-gradient(ellipse,rgba(250,94,15,.19) 0%,rgba(140,37,4,.10) 37%,transparent 70%)} .tbh-orbit{position:absolute;left:50%;top:50%;width:220px;height:220px;transform:translate(-50%,-50%) rotate(-3deg) scaleY(.33)} .tbh-orbit-spin{position:absolute;inset:0;border-radius:50%;animation:tbh-orbit 7.5s linear infinite; background:conic-gradient(from 12deg,transparent 0deg,#9c2e09 19deg,#fa7620 39deg,#ffd18b 44deg,#c74a11 53deg, transparent 83deg,#9b2f0a 126deg,#f36a16 151deg,#ffae45 169deg,#8f2608 205deg,transparent 245deg,#df5515 302deg,transparent 360deg); -webkit-mask:radial-gradient(circle,transparent 0 29%,#000 36%,#000 43%,transparent 67%); mask:radial-gradient(circle,transparent 0 29%,#000 36%,#000 43%,transparent 67%);opacity:.83} .tbh-beam{position:absolute;left:18%;top:36%;width:37%;height:35%; background:radial-gradient(ellipse at 65% 60%,rgba(255,247,210,.66),rgba(255,163,65,.38) 22%,rgba(244,91,23,.16) 45%,transparent 72%); filter:blur(5px)} .tbh-arcs{position:absolute;left:50%;top:50%;width:260px;height:110px;transform:translate(-50%,-50%); filter:drop-shadow(0 0 2px rgba(255,100,24,.42))} .tbh-hotspot{position:absolute;left:35%;top:67%;width:74px;height:10px;transform:translate(-50%,-50%); background:radial-gradient(ellipse,#fffdf4 0%,#ffe6a5 12%,rgba(255,118,24,.72) 32%,transparent 72%); filter:blur(1px);animation:tbh-heat 3.75s ease-in-out infinite} .tbh-grain{position:absolute;inset:0;opacity:.37;background-image:radial-gradient(#e36c20 0.55px,transparent 1px), radial-gradient(#ffad56 0.5px,transparent 1px);background-size:37px 29px,53px 43px;background-position:9px 7px,22px 17px; mask-image:radial-gradient(ellipse at center,#000,transparent 72%)} .tbh-label{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;white-space:pre; text-shadow:0 2px 4px #000,0 0 10px rgba(0,0,0,.95);pointer-events:none} .tbh-letter{display:inline-block;transform-origin:center} .tbh-sr{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0} .tbh-singularity,.tbh-flash{position:absolute;left:50%;top:50%;border-radius:50%;pointer-events:none;opacity:0} .tbh-singularity{width:12px;height:12px;margin:-6px;z-index:1;background:radial-gradient(circle,#fff8df 0 12%,#ffb250 30%,#d75a18 48%,transparent 72%);box-shadow:0 0 10px rgba(255,112,20,.5)} .tbh-flash{width:160px;height:160px;margin:-80px;background:radial-gradient(circle,#fff 0%,rgba(255,255,255,.98) 13%,rgba(255,227,182,.75) 24%,rgba(255,135,37,.22) 44%,transparent 70%);z-index:2} .tbh-wrap[data-tbh-playing=true] .tbh-shell{animation:tbh-collapse 2400ms both} .tbh-wrap[data-tbh-playing=true] .tbh-letter{animation:tbh-letters 2400ms both} .tbh-wrap[data-tbh-playing=true] .tbh-singularity{animation:tbh-singularity 2400ms both} .tbh-wrap[data-tbh-playing=true] .tbh-flash{animation:tbh-flash 2400ms both} @keyframes tbh-orbit{to{transform:rotate(360deg)}} @keyframes tbh-heat{0%,100%{opacity:.7;transform:translate(-50%,-50%) scaleX(.94)}50%{opacity:1;transform:translate(-50%,-50%) scaleX(1.12)}} @keyframes tbh-collapse{ 0%{transform:scale(1);opacity:1;animation-timing-function:cubic-bezier(.2,.7,.3,1)} 7%{transform:scale(1.03,.95);opacity:1;animation-timing-function:cubic-bezier(.55,0,.85,.3)} 16%{transform:scale(.83,1.1) rotate(-3deg);opacity:1;animation-timing-function:cubic-bezier(.55,0,.9,.45)} 25%{transform:scale(.34,.75) rotate(-14deg);opacity:1;animation-timing-function:cubic-bezier(.6,0,.9,.3)} 32%{transform:scale(.008,.04) rotate(-28deg);opacity:0} 42%{transform:scale(.008);opacity:0;animation-timing-function:cubic-bezier(.15,.85,.2,1)} 50%{transform:scale(1.15,.82);opacity:1;animation-timing-function:ease-in-out} 59%{transform:scale(.92,1.08);opacity:1;animation-timing-function:ease-in-out} 68%{transform:scale(1.04,.97);opacity:1;animation-timing-function:ease-out} 79%,100%{transform:scale(1);opacity:1}} @keyframes tbh-letters{0%,7%{transform:none;opacity:1}20%{transform:translateX(var(--tbh-pull)) rotate(var(--tbh-turn)) scale(.36,1.6);opacity:1} 31%,43%{transform:translateX(var(--tbh-pull)) scale(.01,2);opacity:0}52%{transform:scale(1.1,.8);opacity:1}67%,100%{transform:none;opacity:1}} @keyframes tbh-singularity{0%,25%{transform:scale(.05);opacity:0}32%{transform:scale(.65);opacity:1}36%,41%{transform:scale(.42);opacity:1}45%,100%{transform:scale(.05);opacity:0}} @keyframes tbh-flash{0%,41%{transform:scale(.05);opacity:0}45%{transform:scale(.55);opacity:1}51%{transform:scale(1.35);opacity:.85}62%,100%{transform:scale(1.8);opacity:0}} .tbh-wrap[data-tbh-paused=true] *{animation-play-state:paused!important} .tbh-btn:disabled *{animation:none!important} @media(prefers-reduced-motion:reduce){.tbh-wrap *,.tbh-wrap *::before,.tbh-wrap *::after{animation:none!important;transition:none!important}} `;