const socket = io(); const wheelEl = document.getElementById("wheel"); 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: [], angle: 0, spinning: false, }; let baseWheelCanvas = null; let baseWheelEntriesKey = ""; const randomRange = (min, max) => min + Math.random() * (max - min); const normalizeAngle = (angle) => { const full = Math.PI * 2; return ((angle % full) + full) % full; }; const buildBaseWheel = () => { const entries = appState.entries; const count = entries.length; const key = entries.join("\u0001"); if (baseWheelCanvas && baseWheelEntriesKey === key) { return; } const offscreen = document.createElement("canvas"); offscreen.width = wheelEl.width; offscreen.height = wheelEl.height; const baseCtx = offscreen.getContext("2d"); baseCtx.translate(center.x, center.y); if (count === 0) { baseCtx.beginPath(); baseCtx.arc(0, 0, radius - 16, 0, Math.PI * 2); baseCtx.fillStyle = "#ddd"; baseCtx.fill(); } else { const slice = (Math.PI * 2) / count; for (let i = 0; i < count; i += 1) { const start = i * slice; const end = start + slice; baseCtx.beginPath(); baseCtx.moveTo(0, 0); baseCtx.arc(0, 0, radius - 16, start, end); baseCtx.closePath(); baseCtx.fillStyle = palette[i % palette.length]; baseCtx.fill(); baseCtx.save(); baseCtx.rotate(start + slice / 2); baseCtx.textAlign = "right"; baseCtx.fillStyle = "#fffdf8"; baseCtx.font = "700 26px 'Alegreya Sans', sans-serif"; baseCtx.fillText(entries[i], radius - 36, 10); baseCtx.restore(); } } baseCtx.beginPath(); baseCtx.arc(0, 0, 40, 0, Math.PI * 2); baseCtx.fillStyle = "#fef6ea"; baseCtx.fill(); baseCtx.lineWidth = 7; baseCtx.strokeStyle = "#1f1b18"; baseCtx.stroke(); baseWheelCanvas = offscreen; baseWheelEntriesKey = key; }; const drawWheel = () => { buildBaseWheel(); ctx.clearRect(0, 0, wheelEl.width, wheelEl.height); ctx.save(); ctx.translate(center.x, center.y); ctx.rotate(appState.angle); if (baseWheelCanvas) { ctx.drawImage(baseWheelCanvas, -center.x, -center.y); } ctx.restore(); }; 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 animateSpin = (winner) => { if (appState.spinning) { return; } appState.spinning = true; const start = appState.angle; const baseTarget = targetAngleForWinner(winner); const spins = 16 + Math.floor(Math.random() * 11); const forwardToTarget = normalizeAngle(baseTarget - normalizeAngle(start)); const totalRotation = spins * Math.PI * 2 + forwardToTarget; const final = start + totalRotation; const durationMs = randomRange(7600, 10800); const durationSec = durationMs / 1000; const startTs = performance.now(); const initialVelocity = (2 * totalRotation) / durationSec; const deceleration = initialVelocity / durationSec; const frame = (now) => { const elapsedSec = (now - startTs) / 1000; const t = Math.min(elapsedSec, durationSec); appState.angle = start + initialVelocity * t - 0.5 * deceleration * t * t; drawWheel(); if (t < durationSec) { requestAnimationFrame(frame); return; } appState.angle = final; drawWheel(); appState.spinning = false; }; requestAnimationFrame(frame); }; const setState = (next) => { appState.entries = Array.isArray(next.entries) ? next.entries : []; baseWheelCanvas = null; baseWheelEntriesKey = ""; drawWheel(); }; socket.on("state", setState); socket.on("spin", (spin) => { if (!spin || !spin.winner) { return; } animateSpin(spin.winner); }); drawWheel();