summaryrefslogtreecommitdiff
path: root/public/app.js
diff options
context:
space:
mode:
authoralex <[email protected]>2026-11-10 21:13:05 +0100
committeralex <[email protected]>2026-11-10 21:13:05 +0100
commit933913538b5acd8bfebdbf132ff3ff38c566cded (patch)
treeccda500d45f07ac7c45e09fe17564a4bdf2fb569 /public/app.js
parent51f7f58bbb6bdcfd5df86e6b0756545fa251b524 (diff)
downloadwheel-of-names-clone.git-933913538b5acd8bfebdbf132ff3ff38c566cded.tar.xz
wheel-of-names-clone.git-933913538b5acd8bfebdbf132ff3ff38c566cded.zip
improve the animation
Diffstat (limited to 'public/app.js')
-rw-r--r--public/app.js130
1 files changed, 83 insertions, 47 deletions
diff --git a/public/app.js b/public/app.js
index 9c8e6a8..4a722b7 100644
--- a/public/app.js
+++ b/public/app.js
@@ -13,53 +13,83 @@ const appState = {
spinning: false,
};
-const drawWheel = () => {
+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");
- 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();
+ if (baseWheelCanvas && baseWheelEntriesKey === key) {
return;
}
- const slice = (Math.PI * 2) / count;
+ const offscreen = document.createElement("canvas");
+ offscreen.width = wheelEl.width;
+ offscreen.height = wheelEl.height;
+ const baseCtx = offscreen.getContext("2d");
- 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();
+ 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();
+ }
}
- ctx.beginPath();
- ctx.arc(0, 0, 40, 0, Math.PI * 2);
- ctx.fillStyle = "#fef6ea";
- ctx.fill();
- ctx.lineWidth = 7;
- ctx.strokeStyle = "#1f1b18";
- ctx.stroke();
+ 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();
};
@@ -79,8 +109,6 @@ const targetAngleForWinner = (winner) => {
return pointerAngle - centerOfSlice;
};
-const easeOutCubic = (t) => 1 - (1 - t) ** 3;
-
const animateSpin = (winner) => {
if (appState.spinning) {
return;
@@ -90,18 +118,24 @@ const animateSpin = (winner) => {
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 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 progress = Math.min(1, (now - startTs) / duration);
- const eased = easeOutCubic(progress);
- appState.angle = start + (final - start) * eased;
+ const elapsedSec = (now - startTs) / 1000;
+ const t = Math.min(elapsedSec, durationSec);
+ appState.angle = start + initialVelocity * t - 0.5 * deceleration * t * t;
+
drawWheel();
- if (progress < 1) {
+ if (t < durationSec) {
requestAnimationFrame(frame);
return;
}
@@ -116,6 +150,8 @@ const animateSpin = (winner) => {
const setState = (next) => {
appState.entries = Array.isArray(next.entries) ? next.entries : [];
+ baseWheelCanvas = null;
+ baseWheelEntriesKey = "";
drawWheel();
};