"use client"; /** * domino.tsx — a pill-shaped row of standing dominoes. * Hover and the first tile leans in; click and the whole row topples in a chain, holds, then stands back up right to left. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useImperativeHandle, useRef, type ButtonHTMLAttributes, type MouseEvent, type ReactNode, type Ref } from "react"; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; /** number of tiles in the row (default 14) */ count?: number; }; const W = 260; const H = 92; const PL = 60; const PT = 30; const CW = W + 2 * PL; const CH = H + PT + 40; const TH = 60; // tile height const TW = 13; // edge-on tile width const TEND = 24; // end tiles show their face const GAP = 4; const FLOOR = 80; const DW = 3; // receding side wall drawn into the gap const T_REWIND = 1300; const T_SHEEN = 2000; const T_END = 2300; const DEG = Math.PI / 180; const mulberry = (seed: number) => () => { seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; const bezier = (x1: number, y1: number, x2: number, y2: number) => (x: number) => { if (x <= 0) return 0; if (x >= 1) return 1; let t = x; for (let i = 0; i < 8; i++) { const bx = 3 * (1 - t) * (1 - t) * t * x1 + 3 * (1 - t) * t * t * x2 + t * t * t - x; const dx = 3 * (1 - t) * (1 - t) * x1 + 6 * (1 - t) * t * (x2 - x1) + 3 * t * t * (1 - x2); if (Math.abs(dx) < 1e-6) break; t -= bx / dx; } return 3 * (1 - t) * (1 - t) * t * y1 + 3 * (1 - t) * t * t * y2 + t * t * t; }; const rise = bezier(0.3, 1.4, 0.5, 1); const PIPS: number[][] = [[], [4], [0, 8], [0, 4, 8], [0, 2, 6, 8], [0, 2, 4, 6, 8], [0, 2, 3, 5, 6, 8]]; type Tile = { w: number; xl: number; xr: number; lean: number; th: number; om: number; st: 0 | 1 | 2 | 3; phi: number; bounces: number; pips: [number, number]; jolt: number; jt: number; }; const contact = (tiles: Tile[], i: number, p: number, iters = 20) => { const b = tiles[i + 1], ax = b.xr - b.w * Math.cos(p), ay = b.w * Math.sin(p); const nx = -Math.cos(p), ny = Math.sin(p); let lo = 0, hi = Math.PI / 2; for (let k = 0; k < iters; k++) { const m = (lo + hi) / 2; if ((tiles[i].xr + TH * Math.sin(m) - ax) * nx + (TH * Math.cos(m) - ay) * ny > 0) lo = m; else hi = m; } return lo; }; const buildTiles = (n: number): Tile[] => { const r = mulberry(3); const widths = Array.from({ length: n }, (_, i) => (i === 0 || i === n - 1 ? TEND : TW)); const total = widths.reduce((a, b) => a + b, 0) + GAP * (n - 1); let x = (W - total) / 2; const tiles: Tile[] = widths.map((w, i) => { const xl = x; x += w + GAP; return { w, xl, xr: xl + w, lean: (r() - 0.5) * 0.8 * DEG, th: 0, om: 0, st: 0, bounces: 0, jolt: 0, jt: 0, phi: 90 * DEG, pips: [Math.floor(r() * 7), Math.floor(r() * 7)] as [number, number], }; }).map((t) => ({ ...t, th: t.lean })); tiles[n - 1].phi = 90 * DEG; tiles[0].pips = [5, 2]; for (let i = n - 2; i >= 0; i--) { tiles[i].phi = contact(tiles, i, tiles[i + 1].phi, 30); } return tiles; }; export function DominoButton({ children = "go", count = 14, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); const wrapRef = useRef(null); const canvasRef = useRef(null); const play = useRef<() => void>(() => {}); useImperativeHandle(ref, () => btnRef.current!, []); const n = Math.max(4, Math.min(20, Math.round(count))); useEffect(() => { const btn = btnRef.current; const wrap = wrapRef.current; const canvas = canvasRef.current; if (!btn || !wrap || !canvas) return; const ctx = canvas.getContext("2d"); if (!ctx) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const tiles = buildTiles(n); let epoch = 0; let raf = 0; let last = 0; let visible = true; let hovering = false; let rect: DOMRect | null = null; let curU = 0.5; let simT = 0; let t0 = 0; let phase: "idle" | "fall" | "rewind" = "idle"; let ha = 0; let hw = 0; let sprites: HTMLCanvasElement[] = []; let rsprites: HTMLCanvasElement[] = []; const timers = new Set(); 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 allowed = () => !disabled && !btn.matches(":disabled") && visible && !document.hidden && !media.matches; const rr = (g: CanvasRenderingContext2D, x: number, y: number, w: number, h: number, c: [number, number, number, number]) => { g.beginPath(); g.moveTo(x + c[0], y); g.lineTo(x + w - c[1], y); g.arcTo(x + w, y, x + w, y + c[1], c[1]); g.lineTo(x + w, y + h - c[2]); g.arcTo(x + w, y + h, x + w - c[2], y + h, c[2]); g.lineTo(x + c[3], y + h); g.arcTo(x, y + h, x, y + h - c[3], c[3]); g.lineTo(x, y + c[0]); g.arcTo(x, y, x + c[0], y, c[0]); g.closePath(); }; const buildSprites = (dpr: number) => { const capH = 5; const make = (bare: boolean) => tiles.map((t, i) => { const deep = i < n - 1; const c = document.createElement("canvas"); c.width = Math.ceil((t.w + 2 + (deep ? DW : 0)) * dpr); c.height = Math.ceil((TH + 2) * dpr); const g = c.getContext("2d"); if (!g) return c; g.scale(dpr, dpr); g.translate(1, 1); const end = i === 0 ? 0 : i === n - 1 ? 1 : -1; const cr: [number, number, number, number] = end === 0 ? [9, 2.5, 2.5, 9] : end === 1 ? [2.5, 9, 9, 2.5] : [3.5, 3.5, 3, 3]; // body: front face + side face, lit top cap const mid = end === -1; rr(g, 0, 0, t.w, TH, cr); if (mid) { g.save(); rr(g, 0, 0, t.w, TH, cr); g.clip(); const fg = g.createLinearGradient(0, 0, 0, TH); fg.addColorStop(0, "#f1ebe3"); fg.addColorStop(0.55, "#e5dcd2"); fg.addColorStop(1, "#d6cbbf"); g.fillStyle = fg; g.fillRect(0, 0, 9.5, TH); const sg = g.createLinearGradient(0, 0, 0, TH); sg.addColorStop(0, "#b6aa9d"); sg.addColorStop(1, "#8f8376"); g.fillStyle = sg; g.fillRect(9.5, 0, t.w - 9.5, TH); g.fillStyle = "rgba(255,250,242,.55)"; g.fillRect(9.2, 0, 0.6, TH); g.fillStyle = "rgba(70,56,42,.35)"; g.fillRect(9.8, 0, 0.5, TH); g.restore(); } else { const gr = g.createLinearGradient(0, 0, t.w, 0); if (end === 1) { gr.addColorStop(0, "#f4efe8"); gr.addColorStop(0.12, "#e8e0d6"); gr.addColorStop(0.82, "#e0d6ca"); gr.addColorStop(0.93, "#b6aa9d"); gr.addColorStop(1, "#84786c"); } else { gr.addColorStop(0, "#84786c"); gr.addColorStop(0.07, "#b6aa9d"); gr.addColorStop(0.18, "#e0d6ca"); gr.addColorStop(0.88, "#e8e0d6"); gr.addColorStop(1, "#f4efe8"); } g.fillStyle = gr; rr(g, 0, 0, t.w, TH, cr); g.fill(); } g.save(); rr(g, 0, 0, t.w, TH, cr); g.clip(); const cap = g.createLinearGradient(0, 0, 0, capH); cap.addColorStop(0, "#fffdf9"); cap.addColorStop(0.6, "#f3ede5"); cap.addColorStop(1, "#e4dbd0"); g.fillStyle = cap; g.fillRect(0, 0, t.w, capH); const turn = g.createLinearGradient(0, capH, 0, capH + 2.5); turn.addColorStop(0, "rgba(90,74,58,.30)"); turn.addColorStop(1, "rgba(90,74,58,0)"); g.fillStyle = turn; g.fillRect(0, capH, t.w, 2.5); const top = g.createLinearGradient(0, 0, 0, TH); top.addColorStop(0, "rgba(255,255,255,0)"); top.addColorStop(0.82, "rgba(60,48,36,0)"); top.addColorStop(1, "rgba(60,48,36,.38)"); if (!bare) { g.fillStyle = top; g.fillRect(0, 0, t.w, TH); } if (!bare) { const gr2 = mulberry(i + 40); for (let k = 0; k < 45; k++) { const sz = 0.5 + gr2() * 0.7; g.fillStyle = gr2() < 0.7 ? `rgba(120,96,70,${0.10 + gr2() * 0.12})` : "rgba(255,255,255,.22)"; g.beginPath(); g.arc(gr2() * t.w, gr2() * TH, sz / 2, 0, Math.PI * 2); g.fill(); } g.fillStyle = "rgba(150,128,104,.06)"; for (let k = 0; k < 3; k++) g.fillRect(1 + gr2() * (mid ? 8 : t.w - 2), 4, 0.5, TH - 8); const fl = mulberry(i + 90), nF = mid ? 9 : 22, fw = mid ? 9.5 : t.w; for (let k = 0; k < nF; k++) { const fx = 0.8 + fl() * (fw - 1.6), fy = capH + 1 + fl() * (TH - capH - 3), fr = 0.3 + fl() * 0.35, fa = 0.14 + fl() * 0.18; if (Math.abs(fy - TH / 2) < 1.2) continue; g.fillStyle = `rgba(72,60,48,${fa})`; g.beginPath(); g.arc(fx, fy, fr, 0, Math.PI * 2); g.fill(); } } if (mid) { g.fillStyle = "rgba(255,255,255,.18)"; g.fillRect(1.0, capH + 1, 2.6, TH - capH - 6); g.fillStyle = "rgba(255,255,255,.5)"; g.fillRect(1.6, capH + 2, 0.9, TH - capH - 10); const sf = g.createLinearGradient(9.5, 0, 13, 0); sf.addColorStop(0, "rgba(40,30,20,0)"); sf.addColorStop(1, "rgba(40,30,20,.25)"); g.fillStyle = sf; g.fillRect(9.5, 0, t.w - 9.5, TH); } if (!bare && (mid || end === 1)) { const cv = g.createLinearGradient(0, 0, 2.4, 0); cv.addColorStop(0, "rgba(50,38,26,.26)"); cv.addColorStop(1, "rgba(50,38,26,0)"); g.fillStyle = cv; g.fillRect(0, capH, 2.4, TH - capH); } g.restore(); if (deep) { g.beginPath(); g.moveTo(t.w - 0.3, 3.5); g.lineTo(t.w + DW, 5.5); g.lineTo(t.w + DW, TH - 0.5); g.lineTo(t.w - 0.3, TH); g.closePath(); const wv = g.createLinearGradient(0, 0, 0, TH); wv.addColorStop(0, "#7a6e62"); wv.addColorStop(0.5, "#5a5047"); wv.addColorStop(1, "#3b332c"); g.fillStyle = wv; g.fill(); const wh = g.createLinearGradient(t.w, 0, t.w + DW, 0); wh.addColorStop(0, "rgba(20,14,8,0)"); wh.addColorStop(1, "rgba(20,14,8,.45)"); g.fillStyle = wh; g.fill(); } if (!bare) { // engraved divider const dx1 = 1.5, dx2 = mid ? 9.5 : t.w - 1.5; g.fillStyle = "#5c5247"; g.fillRect(dx1, TH / 2 - 0.5, dx2 - dx1, 1); g.fillStyle = "rgba(255,250,242,.6)"; g.fillRect(dx1, TH / 2 + 0.5, dx2 - dx1, 1); const rad = t.w > 20 ? 2.3 : 1.65; t.pips.forEach((count2, half) => { const y0 = half ? TH / 2 + 3 : 5; const hh = TH / 2 - (half ? 6 : 8); for (const idx of PIPS[count2]) { const px = mid ? 9.5 * (0.27 + 0.23 * (idx % 3)) : t.w * (0.28 + 0.22 * (idx % 3)); const py = y0 + hh * (0.17 + 0.33 * Math.floor(idx / 3)); const pg = g.createRadialGradient(px - 0.35 * rad, py - 0.35 * rad, 0, px - 0.35 * rad, py - 0.35 * rad, rad); pg.addColorStop(0, "#000"); pg.addColorStop(1, "#2b241e"); g.fillStyle = pg; g.beginPath(); g.arc(px, py, rad, 0, Math.PI * 2); g.fill(); g.strokeStyle = "rgba(255,252,246,.75)"; g.lineWidth = 0.6; g.beginPath(); g.arc(px, py, rad + 0.35, 0.15 * Math.PI, 0.85 * Math.PI); g.stroke(); } }); } if (!bare) { g.strokeStyle = "rgba(60,48,36,.45)"; g.lineWidth = 0.75; rr(g, 0.375, 0.375, t.w - 0.75, TH - 0.75, cr); g.stroke(); } return c; }); sprites = make(false); rsprites = make(true); }; const setup = () => { const r = btn.getBoundingClientRect(); const scale = r.width / (btn.offsetWidth || 1) || 1; const dpr = Math.min(2, (window.devicePixelRatio || 1) * scale); canvas.width = Math.round(CW * dpr); canvas.height = Math.round(CH * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); buildSprites(dpr); rect = null; draw(); }; const angle = (i: number) => { const t = tiles[i]; if (t.st !== 0) return t.th; if (i === 0) return t.lean + ha; if (i === 1 && hovering && phase === "idle") return t.lean + Math.sin(simT * 0.06) * 1.2 * DEG * Math.min(1, ha * 8 + 0.2); return t.lean; }; const drawTile = (i: number, mirror: boolean) => { const t = tiles[i]; const spr = mirror ? rsprites[i] : sprites[i]; if (!spr) return; ctx.save(); const jx = t.jolt; if (mirror) { ctx.translate(0, 2 * FLOOR + (mirror ? 1 : 0)); ctx.scale(1, -1); } const a = angle(i); if (a < 0) { ctx.translate(t.xl + jx, FLOOR); ctx.rotate(a); ctx.drawImage(spr, -1, -TH - 1, t.w + 2 + (i < n - 1 ? DW : 0), TH + 2); } else { ctx.translate(t.xr + jx, FLOOR); ctx.rotate(a); ctx.drawImage(spr, -t.w - 1, -TH - 1, t.w + 2 + (i < n - 1 ? DW : 0), TH + 2); } ctx.restore(); }; let sheen = -1; const dust = Array.from({ length: 10 }, () => ({ x: 0, y: 0, vx: 0, vy: 0, age: 999, r: 1 })); let shakeT = -1e9; function draw() { ctx.clearRect(0, 0, CW, CH); ctx.save(); ctx.translate(PL, PT); const s = simT - shakeT; if (s >= 0 && s < 160) ctx.translate(0, 1.1 * Math.exp(-s / 45) * Math.cos(s * 0.12)); // floor reflection, faded out with a mask ctx.save(); ctx.beginPath(); ctx.rect(-PL, FLOOR, CW, 16); ctx.clip(); ctx.globalAlpha = 0.2; for (let i = 0; i < n; i++) drawTile(i, true); ctx.globalAlpha = 1; ctx.globalCompositeOperation = "destination-out"; const fade = ctx.createLinearGradient(0, FLOOR, 0, FLOOR + 16); fade.addColorStop(0, "rgba(0,0,0,0)"); fade.addColorStop(0.35, "rgba(0,0,0,.6)"); fade.addColorStop(1, "rgba(0,0,0,1)"); ctx.fillStyle = fade; ctx.fillRect(-PL, FLOOR, CW, 16); ctx.restore(); // contact shadows ctx.save(); ctx.translate(W / 2, FLOOR + 1); ctx.scale(1, 0.06); const pool = ctx.createRadialGradient(0, 0, 0, 0, 0, 150); pool.addColorStop(0, "rgba(0,0,0,.5)"); pool.addColorStop(0.6, "rgba(0,0,0,.22)"); pool.addColorStop(1, "rgba(0,0,0,0)"); ctx.fillStyle = pool; ctx.fillRect(-150, -150, 300, 300); ctx.restore(); for (let i = 0; i < n; i++) { const t = tiles[i]; let x0 = t.xl + 0.5, x1 = t.xr - 0.5, al = 0.55; if (t.st === 1 || t.st === 2) { const th = Math.min(t.th, Math.PI / 2); x0 = t.xr - 2; x1 = t.xr + TH * Math.sin(th) + 2; al = 0.35 * Math.sin(th); } if (x1 <= x0 || al <= 0) continue; const sg = ctx.createLinearGradient(0, FLOOR - 1.5, 0, FLOOR + 2.5); sg.addColorStop(0, `rgba(0,0,0,${al})`); sg.addColorStop(1, "rgba(0,0,0,0)"); ctx.fillStyle = sg; ctx.fillRect(x0, FLOOR - 1.5, x1 - x0, 4); } if (s >= 0 && s < 320) { ctx.save(); ctx.translate(tiles[n - 1].xr + 30, FLOOR - 2); ctx.scale(1, 0.45); const pr = 6 + 14 * (s / 320); const pg = ctx.createRadialGradient(0, 0, 0, 0, 0, pr); pg.addColorStop(0, `rgba(170,155,138,${0.16 * (1 - s / 320)})`); pg.addColorStop(1, "rgba(170,155,138,0)"); ctx.fillStyle = pg; ctx.fillRect(-pr, -pr, 2 * pr, 2 * pr); ctx.restore(); } for (let i = 0; i < n; i++) drawTile(i, false); ctx.save(); ctx.beginPath(); ctx.rect(-PL, -PT, CW, FLOOR + PT); ctx.clip(); ctx.globalCompositeOperation = "source-atop"; ctx.translate(W / 2 + 4, FLOOR - 3); ctx.scale(1, 0.22); const ao = ctx.createRadialGradient(0, 0, 0, 0, 0, 80); ao.addColorStop(0, "rgba(10,7,4,.6)"); ao.addColorStop(0.55, "rgba(10,7,4,.42)"); ao.addColorStop(1, "rgba(10,7,4,0)"); ctx.fillStyle = ao; ctx.fillRect(-80, -80, 160, 160); ctx.restore(); for (const p of dust) if (p.age < 420) { ctx.fillStyle = `rgba(150,135,118,${0.6 * (1 - p.age / 420)})`; ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.fill(); } if (sheen >= 0 && sheen <= 1) { ctx.save(); ctx.globalCompositeOperation = "source-atop"; const cx = -30 + sheen * (W + 60); const g = ctx.createLinearGradient(cx - 22, 0, cx + 22, 0); g.addColorStop(0, "rgba(255,255,255,0)"); g.addColorStop(0.5, "rgba(255,255,255,.85)"); g.addColorStop(1, "rgba(255,255,255,0)"); ctx.fillStyle = g; ctx.fillRect(0, FLOOR - TH - 1, W, 7); ctx.restore(); } ctx.restore(); } const startFall = (i: number, om: number) => { const t = tiles[i]; t.st = 1; t.th = t.lean; t.om = om; }; const settle = (i: number) => { const t = tiles[i]; if (i === n - 1 && t.bounces === 0) { shakeT = simT; for (const p of dust) { const r = Math.random(); p.x = t.xr + 6 + r * 48; p.y = FLOOR - 1; p.vx = (r - 0.5) * 40; p.vy = -(25 + r * 35); p.r = 0.7 + r * 0.8; p.age = 0; } } t.th = t.phi; if (i === n - 1 && t.bounces < 2 && t.om > 1.2) { t.om = -t.om * 0.3; t.bounces++; } else { t.st = 2; t.om = 0; } }; const stepSim = (dt: number) => { simT += dt * 1000; const el = simT - t0; const h = 0.004; let left = dt; while (left > 1e-6) { const d = Math.min(h, left); left -= d; if (phase === "fall") { for (let i = 0; i < n; i++) { const t = tiles[i]; if (t.st !== 1) continue; t.om += 28 * Math.sin(t.th) * d; t.th += t.om * d; if (i < n - 1) { const b = tiles[i + 1]; const lim = contact(tiles, i, Math.max(0, b.th)); if (t.th >= lim) { t.th = lim; if (b.st === 0) { startFall(i + 1, Math.max(2, 0.3 * t.om)); b.jolt = 1.2; b.jt = simT; } else t.om = Math.min(t.om, Math.max(0, b.om)); if (b.st === 2 && t.th >= t.phi - 0.002) { t.th = t.phi; t.st = 2; t.om = 0; } } } else if (t.th >= t.phi) settle(i); } } else if (phase === "idle") { hw += (120 * ((hovering && !disabled ? (curU < 0.25 ? -15 : -9) * DEG : 0) - ha) - 9 * hw) * d; ha += hw * d; } } for (const p of dust) if (p.age < 420) { p.age += dt * 1000; p.vy += 120 * dt; p.x += p.vx * dt; p.y += p.vy * dt; if (p.y > FLOOR - 0.5) { p.y = FLOOR - 0.5; p.vy = 0; p.vx *= 0.9; } } for (const t of tiles) if (t.jolt !== 0) { const k = (simT - t.jt) / 70; t.jolt = k >= 1 ? 0 : 1.2 * (1 - k); } if (phase === "fall" && el >= T_REWIND) { phase = "rewind"; wrap.dataset.tbState = "rewind"; for (const t of tiles) { t.st = 3; t.th = t.th; t.om = t.th; } } if (phase === "rewind") { let done = true; for (let i = 0; i < n; i++) { const t = tiles[i]; if (t.st !== 3) continue; const e = (el - (T_REWIND + (n - 1 - i) * 35)) / 180; if (e >= 1) { t.th = t.lean; t.st = 0; t.om = 0; t.bounces = 0; } else { done = false; if (e > 0) { const k = rise(e); t.th = t.om * (1 - k) + t.lean * k; } } } sheen = el >= T_SHEEN ? (el - T_SHEEN) / 250 : -1; if (done && el >= T_END) { phase = "idle"; sheen = -1; wrap.dataset.tbState = "idle"; ha = 0; hw = 0; for (const p of dust) p.age = 999; shakeT = -1e9; } } }; const needLoop = () => phase !== "idle" || hovering || Math.abs(ha) > 1e-3 || Math.abs(hw) > 1e-3; const tick = (now: number) => { raf = 0; if (!allowed()) { last = 0; return; } const dt = last ? Math.min(0.048, (now - last) / 1000) : 0.016; last = now; stepSim(dt); draw(); if (needLoop()) raf = requestAnimationFrame(tick); else { last = 0; ha = hw = 0; draw(); } }; const wake = () => { if (!raf && allowed()) raf = requestAnimationFrame(tick); }; const locate = (e: PointerEvent) => { if (!rect) rect = btn.getBoundingClientRect(); curU = (e.clientX - rect.left) / (rect.width || 1); }; const enter = (e: PointerEvent) => { if (disabled || media.matches) return; hovering = true; rect = btn.getBoundingClientRect(); locate(e); if (phase === "idle") hw -= 2.6; wake(); }; const move = (e: PointerEvent) => { if (hovering) locate(e); }; const leave = () => { hovering = false; wake(); }; const invalidate = () => { rect = null; }; play.current = () => { if (disabled) return; if (media.matches) { if (phase !== "idle") return; phase = "fall"; tiles.forEach((t) => { t.th = t.phi; t.st = 2; }); wrap.dataset.tbState = "playing"; draw(); later(() => { tiles.forEach((t) => { t.th = t.lean; t.st = 0; }); phase = "idle"; wrap.dataset.tbState = "idle"; draw(); }, 900); return; } if (!allowed() || phase !== "idle") return; phase = "fall"; t0 = simT; wrap.dataset.tbState = "playing"; const t = tiles[0]; t.th = t.lean + ha; ha = 0; hw = 0; startFall(0, 9); wake(); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; wrap.dataset.tbPaused = String(!visible); if (visible) { last = 0; wake(); } }); const mq = () => { if (media.matches) { cancelAnimationFrame(raf); raf = 0; hovering = false; phase = "idle"; ha = hw = 0; sheen = -1; for (const p of dust) p.age = 999; shakeT = -1e9; tiles.forEach((t) => { t.th = t.lean; t.st = 0; }); draw(); } }; setup(); io?.observe(wrap); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointermove", move, { passive: true }); btn.addEventListener("pointerleave", leave); window.addEventListener("resize", setup); window.addEventListener("scroll", invalidate, true); media.addEventListener("change", mq); return () => { epoch++; play.current = () => {}; cancelAnimationFrame(raf); timers.forEach((id) => window.clearTimeout(id)); timers.clear(); io?.disconnect(); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointermove", move); btn.removeEventListener("pointerleave", leave); window.removeEventListener("resize", setup); window.removeEventListener("scroll", invalidate, true); media.removeEventListener("change", mq); }; }, [disabled, n]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; play.current(); }; return ( ); } export default DominoButton; const css = ` .tb-domino-wrap{display:inline-flex;position:relative;vertical-align:middle} .tb-domino-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;border:0;border-radius:46px;background:transparent; overflow:visible;color:#e9e6e1;cursor:pointer;-webkit-tap-highlight-color:transparent; font:700 24px/1 ui-rounded,"Nunito","Segoe UI",system-ui,sans-serif} .tb-domino-btn:focus-visible{outline:3px solid #f0c987;outline-offset:6px} .tb-domino-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-domino-canvas{position:absolute;left:-${PL}px;top:-${PT}px;width:${CW}px;height:${CH}px;pointer-events:none} .tb-domino-plate{position:absolute;left:${(W - 120) / 2}px;top:29px;display:flex;align-items:center;justify-content:center;width:120px;height:46px;box-sizing:border-box; border-radius:23px;background:radial-gradient(90% 120% at 28% -10%,rgba(255,255,255,.10),rgba(255,255,255,0) 60%),linear-gradient(180deg,#2d2e30 0%,#1c1d1e 48%,#121314 100%); box-shadow:inset 0 1px 0 rgba(255,255,255,.2),inset 0 0 0 1.5px rgba(255,255,255,.045),inset 0 -2px 3px rgba(0,0,0,.5),0 0 0 1px rgba(0,0,0,.7),0 2px 3px rgba(0,0,0,.5),0 9px 16px -5px rgba(0,0,0,.6); transition:transform 220ms cubic-bezier(.3,1.6,.5,1),box-shadow 220ms ease-out;pointer-events:none} .tb-domino-label{text-transform:uppercase;white-space:nowrap;padding-left:.14em;color:#efebe5;font-weight:800;letter-spacing:.14em;transition:color 160ms ease-out;text-shadow:0 1px 0 rgba(0,0,0,.75),0 -1px 0 rgba(255,255,255,.05);transform:translateY(1px)} .tb-domino-btn:hover:not(:disabled) .tb-domino-plate{transform:translateY(-1.5px); box-shadow:inset 0 1px 0 rgba(255,255,255,.2),inset 0 0 0 1.5px rgba(255,255,255,.045),inset 0 -2px 3px rgba(0,0,0,.5), 0 0 0 1px rgba(0,0,0,.7),0 3px 4px rgba(0,0,0,.5),0 13px 20px -6px rgba(0,0,0,.65)} .tb-domino-btn:hover:not(:disabled) .tb-domino-label{color:#fff} .tb-domino-btn:active:not(:disabled) .tb-domino-plate{transform:translateY(1.5px) scale(.975);transition-duration:70ms;box-shadow:inset 0 1px 0 rgba(255,255,255,.1),inset 0 2px 5px rgba(0,0,0,.6),0 0 0 1px rgba(0,0,0,.6),0 1px 2px rgba(0,0,0,.5)} .tb-domino-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-domino-wrap *,.tb-domino-wrap *::before,.tb-domino-wrap *::after{animation:none!important;transition:none!important}} `;