"use client"; /** * match.tsx — a match on a wooden striker block. * Hover and the match scratches along the strip; click and it strikes, the flame catches and the label glows like embers, then a fresh match slides in. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type MouseEvent, type ReactNode, type Ref } from "react"; type Palette = { wood: string; grain: string; striker: string; head: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; }; const W = 270; const H = 86; const PL = 60; // overflow layer: sides, top, bottom const PT = 170; const PB = 30; const CW = W + PL * 2; const CH = H + PT + PB; const DEFAULT_PALETTE: Palette = { wood: "#c9935a", grain: "#9a6634", striker: "#5a2c1a", head: "#b5232a" }; const STICK = 150; const HEAD_REST = 160; const HEAD_HOVER = 200; const HEAD_STRIKE = 245; const HEAD_Y = 10; const BURN_END = 3000; const OUT_AT = 2600; const TOTAL = 4000; const LABEL_CX = W / 2; const LABEL_CY = 57; const clamp = (n: number, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, n)); const smooth = (t: number) => { const c = clamp(t); return c * c * (3 - 2 * c); }; const easeIn = (t: number) => { const c = clamp(t); return c * c * c; }; const easeOut = (t: number) => 1 - Math.pow(1 - clamp(t), 3); const rnd = (a: number, b: number) => a + Math.random() * (b - a); const hex = (c: string): [number, number, number] => [parseInt(c.slice(1, 3), 16), parseInt(c.slice(3, 5), 16), parseInt(c.slice(5, 7), 16)]; const mix = (a: string, b: string, t: number) => { const x = hex(a), y = hex(b); return `rgb(${Math.round(x[0] + (y[0] - x[0]) * t)},${Math.round(x[1] + (y[1] - x[1]) * t)},${Math.round(x[2] + (y[2] - x[2]) * t)})`; }; // static wood grain and striker grit, generated once const GRAIN = Array.from({ length: 10 }, (_, i) => { const y = 32 + i * 5.4 + (i % 3) * 1.3; let d = `M6 ${y.toFixed(1)}`; for (let x = 6; x <= W - 6; x += 18) d += `L${x + 18} ${(y + Math.sin(x * 0.045 + i * 1.7) * 2.2).toFixed(1)}`; return d; }); const GRIT = (() => { const dots: { x: number; y: number; light: boolean }[] = []; let s = 7; const r = () => { s = (s * 16807) % 2147483647; return s / 2147483647; }; for (let i = 0; i < 60; i++) dots.push({ x: 24 + r() * 222, y: 10 + r() * 10, light: r() > 0.5 }); return dots; })(); type Spark = { x: number; y: number; vx: number; vy: number; g: number; age: number; life: number; size: number; color: string; kind: 0 | 1 | 2 }; const CHAR_EDGE = Array.from({ length: 28 }, (_, i) => ({ a: (i / 28) * Math.PI * 2, j: 0.74 + (((i * 37) % 11) / 11) * 0.26, s: 0.7 + (((i * 53) % 7) / 7) * 0.9, ph: i * 1.93 })); type Flame = { x: number; y: number; vy: number; age: number; life: number; seed: number; blue: boolean; x0: number; y0: number }; type Puff = { x: number; y: number; age: number; life: number; r0: number; r1: number; dx: number }; const SPRITE_COLORS: [number, number, number][] = [[255, 246, 208], [255, 201, 74], [255, 122, 26], [160, 40, 0], [90, 150, 255]]; const makeSprites = () => SPRITE_COLORS.map(([r, g, b]) => { const c = document.createElement("canvas"); c.width = c.height = 48; const x = c.getContext("2d"); if (x) { const g2 = x.createRadialGradient(24, 24, 0, 24, 24, 24); g2.addColorStop(0, `rgba(${r},${g},${b},1)`); g2.addColorStop(0.5, `rgba(${r},${g},${b},.7)`); g2.addColorStop(1, `rgba(${r},${g},${b},0)`); x.fillStyle = g2; x.fillRect(0, 0, 48, 48); } return c; }); export function MatchButton({ children = "light", 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 labelRef = useRef(null); const play = useRef<() => void>(() => {}); const pal = useRef(palette); pal.current = palette; const uid = `tb-match-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const plain = typeof children === "string" || typeof children === "number" ? String(children) : null; const letters = plain === null ? null : Array.from(plain); useImperativeHandle(ref, () => btnRef.current!, []); useEffect(() => { const el = btnRef.current; const host = wrapRef.current; const cv = canvasRef.current; const label = labelRef.current; if (!el || !host || !cv || !label) return; const ctx = cv.getContext("2d"); if (!ctx) return; const spans = Array.from(label.querySelectorAll("[data-l]")); const sprites = makeSprites(); let raf = 0; let last = 0; let visible = true; let hovering = false; let hv = 0; // hover progress 0..1 let hoverT = 0; let sparked = false; let began = 0; let lastSpawn = 0; let lastPuff = 0; let lastEmber = 0; let flashAt = -1e9; let fromX = HEAD_REST; let ptrX = -1; let haloX = LABEL_CX; let rect: DOMRect | undefined; let dirty = true; let epoch = 0; const timers = new Set(); const sparks: Spark[] = []; const flames: Flame[] = []; const puffs: Puff[] = []; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const allowed = () => !disabled && visible && !document.hidden && !media.matches; 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; }; // ---- drawing helpers ---- const drawMatch = (hx: number, ang: number, char: number, alpha: number) => { if (alpha <= 0.01) return; const p = pal.current; ctx.save(); ctx.globalAlpha = alpha; // cast shadow on the strip, in block space ctx.fillStyle = "rgba(0,0,0,.45)"; ctx.beginPath(); ctx.ellipse(hx - 6, HEAD_Y + 6, 15, 3.5, 0, 0, Math.PI * 2); ctx.fill(); const sg = ctx.createLinearGradient(hx - 10, 0, hx - 95, 0); sg.addColorStop(0, "rgba(0,0,0,.28)"); sg.addColorStop(1, "rgba(0,0,0,0)"); ctx.fillStyle = sg; ctx.beginPath(); ctx.moveTo(hx - 10, HEAD_Y + 4); ctx.lineTo(hx - 95, HEAD_Y + 12); ctx.lineTo(hx - 95, HEAD_Y + 17); ctx.lineTo(hx - 10, HEAD_Y + 9); ctx.closePath(); ctx.fill(); ctx.translate(hx, HEAD_Y); ctx.rotate((ang * Math.PI) / 180); const g = ctx.createLinearGradient(0, -4.5, 0, 4.5); g.addColorStop(0, "#f6deb2"); g.addColorStop(0.45, "#e9c48c"); g.addColorStop(1, "#b98a50"); ctx.fillStyle = g; ctx.beginPath(); ctx.roundRect(-STICK, -4.5, STICK - 6, 9, 1.5); ctx.fill(); ctx.fillStyle = "#d4a970"; ctx.fillRect(-STICK, -4.5, 3, 9); ctx.lineWidth = 0.8; ctx.strokeStyle = "rgba(255,245,225,.7)"; ctx.beginPath(); ctx.moveTo(-STICK + 4, -3); ctx.lineTo(-14, -3); ctx.stroke(); ctx.strokeStyle = "rgba(120,80,40,.6)"; ctx.beginPath(); ctx.moveTo(-STICK + 4, 3.8); ctx.lineTo(-14, 3.8); ctx.stroke(); if (char > 0.05) { const cg = ctx.createLinearGradient(-30, 0, 0, 0); cg.addColorStop(0, "rgba(40,22,12,0)"); cg.addColorStop(1, `rgba(40,22,12,${0.85 * char})`); ctx.fillStyle = cg; ctx.fillRect(-30, -4.5, 26, 9); } // head ctx.fillStyle = mix("#5e0f12", "#0e0806", char); ctx.beginPath(); ctx.ellipse(0, 2, 13, 9.5, 0, 0, Math.PI * 2); ctx.fill(); const hg = ctx.createRadialGradient(-4, -4, 0, -4, -4, 15); hg.addColorStop(0, mix("#e65252", "#5a4030", char)); hg.addColorStop(0.45, mix(p.head, "#2e1d14", char)); hg.addColorStop(1, mix("#6e1015", "#140c08", char)); ctx.fillStyle = hg; ctx.beginPath(); ctx.ellipse(0, 0, 13, 9.5, 0, 0, Math.PI * 2); ctx.fill(); ctx.fillStyle = "rgba(60,8,10,.45)"; for (const [sx, sy] of [[3, -2], [6, 2], [-1, 4], [8, -3], [-6, 2], [2, 5], [-3, -6], [9, 3]]) { ctx.beginPath(); ctx.arc(sx, sy, 0.6, 0, Math.PI * 2); ctx.fill(); } ctx.fillStyle = `rgba(255,215,205,${0.45 * (1 - char)})`; ctx.beginPath(); ctx.ellipse(-4, -4.5, 3.5, 1.8, -0.4, 0, Math.PI * 2); ctx.fill(); ctx.restore(); }; const sprite = (i: number, x: number, y: number, r: number, a: number) => { if (a <= 0.01 || r <= 0.2) return; ctx.globalAlpha = Math.min(1, a); ctx.drawImage(sprites[i], x - r, y - r, r * 2, r * 2); }; const headNow = (t: number) => { if (!began) return HEAD_REST + (HEAD_HOVER - HEAD_REST) * smooth(hv); if (t < 90) return fromX + (HEAD_STRIKE - fromX) * easeIn(t / 90); return HEAD_STRIKE; }; const setLabel = (e: number, now: number, t: number) => { if (e <= 0.003) { label.style.removeProperty("text-shadow"); spans.forEach((s) => { s.style.removeProperty("color"); s.style.removeProperty("text-shadow"); }); label.style.removeProperty("color"); return; } spans.forEach((s, i) => { const fl = 0.85 + 0.15 * Math.sin(now * 0.004 + i * 2.3) * Math.sin(now * 0.0017 + i * 1.1); const n = spans.length; const ei = began && t < OUT_AT ? easeOut((t - 200 - (n - 1 - i) * 70) / 500) : e; const k = clamp(ei * fl); s.style.color = k < 0.5 ? mix("#45240c", "#b8400e", k * 2) : mix("#b8400e", "#ffcf6e", (k - 0.5) * 2); s.style.textShadow = `0 0 1.5px rgba(20,8,2,${0.9 * k}),0 1px 0 rgba(20,8,2,${0.6 * k}),0 0 6px rgba(255,140,40,${0.9 * k}),0 0 14px rgba(255,90,0,${0.55 * k}),0 1px 0 rgba(255,228,184,${0.6 * (1 - k)}),0 -1px 0 rgba(30,12,2,${0.45 * (1 - k)}),0 0 1px rgba(30,12,2,${0.3 * (1 - k)})`; }); }; const frame = (now: number, dt: number) => { const t = began ? now - began : 0; // ---- hover tween ---- const target = hovering && !began ? 1 : 0; const prev = hv; hv = clamp(hv + (target ? dt / 380 : -dt / 300) * (target || hv > 0 ? 1 : 0) * (target ? 1 : 1)); if (target && hv < 1 && hv > prev) { // grit kicked off the head while it scratches const n = (hv - prev) * 18; const cnt = Math.floor(n + Math.random()); const hx = HEAD_REST + (HEAD_HOVER - HEAD_REST) * smooth(hv); for (let i = 0; i < cnt; i++) sparks.push({ x: hx, y: HEAD_Y + 4, vx: -rnd(80, 160), vy: -rnd(20, 90), g: 900, age: 0, life: 400, size: rnd(1, 2), color: "#7a3e24", kind: 0 }); } if (hovering && !began) { hoverT += dt; if (!sparked && hoverT > 300) { sparked = true; const hx = HEAD_REST + (HEAD_HOVER - HEAD_REST) * smooth(hv); for (let i = 0; i < 3; i++) sparks.push({ x: hx, y: HEAD_Y + 3, vx: -rnd(40, 120), vy: -rnd(30, 100), g: 900, age: 0, life: 150, size: 1.6, color: "#ffcf6b", kind: 1 }); } } else { hoverT = 0; sparked = false; } // ---- click timeline ---- let ember = 0; let haloK = 0; let haloA = 0; let kFlame = 0; let char = 0; if (began) { if (t < OUT_AT + 400 + 1) kFlame = t < 90 ? 0 : t < OUT_AT ? 1 : clamp(1 - (t - OUT_AT) / 400); char = clamp((t - 90) / 350); ember = t < 200 ? 0 : t < OUT_AT ? easeOut((t - 200) / 500) : Math.exp(-(t - OUT_AT) / 500) * (t < 4000 ? 1 : 0); haloK = easeOut((t - 200) / 1000); haloA = t < OUT_AT ? 1 : clamp(1 - (t - OUT_AT) / 1400); if (t >= 90 && flashAt < began) { flashAt = now; for (let i = 0; i < 22; i++) { const an = (rnd(-160, -20) * Math.PI) / 180, sp = rnd(120, 300); sparks.push({ x: HEAD_STRIKE, y: HEAD_Y - 2, vx: Math.cos(an) * sp, vy: Math.sin(an) * sp, g: 420, age: 0, life: rnd(350, 700), size: rnd(1, 1.8), color: "#ffd27a", kind: 1 }); } } if (t > 200 && t < 2400 && now - lastEmber > 120) { lastEmber = now; let nEm = 0; for (const s of sparks) if (s.kind === 2) nEm++; if (nEm < 12) sparks.push({ x: HEAD_STRIKE + rnd(-6, 6), y: HEAD_Y - rnd(10, 40), vx: rnd(-30, 30), vy: rnd(-120, -60), g: -60, age: 0, life: rnd(900, 1100), size: 1.2, color: "#ff8a2a", kind: 2 }); } const hx = HEAD_STRIKE; // flame particles if (kFlame > 0.02 && now - lastSpawn > 25 && flames.length < 70) { lastSpawn = now; const life = rnd(380, 520); flames.push({ x: hx + rnd(-2.5, 2.5), y: HEAD_Y - 6, x0: hx, y0: HEAD_Y - 6, vy: -rnd(90, 140), age: 0, life, seed: Math.random() * 100, blue: Math.random() < 0.34 }); } if (t > 1200 && t < OUT_AT && now - lastPuff > 110) { lastPuff = now; puffs.push({ x: hx + rnd(-3, 3), y: HEAD_Y - 62, age: 0, life: 1600, r0: 6, r1: 24, dx: rnd(-6, 6) }); } if (t >= TOTAL) { began = 0; host.dataset.tbState = "idle"; fromX = HEAD_REST; } } // ---- integrate particles ---- const h = dt / 1000; for (let i = sparks.length - 1; i >= 0; i--) { const s = sparks[i]; s.age += dt; if (s.age >= s.life) { sparks.splice(i, 1); continue; } s.vy += s.g * h; s.x += s.vx * h; s.y += s.vy * h; } for (let i = flames.length - 1; i >= 0; i--) { const f = flames[i]; f.age += dt; if (f.age >= f.life) { flames.splice(i, 1); continue; } f.y += f.vy * h; } for (let i = puffs.length - 1; i >= 0; i--) { const p = puffs[i]; p.age += dt; if (p.age >= p.life) { puffs.splice(i, 1); continue; } p.y -= 50 * h; p.x += p.dx * h * 0.6; } // ---- paint ---- ctx.clearRect(0, 0, CW, CH); ctx.save(); ctx.translate(PL, PT); const headX = headNow(t); const lit = began && t >= 90 && t < BURN_END ? kFlame : 0; if (lit > 0.02) { // block top face warms under the flame ctx.save(); ctx.beginPath(); ctx.roundRect(0, 0, W, H, 38); ctx.clip(); ctx.globalCompositeOperation = "lighter"; ctx.translate(headX, 15); ctx.scale(1, 0.4); const g = ctx.createRadialGradient(0, 0, 0, 0, 0, 70); g.addColorStop(0, `rgba(255,140,40,${0.4 * lit})`); g.addColorStop(1, "rgba(255,140,40,0)"); ctx.fillStyle = g; ctx.fillRect(-70, -70, 140, 140); ctx.restore(); } if (began && haloK > 0.01 && haloA > 0.01) { // charred patch crawls from under the head into the label haloX = headX - 10; const cx = headX - 14 - 81 * haloK; const cy = 24 + 30 * haloK; const rx = 14 + 56 * haloK, ry = 9 + 15 * haloK; const ef = 0.85 + 0.15 * Math.sin(now * 0.004); const pts = CHAR_EDGE.map((p) => [cx + Math.cos(p.a) * rx * p.j, cy + Math.sin(p.a) * ry * p.j]); ctx.save(); ctx.beginPath(); ctx.roundRect(0, 0, W, H, 38); ctx.clip(); ctx.save(); ctx.translate(cx, cy); ctx.scale(rx / 40, ry / 40); const g = ctx.createRadialGradient(0, 0, 0, 0, 0, 40); g.addColorStop(0, `rgba(16,7,2,${0.88 * haloA})`); g.addColorStop(0.6, `rgba(28,12,4,${0.78 * haloA})`); g.addColorStop(0.85, `rgba(50,22,6,${0.5 * haloA})`); g.addColorStop(1, `rgba(70,35,12,${0.15 * haloA})`); ctx.beginPath(); pts.forEach(([x, y], i) => { const px = ((x - cx) * 40) / rx, py = ((y - cy) * 40) / ry; if (i === 0) ctx.moveTo(px, py); else ctx.lineTo(px, py); }); ctx.closePath(); ctx.fillStyle = g; ctx.fill(); ctx.restore(); ctx.globalCompositeOperation = "lighter"; ctx.beginPath(); pts.forEach(([x, y], i) => { if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); }); ctx.closePath(); ctx.lineWidth = 1.2; ctx.strokeStyle = `rgba(255,110,25,${0.4 * ember * ef})`; ctx.stroke(); for (const p of CHAR_EDGE) { ctx.fillStyle = `rgba(255,130,40,${clamp(ember * (0.45 + 0.55 * Math.sin(now * 0.007 + p.ph))) * 0.9})`; ctx.beginPath(); ctx.arc(cx + Math.cos(p.a) * rx * p.j * 0.97, cy + Math.sin(p.a) * ry * p.j * 0.97, p.s, 0, Math.PI * 2); ctx.fill(); } ctx.save(); ctx.translate(cx, LABEL_CY); ctx.scale(1, 0.25); const bg = ctx.createRadialGradient(0, 0, 0, 0, 0, 60); bg.addColorStop(0, `rgba(255,120,30,${0.26 * ember})`); bg.addColorStop(1, "rgba(255,120,30,0)"); ctx.fillStyle = bg; ctx.fillRect(-60, -60, 120, 120); ctx.restore(); ctx.restore(); } // match (burnt one fades, a fresh one slides in) if (!began) { const ang = 14 - 6 * smooth(hv); if (hv > 0.01) { ctx.save(); ctx.beginPath(); ctx.roundRect(0, 0, W, H, 38); ctx.clip(); ctx.lineCap = "round"; const tg = ctx.createLinearGradient(HEAD_REST - 30, 0, headX - 8, 0); tg.addColorStop(0, "rgba(255,205,160,0)"); tg.addColorStop(1, `rgba(255,205,160,${0.38 * hv})`); ctx.strokeStyle = tg; ctx.lineWidth = 1.6; ctx.beginPath(); ctx.moveTo(HEAD_REST - 30, 14.5); ctx.lineTo(headX - 8, 13.5); ctx.stroke(); const tg2 = ctx.createLinearGradient(HEAD_REST - 10, 0, headX - 10, 0); tg2.addColorStop(0, "rgba(255,205,160,0)"); tg2.addColorStop(1, `rgba(255,205,160,${0.2 * hv})`); ctx.strokeStyle = tg2; ctx.lineWidth = 0.8; ctx.beginPath(); ctx.moveTo(HEAD_REST - 10, 17.2); ctx.lineTo(headX - 10, 16.8); ctx.stroke(); const ga = hv * (hv >= 1 ? 0.28 + 0.08 * Math.sin(now * 0.113) : 0.32); for (const [mode, k] of [["lighter", 1], ["source-over", 0.5]] as const) { ctx.globalCompositeOperation = mode; const fg = ctx.createRadialGradient(headX - 4, HEAD_Y + 5, 0, headX - 4, HEAD_Y + 5, 12); fg.addColorStop(0, `rgba(255,150,60,${ga * k})`); fg.addColorStop(1, "rgba(255,150,60,0)"); ctx.fillStyle = fg; ctx.fillRect(headX - 16, HEAD_Y - 7, 24, 24); } ctx.globalCompositeOperation = "source-over"; ctx.restore(); if (hv < 1) { const a = 0.4 * Math.sin(Math.PI * hv); ctx.save(); ctx.translate(headX, HEAD_Y); ctx.rotate((ang * Math.PI) / 180); ctx.strokeStyle = `rgba(255,240,215,${a})`; ctx.lineWidth = 1; ctx.lineCap = "round"; [[-7, 22], [-12, 34], [-17, 18]].forEach(([off, L]) => { ctx.beginPath(); ctx.moveTo(-24 - L, off); ctx.lineTo(-24, off); ctx.stroke(); }); ctx.restore(); } } drawMatch(headX + (hv >= 1 ? Math.sin(now * 0.113) * 0.6 : 0), ang, 0, 1); } else if (t < 3200) { drawMatch(headX, 10, char, 1); } else { drawMatch(HEAD_STRIKE, 10, 1, 1 - clamp((t - 3200) / 400)); const k = clamp((t - 3600) / 400); drawMatch(HEAD_REST + 40 * (1 - easeOut(k)), 14, 0, k); } // smoke for (const p of puffs) { const k = p.age / p.life; ctx.globalAlpha = 0.3 * Math.sin(Math.PI * Math.min(1, k * 1.15)) * (1 - k * 0.3); ctx.fillStyle = "rgb(150,150,155)"; ctx.beginPath(); ctx.arc(p.x, p.y, p.r0 + (p.r1 - p.r0) * k, 0, Math.PI * 2); ctx.fill(); } ctx.globalAlpha = 1; let gi = 0; for (const s of sparks) { if (s.kind !== 0) continue; ctx.globalAlpha = (1 - s.age / s.life) * 0.9; ctx.fillStyle = gi++ % 2 ? "#3d1c10" : "#7a3e24"; ctx.beginPath(); ctx.arc(s.x, s.y, s.size * 0.6, 0, Math.PI * 2); ctx.fill(); } ctx.globalAlpha = 1; if (began && t >= OUT_AT && t < OUT_AT + 1000) { const pr = (t - OUT_AT) / 1000; ctx.strokeStyle = `rgba(170,170,176,${0.45 * Math.sin(Math.PI * Math.min(1, pr * 1.1))})`; ctx.lineWidth = 1.6; ctx.lineCap = "round"; ctx.beginPath(); for (let i = 0; i < 30; i++) { const u = i / 29; const x = HEAD_STRIKE + Math.sin(i * 0.35 + t * 0.004) * (2 + i * 0.35) + u * 6; const y = HEAD_Y - 22 - u * 120 * easeOut(pr); if (i === 0) ctx.moveTo(x, y); else ctx.lineTo(x, y); } ctx.stroke(); } ctx.lineCap = "round"; for (const s of sparks) { if (s.kind === 0) continue; const a = Math.pow(1 - s.age / s.life, 1.2); ctx.globalAlpha = 1; ctx.strokeStyle = `rgba(255,120,20,${a * 0.55})`; ctx.lineWidth = s.size * 1.1; ctx.beginPath(); ctx.moveTo(s.x, s.y); ctx.lineTo(s.x - s.vx * 0.018, s.y - s.vy * 0.018); ctx.stroke(); } // fire and sparks add light ctx.globalCompositeOperation = "lighter"; if (began && t >= 90) { const fl = 0.7 + 0.3 * Math.sin(now * 0.02); const gx = HEAD_STRIKE; const gg = ctx.createRadialGradient(gx, HEAD_Y - 24, 0, gx, HEAD_Y - 24, 64); gg.addColorStop(0, `rgba(255,150,50,${0.28 * kFlame * fl})`); gg.addColorStop(1, "rgba(255,120,30,0)"); ctx.globalAlpha = 1; ctx.fillStyle = gg; ctx.fillRect(gx - 64, HEAD_Y - 84, 128, 128); const lean = ptrX < 0 ? 0 : Math.tan(-clamp((ptrX - HEAD_STRIKE) / 220, -1, 1) * 0.17); if (kFlame > 0.02) { const hh = ctx.createRadialGradient(HEAD_STRIKE + 1, HEAD_Y - 1, 0, HEAD_STRIKE + 1, HEAD_Y - 1, 14); hh.addColorStop(0, `rgba(255,180,80,${0.6 * kFlame})`); hh.addColorStop(0.5, `rgba(255,100,30,${0.3 * kFlame})`); hh.addColorStop(1, "rgba(255,90,20,0)"); ctx.globalAlpha = 1; ctx.fillStyle = hh; ctx.fillRect(HEAD_STRIKE - 14, HEAD_Y - 16, 30, 30); [[-6, -2], [-2, 3], [3, -4], [7, 1], [0, 6], [-8, 4]].forEach(([ox, oy], i) => { ctx.fillStyle = `rgba(255,120,30,${0.7 * char * kFlame * (0.5 + 0.5 * Math.sin(now * 0.01 + i * 2.1))})`; ctx.beginPath(); ctx.arc(HEAD_STRIKE + ox, HEAD_Y + oy, 0.9, 0, Math.PI * 2); ctx.fill(); }); const fh = 58 * kFlame * (0.88 + 0.07 * Math.sin(now * 0.025) + 0.05 * Math.sin(now * 0.061)); const sway = Math.sin(now * 0.009) * 3 + lean * fh * 0.5; // nested teardrops, normal blending so the flame reads on light tiles too ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 1; const ag = ctx.createRadialGradient(HEAD_STRIKE, HEAD_Y - 22, 0, HEAD_STRIKE, HEAD_Y - 22, 34); ag.addColorStop(0, `rgba(255,150,50,${0.2 * kFlame})`); ag.addColorStop(1, "rgba(255,150,50,0)"); ctx.fillStyle = ag; ctx.fillRect(HEAD_STRIKE - 34, HEAD_Y - 56, 68, 68); const layer = (fw0: number, hh: number, sw: number, stops: [number, string][], by: number) => { const fw = fw0 * kFlame; const lg = ctx.createLinearGradient(0, by, 0, by - hh); for (const [o, c] of stops) lg.addColorStop(o, c); ctx.fillStyle = lg; ctx.beginPath(); ctx.moveTo(HEAD_STRIKE, by); ctx.bezierCurveTo(HEAD_STRIKE - fw * 1.5, by - 5, HEAD_STRIKE - fw, by - hh * 0.5, HEAD_STRIKE + sw, by - hh); ctx.bezierCurveTo(HEAD_STRIKE + fw, by - hh * 0.5, HEAD_STRIKE + fw * 1.5, by - 5, HEAD_STRIKE, by); ctx.fill(); }; layer(15, fh, sway, [[0, "rgba(255,170,60,.6)"], [0.45, "rgba(255,120,25,.5)"], [0.8, "rgba(220,70,10,.2)"], [1, "rgba(200,50,0,0)"]], HEAD_Y + 3); layer(8, fh * 0.75, sway * 0.7, [[0, "rgba(255,225,130,.85)"], [0.6, "rgba(255,170,50,.6)"], [1, "rgba(255,120,20,0)"]], HEAD_Y - 3); layer(4.2, fh * 0.38, sway * 0.3, [[0, "rgba(255,252,235,.95)"], [1, "rgba(255,240,190,0)"]], HEAD_Y - 5); ctx.globalCompositeOperation = "lighter"; ctx.fillStyle = `rgba(80,140,255,${0.22 * kFlame})`; ctx.beginPath(); ctx.ellipse(HEAD_STRIKE, HEAD_Y - 7, 4.5, 2.2, 0, 0, Math.PI * 2); ctx.fill(); } for (const f of flames) { const k = f.age / f.life; const rise = f.y0 - f.y; const sx = Math.sin(f.y * 0.08 + now * 0.006 + f.seed) * 4 * clamp(rise / 20); const x = f.x + sx + lean * rise; const r = (9 - 7 * k) * kFlame; const band = f.blue && f.age < 70 ? 4 : k < 0.1 ? 0 : k < 0.35 ? 1 : k < 0.7 ? 2 : 3; sprite(band, x, f.y, r * (band === 3 ? 0.9 : 1), band === 3 ? (1 - k) * 0.3 : [0.42, 0.45, 0.35][band] ?? 0.4); } const fa = clamp((now - flashAt) / 80); if (flashAt >= began && fa < 1) { ctx.globalAlpha = (1 - fa) * 0.85; const g = ctx.createRadialGradient(HEAD_STRIKE, HEAD_Y, 0, HEAD_STRIKE, HEAD_Y, 20); g.addColorStop(0, "rgba(255,252,220,1)"); g.addColorStop(0.5, "rgba(255,230,140,.7)"); g.addColorStop(1, "rgba(255,200,80,0)"); ctx.fillStyle = g; ctx.fillRect(HEAD_STRIKE - 20, HEAD_Y - 20, 40, 40); } } ctx.lineCap = "round"; for (const s of sparks) { if (s.kind === 0) continue; const k = s.age / s.life; const a = Math.pow(1 - k, 1.2); ctx.globalAlpha = a; ctx.strokeStyle = s.color; ctx.lineWidth = s.size * 0.6; ctx.beginPath(); ctx.moveTo(s.x, s.y); ctx.lineTo(s.x - s.vx * 0.018, s.y - s.vy * 0.018); ctx.stroke(); ctx.fillStyle = "#fffbe6"; ctx.beginPath(); ctx.arc(s.x, s.y, 0.7, 0, Math.PI * 2); ctx.fill(); } ctx.globalAlpha = 1; ctx.globalCompositeOperation = "source-over"; ctx.restore(); setLabel(ember, now, began ? t : 0); }; const busy = () => began > 0 || sparks.length > 0 || flames.length > 0 || puffs.length > 0 || (hovering && true) || hv > 0; const tick = (now: number) => { raf = 0; if (!allowed()) return; const dt = last ? Math.min(48, now - last) : 16; last = now; frame(now, dt); if (busy()) raf = requestAnimationFrame(tick); else last = 0; }; 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; hv = 0; sparks.length = flames.length = puffs.length = 0; host.dataset.tbPaused = String(disabled || !visible || document.hidden); host.dataset.tbState = "idle"; host.removeAttribute("data-rm"); hovering = allowed() && el.matches(":hover"); frame(performance.now(), 0); if (hovering) wake(); }; play.current = () => { if (disabled || began) return; if (media.matches) { if (host.dataset.rm) return; host.dataset.rm = "on"; host.dataset.tbState = "playing"; later(() => { host.dataset.rm = "off"; }, 1200); later(() => { host.removeAttribute("data-rm"); host.dataset.tbState = "idle"; }, 1400); return; } if (!allowed()) return; fromX = HEAD_REST + (HEAD_HOVER - HEAD_REST) * smooth(hv); began = performance.now(); const dip: Keyframe[] = [{ transform: "translateY(0)" }, { transform: "translateY(2px)", offset: 0.22 }, { transform: "translateY(-0.6px)", offset: 0.6 }, { transform: "translateY(0)" }]; host.querySelectorAll(".tb-match-block,.tb-match-canvas,.tb-match-label").forEach((n) => n.animate(dip, { duration: 340, easing: "cubic-bezier(.25,.8,.35,1)" })); host.querySelector(".tb-match-shadow")?.animate([{ transform: "scaleY(1)" }, { transform: "scaleY(.94)", offset: 0.22 }, { transform: "scaleY(1)" }], { duration: 340, easing: "cubic-bezier(.25,.8,.35,1)" }); flashAt = -1e9; lastSpawn = lastPuff = 0; host.dataset.tbState = "playing"; // 40 sparks spray backward from the strike lastEmber = 0; for (let i = 0; i < 40; i++) sparks.push({ x: fromX + rnd(0, HEAD_STRIKE - fromX), y: HEAD_Y + rnd(2, 6), vx: -rnd(160, 420), vy: -rnd(40, 260), g: 1100, age: 0, life: rnd(160, 320), size: rnd(1, 1.8), color: Math.random() < 0.6 ? "#ffe08a" : "#ffad4a", kind: 1 }); wake(); }; const loc = (e: PointerEvent) => { if (dirty || !rect) { rect = el.getBoundingClientRect(); dirty = false; } const scale = rect.width / (el.offsetWidth || 1) || 1; ptrX = (e.clientX - rect.left) / scale; }; const enter = (e: PointerEvent) => { loc(e); hovering = true; wake(); }; const move = (e: PointerEvent) => { loc(e); }; const leave = () => { hovering = false; ptrX = -1; wake(); }; 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(), 0); }); setup(); io?.observe(host); ro?.observe(el); el.addEventListener("pointerenter", enter); el.addEventListener("pointermove", move, { passive: true }); el.addEventListener("pointerleave", leave); window.addEventListener("resize", inval); window.addEventListener("scroll", inval, true); document.addEventListener("visibilitychange", sync); media.addEventListener("change", sync); sync(); return () => { play.current = () => {}; cancelAnimationFrame(raf); clearTimers(); io?.disconnect(); ro?.disconnect(); el.removeEventListener("pointerenter", enter); el.removeEventListener("pointermove", move); el.removeEventListener("pointerleave", leave); window.removeEventListener("resize", inval); window.removeEventListener("scroll", inval, true); 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(); }; return ( ); } export default MatchButton; const css = ` .tb-match-wrap{display:inline-flex;position:relative;vertical-align:middle;width:${W}px;height:${H}px} .tb-match-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;border:0;border-radius:38px; background:transparent;color:#45240c;cursor:pointer;overflow:visible;-webkit-tap-highlight-color:transparent; font:800 30px/1 ui-rounded,"Nunito","Segoe UI",system-ui,sans-serif;letter-spacing:.1em;text-transform:uppercase} .tb-match-btn:focus-visible{outline:3px solid #ff9a3a;outline-offset:4px} .tb-match-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-match-shadow{position:absolute;inset:0;border-radius:38px;box-shadow:0 2px 2px rgba(0,0,0,.55),0 8px 10px -4px rgba(0,0,0,.45),0 22px 30px -12px rgba(0,0,0,.6);pointer-events:none} .tb-match-block{position:absolute;left:0;top:0;width:${W}px;height:${H}px;pointer-events:none} .tb-match-canvas{position:absolute;left:-${PL}px;top:-${PT}px;width:${CW}px;height:${CH}px;pointer-events:none} .tb-match-label{position:absolute;left:0;right:0;top:28px;bottom:0;display:flex;align-items:center;justify-content:center;pointer-events:none; color:#45240c;text-shadow:0 1px 0 rgba(255,228,184,.6),0 -1px 0 rgba(30,12,2,.45),0 0 1px rgba(30,12,2,.3);padding-left:.1em} .tb-match-label span{display:inline-block} .tb-match-sr{position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0} .tb-match-rmflame{position:absolute;left:140px;top:-55px;width:40px;height:64px;pointer-events:none;opacity:0;filter:drop-shadow(0 0 6px rgba(255,140,40,.85))} .tb-match-wrap[data-rm=on] .tb-match-rmflame{opacity:1} .tb-match-wrap[data-rm=on] .tb-match-label{background:radial-gradient(48% 62% at 55% 52%,rgba(26,11,3,.78),rgba(26,11,3,.45) 60%,rgba(26,11,3,0) 100%)} .tb-match-wrap[data-rm=on] .tb-match-label span{color:#ffcf6e!important;text-shadow:0 0 1.5px rgba(20,8,2,.95),0 1px 0 rgba(20,8,2,.7),0 0 6px rgba(255,122,26,.9),0 0 14px rgba(255,90,0,.55)!important} .tb-match-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-match-wrap *,.tb-match-wrap *::before,.tb-match-wrap *::after{animation:none!important;transition:none!important}} `;