1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
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();
|