Progress ring fills each segment with a sweep instead of redrawing

This commit is contained in:
2026-08-16 22:44:02 +01:00
parent 13d42db0a3
commit aac778ceba
3 changed files with 48 additions and 15 deletions
+10 -3
View File
@@ -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; } .progress > div { height: 100%; background: var(--ink); border-radius: 99px; transition: width .3s; }
.ring { display: block; overflow: visible; } .ring { display: block; overflow: visible; }
.ring .seg { fill: none; stroke: var(--line); stroke-linecap: round; transition: stroke .3s; } .ring .seg, .ring .fill { fill: none; stroke-linecap: round; }
.ring .seg.on { stroke: var(--ink); } .ring .seg { stroke: var(--line); }
.ring .seg.last { stroke: var(--mark); } /* 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); } .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; } .codes-print { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; margin-top: 12px; }
+35 -9
View File
@@ -174,20 +174,46 @@ export function ring(n, total, size = 160, label = `${n}/${total}`) {
const r = 40, sw = 10, c = 2 * Math.PI * r; const r = 40, sw = 10, c = 2 * Math.PI * r;
// round caps overhang sw/2 on each side, so shorten each dash by sw // 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 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++) { for (let i = 0; i < total; i++) {
const el = document.createElementNS(SVGNS, "circle"); const s0 = i * slot + (gap + sw) / 2;
el.setAttribute("cx", 50); el.setAttribute("cy", 50); el.setAttribute("r", r); const d = `M ${at(s0)} A ${r} ${r} 0 ${seg / r > Math.PI ? 1 : 0} 1 ${at(s0 + seg)}`;
el.setAttribute("stroke-width", sw); // track underneath, fill on top sweeps clockwise through pathLength 1
el.setAttribute("stroke-dasharray", `${seg} ${c - seg}`); for (const cls of ["seg", "fill"]) {
// start at 12 o'clock const el = document.createElementNS(SVGNS, "path");
el.setAttribute("stroke-dashoffset", String(c / 4 - i * slot - (gap + sw) / 2)); el.setAttribute("d", d);
el.setAttribute("class", "seg" + (i < n ? " on" : "") + (i === n - 1 && n < total ? " last" : "")); el.setAttribute("stroke-width", sw);
svg.append(el); el.setAttribute("class", cls);
if (cls === "fill") { el.setAttribute("pathLength", 1); fills.push(el); }
svg.append(el);
}
} }
const t = document.createElementNS(SVGNS, "text"); const t = document.createElementNS(SVGNS, "text");
t.setAttribute("x", 50); t.setAttribute("y", 50); t.setAttribute("x", 50); t.setAttribute("y", 50);
t.setAttribute("text-anchor", "middle"); t.setAttribute("dominant-baseline", "central"); t.setAttribute("text-anchor", "middle"); t.setAttribute("dominant-baseline", "central");
t.textContent = label;
svg.append(t); 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; 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);
}
+3 -3
View File
@@ -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"; import { makeRingScanner } from "./ringscan.js";
const pid = pollId(); const pid = pollId();
@@ -36,7 +36,7 @@ function lsSet(k, v) { try { localStorage.setItem(k, v); } catch { /* private mo
function renderDots(n) { function renderDots(n) {
const box = $("#dots"); const box = $("#dots");
box.replaceChildren(ring(n, required, 150)); paintRing(box, n, required, 150);
box.setAttribute("aria-label", `${n} codes lus sur ${required}`); box.setAttribute("aria-label", `${n} codes lus sur ${required}`);
} }
@@ -327,7 +327,7 @@ async function confirmVote(q) {
function collectPresence(n) { function collectPresence(n) {
const dlg = $("#presence"); const dlg = $("#presence");
const got = []; const got = [];
const paint = () => $("#p-ring").replaceChildren(ring(got.length, n, 120)); const paint = () => paintRing($("#p-ring"), got.length, n, 120);
paint(); paint();
$("#p-msg").textContent = ""; $("#p-msg").textContent = "";
dlg.showModal(); dlg.showModal();