"use client"; /** * bubblewrap.tsx — a sheet of clear bubble wrap. * Hover swells the bubbles near the cursor; each click pops one, and a fresh sheet slides in when the sheet is spent. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useImperativeHandle, useRef, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type Ref, type ReactNode } from "react"; type Palette = { film: string; rim: string; plate: string; ink: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; palette?: Palette; /** pops before a fresh sheet slides in */ perSheet?: number; }; const W = 240; const H = 96; const PAD = 24; const CW = W + PAD * 2; const CH = H + PAD * 2; const R = 13.5; const SPR = R + 6; const PITCH = 28; const ROW_Y = [19, 48, 77]; const PLATE_W = 112; const PLATE_H = 32; const ACCENT = "#cfe0ff"; const SLIDE = W * 1.1; const DEFAULT_PALETTE: Palette = { film: "rgba(214,222,232,.14)", rim: "rgba(255,255,255,.72)", plate: "rgba(104,114,128,.66)", ink: "#ffffff", }; type Cell = { x: number; y: number; seed: number; popped: boolean; sc: number; jx: number; jy: number; jvx: number; jvy: number; popT: number; popAt: number; squashT0: number; inflate: number; vs: number; sprite: HTMLCanvasElement | null; }; type Sheet = { cells: Cell[]; ox: number }; type Tick = { x: number; y: number; a: number; len: number; t0: number; first: boolean }; const clamp01 = (u: number) => (u < 0 ? 0 : u > 1 ? 1 : u); const outCubic = (u: number) => 1 - Math.pow(1 - clamp01(u), 3); const outBack = (u: number) => { const c = 1.9, v = clamp01(u) - 1; return 1 + (c + 1) * v * v * v + c * v * v; }; 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 outQuad = (u: number) => 1 - (1 - clamp01(u)) * (1 - clamp01(u)); const outQuart = (u: number) => 1 - Math.pow(1 - clamp01(u), 4); const stadiumPath = (g: CanvasRenderingContext2D, x: number, y: number, w: number, h: number) => { const r = h / 2; g.beginPath(); g.moveTo(x + r, y); g.lineTo(x + w - r, y); g.arc(x + w - r, y + r, r, -Math.PI / 2, Math.PI / 2); g.lineTo(x + r, y + h); g.arc(x + r, y + r, r, Math.PI / 2, Math.PI * 1.5); g.closePath(); }; let sheetCounter = 1; function makeSheet(): Sheet { const cells: Cell[] = []; ROW_Y.forEach((y, r) => { for (let k = -4; k <= 4; k++) { const x = W / 2 + (r === 1 ? (k + 0.5) * PITCH : k * PITCH); if (x < 0 || x > W) continue; const cx = x < H / 2 ? H / 2 : x > W - H / 2 ? W - H / 2 : x; if (Math.hypot(x - cx, y - H / 2) > H / 2 - R - 3) continue; if (r === 1 && Math.abs(x - W / 2) < PLATE_W / 2 + 8) continue; // keep the label plate free cells.push({ x, y, seed: sheetCounter * 131 + cells.length * 17, popped: false, sc: 1, jx: 0, jy: 0, jvx: 0, jvy: 0, popT: 0, popAt: 0, squashT0: 0, inflate: 0, vs: 0.96 + rng(sheetCounter * 131 + cells.length * 17)() * 0.08, sprite: null }); } }); sheetCounter++; return { cells, ox: 0 }; } export function BubbleWrapButton({ children = "press", palette = DEFAULT_PALETTE, perSheet = 6, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); useImperativeHandle(ref, () => btnRef.current!, []); const wrapRef = useRef(null); const canvasRef = useRef(null); const api = useRef<{ play: (e: MouseEvent) => void }>({ play: () => {} }); const { film, rim } = palette; useEffect(() => { const btn = btnRef.current, wrap = wrapRef.current, canvas = canvasRef.current; if (!btn || !wrap || !canvas) return; const ctx = canvas.getContext("2d"); if (!ctx) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let reduced = media.matches; let visible = true; let hover = btn.matches(":hover"); let raf = 0, last = 0; let rect: DOMRect | undefined; let scale = 1, dpr = 1; let dirty = true; let curX = -1e4, curY = -1e4; let cur = makeSheet(); let old: Sheet | null = null; let slideT0 = 0; let pops = 0; let sheetDone = false; let token = 0; let ticks: Tick[] = []; let queue: { at: number; fn: () => void }[] = []; const timers = new Set(); let bubbleSprites: HTMLCanvasElement[] = []; let builtFor = ""; let lift = 0, dip = 0, dipV = 0, rt0 = -1e6; const plate = wrap.querySelector(".tb-bubblewrap-plate"); const Q = 1.3; // sprite oversampling so the bulge stays sharp 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; const schedule = (fn: () => void, ms: number) => { if (reduced) later(fn, ms); else { queue.push({ at: performance.now() + ms, fn }); start(); } }; // ---- sprites (built once per dpr / sheet) ---- const mkCanvas = (size: number, h = size) => { const c = document.createElement("canvas"); c.width = Math.max(2, Math.ceil(size)); c.height = Math.max(2, Math.ceil(h)); return c; }; const PI2 = Math.PI * 2; const buildBubble = (v: number) => { const c = mkCanvas(SPR * 2 * dpr * Q); const g = c.getContext("2d"); if (!g) return c; g.scale(dpr * Q, dpr * Q); g.translate(SPR, SPR); g.save(); g.shadowColor = "rgba(0,0,0,.20)"; g.shadowBlur = 3 * dpr * Q; g.shadowOffsetY = 2 * dpr * Q; g.fillStyle = "rgba(0,0,0,.08)"; g.beginPath(); g.arc(0, 0, R - 0.5, 0, PI2); g.fill(); g.restore(); g.strokeStyle = "rgba(255,255,255,.16)"; g.lineWidth = 1.4; g.beginPath(); g.arc(0, 0, R + 1.8, 0, PI2); g.stroke(); const body = g.createRadialGradient(0, 0, 0, 0, 0, R); body.addColorStop(0, "rgba(214,224,236,.22)"); body.addColorStop(0.6, "rgba(214,224,236,.14)"); body.addColorStop(0.82, "rgba(226,234,244,.20)"); body.addColorStop(0.94, "rgba(240,245,251,.46)"); body.addColorStop(1, "rgba(246,249,253,.66)"); g.save(); g.beginPath(); g.arc(0, 0, R, 0, PI2); g.clip(); g.fillStyle = body; g.fillRect(-R - 2, -R - 2, R * 2 + 4, R * 2 + 4); const pool = g.createRadialGradient(2.5, 4.5, 0, 2.5, 4.5, 8.5); pool.addColorStop(0, "rgba(255,255,255,.30)"); pool.addColorStop(0.6, "rgba(255,255,255,.10)"); pool.addColorStop(1, "rgba(255,255,255,0)"); g.fillStyle = pool; g.fillRect(-R - 2, -R - 2, R * 2 + 4, R * 2 + 4); const rc = rng(101 + v * 37); g.lineJoin = "round"; g.lineCap = "round"; const cre: number[][] = []; for (let i = 0; i < 6; i++) { const ang = rc() * PI2, rad = rc() * (R - 4), a = rc() * PI2, len = 5 + rc() * 4; const x = Math.cos(ang) * rad, y = Math.sin(ang) * rad; const mx = x + Math.cos(a) * len / 2, my = y + Math.sin(a) * len / 2, a2 = a + (rc() - 0.5) * 0.9; cre.push([x, y, mx, my, mx + Math.cos(a2) * len / 2, my + Math.sin(a2) * len / 2]); } for (const [col, o, lw] of [["rgba(0,0,0,.14)", 0.5, 0.7], ["rgba(255,255,255,.24)", 0, 0.8]] as const) { g.strokeStyle = col; g.lineWidth = lw; g.beginPath(); for (const p of cre) { g.moveTo(p[0] + o, p[1] + o); g.lineTo(p[2] + o, p[3] + o); g.lineTo(p[4] + o, p[5] + o); } g.stroke(); } for (let i = 0; i < 3; i++) { const ang = rc() * PI2, rad = rc() * (R - 3), x = Math.cos(ang) * rad, y = Math.sin(ang) * rad; g.fillStyle = i % 2 ? "rgba(0,0,0,.07)" : "rgba(255,255,255,.09)"; g.beginPath(); g.moveTo(x, y); g.lineTo(x + (rc() - 0.5) * 10, y + (rc() - 0.5) * 10); g.lineTo(x + (rc() - 0.5) * 10, y + (rc() - 0.5) * 10); g.closePath(); g.fill(); } g.restore(); g.save(); g.strokeStyle = "rgba(255,255,255,.22)"; g.lineWidth = 3.2; g.shadowBlur = 3 * dpr * Q; g.shadowColor = "rgba(255,255,255,.45)"; g.beginPath(); g.arc(0, 0, R - 3, 0.25, 1.75); g.stroke(); g.restore(); g.strokeStyle = "rgba(255,255,255,.55)"; g.lineWidth = 0.8; g.beginPath(); g.arc(0, 0, R - 1.3, 0.3, 1.5); g.stroke(); g.fillStyle = "rgba(30,36,44,.12)"; g.beginPath(); g.arc(0, 0, R, 0, PI2); g.arc(-1.8, -1.8, R - 0.2, 0, PI2, true); g.fill("evenodd"); g.strokeStyle = "rgba(255,255,255,.38)"; g.lineWidth = 0.9; g.beginPath(); g.arc(0, 0, R - 0.5, 0, PI2); g.stroke(); g.strokeStyle = "rgba(255,255,255,.85)"; g.lineWidth = 1.3; g.lineCap = "round"; g.beginPath(); g.arc(0, 0, R - 0.6, 3.5, 5.6); g.stroke(); g.strokeStyle = "rgba(255,255,255,.30)"; g.lineWidth = 1.4; g.beginPath(); g.arc(0, 0, R - 4.2, 3.75, 4.55); g.stroke(); g.strokeStyle = "rgba(16,22,30,.26)"; g.lineWidth = 0.75; g.beginPath(); g.arc(0, 0, R + 0.4, 0, PI2); g.stroke(); const hl = g.createRadialGradient(-3.5, -4.5, 0, -3.5, -4.5, 8.5); hl.addColorStop(0, "rgba(255,255,255,.40)"); hl.addColorStop(1, "rgba(255,255,255,0)"); g.fillStyle = hl; g.beginPath(); g.arc(-3.5, -4.5, 8.5, 0, PI2); g.fill(); g.fillStyle = "rgba(255,255,255,.55)"; g.beginPath(); g.arc(5.2, 5.6, 1.1, 0, PI2); g.fill(); return c; }; const buildPopped = (cell: Cell) => { const c = mkCanvas(SPR * 2 * dpr * Q); const g = c.getContext("2d"); if (!g) return c; const r = rng(cell.seed); g.scale(dpr * Q, dpr * Q); g.translate(SPR, SPR); g.lineJoin = "round"; g.lineCap = "round"; g.fillStyle = "rgba(232,240,250,.16)"; g.strokeStyle = "rgba(255,255,255,.22)"; g.lineWidth = 0.7; g.beginPath(); for (let i = 0; i < 14; i++) { const a = (i / 14) * PI2 + (r() - 0.5) * 0.25, rr = R * (0.78 + r() * 0.2); if (i) g.lineTo(Math.cos(a) * rr, Math.sin(a) * rr); else g.moveTo(Math.cos(a) * rr, Math.sin(a) * rr); } g.closePath(); g.fill(); g.stroke(); g.strokeStyle = "rgba(16,22,30,.18)"; g.beginPath(); g.arc(0, 0, R + 0.4, 0, PI2); g.stroke(); const n = 9 + Math.floor(r() * 3); const paths: number[][] = []; for (let i = 0; i < n; i++) { const a = (i / n) * PI2 + r() * 0.6; const l0 = 0.5 + r() * 1.5, l1 = R * (0.55 + r() * 0.35), kt = 0.45 + r() * 0.15; const ak = a + (r() - 0.5) * 0.7, ae = ak + (r() - 0.5) * 0.7; paths.push([Math.cos(a) * l0, Math.sin(a) * l0, Math.cos(ak) * l1 * kt, Math.sin(ak) * l1 * kt, Math.cos(ae) * l1, Math.sin(ae) * l1]); } for (const [col, o, lw] of [["rgba(10,14,20,.40)", 0.5, 0.7], ["rgba(255,255,255,.70)", 0, 1.1]] as const) { g.strokeStyle = col; g.lineWidth = lw; g.beginPath(); for (const p of paths) { g.moveTo(p[0] + o, p[1] + o); g.lineTo(p[2] + o, p[3] + o); g.lineTo(p[4] + o, p[5] + o); } g.stroke(); } const fc = ["rgba(255,255,255,.16)", "rgba(0,0,0,.12)", "rgba(255,255,255,.08)"]; let fk = 0; for (let i = 0; i < n; i++) { if (i % 3 === 2) continue; g.fillStyle = fc[fk++ % 3]; const t0 = paths[i], t1 = paths[(i + 1) % n]; g.beginPath(); g.moveTo(0, 0); g.lineTo(t0[4], t0[5]); g.lineTo(t1[4], t1[5]); g.closePath(); g.fill(); } g.fillStyle = "rgba(255,255,255,.45)"; for (const k of [0, 3, 6]) { const p = paths[k % n], x = p[4], y = p[5]; g.beginPath(); g.moveTo(x + (r() - 0.5) * 4, y + (r() - 0.5) * 4); g.lineTo(x + (r() - 0.5) * 4, y + (r() - 0.5) * 4); g.lineTo(x + (r() - 0.5) * 4, y + (r() - 0.5) * 4); g.closePath(); g.fill(); } g.fillStyle = "rgba(255,255,255,.35)"; g.beginPath(); g.arc(0, 0, 1.6, 0, PI2); g.fill(); return c; }; let shadowSprite: HTMLCanvasElement | null = null, filmSprite: HTMLCanvasElement | null = null, flashSprite: HTMLCanvasElement | null = null; const buildSheetSprites = () => { const w = CW * dpr, h = CH * dpr; const sh = mkCanvas(w, h), sg = sh.getContext("2d"); if (sg) { sg.scale(dpr, dpr); sg.translate(PAD, PAD); sg.fillStyle = "#000"; for (const [col, bl, oy] of [["rgba(8,12,18,.30)", 14, 6], ["rgba(8,12,18,.22)", 3, 1.5]] as const) { sg.save(); sg.shadowColor = col; sg.shadowBlur = bl * dpr; sg.shadowOffsetY = oy * dpr; sg.shadowOffsetX = 2000 * dpr; stadiumPath(sg, -2000, 0, W, H); sg.fill(); sg.restore(); } } shadowSprite = sh; const fl = mkCanvas(w, h), g = fl.getContext("2d"); if (g) { g.scale(dpr, dpr); g.translate(PAD, PAD); const fg = g.createLinearGradient(0, 0, 0, H); fg.addColorStop(0, "rgba(226,234,244,.24)"); fg.addColorStop(0.45, "rgba(226,234,244,.14)"); fg.addColorStop(1, "rgba(226,234,244,.20)"); stadiumPath(g, 0, 0, W, H); g.fillStyle = fg; g.fill(); g.save(); g.clip(); const sg2 = g.createLinearGradient(40, 0, 110, H); sg2.addColorStop(0, "rgba(255,255,255,0)"); sg2.addColorStop(0.45, "rgba(255,255,255,.07)"); sg2.addColorStop(0.55, "rgba(255,255,255,.07)"); sg2.addColorStop(1, "rgba(255,255,255,0)"); g.fillStyle = sg2; g.fillRect(0, 0, W, H); g.restore(); g.save(); stadiumPath(g, 4, 4, W - 8, H - 8); g.clip(); const r = rng(7); { const gx = Math.floor((W + 10) / 10) + 1, gy = Math.floor((H + 10) / 10) + 1; const pts: number[][] = []; for (let j = 0; j < gy; j++) for (let i = 0; i < gx; i++) pts.push([-5 + i * 10 + (r() - 0.5) * 7, -5 + j * 10 + (r() - 0.5) * 7]); const tris: number[][][] = []; for (let j = 0; j < gy - 1; j++) for (let i = 0; i < gx - 1; i++) { const a = pts[j * gx + i], b = pts[j * gx + i + 1], c = pts[(j + 1) * gx + i], d = pts[(j + 1) * gx + i + 1]; if ((i + j) % 2) tris.push([a, b, d], [a, d, c]); else tris.push([a, b, c], [b, d, c]); } for (const t of tris) { const v = r(); g.fillStyle = v < 0.5 ? `rgba(255,255,255,${(0.02 + v * 0.10).toFixed(3)})` : `rgba(0,0,0,${(0.02 + (v - 0.5) * 0.12).toFixed(3)})`; g.beginPath(); g.moveTo(t[0][0], t[0][1]); g.lineTo(t[1][0], t[1][1]); g.lineTo(t[2][0], t[2][1]); g.closePath(); g.fill(); } g.strokeStyle = "rgba(255,255,255,.06)"; g.lineWidth = 0.5; g.beginPath(); for (const t of tris) { g.moveTo(t[0][0], t[0][1]); g.lineTo(t[1][0], t[1][1]); g.lineTo(t[2][0], t[2][1]); g.closePath(); } g.stroke(); } const cr: number[][] = []; for (let i = 0; i < 40; i++) { const x = r() * W, y = r() * H, a = r() * PI2, len = 4 + r() * 7, a2 = a + (r() - 0.5) * 0.8; const mx = x + Math.cos(a) * len / 2, my = y + Math.sin(a) * len / 2; cr.push([x, y, mx, my, mx + Math.cos(a2) * len / 2, my + Math.sin(a2) * len / 2]); } g.lineJoin = "round"; g.lineCap = "round"; g.lineWidth = 0.6; for (const [col, o] of [["rgba(0,0,0,.14)", 0.5], ["rgba(255,255,255,.16)", 0]] as const) { g.strokeStyle = col; g.beginPath(); for (const p of cr) { g.moveTo(p[0] + o, p[1] + o); g.lineTo(p[2] + o, p[3] + o); g.lineTo(p[4] + o, p[5] + o); } g.stroke(); } g.restore(); g.strokeStyle = "rgba(16,22,30,.28)"; g.lineWidth = 0.8; stadiumPath(g, 0.4, 0.4, W - 0.8, H - 0.8); g.stroke(); const rg = g.createLinearGradient(0, 0, W, H); rg.addColorStop(0, rim); rg.addColorStop(0.35, "rgba(255,255,255,.34)"); rg.addColorStop(0.7, "rgba(255,255,255,.16)"); rg.addColorStop(1, "rgba(255,255,255,.42)"); g.strokeStyle = rg; g.lineWidth = 1.8; stadiumPath(g, 1.2, 1.2, W - 2.4, H - 2.4); g.stroke(); g.strokeStyle = "rgba(255,255,255,.05)"; g.lineWidth = 3; stadiumPath(g, 3, 3, W - 6, H - 6); g.stroke(); g.lineCap = "round"; g.strokeStyle = "rgba(255,255,255,.92)"; g.lineWidth = 1.8; g.beginPath(); g.arc(48, 48, H / 2 - 1.2, 3.55, 4.45); g.stroke(); g.strokeStyle = "rgba(255,255,255,.50)"; g.lineWidth = 1.3; g.beginPath(); g.arc(192, 48, H / 2 - 1.2, 0.35, 0.9); g.stroke(); } filmSprite = fl; const fs = mkCanvas(R * 2 * dpr), fx = fs.getContext("2d"); if (fx) { const m = fs.width / 2, gr = fx.createRadialGradient(m, m, 0, m, m, m); gr.addColorStop(0, "rgba(255,255,255,1)"); gr.addColorStop(1, "rgba(255,255,255,0)"); fx.fillStyle = gr; fx.fillRect(0, 0, fs.width, fs.width); } flashSprite = fs; }; const buildSheet = (s: Sheet) => { for (const c of s.cells) c.sprite = buildPopped(c); }; const fit = () => { rect = btn.getBoundingClientRect(); scale = rect.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); if (canvas.width !== w || canvas.height !== h) { canvas.width = w; canvas.height = h; } if (!bubbleSprites.length || nd !== dpr || builtFor !== rim) { dpr = nd; builtFor = rim; bubbleSprites = [0, 1, 2, 3].map(buildBubble); buildSheetSprites(); buildSheet(cur); if (old) buildSheet(old); } dpr = nd; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); dirty = false; }; // ---- actions ---- const nearest = (x: number, y: number) => { let best: Cell | null = null, bd = 1e9; for (const c of cur.cells) { if (c.popped) continue; const d = Math.hypot(c.x + cur.ox - x, c.y - y); if (d < bd) { bd = d; best = c; } } return best; }; const popCell = (c: Cell, now: number, flash: boolean) => { c.popped = true; c.sc = 1; c.popAt = 0; c.inflate = 0; c.popT = reduced ? 0 : now; if (!reduced) { if (flash) { const out = Math.atan2(c.y - 48, c.x - 120); for (let i = 0; i < 5; i++) ticks.push({ x: c.x, y: c.y, a: out + (i - 2) * 0.62 + ((c.seed % 7) - 3) * 0.03, len: 7 + ((i * 5 + c.seed) % 4), t0: now, first: i === 0 }); } for (const o of cur.cells) { if (o === c) continue; const dx = o.x - c.x, dy = o.y - c.y, d = Math.hypot(dx, dy); if (d < 32 && d > 0) { o.jvx += (dx / d) * 28; o.jvy += (dy / d) * 28; } } dipV += 60; } }; const refresh = () => { if (reduced) { old = null; cur = makeSheet(); buildSheet(cur); pops = 0; sheetDone = false; draw(performance.now()); return; } old = cur; cur = makeSheet(); buildSheet(cur); cur.ox = SLIDE; slideT0 = performance.now(); pops = 0; sheetDone = false; start(); }; const reinflate = (tk: number) => { if (tk !== token || sheetDone) return; const now = performance.now(); const popped = cur.cells.filter((c) => c.popped); if (reduced) { popped.forEach((c) => { c.popped = false; c.popT = 0; }); pops = 0; draw(now); return; } popped.forEach((c, i) => { c.inflate = now + i * 60 + 1; }); pops = 0; start(); }; api.current.play = (e) => { if (disabled) return; const now = performance.now(); if (dirty) fit(); let target: Cell | null = null; if (e.detail === 0 || !rect) target = cur.cells.find((c) => !c.popped) ?? null; else target = nearest((e.clientX - rect.left) / scale, (e.clientY - rect.top) / scale); if (!target) return; token++; pops++; const intact = cur.cells.filter((c) => !c.popped).length - 1; const finish = !sheetDone && (pops % Math.max(1, perSheet) === 0 || intact < 4); if (reduced) { popCell(target, now, false); draw(now); } else { target.squashT0 = now; target.popAt = now + 40; } let lastPopMs = 40; if (finish) { sheetDone = true; const rest = cur.cells.filter((c) => !c.popped && c !== target).sort((a, b) => Math.hypot(a.x - target.x, a.y - target.y) - Math.hypot(b.x - target.x, b.y - target.y)); rest.forEach((c, i) => { const ms = reduced ? 0 : 40 + (i + 1) * 25; lastPopMs = Math.max(lastPopMs, ms); if (reduced) popCell(c, now, false); else schedule(() => { if (!c.popped) popCell(c, performance.now(), true); }, ms); }); if (reduced) draw(now); schedule(refresh, lastPopMs + 700); } else { const tk = token; schedule(() => reinflate(tk), 2400); } start(); }; // ---- drawing ---- const drawSheet = (s: Sheet, now: number, active: boolean) => { const oy = active ? lift + dip : 0; if (shadowSprite) ctx.drawImage(shadowSprite, s.ox, 0, CW, CH); if (filmSprite) ctx.drawImage(filmSprite, s.ox, oy, CW, CH); ctx.save(); ctx.translate(PAD + s.ox, PAD + oy); const near = active && hover && !reduced ? nearest(curX0(), curY0()) : null; const specs: number[] = []; const sz = SPR * 2; for (const c of s.cells) { const px = c.x + c.jx, py = c.y + c.jy; let sx = c.sc, sy = c.sc; let showPopped = c.popped; if (c.inflate && now >= c.inflate) { const u = (now - c.inflate) / 180; const k = outBack(u); sx = sy = 0.5 + 0.5 * k; showPopped = u < 0.3; } if (c.popAt) { const u = outQuad((now - c.squashT0) / 40); sx = 1 + 0.18 * u; sy = 1 - 0.22 * u; } if (showPopped) { const u = c.popT ? (now - c.popT) / 160 : 1; const f = u < 1 ? 0.94 + 0.21 * (1 - outCubic(u)) : 0.94; sx *= f; sy *= f; } sx *= c.vs; sy *= c.vs; ctx.save(); ctx.translate(px, py); ctx.scale(sx, sy); const bs = bubbleSprites[c.seed % 4]; const spr = showPopped ? c.sprite : bs; if (spr) ctx.drawImage(spr, -sz / 2, -sz / 2, sz, sz); if (!showPopped) { if (c.popAt) { ctx.strokeStyle = "#fff"; ctx.lineWidth = 1.2; ctx.beginPath(); ctx.arc(0, 0, R - 0.5, 0, Math.PI * 2); ctx.stroke(); } else if (c === near && bs) { ctx.globalCompositeOperation = "lighter"; ctx.globalAlpha = 0.22; ctx.drawImage(bs, -sz / 2, -sz / 2, sz, sz); ctx.globalCompositeOperation = "source-over"; ctx.globalAlpha = 1; } let sh = 0, sv = 0; if (active && hover && !reduced) { const dx = curX0() - c.x, dy = curY0() - c.y, d = Math.hypot(dx, dy); if (d < 52 && d > 0.1) { sh = (dx / d) * 1.5; sv = (dy / d) * 1.5; } } const k2 = c === near ? 1.2 : 1; specs.push(px + (-4.2 + sh) * sx, py + (-5.2 + sv) * sy, sx * k2, sy * k2, -0.62 + (c.vs - 1) * 3); } ctx.restore(); } ctx.fillStyle = "rgba(255,255,255,.92)"; ctx.beginPath(); for (let i = 0; i < specs.length; i += 5) { ctx.moveTo(specs[i] + 3.0 * specs[i + 2], specs[i + 1]); ctx.ellipse(specs[i], specs[i + 1], 3.0 * specs[i + 2], 1.7 * specs[i + 3], specs[i + 4], 0, Math.PI * 2); } ctx.fill(); ctx.fillStyle = "rgba(255,255,255,.55)"; ctx.beginPath(); for (let i = 0; i < specs.length; i += 5) { const ex = specs[i] + 2.6 * specs[i + 2], ey = specs[i + 1] + 2.4 * specs[i + 3]; ctx.moveTo(ex + 1.1 * specs[i + 2], ey); ctx.ellipse(ex, ey, 1.1 * specs[i + 2], 0.7 * specs[i + 3], -0.62, 0, Math.PI * 2); } ctx.fill(); ctx.restore(); }; const curX0 = () => (rect ? (curX - rect.left) / scale : -1e4); const curY0 = () => (rect ? (curY - rect.top) / scale : -1e4); const draw = (now: number) => { ctx.clearRect(0, 0, CW, CH); if (old) drawSheet(old, now, false); drawSheet(cur, now, true); const ox0 = PAD + cur.ox, oy0 = PAD + lift + dip; if (!reduced) { const rtT = now - rt0; if (rtT >= 0 && rtT < 700) { const al = rtT < 160 ? outCubic(rtT / 160) : rtT < 420 ? 1 : 1 - (rtT - 420) / 280; const dr = 2 * clamp01(rtT / 700); ctx.strokeStyle = `rgba(170,180,194,${(0.75 * al).toFixed(3)})`; ctx.lineWidth = 1.5; ctx.lineCap = "round"; ctx.beginPath(); for (const [cx, angs] of [[48, [-120, -135, -150]], [192, [-60, -45, -30]]] as const) { for (const dg of angs) { const a = (dg * Math.PI) / 180, r0 = H / 2 + 7 + dr, r1 = r0 + 5; ctx.moveTo(ox0 + cx + Math.cos(a) * r0, oy0 + 48 + Math.sin(a) * r0); ctx.lineTo(ox0 + cx + Math.cos(a) * r1, oy0 + 48 + Math.sin(a) * r1); } } ctx.stroke(); } } for (const t of ticks) { const tt = now - t.t0, ox = ox0 + t.x, oy = oy0 + t.y; const u = tt / 200; if (u <= 1) { const dist = 10 * outQuart(u), al = u < 0.35 ? 1 : 1 - (u - 0.35) / 0.65; const b0 = R + 3 + dist, b1 = b0 + t.len, cs = Math.cos(t.a), sn = Math.sin(t.a), hw = 1.5; const tri = (dy: number) => { ctx.beginPath(); ctx.moveTo(ox + cs * b0 - sn * hw, oy + sn * b0 + cs * hw + dy); ctx.lineTo(ox + cs * b0 + sn * hw, oy + sn * b0 - cs * hw + dy); ctx.lineTo(ox + cs * b1, oy + sn * b1 + dy); ctx.closePath(); ctx.fill(); }; ctx.globalAlpha = al; ctx.fillStyle = "rgba(16,22,30,.25)"; tri(0.8); ctx.fillStyle = "#ffffff"; tri(0); ctx.globalAlpha = 1; } if (t.first) { if (flashSprite && tt < 90) { ctx.globalAlpha = 0.5 * (1 - tt / 90); ctx.drawImage(flashSprite, ox - R * 0.85, oy - R * 0.85, R * 1.7, R * 1.7); ctx.globalAlpha = 1; } const ru = tt / 240; if (ru <= 1) { ctx.strokeStyle = `rgba(255,255,255,${(0.55 * (1 - ru)).toFixed(3)})`; ctx.lineWidth = 1.4 - 1.1 * ru; ctx.beginPath(); ctx.arc(ox, oy, R + 10 * outCubic(ru), 0, Math.PI * 2); ctx.stroke(); } } } if (plate) plate.style.transform = reduced || (!lift && !dip) ? "" : `translateY(${(lift + dip).toFixed(2)}px)`; }; 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; // queued events 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; } // slide if (old) { const t = now - slideT0; old.ox = -SLIDE * Math.pow(clamp01(t / 420), 3); cur.ox = SLIDE * (1 - outCubic(t / 520)); if (t >= 520) { old = null; cur.ox = 0; } else moving = true; } const near = hover ? nearest(curX0(), curY0()) : null; const k = 1 - Math.exp(-dt / 0.09); for (const c of cur.cells) { if (c.popAt) { moving = true; if (now >= c.popAt) popCell(c, now, true); } if (c.inflate) { moving = true; if (now - c.inflate >= 180) { c.popped = false; c.inflate = 0; c.popT = 0; } } let tgt = 1; if (hover && !c.popped) { tgt = 1.035; const d = Math.hypot(c.x - curX0(), c.y - curY0()); if (d < 52) tgt += 0.16 * Math.pow(1 - d / 52, 2); if (c === near) tgt += 0.08; } if (Math.abs(tgt - c.sc) > 0.0015) { c.sc += (tgt - c.sc) * k; moving = true; } else c.sc = tgt; if (c.jx || c.jy || c.jvx || c.jvy) { c.jvx += (-300 * c.jx - 18 * c.jvx) * dt; c.jvy += (-300 * c.jy - 18 * c.jvy) * dt; c.jx += c.jvx * dt; c.jy += c.jvy * dt; if (Math.abs(c.jx) + Math.abs(c.jy) + Math.abs(c.jvx) + Math.abs(c.jvy) < 0.02) { c.jx = c.jy = c.jvx = c.jvy = 0; } else moving = true; } } if (ticks.length) { ticks = ticks.filter((t) => now - t.t0 < 260); if (ticks.length) moving = true; } const lt = hover ? -2 : 0; if (Math.abs(lt - lift) > 0.01) { lift += (lt - lift) * (1 - Math.exp(-dt / 0.14)); moving = true; } else lift = lt; if (Math.abs(dip) + Math.abs(dipV) > 0.01) { dipV += (-260 * dip - 16 * dipV) * dt; dip += dipV * dt; moving = true; } else { dip = 0; dipV = 0; } if (now - rt0 < 700) moving = true; draw(now); if (moving) raf = requestAnimationFrame(tick); }; function 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 = (e: PointerEvent) => { if (disabled) return; hover = true; dirty = true; if (!reduced) rt0 = performance.now(); curX = e.clientX; curY = e.clientY; wrap.dataset.tbState = "hover"; start(); }; const leave = () => { hover = false; wrap.dataset.tbState = "rest"; start(); if (!raf) draw(performance.now()); }; const move = (e: PointerEvent) => { curX = e.clientX; curY = e.clientY; if (hover) start(); }; const onMedia = () => { reduced = media.matches; queue = []; ticks = []; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); old = null; cur.ox = 0; cur.cells.forEach((c) => { c.popped = false; c.sc = 1; c.popAt = 0; c.inflate = 0; c.popT = 0; }); pops = 0; sheetDone = false; 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("pointermove", move, { passive: true }); 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); fit(); wrap.dataset.tbState = hover && !disabled ? "hover" : "rest"; pause(); return () => { api.current.play = () => {}; cancelAnimationFrame(raf); timers.forEach((id) => window.clearTimeout(id)); timers.clear(); queue = []; io?.disconnect(); ro?.disconnect(); window.removeEventListener("pointermove", move); 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, film, rim, perSheet]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; api.current.play(e); }; return ( ); } export default BubbleWrapButton; const css = ` .tb-bubblewrap-wrap{display:inline-block;position:relative;width:${W}px;height:${H}px;vertical-align:middle} .tb-bubblewrap-btn{position:relative;display:block;width:${W}px;height:${H}px;padding:0;margin:0;border:0;border-radius:${H / 2}px;background:none;cursor:pointer;overflow:visible;-webkit-tap-highlight-color:transparent;font:inherit} .tb-bubblewrap-btn:focus-visible{outline:3px solid ${ACCENT};outline-offset:5px} .tb-bubblewrap-canvas{position:absolute;left:-${PAD}px;top:-${PAD}px;width:${CW}px;height:${CH}px;max-width:none;pointer-events:none} .tb-bubblewrap-plate{position:absolute;left:50%;top:50%;width:${PLATE_W}px;height:${PLATE_H}px;margin:-${PLATE_H / 2}px 0 0 -${PLATE_W / 2}px;border-radius:16px;display:flex;align-items:center;justify-content:center; background:linear-gradient(180deg,rgba(255,255,255,.22),rgba(255,255,255,.08) 45%,rgba(255,255,255,0) 70%,rgba(0,0,0,.10)),var(--tb-bubblewrap-plate);border:0; box-shadow:inset 0 1px 0 rgba(255,255,255,.42),inset 0 0 6px rgba(255,255,255,.06),inset 0 -1px 0 rgba(0,0,0,.30),0 0 0 1px rgba(255,255,255,.10),0 0 0 2.5px rgba(16,22,30,.22),0 1px 2px rgba(0,0,0,.25); box-sizing:border-box;text-transform:uppercase;letter-spacing:.08em;padding-left:.08em;pointer-events:none;will-change:transform; font:800 21px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;color:var(--tb-bubblewrap-ink);text-shadow:0 1px 0 rgba(0,0,0,.55),0 0 1px rgba(0,0,0,.55),0 2px 6px rgba(0,0,0,.30)} .tb-bubblewrap-btn:disabled{cursor:not-allowed;opacity:.6;filter:saturate(.5)} .tb-bubblewrap-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-bubblewrap-wrap *,.tb-bubblewrap-wrap *::before,.tb-bubblewrap-wrap *::after{animation:none!important;transition:none!important}} `;