From 413ea35bf6e0f9bf5420ce2c178ecd4db58dcee3 Mon Sep 17 00:00:00 2001 From: Showdown76py Date: Mon, 16 Nov 2026 22:44:02 +0100 Subject: [PATCH] Progress ring fills each segment with a sweep instead of redrawing --- static/css/app.css | 13 ++++++++++--- static/js/common.js | 44 +++++++++++++++++++++++++++++++++++--------- static/js/vote.js | 6 +++--- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/static/css/app.css b/static/css/app.css index a00feb7..2fe69e4 100644 --- a/static/css/app.css +++ b/static/css/app.css @@ -126,9 +126,16 @@ button:disabled { opacity: .35; cursor: not-allowed; transform: none; } .progress > div { height: 100%; background: var(--ink); border-radius: 99px; transition: width .3s; } .ring { display: block; overflow: visible; } -.ring .seg { fill: none; stroke: var(--line); stroke-linecap: round; transition: stroke .3s; } -.ring .seg.on { stroke: var(--ink); } -.ring .seg.last { stroke: var(--mark); } +.ring .seg, .ring .fill { fill: none; stroke-linecap: round; } +.ring .seg { stroke: var(--line); } +/* empty fill sits one length ahead and hidden, so the round cap dot doesn't show */ +.ring .fill { + stroke: var(--ink); stroke-dasharray: 1 1; stroke-dashoffset: 1; opacity: 0; + transition: stroke-dashoffset .5s cubic-bezier(.3, .7, .2, 1), opacity .12s, stroke .22s; +} +.ring .fill.on { stroke-dashoffset: 0; opacity: 1; } +.ring .fill.last { stroke: var(--mark); } +@media (prefers-reduced-motion: reduce) { .ring .fill { transition: opacity .2s, stroke .2s; } } .ring text { font: 700 20px var(--round); fill: var(--ink); } .codes-print { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin-top: 12px; } diff --git a/static/js/common.js b/static/js/common.js index 6af3215..43c3022 100644 --- a/static/js/common.js +++ b/static/js/common.js @@ -174,20 +174,46 @@ export function ring(n, total, size = 160, label = `${n}/${total}`) { const r = 40, sw = 10, c = 2 * Math.PI * r; // round caps overhang sw/2 on each side, so shorten each dash by sw const slot = c / total, gap = total > 1 ? 5 : 0, seg = slot - gap - sw; + const at = (len) => { const a = -Math.PI / 2 + len / r; return `${50 + r * Math.cos(a)} ${50 + r * Math.sin(a)}`; }; + const fills = []; for (let i = 0; i < total; i++) { - const el = document.createElementNS(SVGNS, "circle"); - el.setAttribute("cx", 50); el.setAttribute("cy", 50); el.setAttribute("r", r); - el.setAttribute("stroke-width", sw); - el.setAttribute("stroke-dasharray", `${seg} ${c - seg}`); - // start at 12 o'clock - el.setAttribute("stroke-dashoffset", String(c / 4 - i * slot - (gap + sw) / 2)); - el.setAttribute("class", "seg" + (i < n ? " on" : "") + (i === n - 1 && n < total ? " last" : "")); - svg.append(el); + const s0 = i * slot + (gap + sw) / 2; + const d = `M ${at(s0)} A ${r} ${r} 0 ${seg / r > Math.PI ? 1 : 0} 1 ${at(s0 + seg)}`; + // track underneath, fill on top sweeps clockwise through pathLength 1 + for (const cls of ["seg", "fill"]) { + const el = document.createElementNS(SVGNS, "path"); + el.setAttribute("d", d); + el.setAttribute("stroke-width", sw); + el.setAttribute("class", cls); + if (cls === "fill") { el.setAttribute("pathLength", 1); fills.push(el); } + svg.append(el); + } } const t = document.createElementNS(SVGNS, "text"); t.setAttribute("x", 50); t.setAttribute("y", 50); t.setAttribute("text-anchor", "middle"); t.setAttribute("dominant-baseline", "central"); - t.textContent = label; svg.append(t); + // updates in place so the css transitions run + svg.set = (k, text = `${k}/${total}`) => { + fills.forEach((el, i) => { + el.classList.toggle("on", i < k); + el.classList.toggle("last", i === k - 1 && k < total); + }); + t.textContent = text; + }; + svg.total = total; + svg.set(n, label); return svg; } + +// keeps the same svg between counts so each new segment fills instead of popping in +export function paintRing(box, n, total, size) { + let svg = box.querySelector("svg.ring"); + if (!svg || svg.total !== total) { + svg = ring(0, total, size); + box.replaceChildren(svg); + // one frame at the old count, so the first fill animates too + svg.getBoundingClientRect(); + } + svg.set(n); +} diff --git a/static/js/vote.js b/static/js/vote.js index a14d808..fcdcab2 100644 --- a/static/js/vote.js +++ b/static/js/vote.js @@ -1,4 +1,4 @@ -import { $, h, api, pollId, liveSocket, resultsView, toast, ring, icon } from "./common.js"; +import { $, h, api, pollId, liveSocket, resultsView, toast, paintRing, icon } from "./common.js"; import { makeRingScanner } from "./ringscan.js"; const pid = pollId(); @@ -36,7 +36,7 @@ function lsSet(k, v) { try { localStorage.setItem(k, v); } catch { /* private mo function renderDots(n) { const box = $("#dots"); - box.replaceChildren(ring(n, required, 150)); + paintRing(box, n, required, 150); box.setAttribute("aria-label", `${n} codes lus sur ${required}`); } @@ -327,7 +327,7 @@ async function confirmVote(q) { function collectPresence(n) { const dlg = $("#presence"); const got = []; - const paint = () => $("#p-ring").replaceChildren(ring(got.length, n, 120)); + const paint = () => paintRing($("#p-ring"), got.length, n, 120); paint(); $("#p-msg").textContent = ""; dlg.showModal();