Wire the ring code into the app: per-poll code style (ring default for new polls), projector rings + entry qr, voter camera loop, both token formats
This commit is contained in:
+44
-3
@@ -1,10 +1,13 @@
|
||||
import { $, h, pollId, takeKey, liveSocket, drawQR, resultsView, fill, pct } from "./common.js";
|
||||
import { render as drawRing, DISC } from "./ringcode.js";
|
||||
|
||||
const pid = pollId();
|
||||
const key = takeKey(`rv_screen_${pid}`);
|
||||
let st = null;
|
||||
let qr = null;
|
||||
let qr = null; // current code message, qr or ring
|
||||
let raf = 0;
|
||||
let entry = null;
|
||||
let prevBits = null, switchedAt = 0;
|
||||
|
||||
function layout() {
|
||||
if (!st) return;
|
||||
@@ -15,8 +18,13 @@ function layout() {
|
||||
$("#focus").hidden = !focus;
|
||||
$("#idle").hidden = qrOn || !!focus;
|
||||
$(".s-main").classList.toggle("split", qrOn && !!focus);
|
||||
$("#qr-title").textContent = st.enrollment_open ? "Inscription" : "Scannez pour voter";
|
||||
const ring = st.code_style === "ring";
|
||||
$("#qr-title").textContent = st.enrollment_open ? "Inscription" : ring ? "Visez le cercle pour voter" : "Scannez pour voter";
|
||||
$("#qr-steps").hidden = !st.enrollment_open;
|
||||
// rings can't be opened by the camera app, so a small fixed qr leads to the page
|
||||
$("#step1").textContent = ring ? "Ouvrez la page de vote" : "Scannez ce code";
|
||||
$("#entry").hidden = !(ring && st.enrollment_open && entry);
|
||||
$(".qr-wrap").classList.toggle("ring", ring);
|
||||
}
|
||||
|
||||
function render() {
|
||||
@@ -50,12 +58,33 @@ function render() {
|
||||
}
|
||||
|
||||
function paintQR() {
|
||||
if (!qr) return;
|
||||
if (!qr || qr.type !== "qr") return;
|
||||
const c = $("#qr");
|
||||
const px = Math.min(c.clientWidth || 600, c.clientHeight || 600) * (window.devicePixelRatio || 1);
|
||||
drawQR(c, qr.modules, px);
|
||||
}
|
||||
|
||||
function paintEntry() {
|
||||
if (!entry) return;
|
||||
drawQR($("#entry-qr"), entry.modules, 220 * (window.devicePixelRatio || 1));
|
||||
$("#entry-url").textContent = entry.url.replace(/^https?:\/\//, "");
|
||||
}
|
||||
|
||||
// rings rotate continuously (one turn every 45 s); changed dashes cross-fade when the code switches
|
||||
function ringLoop(t) {
|
||||
if (!qr || qr.type !== "ring") return;
|
||||
const c = $("#qr"), dpr = window.devicePixelRatio || 1;
|
||||
const size = Math.round(Math.min(c.clientWidth || 600, c.clientHeight || 600) * dpr);
|
||||
if (c.width !== size) { c.width = c.height = size; }
|
||||
const ctx = c.getContext("2d");
|
||||
ctx.clearRect(0, 0, size, size);
|
||||
const fadeMs = Math.min(220, qr.interval_ms * 0.25);
|
||||
const k = prevBits ? Math.min(1, (t - switchedAt) / fadeMs) : 1;
|
||||
drawRing(ctx, size / 2, size / 2, (size / 2) / DISC * 0.98, (t / 1000) * (2 * Math.PI / 45), qr.bits, t / 1000,
|
||||
prevBits ? { from: prevBits, k } : null);
|
||||
requestAnimationFrame(ringLoop);
|
||||
}
|
||||
|
||||
function animateTick() {
|
||||
cancelAnimationFrame(raf);
|
||||
const bar = $("#tick-bar");
|
||||
@@ -80,6 +109,18 @@ liveSocket(`/ws/${pid}/screen`, {
|
||||
st = msg;
|
||||
if (!st.enrollment_open && !st.presence) qr = null;
|
||||
render();
|
||||
} else if (msg.type === "entry") {
|
||||
entry = msg;
|
||||
paintEntry();
|
||||
if (st) layout();
|
||||
} else if (msg.type === "ring") {
|
||||
skew = Date.now() - msg.slot * msg.interval_ms;
|
||||
const wasRing = qr && qr.type === "ring";
|
||||
prevBits = wasRing ? qr.bits : null;
|
||||
switchedAt = performance.now();
|
||||
qr = msg;
|
||||
if (!wasRing) requestAnimationFrame(ringLoop);
|
||||
animateTick();
|
||||
} else if (msg.type === "qr") {
|
||||
// server clock drives validity, estimate offset so the countdown bar is right
|
||||
skew = Date.now() - msg.slot * msg.interval_ms;
|
||||
|
||||
Reference in New Issue
Block a user