"use client"; /** * slime.tsx — a glossy little button made of very stretchy slime. * Hover to nudge it, press to make a dent, and pull away to draw out a strand. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type Ref } from "react"; type Props = ButtonHTMLAttributes & { ref?: Ref }; type Point = { x: number; y: number; x0: number; y0: number; nx: number; ny: number; vx: number; vy: number }; type Drop = { x: number; y: number; vx: number; vy: number; size: number; age: number }; type SpringPoint = { x: number; y: number; vx: number; vy: number }; const W = 260; const H = 132; const COUNT = 36; const SNAP_DISTANCE = 158; const clamp = (n: number, min: number, max: number) => Math.max(min, Math.min(max, n)); // A rounded rectangle sampled into independent spring points. function makePoints(): Point[] { return Array.from({ length: COUNT }, (_, i) => { const a = -Math.PI / 2 + (i * Math.PI * 2) / COUNT; const c = Math.cos(a), s = Math.sin(a); const x = 130 + 124 * Math.sign(c) * Math.pow(Math.abs(c), .52); const y = 66 + 59 * Math.sign(s) * Math.pow(Math.abs(s), .52); let nx = Math.sign(c) * Math.pow(Math.abs((x - 130) / 124), 2.85); let ny = Math.sign(s) * Math.pow(Math.abs((y - 66) / 59), 2.85); const length = Math.hypot(nx, ny) || 1; nx /= length; ny /= length; return { x, y, x0: x, y0: y, nx, ny, vx: 0, vy: 0 }; }); } function outline(points: Point[]) { const last = points[points.length - 1], first = points[0]; let d = `M${((last.x + first.x) / 2).toFixed(2)} ${((last.y + first.y) / 2).toFixed(2)}`; for (let i = 0; i < points.length; i++) { const p = points[i], next = points[(i + 1) % points.length]; d += `Q${p.x.toFixed(2)} ${p.y.toFixed(2)} ${((p.x + next.x) / 2).toFixed(2)} ${((p.y + next.y) / 2).toFixed(2)}`; } return `${d}Z`; } // A ribbon around a hanging, oscillating centreline. Both ends are plump, while // the narrow middle becomes a thread as the slime is pulled taut. function ribbon(ax: number, ay: number, bx: number, by: number, sag: number, wave: number, root: number, neck: number, tip: number) { const upper: [number, number][] = [], lower: [number, number][] = [], glint: [number, number][] = []; const dx = bx - ax, dy = by - ay; const steps = 20; for (let i = 0; i <= steps; i++) { const t = i / steps; const arch = Math.sin(Math.PI * t), oscillation = Math.sin(2 * Math.PI * t); const x = ax + dx * t; const y = ay + dy * t + sag * arch + wave * oscillation * arch; const tangentY = dy + Math.PI * sag * Math.cos(Math.PI * t) + wave * (2 * Math.PI * Math.cos(2 * Math.PI * t) * arch + Math.PI * oscillation * Math.cos(Math.PI * t)); const length = Math.hypot(dx, tangentY) || 1; const nx = -tangentY / length, ny = dx / length; const taper = 5 + clamp((3 - neck) / 3, 0, 1) * 3; const radius = Math.max(.35, neck + (root - neck) * Math.exp(-t * taper) + (tip - neck) * Math.exp(-(1 - t) * taper)); upper.push([x + nx * radius, y + ny * radius]); lower.push([x - nx * radius, y - ny * radius]); glint.push([x - nx * radius * .48, y - ny * radius * .48]); } const fmt = ([x, y]: [number, number]) => `${x.toFixed(2)} ${y.toFixed(2)}`; const rounded = (pts: [number, number][]) => { let d = `M${fmt(pts[0])}`; for (let i = 1; i < pts.length - 1; i++) { const p = pts[i], next = pts[i + 1]; d += `Q${fmt(p)} ${fmt([(p[0] + next[0]) / 2, (p[1] + next[1]) / 2])}`; } return `${d}L${fmt(pts[pts.length - 1])}`; }; const contour = [...upper, ...lower.reverse()]; const last = contour[contour.length - 1], first = contour[0]; let body = `M${fmt([(last[0] + first[0]) / 2, (last[1] + first[1]) / 2])}`; contour.forEach((p, i) => { const next = contour[(i + 1) % contour.length]; body += `Q${fmt(p)} ${fmt([(p[0] + next[0]) / 2, (p[1] + next[1]) / 2])}`; }); return { body: `${body}Z`, glint: rounded(glint) }; } const INITIAL_OUTLINE = outline(makePoints()); const BUBBLES: [number, number, number][] = [ [27, 37, 2.5], [38, 89, 1.9], [47, 24, 1.5], [51, 107, 3], [67, 31, 2.7], [75, 99, 1.3], [89, 20, 1.4], [95, 109, 2], [106, 29, 1.2], [114, 99, 1.6], [130, 20, 1.8], [143, 109, 2.7], [153, 27, 1.2], [169, 101, 1.5], [182, 24, 2.8], [197, 103, 2.5], [206, 33, 1.6], [220, 90, 2], [228, 48, 2.8], [233, 73, 1.2], [31, 65, 1.3], [58, 50, 1.2], [85, 43, 1], [165, 44, 1], [210, 67, 1.1], ]; export function SlimeButton({ children = "squish", className, type = "button", disabled = false, ref, ...rest }: Props) { const button = useRef(null); const shape = useRef(null); const dent = useRef(null); const hoverLight = useRef(null); const bubbleNodes = useRef<(SVGGElement | null)[]>([]); const strand = useRef(null); const rootGoo = useRef(null); const web = useRef(null); const filament1 = useRef(null); const bodyRecoil = useRef(null); const bodyRecoilWeb = useRef(null); const bodyRecoilGlint = useRef(null); const tipRecoil = useRef(null); const tipRecoilWeb = useRef(null); const tipRecoilGlint = useRef(null); const cursorGoo = useRef(null); const mergeRipple = useRef(null); const dropNodes = useRef<(SVGGElement | null)[]>([]); const bridgeNodes = useRef<(SVGPathElement | null)[]>([]); const label = useRef(null); const uid = `tbm${useId().replace(/:/g, "")}`; useImperativeHandle(ref, () => button.current!, []); useEffect(() => { const el = button.current; if (!el || !shape.current) return; const points = makePoints(); const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let raf = 0, timer = 0, visible = true, last = 0, rect: DOMRect; let px = -1000, py = -1000, near = false, pressed = false, keyPressed = false; let anchorX = 130, anchorY = 66, pressure = 0, pressureV = 0; let hover = 0, hoverV = 0, wave = 0, waveV = 0; let releasedAt = 0, tethered = false, drops: Drop[] = [], cursorDrop: Drop | null = null; let split: { body: SpringPoint; tip: SpringPoint; age: number } | null = null; let rippleAge = 1; const allowed = () => !disabled && !el.matches(":disabled") && visible && !document.hidden && !media.matches; const measure = () => { rect = el.getBoundingClientRect(); }; const location = (x: number, y: number) => { measure(); return { x: (x - rect.left) * W / (rect.width || W), y: (y - rect.top) * H / (rect.height || H) }; }; const start = () => { if (!raf && allowed()) { last = 0; raf = requestAnimationFrame(tick); } }; const clearStrand = () => { tethered = false; strand.current?.setAttribute("opacity", "0"); }; const snap = () => { if (!tethered) return; const dx = px - anchorX, dy = py - anchorY, distance = Math.hypot(dx, dy) || 1; const stretch = clamp((distance - 20) / (SNAP_DISTANCE - 20), 0, 1); const sag = Math.min(29, distance * .21) * (1 - .36 * stretch); const middle = (t: number) => ({ x: anchorX + dx * t, y: anchorY + dy * t + Math.sin(Math.PI * t) * sag + wave * Math.sin(2 * Math.PI * t) * Math.sin(Math.PI * t), }); const breakAt = middle(.52); split = { body: { ...breakAt, vx: -dx * .55, vy: -dy * .55 }, tip: { ...breakAt, vx: dx * .55, vy: dy * .55 }, age: 0, }; drops = [.39, .63].map((t, i) => ({ ...middle(t), vx: -dx * (.22 + i * .08), vy: -dy * .15 + 16, size: i ? 4.2 : 5.2, age: 0, })); cursorDrop = { x: px, y: py, vx: dx / distance * 5, vy: 0, size: 8, age: 0 }; points.forEach((p) => { const influence = Math.exp(-((p.x0 - anchorX) ** 2 + (p.y0 - anchorY) ** 2) / 7000); p.vx -= dx / distance * 115 * influence; p.vy -= dy / distance * 115 * influence; }); clearStrand(); }; function tick(now: number) { raf = 0; if (!allowed()) return; const dt = last ? Math.min((now - last) / 1000, .032) : 1 / 60; last = now; const elapsed = tethered ? now - releasedAt : 0; const distance = Math.hypot(px - anchorX, py - anchorY); if (tethered && (distance >= SNAP_DISTANCE || (elapsed > 2600 && distance > 28))) snap(); else if (tethered && elapsed > 2600) clearStrand(); const targetPressure = pressed ? 1 : 0; pressureV += ((targetPressure - pressure) * 118 - pressureV * 12) * dt; pressure = clamp(pressure + pressureV * dt, -.2, 1.15); const targetHover = near && !pressed && !tethered && !split ? 1 : 0; hoverV += ((targetHover - hover) * 95 - hoverV * 15) * dt; hover = clamp(hover + hoverV * dt, 0, 1.1); waveV += (-wave * 70 - waveV * 7) * dt; wave = clamp(wave + waveV * dt, -14, 14); let moving = Math.abs(pressure - targetPressure) > .002 || Math.abs(pressureV) > .02 || Math.abs(hover - targetHover) > .003 || Math.abs(hoverV) > .025; const stretch = tethered ? clamp(distance / SNAP_DISTANCE, 0, 1) : 0; const dirX = tethered ? (px - anchorX) / (distance || 1) : 0; const dirY = tethered ? (py - anchorY) / (distance || 1) : 0; for (const p of points) { const dx = px - p.x0, dy = py - p.y0, dist = Math.hypot(dx, dy) || 1; const influence = near && !pressed && !tethered ? Math.exp(-(dist * dist) / 7000) : 0; const pull = tethered ? Math.exp(-((p.x0 - anchorX) ** 2 + (p.y0 - anchorY) ** 2) / 7000) * stretch * 13 : 0; const hx = (p.nx * 15 + dx / dist * 2) * influence + dirX * pull; const hy = (p.ny * 15 + dy / dist * 2) * influence + dirY * pull; const targetX = p.x0 + hx + (p.x0 - 130) * pressure * .045; const targetY = p.y0 + hy - (p.y0 - 66) * pressure * .13 + pressure * 2.5; p.vx += ((targetX - p.x) * 105 - p.vx * 13) * dt; p.vy += ((targetY - p.y) * 105 - p.vy * 13) * dt; p.x += p.vx * dt; p.y += p.vy * dt; if (Math.abs(p.x - targetX) + Math.abs(p.y - targetY) > .04 || Math.abs(p.vx) + Math.abs(p.vy) > .12) moving = true; } shape.current?.setAttribute("d", outline(points)); if (hoverLight.current) { hoverLight.current.setAttribute("transform", `translate(${clamp(px, 18, 242).toFixed(1)} ${clamp(py, 17, 115).toFixed(1)})`); hoverLight.current.setAttribute("opacity", String(clamp(hover, 0, 1))); } if (dent.current) { dent.current.setAttribute("transform", `translate(${anchorX.toFixed(1)} ${anchorY.toFixed(1)}) scale(${(.54 + Math.max(0, pressure) * .62).toFixed(3)})`); dent.current.setAttribute("opacity", String(clamp(pressure * 1.6, 0, 1))); } bubbleNodes.current.forEach((node, i) => { if (!node) return; const [x, y] = BUBBLES[i]; const dx = x - anchorX, dy = y - anchorY, dist = Math.hypot(dx, dy) || 1; const displacement = Math.max(0, pressure) * 11 * Math.exp(-(dist * dist) / 2600); const scale = 1 + Math.max(0, pressure) * .4 * Math.exp(-(dist * dist) / 2300); node.setAttribute("transform", `translate(${(x + dx / dist * displacement).toFixed(2)} ${(y + dy / dist * displacement).toFixed(2)}) scale(${scale.toFixed(3)}) translate(${-x} ${-y})`); }); if (label.current) { const squash = clamp(pressure, -.16, 1.1); const skew = clamp((anchorX - 130) / 130, -1, 1) * squash * 5; label.current.style.transform = `translate(${((130 - anchorX) * squash * .04).toFixed(2)}px,${(squash * 4).toFixed(2)}px) skewX(${skew.toFixed(2)}deg) scale(${(1 + squash * .09).toFixed(3)},${(1 - squash * .15).toFixed(3)})`; } if (tethered && distance > 7 && strand.current && web.current && filament1.current) { const tension = clamp((distance - 20) / (SNAP_DISTANCE - 20), 0, 1); const sag = Math.min(29, distance * .21) * (1 - .36 * tension); const neck = .65 + 5 * (1 - tension) ** 2; const shape = ribbon(anchorX, anchorY, px, py, sag, wave, 12, neck, 8); strand.current.setAttribute("opacity", String(clamp((distance - 7) / 12, 0, 1))); web.current.setAttribute("d", shape.body); filament1.current.setAttribute("d", shape.glint); rootGoo.current?.setAttribute("transform", `translate(${anchorX.toFixed(1)} ${anchorY.toFixed(1)})`); } else strand.current?.setAttribute("opacity", "0"); if (split) { split.age += dt; const spring = (p: SpringPoint, tx: number, ty: number) => { p.vx += ((tx - p.x) * 93 - p.vx * 10) * dt; p.vy += ((ty - p.y) * 93 - p.vy * 10) * dt; p.x += p.vx * dt; p.y += p.vy * dt; }; spring(split.body, anchorX, anchorY); spring(split.tip, px, py); const body = ribbon(anchorX, anchorY, split.body.x, split.body.y, 5, wave * .3, 13, 1.3, .5); const tip = ribbon(px, py, split.tip.x, split.tip.y, 4, wave * .25, 8, 1, .5); bodyRecoilWeb.current?.setAttribute("d", body.body); bodyRecoilGlint.current?.setAttribute("d", body.glint); tipRecoilWeb.current?.setAttribute("d", tip.body); tipRecoilGlint.current?.setAttribute("d", tip.glint); const opacity = clamp((.68 - split.age) * 4, 0, 1); bodyRecoil.current?.setAttribute("opacity", String(opacity)); tipRecoil.current?.setAttribute("opacity", String(opacity)); if (split.age >= .68) split = null; } else { bodyRecoil.current?.setAttribute("opacity", "0"); tipRecoil.current?.setAttribute("opacity", "0"); } if (cursorDrop) { cursorDrop.age += dt; if (cursorDrop.age < .2) { cursorDrop.x = px; cursorDrop.y = py; } else { cursorDrop.vx += ((anchorX + 22 - cursorDrop.x) * 4 - cursorDrop.vx * 1.2) * dt; cursorDrop.vy += 280 * dt; cursorDrop.x += cursorDrop.vx * dt; cursorDrop.y += cursorDrop.vy * dt; } const falling = clamp((cursorDrop.age - .18) * 3, 0, 1); cursorGoo.current?.setAttribute("transform", `translate(${cursorDrop.x.toFixed(1)} ${cursorDrop.y.toFixed(1)}) scale(${(1 - falling * .18).toFixed(2)} ${(1 + falling * .35).toFixed(2)})`); const landing = cursorDrop.x < W - 8 ? clamp((22 - cursorDrop.y) / 10, 0, 1) : 1; cursorGoo.current?.setAttribute("opacity", String(Math.min(landing, clamp((1.5 - cursorDrop.age) * 4, 0, 1)))); if (landing <= 0 || cursorDrop.age >= 1.5) cursorDrop = null; } else if (tethered && distance > 7) { cursorGoo.current?.setAttribute("transform", `translate(${px.toFixed(1)} ${py.toFixed(1)})`); cursorGoo.current?.setAttribute("opacity", String(clamp((distance - 7) / 12, 0, 1))); } else cursorGoo.current?.setAttribute("opacity", "0"); drops = drops.filter((drop) => { drop.age += dt; drop.vx += ((anchorX - drop.x) * 34 - drop.vx * 7) * dt; drop.vy += ((anchorY - drop.y) * 34 - drop.vy * 7 + 55) * dt; drop.x += drop.vx * dt; drop.y += drop.vy * dt; if (Math.hypot(anchorX - drop.x, anchorY - drop.y) < 9 || drop.age > 1.35) { rippleAge = 0; points.forEach((p) => { const influence = Math.exp(-((p.x0 - anchorX) ** 2 + (p.y0 - anchorY) ** 2) / 5000); p.vx += p.nx * 28 * influence; p.vy += p.ny * 28 * influence; }); moving = true; return false; } return true; }); dropNodes.current.forEach((node, i) => { const drop = drops[i], bridge = bridgeNodes.current[i]; if (!node || !drop) { node?.setAttribute("opacity", "0"); bridge?.setAttribute("opacity", "0"); return; } const d = Math.hypot(anchorX - drop.x, anchorY - drop.y); node.setAttribute("transform", `translate(${drop.x.toFixed(1)} ${drop.y.toFixed(1)}) scale(${(drop.size / 4).toFixed(2)})`); node.setAttribute("opacity", String(clamp(d / 10, 0, 1))); if (bridge && d < 33) { bridge.setAttribute("d", ribbon(anchorX, anchorY, drop.x, drop.y, 2, 0, 7, 2, drop.size).body); bridge.setAttribute("opacity", String(clamp((33 - d) / 12, 0, .9))); } else bridge?.setAttribute("opacity", "0"); }); if (rippleAge < .55) { rippleAge += dt; mergeRipple.current?.setAttribute("transform", `translate(${anchorX.toFixed(1)} ${anchorY.toFixed(1)}) scale(${(.55 + rippleAge * 1.5).toFixed(2)})`); mergeRipple.current?.setAttribute("opacity", String(clamp((.55 - rippleAge) * 2.1, 0, .8))); } else mergeRipple.current?.setAttribute("opacity", "0"); if (moving || tethered || split || cursorDrop || drops.length || rippleAge < .55) raf = requestAnimationFrame(tick); else if (!near && !pressed) { // Settle to the exact idle outline so repeated clips loop without a seam. points.forEach((p) => { p.x = p.x0; p.y = p.y0; p.vx = 0; p.vy = 0; }); shape.current?.setAttribute("d", INITIAL_OUTLINE); bubbleNodes.current.forEach((node) => node?.removeAttribute("transform")); if (label.current) label.current.style.transform = ""; pressure = 0; pressureV = 0; hover = 0; hoverV = 0; } } const scheduleBreath = () => { window.clearTimeout(timer); if (!allowed()) return; timer = window.setTimeout(() => { if (!pressed && !tethered && !split) { points.forEach((p) => { p.vx += p.nx * 18; p.vy += p.ny * 20; }); start(); } scheduleBreath(); }, 4200); }; const move = (e: PointerEvent) => { const p = location(e.clientX, e.clientY); if (tethered) waveV += clamp((p.y - py) * 1.2, -7, 7); px = p.x; py = p.y; const wasNear = near; near = px > -65 && px < W + 65 && py > -65 && py < H + 65; if (near || wasNear || tethered || pressed || split || cursorDrop) start(); }; const down = (e: PointerEvent) => { if (!allowed() || e.button !== 0) return; const p = location(e.clientX, e.clientY); px = p.x; py = p.y; near = true; anchorX = clamp(px, 28, W - 28); anchorY = clamp(py, 26, H - 26); clearStrand(); drops = []; split = null; cursorDrop = null; rippleAge = 1; pressed = true; start(); }; const up = (e: PointerEvent) => { if (!pressed || keyPressed) return; const p = location(e.clientX, e.clientY); px = p.x; py = p.y; pressed = false; tethered = true; releasedAt = performance.now(); wave = 0; waveV = 0; start(); }; const cancel = () => { pressed = false; keyPressed = false; clearStrand(); start(); }; const keyDown = (e: KeyboardEvent) => { if (!allowed() || e.repeat || (e.key !== " " && e.key !== "Enter")) return; pressed = true; keyPressed = true; anchorX = 130; anchorY = 66; clearStrand(); start(); }; const keyUp = (e: KeyboardEvent) => { if (!keyPressed || (e.key !== " " && e.key !== "Enter")) return; pressed = false; keyPressed = false; start(); }; const reset = () => { cancelAnimationFrame(raf); raf = 0; window.clearTimeout(timer); pressed = false; keyPressed = false; near = false; pressure = 0; pressureV = 0; hover = 0; hoverV = 0; wave = 0; waveV = 0; drops = []; split = null; cursorDrop = null; rippleAge = 1; clearStrand(); points.forEach((p) => { p.x = p.x0; p.y = p.y0; p.vx = 0; p.vy = 0; }); shape.current?.setAttribute("d", INITIAL_OUTLINE); [dent.current, hoverLight.current, bodyRecoil.current, tipRecoil.current, cursorGoo.current, mergeRipple.current, ...dropNodes.current, ...bridgeNodes.current].forEach((node) => node?.setAttribute("opacity", "0")); bubbleNodes.current.forEach((node) => node?.removeAttribute("transform")); if (label.current) label.current.style.transform = ""; }; const pause = () => { if (!allowed()) reset(); else { measure(); scheduleBreath(); } }; const resize = () => { measure(); }; const observer = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; pause(); }); observer?.observe(el); el.addEventListener("pointerdown", down); el.addEventListener("keydown", keyDown); el.addEventListener("keyup", keyUp); el.addEventListener("blur", cancel); window.addEventListener("pointermove", move, { passive: true }); window.addEventListener("pointerup", up); window.addEventListener("pointercancel", cancel); window.addEventListener("resize", resize); document.addEventListener("visibilitychange", pause); media.addEventListener("change", pause); measure(); scheduleBreath(); return () => { reset(); observer?.disconnect(); el.removeEventListener("pointerdown", down); el.removeEventListener("keydown", keyDown); el.removeEventListener("keyup", keyUp); el.removeEventListener("blur", cancel); window.removeEventListener("pointermove", move); window.removeEventListener("pointerup", up); window.removeEventListener("pointercancel", cancel); window.removeEventListener("resize", resize); document.removeEventListener("visibilitychange", pause); media.removeEventListener("change", pause); }; }, [disabled]); return ( ); } export default SlimeButton; const css = ` .tbm-btn{position:relative;display:inline-flex;align-items:center;justify-content:center;width:260px;height:132px; padding:0;border:0;background:transparent;overflow:visible;cursor:pointer;isolation:isolate; -webkit-tap-highlight-color:transparent;touch-action:manipulation;vertical-align:middle} .tbm-btn:focus-visible{outline:3px solid #548a05;outline-offset:5px;border-radius:55px} .tbm-btn:disabled{cursor:default} .tbm-art{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none} .tbm-label{position:relative;pointer-events:none;color:#e2ff9b;font:900 50px/1 "Arial Rounded MT Bold","Trebuchet MS",ui-rounded,system-ui,sans-serif; letter-spacing:-.055em;-webkit-text-stroke:1px rgba(240,255,194,.9); text-shadow:0 -1px 0 #f4ffd1,0 2px 0 #a5df3a,0 4px 1px rgba(81,153,0,.65),0 5px 5px rgba(61,125,0,.22)} `;