const socket = io(); const wheelEl = document.getElementById("wheel"); const winnerChip = document.getElementById("winnerChip"); const exampleCommandsEl = document.getElementById("exampleCommands"); const ctx = wheelEl.getContext("2d"); const radius = wheelEl.width / 2; const center = { x: radius, y: radius }; const palette = ["#d95d39", "#f2a541", "#4f6d7a", "#90be6d", "#f28482", "#8d5a97", "#2f3e46", "#577590"]; const appState = { entries: [], selectedWinner: null, angle: 0, spinning: false, }; const drawWheel = () => { const entries = appState.entries; const count = entries.length; ctx.clearRect(0, 0, wheelEl.width, wheelEl.height); ctx.save(); ctx.translate(center.x, center.y); ctx.rotate(appState.angle); if (count === 0) { ctx.beginPath(); ctx.arc(0, 0, radius - 16, 0, Math.PI * 2); ctx.fillStyle = "#ddd"; ctx.fill(); ctx.restore(); return; } const slice = (Math.PI * 2) / count; for (let i = 0; i < count; i += 1) { const start = i * slice; const end = start + slice; ctx.beginPath(); ctx.moveTo(0, 0); ctx.arc(0, 0, radius - 16, start, end); ctx.closePath(); ctx.fillStyle = palette[i % palette.length]; ctx.fill(); ctx.save(); ctx.rotate(start + slice / 2); ctx.textAlign = "right"; ctx.fillStyle = "#fffdf8"; ctx.font = "700 26px 'Alegreya Sans', sans-serif"; ctx.fillText(entries[i], radius - 36, 10); ctx.restore(); } ctx.beginPath(); ctx.arc(0, 0, 40, 0, Math.PI * 2); ctx.fillStyle = "#fef6ea"; ctx.fill(); ctx.lineWidth = 7; ctx.strokeStyle = "#1f1b18"; ctx.stroke(); ctx.restore(); }; const normalizeAngle = (angle) => { const full = Math.PI * 2; return ((angle % full) + full) % full; }; const indexAtPointer = () => { const count = appState.entries.length; if (!count) { return -1; } const slice = (Math.PI * 2) / count; const pointerAngle = -Math.PI / 2; const relative = normalizeAngle(pointerAngle - appState.angle); return Math.floor(relative / slice) % count; }; const targetAngleForWinner = (winner) => { const entries = appState.entries; const winnerIndex = entries.indexOf(winner); if (winnerIndex === -1) { return appState.angle; } const count = entries.length; const slice = (Math.PI * 2) / count; const centerOfSlice = winnerIndex * slice + slice / 2; const pointerAngle = -Math.PI / 2; return pointerAngle - centerOfSlice; }; const easeOutCubic = (t) => 1 - (1 - t) ** 3; const animateSpin = (winner) => { if (appState.spinning) { return; } appState.spinning = true; const start = appState.angle; const baseTarget = targetAngleForWinner(winner); const spins = 6 + Math.floor(Math.random() * 3); const final = baseTarget + spins * Math.PI * 2; const duration = 5200; const startTs = performance.now(); const frame = (now) => { const progress = Math.min(1, (now - startTs) / duration); const eased = easeOutCubic(progress); appState.angle = start + (final - start) * eased; drawWheel(); if (progress < 1) { requestAnimationFrame(frame); return; } appState.angle = final; drawWheel(); appState.spinning = false; const idx = indexAtPointer(); const landed = idx >= 0 ? appState.entries[idx] : winner; winnerChip.textContent = `Winner: ${landed}`; winnerChip.classList.remove("pulse"); void winnerChip.offsetWidth; winnerChip.classList.add("pulse"); }; requestAnimationFrame(frame); }; const setState = (next) => { appState.entries = Array.isArray(next.entries) ? next.entries : []; appState.selectedWinner = next.selectedWinner || null; drawWheel(); if (!appState.selectedWinner) { winnerChip.textContent = "Waiting for spin..."; } const commands = [ `curl -X POST http://localhost:3000/api/entries \\\n+ -H 'Content-Type: application/json' \\\n+ -d '{"entries":["Alice","Bob","Charlie","Diana"]}'`, `curl -X POST http://localhost:3000/api/spin \\\n+ -H 'Content-Type: application/json' \\\n+ -d '{"winner":"Charlie"}'`, ]; exampleCommandsEl.textContent = commands.join("\n\n"); }; socket.on("state", setState); socket.on("spin", (spin) => { if (!spin || !spin.winner) { return; } animateSpin(spin.winner); }); drawWheel();