"use client"; /** * popcorn.tsx — a striped bucket of popcorn that will not sit still. * Hover and the kernels jiggle and an odd one pops; click and a handful bursts out, then the bucket settles. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type MouseEvent, type Ref, type ReactNode } from "react"; type Palette = { stripe: string; paper: string; corn: string; hull: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; }; const W = 200; const H = 190; const BUCKET_Y = 40; // bucket starts this far below the button top (the crown sits above) const BUCKET_H = 150; const PAD_X = 160; const PAD_T = 220; const PAD_B = 60; const CW = W + PAD_X * 2; const CH = H + PAD_T + PAD_B; const CX = PAD_X + W / 2; const RIM = PAD_T + BUCKET_Y; const FLOOR = PAD_T + H + 42; const HEAP = 34; const MAX_LIVE = 120; const ACCENT = "#ff6a5a"; const SPR = 4; // sprite resolution multiplier const SPR_UNITS = 48; const DEFAULT_PALETTE: Palette = { stripe: "#c8201e", paper: "#f1e3c3", corn: "#f7ecd2", hull: "#a86a1e" }; const clamp01 = (u: number) => (u < 0 ? 0 : u > 1 ? 1 : u); const lerp = (a: number, b: number, u: number) => a + (b - a) * u; const rng = (seed: number) => () => { seed |= 0; 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 surfY = (x: number) => RIM + 4 - 44 * Math.sqrt(Math.max(0, 1 - ((x - CX) / 98) ** 2)); type Heap = { hx: number; hy: number; s: number; spr: number; rot0: number; p: number[]; f: number[]; ny: number; nv: number; extra: boolean; sinkAt: number; born: number }; type Fly = { x: number; y: number; vx: number; vy: number; rot: number; vr: number; s: number; spr: number; mode: "air" | "floor" | "hop"; bounced: boolean; fadeAt: number; born: number; popT0: number; settleY: number; }; type Crumb = { x: number; y: number; vx: number; vy: number; r: number; col: string; born: number; life: number; g: number; back?: boolean }; export function PopcornButton({ children = "snack", palette = DEFAULT_PALETTE, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); useImperativeHandle(ref, () => btnRef.current!, []); const wrapRef = useRef(null); const visRef = useRef(null); const backRef = useRef(null); const frontRef = useRef(null); const api = useRef<{ play: () => void }>({ play: () => {} }); const uid = `tb-popcorn-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const { stripe, paper, corn, hull } = palette; useEffect(() => { const btn = btnRef.current, wrap = wrapRef.current, vis = visRef.current, back = backRef.current, front = frontRef.current; if (!btn || !wrap || !vis || !back || !front) return; const cb = back.getContext("2d"), cf = front.getContext("2d"); if (!cb || !cf) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let reduced = media.matches; let visible = true; let hover = btn.matches(":hover"); let hoverT0 = hover ? performance.now() : 0; let raf = 0, last = 0; let dpr = 1, dirty = true; let jig = 0, swell = 0; let clickT0 = -1e9; let squashT0 = -1e9; let sx = 1, sy = 1, vsx = 0, vsy = 0; let reducedLit = false; let burstT0 = -1e9; let nextPop = 0, hoverPops = 0; let queue: { at: number; fn: () => void }[] = []; const timers = new Set(); const later = (fn: () => void, ms: number) => { const id = window.setTimeout(() => { timers.delete(id); fn(); }, ms); timers.add(id); }; const allowed = () => !disabled && !btn.matches(":disabled") && visible && !document.hidden; // ---- sprites ---- const sprites: HTMLCanvasElement[] = []; const makeSprites = () => { sprites.length = 0; const r = rng(41); for (let v = 0; v < 10; v++) { const c = document.createElement("canvas"); c.width = c.height = SPR_UNITS * SPR; const g = c.getContext("2d"); if (!g) continue; g.scale(SPR, SPR); g.translate(SPR_UNITS / 2, SPR_UNITS / 2); const n = 4 + Math.floor(r() * 3); const balls: { x: number; y: number; r: number }[] = []; balls.push({ x: (r() - 0.5) * 2, y: (r() - 0.5) * 2, r: 9.5 + r() * 1.5 }); for (let i = 1; i < n; i++) { const a = (i / n) * Math.PI * 2 + r() * 0.8, d = 6.5 + r() * 2.5; balls.push({ x: Math.cos(a) * d, y: Math.sin(a) * d * 0.85, r: 5.8 + r() * 3 }); } const b0 = balls[0]; balls.sort((p, q) => p.y - q.y); g.fillStyle = "rgba(150,95,30,.55)"; for (const b of balls) { g.beginPath(); g.arc(b.x, b.y + 0.4, b.r + 0.6, 0, Math.PI * 2); g.fill(); } g.shadowColor = "rgba(200,125,25,.55)"; g.shadowBlur = 1.6 * SPR; g.shadowOffsetX = 0.2 * SPR; g.shadowOffsetY = 0.7 * SPR; const lump = (b: { x: number; y: number; r: number }) => { const gr = g.createRadialGradient(b.x - 0.38 * b.r, b.y - 0.45 * b.r, 0, b.x + 0.1 * b.r, b.y + 0.1 * b.r, b.r * 1.08); gr.addColorStop(0, "#ffffff"); gr.addColorStop(0.3, "#fffaf0"); gr.addColorStop(0.62, corn); gr.addColorStop(0.85, "#f2cf7e"); gr.addColorStop(1, "#e0a547"); g.fillStyle = gr; g.beginPath(); g.arc(b.x, b.y, b.r, 0, Math.PI * 2); g.fill(); }; for (const b of balls) lump(b); const bumps: { x: number; y: number; r: number }[] = []; for (const b of balls) { if (b.r <= 6) continue; for (let k = 0; k < 2; k++) { const an = r() * Math.PI * 2; bumps.push({ x: b.x + Math.cos(an) * b.r * 0.78, y: b.y + Math.sin(an) * b.r * 0.78, r: 2.2 + r() * 1.6 }); } } for (const q of bumps) lump(q); g.shadowColor = "transparent"; g.shadowBlur = 0; g.shadowOffsetX = g.shadowOffsetY = 0; for (const b of [...balls, ...bumps]) { const hx = b.x - 0.36 * b.r, hy = b.y - 0.42 * b.r, hr = 0.42 * b.r; const hg = g.createRadialGradient(hx, hy, 0, hx, hy, hr); hg.addColorStop(0, "rgba(255,255,255,.7)"); hg.addColorStop(1, "rgba(255,255,255,0)"); g.fillStyle = hg; g.beginPath(); g.arc(hx, hy, hr, 0, Math.PI * 2); g.fill(); } g.globalCompositeOperation = "source-atop"; const bg = g.createLinearGradient(0, -24, 0, 24); bg.addColorStop(0, "rgba(255,230,150,0)"); bg.addColorStop(0.55, "rgba(255,205,95,.14)"); bg.addColorStop(1, "rgba(205,135,45,.34)"); g.fillStyle = bg; g.fillRect(-SPR_UNITS / 2, -SPR_UNITS / 2, SPR_UNITS, SPR_UNITS); if (v % 3 === 0) { const k = balls[1 + Math.floor(r() * (balls.length - 1))]; const w = b0.r / (b0.r + k.r), px = b0.x + (k.x - b0.x) * w, py = b0.y + (k.y - b0.y) * w; const pg = g.createRadialGradient(px, py, 0, px, py, 3.0); pg.addColorStop(0, "rgba(150,90,25,.8)"); pg.addColorStop(0.6, "rgba(190,120,40,.35)"); pg.addColorStop(1, "rgba(220,160,70,0)"); g.fillStyle = pg; g.beginPath(); g.ellipse(px, py, 3.0, 1.0, Math.atan2(k.y - b0.y, k.x - b0.x) + Math.PI / 2, 0, Math.PI * 2); g.fill(); } g.globalCompositeOperation = "destination-in"; g.fillStyle = "#000"; g.beginPath(); for (const q of [...balls, ...bumps]) { g.moveTo(q.x + q.r + 0.7, q.y + 0.4); g.arc(q.x, q.y + 0.4, q.r + 0.7, 0, Math.PI * 2); } g.fill(); g.globalCompositeOperation = "source-over"; sprites.push(c); } }; // ---- heap ---- const heap: Heap[] = []; { const r = rng(21); let minD = 12, tries = 0; const pts: { x: number; y: number }[] = []; while (pts.length < HEAP && tries < 4000) { tries++; if (tries % 150 === 0) minD -= 0.6; const x = CX + (r() * 2 - 1) * 86; const top = surfY(x); const y = top + Math.pow(r(), 1.8) * (RIM + 6 - top); if (pts.every((p) => Math.hypot(p.x - x, p.y - y) >= minD)) pts.push({ x, y }); } for (const p of pts) heap.push({ hx: p.x, hy: p.y, s: p.y - surfY(p.x) < 10 ? 1.04 + r() * 0.3 : 0.88 + r() * 0.18, spr: Math.floor(r() * 10), rot0: (r() * 2 - 1) * 0.45, p: [r() * 6.28, r() * 6.28, r() * 6.28], f: [14 + r() * 8, 14 + r() * 8, 14 + r() * 8], ny: 0, nv: 0, extra: false, sinkAt: 0, born: 0 }); } let fly: Fly[] = []; let crumbs: Crumb[] = []; const reducedPts = [[-52, -62], [-24, -92], [6, -74], [34, -100], [58, -66], [-4, -120]]; const reducedSprites = [0, 3, 1, 4, 2, 5]; const fit = () => { const rc = btn.getBoundingClientRect(); const scale = rc.width / (btn.offsetWidth || 1) || 1; const nd = Math.min(2, (window.devicePixelRatio || 1) * scale); const w = Math.round(CW * nd), h = Math.round(CH * nd); for (const c of [back, front]) if (c.width !== w || c.height !== h) { c.width = w; c.height = h; } dpr = nd; cb.setTransform(dpr, 0, 0, dpr, 0, 0); cf.setTransform(dpr, 0, 0, dpr, 0, 0); dirty = false; }; const blit = (g: CanvasRenderingContext2D, spr: number, x: number, y: number, s: number, rot: number, alpha: number) => { const img = sprites[spr % sprites.length]; if (!img || alpha <= 0) return; g.globalAlpha = alpha; g.translate(x, y); g.rotate(rot); g.scale(s, s); g.drawImage(img, -SPR_UNITS / 2, -SPR_UNITS / 2, SPR_UNITS, SPR_UNITS); g.setTransform(dpr, 0, 0, dpr, 0, 0); g.globalAlpha = 1; }; // ---- drawing ---- const draw = (now: number) => { const t = now / 1000; cb.clearRect(0, 0, CW, CH); cf.clearRect(0, 0, CW, CH); const a = reduced ? 0 : jig; const items = heap.filter((h) => !(h.extra && h.sinkAt && now - h.sinkAt >= 600)); items.sort((p, q) => p.hy + p.ny - (q.hy + q.ny)); for (const h of items) { let al = 1, dy = 0; if (h.extra && h.sinkAt && now >= h.sinkAt) { const u = (now - h.sinkAt) / 600; al = 1 - u; dy = 14 * u; } if (h.extra && now < h.born) continue; const ox = a * 1.2 * Math.sin(2 * Math.PI * h.f[0] * t + h.p[0]); const oy = a * 1.8 * Math.sin(2 * Math.PI * h.f[1] * t + h.p[1]); const rr = a * ((6 * Math.PI) / 180) * Math.sin(2 * Math.PI * h.f[2] * t + h.p[2]); const lift = swell * 9 * clamp01(1 - (h.hy - surfY(h.hx)) / 30); blit(cb, h.spr, h.hx + ox, h.hy + oy + h.ny + dy - lift, h.s, h.rot0 + rr, al); } cb.globalCompositeOperation = "source-atop"; const dg = cb.createLinearGradient(0, RIM - 34, 0, RIM + 8); dg.addColorStop(0, "rgba(120,60,10,0)"); dg.addColorStop(0.65, "rgba(150,80,15,.10)"); dg.addColorStop(1, "rgba(110,50,8,.30)"); cb.fillStyle = dg; cb.fillRect(CX - 110, RIM - 60, 220, 72); cb.globalCompositeOperation = "source-over"; if (now - burstT0 < 140 && !reduced) { const u = (now - burstT0) / 140; const fg = cf.createRadialGradient(CX, RIM - 14, 0, CX, RIM - 14, 80); fg.addColorStop(0, `rgba(255,248,225,${(0.38 * (1 - u)).toFixed(3)})`); fg.addColorStop(0.5, `rgba(255,236,190,${(0.16 * (1 - u)).toFixed(3)})`); fg.addColorStop(1, "rgba(255,236,190,0)"); cf.fillStyle = fg; cf.beginPath(); cf.arc(CX, RIM - 14, 80, 0, Math.PI * 2); cf.fill(); } if (!reduced && now - burstT0 > 60 && now - burstT0 < 360) { const u = (now - burstT0 - 60) / 300, e = 1 - (1 - u) ** 3; cf.strokeStyle = "rgba(176,166,150,1)"; cf.globalAlpha = 0.8 * (1 - u); cf.lineWidth = 1.8; cf.lineCap = "round"; for (let i = 0; i < 10; i++) { const ang = -Math.PI / 2 + (i - 4.5) * 0.3, r0 = 104 + e * 70, len = 12 * (1 - u) + 3; const c = Math.cos(ang), sn = Math.sin(ang); cf.beginPath(); cf.moveTo(CX + c * r0, RIM - 10 + sn * r0 * 0.8); cf.lineTo(CX + c * (r0 + len), RIM - 10 + sn * (r0 + len) * 0.8); cf.stroke(); } cf.globalAlpha = 1; } // front: seeds about to pop, airborne and floor kernels, crumbs for (const f of fly) { if (f.mode === "hop" && now < f.born) { // the seed, shaking in the heap cf.fillStyle = "#d9a441"; cf.save(); cf.translate(f.x + Math.sin(now * 0.09) * 0.6, f.y); cf.rotate(0.4); cf.beginPath(); cf.ellipse(0, 0, 3, 2.5, 0, 0, Math.PI * 2); cf.fill(); cf.restore(); continue; } if (f.mode === "air" && now < f.born) continue; let al = 1; if (f.fadeAt && now >= f.fadeAt) al = 1 - (now - f.fadeAt) / 500; if (al <= 0) continue; let s = f.s; if (f.mode === "hop") { const u = (now - f.popT0) / 120; s = f.s * (u < 1 ? (u < 0.6 ? lerp(0.3, 1.15, u / 0.6) : lerp(1.15, 1, (u - 0.6) / 0.4)) : 1); const fl = (now - f.popT0) / 40; if (fl < 1) { cf.fillStyle = `rgba(255,250,235,${(0.7 * (1 - fl)).toFixed(3)})`; cf.beginPath(); cf.arc(f.x, f.y, 10, 0, Math.PI * 2); cf.fill(); } } if (f.mode === "air") s *= Math.min(1, 0.45 + ((now - f.born) / 70) * 0.55); blit(cf, f.spr, f.x, f.y, s, f.rot, al); } if (crumbs.length) { for (const c of crumbs) { const age = now - c.born; if (age < 0 || age > c.life) continue; const cg = c.back ? cb : cf; cg.globalAlpha = 1 - age / c.life; cg.fillStyle = c.col; cg.beginPath(); cg.arc(c.x, c.y, c.r, 0, Math.PI * 2); cg.fill(); cg.globalAlpha = 1; } cf.globalAlpha = 1; } if (reduced && reducedLit) { reducedPts.forEach(([dx, dy], i) => blit(cf, reducedSprites[i], CX + dx, RIM + dy + 20, 1.15, i, 1)); } if (a > 0.3 && !reduced) { cf.strokeStyle = "rgba(176,166,150,1)"; cf.lineCap = "round"; for (const sd of [-1, 1]) { const c = sd > 0 ? -0.15 : Math.PI + 0.15, c2 = sd > 0 ? -0.75 : Math.PI + 0.75; for (let i = 0; i < 3; i++) { cf.lineWidth = [2.2, 1.7, 1.2][i]; cf.globalAlpha = [0.9, 0.65, 0.45][i] * (0.55 + 0.45 * Math.abs(Math.sin(2 * Math.PI * 5 * t + i * 1.3))); cf.beginPath(); cf.arc(CX + sd * 70, RIM + 26, 46 + i * 8, c - 0.26, c + 0.26); cf.stroke(); } for (let i = 0; i < 2; i++) { cf.lineWidth = [2.2, 1.7][i]; cf.globalAlpha = [0.9, 0.65][i] * (0.55 + 0.45 * Math.abs(Math.sin(2 * Math.PI * 5 * t + i * 1.3))); cf.beginPath(); cf.arc(CX + sd * 44, RIM - 34, 30 + i * 7, c2 - 0.22, c2 + 0.22); cf.stroke(); } } cf.globalAlpha = 1; } // bucket pose const shiver = a * ((0.8 * Math.PI) / 180) * Math.sin(2 * Math.PI * 12 * t); vis.style.transform = `rotate(${((shiver * 180) / Math.PI).toFixed(3)}deg) scale(${sx.toFixed(4)},${sy.toFixed(4)})`; }; // ---- simulation ---- const spawnBurst = (now: number, n: number) => { const live = fly.filter((f) => f.mode !== "hop").length; n = Math.min(n, MAX_LIVE - live); const r = Math.random; for (let i = 0; i < n; i++) { const x = CX + (r() * 2 - 1) * 70, u = r(); const s = u < 0.55 ? 0.8 + r() * 0.25 : u < 0.9 ? 1.15 + r() * 0.25 : 1.6 + r() * 0.35; const hero = s >= 1.6; let vx = ((x - CX) / 70) * 190 + (r() * 2 - 1) * 90, vy = -(540 + r() * 320); if (hero) { vx *= 1.35; vy *= 0.88; } const lim = hero ? 260 : 240; vx = Math.max(-lim, Math.min(lim, vx)); fly.push({ x, y: RIM - 6 + r() * 8, vx, vy, rot: r() * 6, vr: (r() * 2 - 1) * 8, s, spr: Math.floor(r() * 10), mode: "air", bounced: false, fadeAt: 0, born: now + i * 3.5, popT0: 0, settleY: 0, }); } fly.sort((p, q) => p.s - q.s); burstT0 = now; for (let i = 0; i < 18; i++) { crumbs.push({ x: CX + (r() * 2 - 1) * 70, y: RIM - 4, vx: (r() * 2 - 1) * 280, vy: -(300 + r() * 500), r: 1.4 + r() * 1.2, col: r() < 0.25 ? "#e2b25e" : "#f6e6bf", born: now, life: 520, g: 1600 }); } }; const nudge = (x: number, y: number) => { const near = heap.filter((h) => !(h.extra && h.sinkAt)).map((h) => ({ h, d: Math.hypot(h.hx - x, h.hy - y) })).sort((p, q) => p.d - q.d).slice(0, 3); for (const { h } of near) { h.ny = 2; h.nv = 0; } }; const addExtra = (f: Fly, now: number) => { const stack = heap.filter((h) => h.extra && Math.abs(h.hx - f.x) < 9).length; const y = Math.max(surfY(f.x) - 14, surfY(f.x) - 2 - stack * 3 - Math.random() * 4); const sinkAt = Math.max(now + 400, clickT0 + 2200 + Math.random() * 800); heap.push({ hx: f.x, hy: y, s: f.s > 1.2 ? 1.1 : f.s, spr: f.spr, rot0: f.rot, p: [Math.random() * 6, Math.random() * 6, Math.random() * 6], f: [14 + Math.random() * 8, 14 + Math.random() * 8, 14 + Math.random() * 8], ny: 0, nv: 0, extra: true, sinkAt: f.mode === "hop" ? now + 1500 : sinkAt, born: now }); nudge(f.x, y); }; const step = (h: number, now: number) => { const drag = Math.exp(-0.4 * h); for (const f of fly) { if (now < f.born) continue; if (f.mode === "hop") { f.vy += 1400 * h; f.x += f.vx * h; f.y += f.vy * h; f.rot += f.vr * h; if (f.vy > 0 && f.y >= surfY(f.x) - 3) { f.settleY = 1; } continue; } if (f.mode === "floor" && f.settleY) continue; f.vx *= drag; f.vy = f.vy * drag + 1600 * h; f.x += f.vx * h; f.y += f.vy * h; f.rot += f.vr * h; const wl = PAD_X + W / 2 - 4 - 20 * f.s; if (Math.abs(f.x - CX) > wl) { f.x = CX + Math.sign(f.x - CX) * wl; f.vx *= -0.3; } if (f.mode === "air" && f.vy > 0 && f.y >= RIM) { if (Math.abs(f.x - CX) < 86) { // falls back into the bucket const ty = surfY(f.x) - 3; if (f.y >= ty) { if (!f.bounced) { f.bounced = true; f.vy = -f.vy * 0.25; f.vx *= 0.4; f.y = ty; } else { f.settleY = 2; } } } else f.mode = "floor"; } if (f.mode === "floor" && f.y >= FLOOR) { f.y = FLOOR; if (!f.bounced) { f.bounced = true; f.vy = -f.vy * 0.3; f.vx *= 0.5; f.vr *= 0.4; } else { f.settleY = 3; f.vx = f.vy = f.vr = 0; f.fadeAt = Math.max(now + 500, clickT0 + 2200 + Math.random() * 600); } } } for (const c of crumbs) { c.vy += c.g * h; c.x += c.vx * h; c.y += c.vy * h; } for (const k of heap) { if (k.ny || k.nv) { k.nv += (-400 * k.ny - 22 * k.nv) * h; k.ny += k.nv * h; if (Math.abs(k.ny) < 0.01 && Math.abs(k.nv) < 0.05) { k.ny = 0; k.nv = 0; } } } }; const tick = (now: number) => { raf = 0; if (!allowed() || reduced) return; if (dirty) fit(); const dt = last ? Math.min(0.048, (now - last) / 1000) : 0.016; last = now; let moving = false; // queue if (queue.length) { const due = queue.filter((q) => q.at <= now); if (due.length) { queue = queue.filter((q) => q.at > now); due.forEach((q) => q.fn()); } if (queue.length) moving = true; } // jiggle amplitude: hover 1, click boost to 2 const boost = now - clickT0 < 300 ? 1 + (1 - (now - clickT0) / 300) : 1; const jt = hover || now - clickT0 < 1200 ? boost : 0; swell += ((hover ? 1 : 0) - swell) * (1 - Math.exp(-dt / 0.09)); if (Math.abs((hover ? 1 : 0) - swell) > 0.005) moving = true; else swell = hover ? 1 : 0; jig += (jt - jig) * (1 - Math.exp(-dt / 0.085)); if (Math.abs(jt - jig) > 0.01 || jig > 0.01) moving = true; else jig = 0; // hover pops if (hover && hoverPops < 4 && now >= nextPop) { hoverPops++; const top = heap.filter((h) => !h.extra).sort((p, q) => p.hy - q.hy).slice(0, 10); const pick = top[Math.floor(Math.random() * top.length)]; if (pick) { fly.push({ x: pick.hx, y: pick.hy - 2, vx: (Math.random() * 2 - 1) * 40, vy: -460, rot: 0, vr: (Math.random() * 2 - 1) * 6, s: 1.25, spr: Math.floor(Math.random() * 10), mode: "hop", bounced: false, fadeAt: 0, born: now + 150, popT0: now + 150, settleY: 0 }); queue.push({ at: now + 150, fn: () => { for (let i = 0; i < 3; i++) crumbs.push({ x: pick.hx, y: pick.hy - 4, vx: (Math.random() * 2 - 1) * 50, vy: -40 - Math.random() * 60, r: 1.1, col: hull, born: performance.now(), life: 420, g: 1400, back: true }); } }); } nextPop = now + (hoverPops === 1 ? 650 : 700 + Math.random() * 600); } // spring for the squash const sq = now - squashT0, tgt = hover ? 1.02 : 1; if (sq < 80) { const u = clamp01(sq / 80); sx = lerp(tgt, tgt * 1.06, u); sy = lerp(tgt, tgt * 0.92, u); vsx = vsy = 0; moving = true; } else if (Math.abs(sx - tgt) > 0.0005 || Math.abs(sy - tgt) > 0.0005 || Math.abs(vsx) > 0.01 || Math.abs(vsy) > 0.01) { let rem = dt; while (rem > 0) { const h = Math.min(rem, 0.004); rem -= h; vsx += (-300 * (sx - tgt) - 16 * vsx) * h; sx += vsx * h; vsy += (-300 * (sy - tgt) - 16 * vsy) * h; sy += vsy * h; } moving = true; } else { sx = sy = tgt; } // physics, fixed sub-steps let rem = dt; while (rem > 0) { const h = Math.min(rem, 0.008); rem -= h; step(h, now); } // bookkeeping for (const f of fly) { if (f.mode === "hop" && f.settleY === 1 && now >= f.born) addExtra(f, now); else if (f.mode !== "hop" && f.mode === "air" && f.settleY === 2) { addExtra(f, now); f.fadeAt = 1; f.born = 1e15; } } fly = fly.filter((f) => !((f.mode === "hop" && f.settleY === 1) || (f.mode === "air" && f.settleY === 2) || (f.fadeAt > 1 && now - f.fadeAt > 500))); crumbs = crumbs.filter((c) => now - c.born < c.life); for (let i = heap.length - 1; i >= HEAP; i--) if (heap[i].extra && heap[i].sinkAt && now - heap[i].sinkAt >= 600) heap.splice(i, 1); if (fly.length || crumbs.length || heap.length > HEAP) moving = true; if (hover) moving = true; draw(now); if (moving) raf = requestAnimationFrame(tick); }; const start = () => { if (!raf && allowed() && !reduced) { last = 0; raf = requestAnimationFrame(tick); } }; const pause = () => { wrap.dataset.tbPaused = String(disabled || !visible || document.hidden); if (!allowed() || reduced) { cancelAnimationFrame(raf); raf = 0; } if (dirty) fit(); draw(performance.now()); start(); }; const invalidate = () => { dirty = true; if (!raf) { fit(); draw(performance.now()); } }; const enter = () => { if (disabled) return; hover = true; hoverT0 = performance.now(); hoverPops = 0; nextPop = hoverT0 + 120; wrap.dataset.tbState = "hover"; start(); }; const leave = () => { hover = false; wrap.dataset.tbState = "rest"; start(); if (!raf) draw(performance.now()); }; api.current.play = () => { if (disabled) return; const now = performance.now(); if (reduced) { reducedLit = true; wrap.dataset.tbState = "lit"; draw(now); later(() => { reducedLit = false; wrap.dataset.tbState = hover ? "hover" : "rest"; draw(performance.now()); }, 1100); return; } const again = now - clickT0 < 1600; clickT0 = now; squashT0 = now; wrap.dataset.tbState = "click"; queue.push({ at: now + 80, fn: () => spawnBurst(performance.now(), again ? 20 : 34) }); later(() => { if (!hover) wrap.dataset.tbState = "rest"; else wrap.dataset.tbState = "hover"; }, 3800); start(); }; const onMedia = () => { reduced = media.matches; queue = []; fly = []; crumbs = []; reducedLit = false; for (let i = heap.length - 1; i >= HEAP; i--) heap.splice(i, 1); jig = 0; sx = sy = 1; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); pause(); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; pause(); }); io?.observe(wrap); const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(invalidate); ro?.observe(btn); window.addEventListener("resize", invalidate); window.addEventListener("scroll", invalidate, true); document.addEventListener("visibilitychange", pause); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointerleave", leave); media.addEventListener("change", onMedia); makeSprites(); wrap.dataset.tbState = hover && !disabled ? "hover" : "rest"; nextPop = hoverT0 + 120; fit(); pause(); return () => { api.current.play = () => {}; cancelAnimationFrame(raf); timers.forEach((id) => window.clearTimeout(id)); timers.clear(); queue = []; io?.disconnect(); ro?.disconnect(); window.removeEventListener("resize", invalidate); window.removeEventListener("scroll", invalidate, true); document.removeEventListener("visibilitychange", pause); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointerleave", leave); media.removeEventListener("change", onMedia); }; }, [disabled, corn, hull]); const press = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; api.current.play(); }; // bucket art const stripes: ReactNode[] = []; for (let i = 0; i < 10; i++) { const t0 = i * 20, t1 = t0 + 20, b0 = 20 + i * 16, b1 = b0 + 16; const red = i % 2 === 0; stripes.push(); const m0 = t0 + 14, m1 = t1, c0 = b0 + 11.2, c1 = b1; stripes.push(); } let rim = "M0,6"; for (let i = 0; i < 10; i++) rim += ` Q${i * 20 + 10},-2 ${i * 20 + 20},6`; const outline = `${rim} L${W - 20},${BUCKET_H} Q${W / 2},${BUCKET_H + 8} 20,${BUCKET_H}Z`; return ( ); } export default PopcornButton; const css = ` .tb-popcorn-wrap{display:inline-block;position:relative;width:${W}px;height:${H}px;vertical-align:middle} .tb-popcorn-btn{position:relative;display:block;width:${W}px;height:${H}px;padding:0;margin:0;border:0;background:none;cursor:pointer;overflow:visible;-webkit-tap-highlight-color:transparent;font:inherit} .tb-popcorn-btn:focus-visible{outline:3px solid ${ACCENT};outline-offset:16px;border-radius:30px} .tb-popcorn-vis{position:absolute;inset:0;display:block;transform-origin:50% 100%;will-change:transform;pointer-events:none} .tb-popcorn-svg{position:absolute;left:0;top:${BUCKET_Y}px;overflow:visible;pointer-events:none} .tb-popcorn-canvas{position:absolute;left:-${PAD_X}px;top:-${PAD_T}px;width:${CW}px;height:${CH}px;max-width:none;pointer-events:none} .tb-popcorn-label{position:absolute;left:${(W - 150) / 2}px;top:${BUCKET_Y + 62}px;width:150px;height:56px;display:flex;align-items:center;justify-content:center;text-transform:uppercase;letter-spacing:.05em;text-shadow:0 1px 0 rgba(255,255,255,.55);pointer-events:none; font:900 26px/1 ui-rounded,"Nunito","Comic Sans MS","Segoe UI",system-ui,sans-serif;color:#1f1510} .tb-popcorn-btn:disabled{cursor:not-allowed;opacity:.6;filter:saturate(.5)} .tb-popcorn-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-popcorn-wrap *,.tb-popcorn-wrap *::before,.tb-popcorn-wrap *::after{animation:none!important;transition:none!important}} `;