"use client"; /** * ice.tsx — a clear block of ice that frosts, shatters and freezes again. * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, useState, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type PointerEvent, type ReactNode, type Ref, } from "react"; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; }; type Point = { x: number; y: number }; type Frost = Point & { id: number; turn: number; size: number }; type Phase = "idle" | "crack" | "burst" | "rebuild"; const W = 260; const H = 132; const random = (n: number) => { const v = Math.sin(n * 127.1 + 311.7) * 43758.5453; return v - Math.floor(v); }; // Clip each cell by the perpendicular bisector of every pair of sites. // The result covers the whole block, including the middle, without a large intact piece. function makePieces() { const sites: Point[] = []; for (let row = 0; row < 4; row++) for (let col = 0; col < 7; col++) { const i = row * 7 + col; sites.push({ x: (col + 0.2 + random(i + 3) * 0.6) * W / 7, y: (row + 0.18 + random(i + 61) * 0.64) * H / 4 }); } return sites.map((site, i) => { let poly: Point[] = [{ x: 0, y: 0 }, { x: W, y: 0 }, { x: W, y: H }, { x: 0, y: H }]; for (const other of sites) { if (other === site) continue; const nx = other.x - site.x, ny = other.y - site.y; const limit = (other.x ** 2 + other.y ** 2 - site.x ** 2 - site.y ** 2) / 2; const value = (p: Point) => p.x * nx + p.y * ny - limit; const next: Point[] = []; for (let k = 0; k < poly.length; k++) { const a = poly[k], b = poly[(k + 1) % poly.length]; const da = value(a), db = value(b); if (da <= 0) next.push(a); if ((da <= 0) !== (db <= 0)) { const t = da / (da - db); next.push({ x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t }); } } poly = next; if (!poly.length) break; } const d = poly.map((p, k) => `${k ? "L" : "M"}${p.x.toFixed(1)} ${p.y.toFixed(1)}`).join("") + "Z"; const a = poly[0], b = poly[Math.floor(poly.length / 2)]; const facet = a && b ? `M${a.x.toFixed(1)} ${a.y.toFixed(1)}L${site.x.toFixed(1)} ${site.y.toFixed(1)}L${b.x.toFixed(1)} ${b.y.toFixed(1)}` : ""; return { site, d, facet, i }; }); } const PIECES = makePieces(); // An asymmetric, branching crystal, rather than a six-point snowflake. function fern(length: number) { let d = `M0 0Q${length * .025} ${-length * .45} 0 ${-length}`; for (let i = 1; i <= 6; i++) { const y = -length * (i / 7), arm = length * (0.27 - i * 0.022); for (const side of [-1, 1]) { const x = side * arm, end = y - arm * .75; d += `M0 ${y}L${x} ${end}M${x * .57} ${y + (end - y) * .57}l${side * arm * .08} ${-arm * .34}`; } } return d; } const FERNS = [fern(29), fern(22), fern(18)]; function crackPaths(origin: Point) { return Array.from({ length: 9 }, (_, i) => { const angle = i * Math.PI * 2 / 9 + (random(i + 120) - 0.5) * 0.28; const dx = Math.cos(angle), dy = Math.sin(angle); const tx = dx > 0 ? (W - 3 - origin.x) / dx : (3 - origin.x) / dx; const ty = dy > 0 ? (H - 3 - origin.y) / dy : (3 - origin.y) / dy; const length = Math.max(1, Math.min(tx, ty)); const at = (t: number, jitter: number) => ({ x: origin.x + dx * length * t - dy * jitter, y: origin.y + dy * length * t + dx * jitter, }); const a = at(0.32, (random(i + 14) - 0.5) * 8); const b = at(0.65, (random(i + 34) - 0.5) * 10); const end = at(1, 0); const branch = at(0.78, (i % 2 ? 1 : -1) * 12); return { main: `M${origin.x} ${origin.y}L${a.x} ${a.y}L${b.x} ${b.y}L${end.x} ${end.y}`, branch: `M${b.x} ${b.y}L${branch.x} ${branch.y}` }; }); } const bubbles = [ [31, 37, 2.3], [48, 87, 1.2], [70, 23, 1.3], [83, 104, 1.8], [111, 24, 1.1], [151, 103, 1.5], [185, 23, 1.6], [207, 94, 1.2], [227, 39, 2.1], [233, 76, 1.1], [35, 68, .9], [211, 57, .9], ]; export function IceButton({ children = "freeze", onClick, onPointerEnter, onPointerMove, onPointerLeave, className, type = "button", disabled = false, ref, ...rest }: Props) { const button = useRef(null); useImperativeHandle(ref, () => button.current!, []); const uid = `tbi-${useId().replace(/:/g, "")}`; const [phase, setPhase] = useState("idle"); const [origin, setOrigin] = useState({ x: W / 2, y: H / 2 }); const [frost, setFrost] = useState([]); const busy = useRef(false); const timers = useRef(new Set()); const sequence = useRef(0); const lastFrost = useRef(null); const later = (fn: () => void, ms: number) => { const id = window.setTimeout(() => { timers.current.delete(id); fn(); }, ms); timers.current.add(id); }; useEffect(() => { const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const clear = () => { timers.current.forEach((id) => window.clearTimeout(id)); timers.current.clear(); busy.current = false; lastFrost.current = null; setFrost([]); setPhase("idle"); }; if (disabled) clear(); media.addEventListener("change", clear); return () => { timers.current.forEach((id) => window.clearTimeout(id)); timers.current.clear(); busy.current = false; media.removeEventListener("change", clear); }; }, [disabled]); const point = (e: { clientX: number; clientY: number; currentTarget: HTMLButtonElement }): Point => { const r = e.currentTarget.getBoundingClientRect(); return { x: Math.max(4, Math.min(W - 4, (e.clientX - r.left) / r.width * W)), y: Math.max(4, Math.min(H - 4, (e.clientY - r.top) / r.height * H)) }; }; const grow = (e: PointerEvent, force = false) => { if (disabled || busy.current || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; const p = point(e); if (!force && lastFrost.current && Math.hypot(p.x - lastFrost.current.x, p.y - lastFrost.current.y) < 22) return; lastFrost.current = p; const id = ++sequence.current; setFrost((old) => [...old.slice(-10), { ...p, id, turn: -35 + random(id + 5) * 70, size: 0.8 + random(id + 17) * 0.25 }]); later(() => setFrost((old) => old.filter((f) => f.id !== id)), 3000); }; const shatter = (e: MouseEvent) => { onClick?.(e); if (e.defaultPrevented || disabled || e.currentTarget.matches(":disabled") || busy.current) return; if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; busy.current = true; setOrigin(e.detail === 0 ? { x: W / 2, y: H / 2 } : point(e)); setPhase("crack"); later(() => { setFrost([]); setPhase("burst"); }, 360); later(() => setPhase("rebuild"), 2600); later(() => { setPhase("idle"); busy.current = false; lastFrost.current = null; }, 4150); }; return ( ); } export default IceButton; const css = ` .tbi-wrap{display:inline-block;line-height:0;vertical-align:middle} .tbi-btn{position:relative;display:inline-block;box-sizing:border-box;width:260px;height:132px;padding:0;border:0;outline:0;background:transparent;overflow:visible;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent} .tbi-btn:focus-visible{outline:3px solid #5389ab;outline-offset:7px;border-radius:23px} .tbi-btn:disabled{cursor:not-allowed;opacity:.58} .tbi-sr{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0} .tbi-shadow{position:absolute;left:6%;right:6%;bottom:-13px;height:25px;border-radius:50%;background:radial-gradient(ellipse,rgba(43,94,122,.27),rgba(94,145,169,.1) 48%,transparent 73%);filter:blur(7px);pointer-events:none} .tbi-caustic{position:absolute;left:0;bottom:-20px;width:100%;height:25px;overflow:visible;pointer-events:none;opacity:.24;filter:blur(.5px) drop-shadow(0 0 2px #b7eefa)} .tbi-caustic path{fill:none;stroke:#8dcce4;stroke-width:1;stroke-linecap:round} .tbi-mist{position:absolute;pointer-events:none;border-radius:50%;filter:blur(7px);opacity:.09;background:radial-gradient(ellipse,rgba(173,224,242,.65),rgba(229,249,255,.2) 52%,transparent 75%);transition:opacity .45s ease} .tbi-btn:hover .tbi-mist{opacity:.22}.tbi-mist-a{width:100px;height:26px;left:-20px;bottom:-3px}.tbi-mist-b{width:95px;height:23px;right:-18px;top:-8px} .tbi-block{position:absolute;inset:0;display:block;pointer-events:none;filter:drop-shadow(0 7px 5px rgba(82,139,166,.1))} .tbi-face,.tbi-cracks,.tbi-frost,.tbi-refrost,.tbi-shards{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none} .tbi-label{position:absolute;left:12px;right:12px;top:50%;transform:translateY(-54%);text-align:center;font:650 28px/1.1 ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;letter-spacing:.02em;color:#245d7f;text-shadow:-1px 0 1px rgba(72,201,231,.55),1.1px .3px 1px rgba(105,108,183,.32),0 1.5px 1px rgba(255,255,255,.95),1px 2px 3px rgba(36,101,139,.26);filter:blur(.15px);white-space:nowrap} .tbi-btn[data-phase="burst"] .tbi-block{visibility:hidden} .tbi-btn[data-phase="rebuild"] .tbi-block{mask-image:linear-gradient(to top,#000 0%,#000 45%,transparent 55%,transparent 100%);mask-size:100% 220%;mask-repeat:no-repeat;-webkit-mask-image:linear-gradient(to top,#000 0%,#000 45%,transparent 55%,transparent 100%);-webkit-mask-size:100% 220%;-webkit-mask-repeat:no-repeat;animation:tbi-refreeze 1.45s cubic-bezier(.45,0,.2,1) both} .tbi-cracks{overflow:hidden;border-radius:22px}.tbi-cracks path,.tbi-fern-shadow,.tbi-fern-light{fill:none;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:1;stroke-dashoffset:1;animation:tbi-draw .38s ease-out forwards} .tbi-crack-shadow{stroke:#397b9d;stroke-width:1.5;opacity:.8}.tbi-crack-light{stroke:#fff;stroke-width:.7;opacity:.95}.tbi-crack-branch{animation-duration:.18s} .tbi-frost{z-index:3;overflow:hidden;border-radius:20px;transition:opacity .26s ease}.tbi-btn[data-phase="crack"] .tbi-frost{opacity:0}.tbi-flake{animation:tbi-frost-fade 3s ease-out forwards}.tbi-fern-shadow{stroke:#83bad1;stroke-width:1.15;opacity:.8;animation-duration:.8s}.tbi-fern-light{stroke:#fff;stroke-width:.65;opacity:1;animation-duration:.8s} .tbi-shards{z-index:4}.tbi-shard,.tbi-glitter{transform-box:fill-box;transform-origin:center;animation:tbi-fly 1.72s cubic-bezier(.16,.5,.3,1) var(--tbi-delay) both}.tbi-glitter{animation-duration:1.45s} .tbi-tumble{transform-box:fill-box;transform-origin:center;animation:tbi-tumble var(--tbi-flip-duration) ease-in-out both}.tbi-catchlight{transform-box:fill-box;transform-origin:center;animation:tbi-catchlight 1.1s ease-in-out .15s both} .tbi-puddle{position:absolute;z-index:0;left:-45px;top:99px;width:350px;height:72px;overflow:visible;pointer-events:none;transform-origin:50% 50%;animation:tbi-water-in 1.75s ease-out both}.tbi-puddle-out{animation:tbi-water-out 1.45s ease-in both} .tbi-refrost{overflow:hidden;border-radius:20px;opacity:0;animation:tbi-refrost 1.45s ease-out both}.tbi-refrost path{fill:none;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:1;stroke-dashoffset:1;animation:tbi-draw 1s ease-out var(--tbi-delay) both}.tbi-refrost-shadow{stroke:#69b3d0;stroke-width:1.7;opacity:.85}.tbi-refrost-light{stroke:#fff;stroke-width:.85;opacity:.98} @keyframes tbi-draw{to{stroke-dashoffset:0}} @keyframes tbi-frost-fade{0%{opacity:.9}66%{opacity:.98}100%{opacity:0}} @keyframes tbi-fly{0%{transform:translate3d(0,0,0) rotate(0);opacity:1}38%{transform:translate3d(calc(var(--tbi-x)*.74),calc(var(--tbi-lift)*-1),0) rotate(calc(var(--tbi-spin)*.55));opacity:1}72%{transform:translate3d(var(--tbi-x),var(--tbi-fall),0) rotate(var(--tbi-spin));opacity:.95}100%{transform:translate3d(calc(var(--tbi-x)*1.03),calc(var(--tbi-fall) + 12px),0) rotate(var(--tbi-spin)) scale(.12,.06);opacity:0}} @keyframes tbi-tumble{0%{transform:scaleX(1)}30%{transform:scaleX(.12)}55%{transform:scaleX(-.95)}78%{transform:scaleX(-.15)}100%{transform:scaleX(.85)}} @keyframes tbi-catchlight{0%,30%,100%{opacity:.15;transform:scale(.6)}50%{opacity:1;transform:scale(1.15)}} @keyframes tbi-water-in{0%,28%{transform:scale(.22,.2);opacity:0}78%,100%{transform:scale(1);opacity:1}} @keyframes tbi-water-out{0%{transform:scale(1);opacity:1}80%{transform:scale(.7,.65);opacity:.6}100%{transform:scale(.4,.3);opacity:0}} @keyframes tbi-refreeze{0%{mask-position:0 0;-webkit-mask-position:0 0;opacity:.55}40%{opacity:.9}100%{mask-position:0 100%;-webkit-mask-position:0 100%;opacity:1}} @keyframes tbi-refrost{0%{opacity:0}20%{opacity:.9}65%{opacity:1}100%{opacity:0}} @media(prefers-reduced-motion:reduce){.tbi-mist{transition:none}.tbi-btn:active .tbi-block{opacity:.86}} `;