From 4dc482c89ae485b8c5da6c70ac67f7ad87a85ec8 Mon Sep 17 00:00:00 2001 From: Benji Greig Date: Mon, 5 Oct 2026 10:06:11 +1100 Subject: [PATCH] feat: visual refresh - gradient wheel, merged spin button, confetti and shareable winner card - Render each wedge as a radial gradient (lighter near the hub, full accent colour at the rim) instead of a flat fill. - Replace the separate centre hub and edge pointer with a single merged SVG hub button - the circle and its flag/pointer are one continuous outlined shape, still spins the wheel on click. - Celebrate the result with a confetti burst and an animated, dismissible winner card. - Add a "Copy image" action that renders a shareable PNG card of the winner (wheel snapshot + big name), copying it to the clipboard with a download fallback when the Clipboard API isn't available. Generated with Claude --- index.html | 23 +++- src/main.ts | 297 ++++++++++++++++++++++++++++++++++++++++++++------ src/style.css | 190 ++++++++++++++++++++++++++++---- 3 files changed, 452 insertions(+), 58 deletions(-) diff --git a/index.html b/index.html index 470ad74..e525bb1 100644 --- a/index.html +++ b/index.html @@ -16,16 +16,33 @@
- -
+
-

+
+ +

Winner!

+

+
+ +

+
+
+ +

Edit options

diff --git a/src/main.ts b/src/main.ts index 9fd67ed..b14f9e5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -47,7 +47,14 @@ const ctx = canvas.getContext("2d")!; const hubBtn = document.querySelector("#hub-btn")!; const spinBtn = document.querySelector("#spin-btn")!; const editBtn = document.querySelector("#edit-btn")!; -const winnerEl = document.querySelector("#winner")!; +const winnerEl = document.querySelector("#winner")!; +const winnerNameEl = + document.querySelector("#winner-name")!; +const winnerCloseBtn = + document.querySelector("#winner-close")!; +const copyBtn = document.querySelector("#copy-btn")!; +const copyStatusEl = + document.querySelector("#copy-status")!; const editDialog = document.querySelector("#edit-dialog")!; const optionsInput = document.querySelector("#options-input")!; @@ -62,10 +69,15 @@ function resizeCanvas(): void { syncHubButton(size); } +// The hub is a square box exactly matching its SVG circle (the flag renders +// as pure overflow outside that box), so sizing it keeps the circle itself +// centered on the wheel. Clamped so it stays legible on small wheels and +// doesn't balloon on large ones. function syncHubButton(size: number): void { - const hubSize = size * 0.1; + const hubSize = Math.min(160, Math.max(80, size * 0.22)); hubBtn.style.width = `${hubSize}px`; hubBtn.style.height = `${hubSize}px`; + hubBtn.style.fontSize = `${hubSize * 0.2}px`; } resizeCanvas(); @@ -76,6 +88,7 @@ new ResizeObserver(resizeCanvas).observe(document.body); let rotation = Math.random() * 2 * Math.PI; let velocity = 0; // radians per frame let spinning = false; +let lastWinner = ""; function setButtonsDisabled(disabled: boolean): void { hubBtn.disabled = disabled; @@ -97,7 +110,8 @@ function triggerSpin(): void { if (spinning) return; spinning = true; velocity = INITIAL_SPEED; - winnerEl.textContent = ""; + winnerEl.classList.remove("show"); + copyStatusEl.textContent = ""; setButtonsDisabled(true); } @@ -169,13 +183,31 @@ if (new URLSearchParams(window.location.search).has("edit")) { // --- Drawing ----------------------------------------------------------------- -function drawWheel(angleOffset: number): void { - const size = canvas.width; +// Mixes a hex colour toward white by `amount` (0-1) for the gradient's +// hub-facing stop. +function lighten(hex: string, amount: number): string { + const r = parseInt(hex.slice(1, 3), 16); + const g = parseInt(hex.slice(3, 5), 16); + const b = parseInt(hex.slice(5, 7), 16); + const mix = (channel: number) => + Math.round(channel + (255 - channel) * amount); + return `rgb(${mix(r)}, ${mix(g)}, ${mix(b)})`; +} + +function drawWheel( + targetCtx: CanvasRenderingContext2D, + size: number, + angleOffset: number, + clearBackground = true, +): void { const cx = size / 2; const cy = size / 2; const radius = size / 2 - 8; - ctx.clearRect(0, 0, size, size); + if (clearBackground) { + targetCtx.clearRect(0, 0, size, size); + } + if (radius <= 0) return; const sliceAngle = (2 * Math.PI) / items.length; @@ -187,18 +219,22 @@ function drawWheel(angleOffset: number): void { Math.floor(i / COLORS.length) * Math.round(COLORS.length / 2); const color = COLORS[(i + modifier) % COLORS.length]; - // --- Wedge fill --- - ctx.beginPath(); - ctx.moveTo(cx, cy); - ctx.arc(cx, cy, radius, startAngle, endAngle); - ctx.closePath(); - ctx.fillStyle = color; - ctx.fill(); + // --- Wedge fill (gradient: lighter near the hub, full accent at the rim) --- + const gradient = targetCtx.createRadialGradient(cx, cy, 0, cx, cy, radius); + gradient.addColorStop(0, lighten(color, 0.45)); + gradient.addColorStop(1, color); + + targetCtx.beginPath(); + targetCtx.moveTo(cx, cy); + targetCtx.arc(cx, cy, radius, startAngle, endAngle); + targetCtx.closePath(); + targetCtx.fillStyle = gradient; + targetCtx.fill(); // --- Wedge border --- - ctx.strokeStyle = "rgba(0,0,0,0.35)"; - ctx.lineWidth = 2; - ctx.stroke(); + targetCtx.strokeStyle = "rgba(0,0,0,0.35)"; + targetCtx.lineWidth = 2; + targetCtx.stroke(); // --- Label --- const labelAngle = startAngle + sliceAngle / 2; @@ -206,27 +242,218 @@ function drawWheel(angleOffset: number): void { const lx = cx + Math.cos(labelAngle) * labelRadius; const ly = cy + Math.sin(labelAngle) * labelRadius; - ctx.save(); - ctx.translate(lx, ly); - ctx.rotate(labelAngle); + targetCtx.save(); + targetCtx.translate(lx, ly); + targetCtx.rotate(labelAngle); - ctx.font = `bold ${Math.max(11, Math.round(size / 28))}px Inter, system-ui, sans-serif`; - ctx.textAlign = "center"; - ctx.textBaseline = "middle"; - ctx.fillStyle = "#11111b"; // Catppuccin Mocha "Crust" - ctx.fillText(items[i], 0, 0); + targetCtx.font = `bold ${Math.max(11, Math.round(size / 28))}px Inter, system-ui, sans-serif`; + targetCtx.textAlign = "center"; + targetCtx.textBaseline = "middle"; + targetCtx.fillStyle = "#11111b"; // Catppuccin Mocha "Crust" + targetCtx.fillText(items[i], 0, 0); - ctx.restore(); + targetCtx.restore(); } // --- Outer ring --- - ctx.beginPath(); - ctx.arc(cx, cy, radius, 0, 2 * Math.PI); - ctx.strokeStyle = "rgba(255,255,255,0.2)"; - ctx.lineWidth = 4; - ctx.stroke(); + targetCtx.beginPath(); + targetCtx.arc(cx, cy, radius, 0, 2 * Math.PI); + targetCtx.strokeStyle = "rgba(255,255,255,0.2)"; + targetCtx.lineWidth = 4; + targetCtx.stroke(); +} + +// Draws the same merged circle+flag hub shape as the live app's SVG button +// (see index.html's #hub-btn path), minus the "SPIN" text — used for the +// static share card, where the wheel has already stopped. +function drawHubShape( + targetCtx: CanvasRenderingContext2D, + cx: number, + cy: number, + radius: number, +): void { + const halfAngle = (24 * Math.PI) / 180; + const flagLength = radius * 0.5; + const p1x = cx + radius * Math.cos(-halfAngle); + const p1y = cy + radius * Math.sin(-halfAngle); + const apexX = cx + radius + flagLength; + + targetCtx.beginPath(); + targetCtx.moveTo(p1x, p1y); + targetCtx.arc(cx, cy, radius, -halfAngle, halfAngle, true); + targetCtx.lineTo(apexX, cy); + targetCtx.closePath(); + + targetCtx.fillStyle = "#11111b"; + targetCtx.fill(); + targetCtx.lineJoin = "round"; + targetCtx.lineWidth = radius * 0.14; + targetCtx.strokeStyle = "#ffffff"; + targetCtx.stroke(); +} + +// --- Confetti ------------------------------------------------------------ + +interface ConfettiParticle { + x: number; + y: number; + vx: number; + vy: number; + rotation: number; + spin: number; + size: number; + color: string; +} + +const CONFETTI_DURATION = 1500; // ms +const CONFETTI_COUNT = 550; +const CONFETTI_GRAVITY = 0.18; + +const confettiCanvas = + document.querySelector("#confetti-canvas")!; +const confettiCtx = confettiCanvas.getContext("2d")!; +let confettiParticles: ConfettiParticle[] = []; +let confettiRafId: number | null = null; +let confettiStart = 0; + +function fireConfetti(): void { + const rect = canvas.getBoundingClientRect(); + const originX = rect.left + rect.width / 2; + const originY = rect.top + rect.height / 2; + + confettiCanvas.width = window.innerWidth; + confettiCanvas.height = window.innerHeight; + + confettiParticles = Array.from({ length: CONFETTI_COUNT }, () => { + const angle = Math.random() * 2 * Math.PI; + const speed = 4 + Math.random() * 7; + return { + x: originX, + y: originY, + vx: Math.cos(angle) * speed, + vy: Math.sin(angle) * speed - 4, + rotation: Math.random() * 2 * Math.PI, + spin: (Math.random() - 0.5) * 0.4, + size: 6 + Math.random() * 6, + color: COLORS[Math.floor(Math.random() * COLORS.length)], + }; + }); + + confettiStart = performance.now(); + if (confettiRafId === null) { + confettiRafId = requestAnimationFrame(confettiTick); + } +} + +function confettiTick(now: number): void { + const elapsed = now - confettiStart; + confettiCtx.clearRect(0, 0, confettiCanvas.width, confettiCanvas.height); + + if (elapsed >= CONFETTI_DURATION) { + confettiParticles = []; + confettiRafId = null; + return; + } + + const fade = 1 - elapsed / CONFETTI_DURATION; + + for (const p of confettiParticles) { + p.vy += CONFETTI_GRAVITY; + p.x += p.vx; + p.y += p.vy; + p.rotation += p.spin; + + confettiCtx.save(); + confettiCtx.globalAlpha = Math.max(fade, 0); + confettiCtx.translate(p.x, p.y); + confettiCtx.rotate(p.rotation); + confettiCtx.fillStyle = p.color; + confettiCtx.fillRect(-p.size / 2, -p.size / 4, p.size, p.size / 2); + confettiCtx.restore(); + } + + confettiRafId = requestAnimationFrame(confettiTick); +} + +// --- Shareable winner card ------------------------------------------------- + +function buildShareCard(winnerName: string): HTMLCanvasElement { + const width = 800; + const pad = 28; + const wheelSize = width - pad * 2; + const textGap = 20; + const labelHeight = 34; + const nameHeight = 128; + const bottomPad = 32; + const height = + pad + wheelSize + textGap + labelHeight + nameHeight + bottomPad; + + const card = document.createElement("canvas"); + card.width = width; + card.height = height; + const cardCtx = card.getContext("2d")!; + + const bg = cardCtx.createLinearGradient(0, 0, width, height); + bg.addColorStop(0, "#1e1e2e"); + bg.addColorStop(1, "#11111b"); + cardCtx.fillStyle = bg; + cardCtx.fillRect(0, 0, width, height); + + cardCtx.save(); + cardCtx.translate(pad, pad); + drawWheel(cardCtx, wheelSize, rotation, false); + drawHubShape(cardCtx, wheelSize / 2, wheelSize / 2, wheelSize * 0.11); + cardCtx.restore(); + + cardCtx.textAlign = "center"; + let textY = pad + wheelSize + textGap + labelHeight * 0.7; + + cardCtx.fillStyle = "#a6adc8"; + cardCtx.font = "700 24px Inter, system-ui, sans-serif"; + cardCtx.fillText("WINNER", width / 2, textY); + + textY += nameHeight * 0.75; + cardCtx.fillStyle = "#cba6f7"; + cardCtx.font = "bold 104px Inter, system-ui, sans-serif"; + cardCtx.fillText(`🎉 ${winnerName}`, width / 2, textY, width - pad * 2); + + return card; } +winnerCloseBtn.addEventListener("click", () => { + winnerEl.classList.remove("show"); +}); + +copyBtn.addEventListener("click", () => { + const card = buildShareCard(lastWinner); + + card.toBlob(async (blob) => { + if (!blob) return; + + try { + if (!navigator.clipboard || typeof ClipboardItem === "undefined") { + throw new Error("Clipboard API unavailable"); + } + await navigator.clipboard.write([ + new ClipboardItem({ "image/png": blob }), + ]); + copyStatusEl.textContent = "Copied!"; + } catch { + const url = URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = `${lastWinner || "winner"}.png`; + link.click(); + URL.revokeObjectURL(url); + copyStatusEl.textContent = "Clipboard unavailable — downloaded instead."; + } + + setTimeout(() => { + copyStatusEl.textContent = ""; + }, 2500); + }, "image/png"); +}); + // --- Animation loop ---------------------------------------------------------- function tick(): void { @@ -238,11 +465,17 @@ function tick(): void { velocity = 0; spinning = false; setButtonsDisabled(false); - winnerEl.textContent = `The winner is... ${getWinner()} 🎉`; + + lastWinner = getWinner(); + winnerNameEl.textContent = `🎉 ${lastWinner}`; + winnerEl.classList.remove("show"); + void winnerEl.offsetWidth; // reflow so the pop-in animation restarts + winnerEl.classList.add("show"); + fireConfetti(); } } - drawWheel(rotation); + drawWheel(ctx, canvas.width, rotation); requestAnimationFrame(tick); } diff --git a/src/style.css b/src/style.css index c7767ca..b936b83 100644 --- a/src/style.css +++ b/src/style.css @@ -21,6 +21,7 @@ body { justify-content: center; min-height: 100svh; min-width: 320px; + overflow-x: hidden; font-family: Inter, system-ui, sans-serif; } @@ -44,22 +45,52 @@ canvas { #hub-btn { position: absolute; - border-radius: 50%; - border: 3px solid rgba(205, 214, 244, 0.4); - background: #cdd6f4; + display: flex; + align-items: center; + justify-content: center; + border: none; + background: none; + padding: 0; cursor: pointer; + filter: drop-shadow(0 4px 14px rgba(17, 17, 27, 0.6)); transition: - background 0.2s, - border-color 0.2s, - transform 0.1s; + transform 0.1s, + filter 0.2s; + + /* The circle and its flag/pointer (toward 3 o'clock) are one continuous + SVG path with a single stroke, so there's no seam where they meet. + The flag intentionally extends past the viewBox — overflow: visible + lets it render without affecting the button's own (square) box, which + keeps the circle itself centered on the wheel. */ + svg { + position: absolute; + inset: 0; + width: 100%; + height: 100%; + overflow: visible; + } + + path { + fill: #11111b; + stroke: #ffffff; + stroke-width: 6; + } + + span { + position: relative; + font-weight: 800; + letter-spacing: 0.06em; + color: #ffffff; + pointer-events: none; + } &:hover { - background: #cba6f7; - border-color: rgba(203, 166, 247, 0.8); + filter: drop-shadow(0 4px 14px rgba(17, 17, 27, 0.6)) + drop-shadow(0 0 10px rgba(255, 255, 255, 0.4)); } &:active { - transform: scale(0.92); + transform: scale(0.94); } &:disabled { @@ -242,23 +273,136 @@ canvas { } } -#pointer { +#winner { + display: none; + position: fixed; + left: 50%; + bottom: max(24px, 4vh); + z-index: 60; + flex-direction: column; + align-items: center; + gap: 8px; + min-width: 260px; + padding: 28px 28px 20px; + border-radius: 20px; + background: linear-gradient( + 160deg, + rgba(49, 50, 68, 0.95), + rgba(24, 24, 37, 0.95) + ); + border: 1px solid rgba(203, 166, 247, 0.25); + box-shadow: 0 12px 40px rgba(17, 17, 27, 0.5); + + &.show { + display: flex; + animation: winner-pop 0.55s cubic-bezier(0.34, 1.56, 0.64, 1) forwards; + } +} + +#winner-close { position: absolute; - right: 0; - transform: translateX(50%); - width: 0; - height: 0; - border-top: 12px solid transparent; - border-bottom: 12px solid transparent; - border-right: 22px solid #cdd6f4; - filter: drop-shadow(-2px 0 4px rgba(17, 17, 27, 0.5)); + top: 8px; + right: 10px; + width: 26px; + height: 26px; + display: flex; + align-items: center; + justify-content: center; + border: none; + border-radius: 50%; + background: transparent; + color: #a6adc8; + font-size: 18px; + line-height: 1; + cursor: pointer; + transition: + background 0.15s, + color 0.15s; + + &:hover { + background: rgba(205, 214, 244, 0.12); + color: #cdd6f4; + } + + &:active { + transform: scale(0.9); + } } -#winner { - font-size: 24px; +@keyframes winner-pop { + 0% { + opacity: 0; + transform: translateX(-50%) scale(0.7) translateY(14px); + } + 60% { + opacity: 1; + transform: translateX(-50%) scale(1.08) translateY(-4px); + } + 100% { + opacity: 1; + transform: translateX(-50%) scale(1) translateY(0); + } +} + +#winner-label { + font-size: 13px; font-weight: 700; - color: #cdd6f4; - min-height: 1.5em; + letter-spacing: 0.12em; + text-transform: uppercase; + color: #a6adc8; +} + +#winner-name { + font-size: 28px; + font-weight: 800; text-align: center; - padding-inline: 24px; + background: linear-gradient(135deg, #f5c2e7, #cba6f7 45%, #89b4fa); + background-clip: text; + -webkit-background-clip: text; + color: transparent; +} + +#winner-actions { + display: flex; + flex-direction: column; + align-items: center; + gap: 6px; + margin-top: 4px; +} + +#copy-btn { + padding: 8px 20px; + font-family: Inter, system-ui, sans-serif; + font-size: 13px; + font-weight: 700; + letter-spacing: 0.03em; + color: #11111b; + background: #a6e3a1; + border: none; + border-radius: 999px; + cursor: pointer; + transition: + background 0.2s, + transform 0.1s; + + &:hover { + background: #94e2d5; + } + + &:active { + transform: scale(0.95); + } +} + +#copy-status { + font-size: 12px; + min-height: 1.2em; + color: #a6adc8; +} + +#confetti-canvas { + position: fixed; + inset: 0; + pointer-events: none; + z-index: 50; }