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
|
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,
};
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 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;
};
requestAnimationFrame(frame);
};
const setState = (next) => {
appState.entries = Array.isArray(next.entries) ? next.entries : [];
drawWheel();
};
socket.on("state", setState);
socket.on("spin", (spin) => {
if (!spin || !spin.winner) {
return;
}
animateSpin(spin.winner);
});
drawWheel();
|