summaryrefslogtreecommitdiff
path: root/public/app.js
blob: a757086a394e983b205ff6c91b739e5865230e45 (plain)
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
169
170
171
172
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();