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:
+53
-21
@@ -1,4 +1,5 @@
|
||||
import { $, h, api, pollId, liveSocket, resultsView, toast, ring, icon } from "./common.js";
|
||||
import { decodeFrame, grayOf } from "./ringcode.js";
|
||||
|
||||
const pid = pollId();
|
||||
const DONE_KEY = `rv_done_${pid}`;
|
||||
@@ -15,6 +16,8 @@ const sent = new Set();
|
||||
const selections = new Map();
|
||||
let queue = Promise.resolve();
|
||||
let lastState = null;
|
||||
let codeStyle = "qr"; // what the projector shows: rotating qr or ring code
|
||||
let qrEngine = null;
|
||||
let lastJson = "";
|
||||
|
||||
function show(id) {
|
||||
@@ -50,7 +53,7 @@ async function startScanner() {
|
||||
return;
|
||||
}
|
||||
$("#cam-box").hidden = false;
|
||||
scanner = makeScanner($("#video"), onScan);
|
||||
scanner = makeScanner($("#video"), onScan, () => scanMsg("QR d'un autre scrutin.", "bad"));
|
||||
try {
|
||||
await scanner.start();
|
||||
$("#start-cam").hidden = true;
|
||||
@@ -66,18 +69,51 @@ async function startScanner() {
|
||||
}
|
||||
}
|
||||
|
||||
function makeScanner(video, onData) {
|
||||
return new window.QrScanner(video, (r) => onData(r.data), {
|
||||
returnDetailedScanResult: true,
|
||||
preferredCamera: "environment",
|
||||
maxScansPerSecond: 15,
|
||||
// whole frame, the projected code is often small and off-center
|
||||
calculateScanRegion: (v) => {
|
||||
const w = Math.min(960, v.videoWidth);
|
||||
return { x: 0, y: 0, width: v.videoWidth, height: v.videoHeight,
|
||||
downScaledWidth: w, downScaledHeight: Math.round((w * v.videoHeight) / v.videoWidth) };
|
||||
// own camera loop: decodes the ring code, or the classic qr through qr-scanner's engine.
|
||||
// calls onToken(token) for every code of this poll it reads
|
||||
function makeScanner(video, onToken, onOther) {
|
||||
let stream = null, running = false;
|
||||
const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true });
|
||||
async function loop() {
|
||||
if (!running) return;
|
||||
const t0 = performance.now();
|
||||
if (video.readyState >= 2 && video.videoWidth) {
|
||||
// 960 px is plenty and keeps low-end phones near 7 fps
|
||||
const k = Math.min(1, 960 / video.videoWidth);
|
||||
canvas.width = Math.round(video.videoWidth * k);
|
||||
canvas.height = Math.round(video.videoHeight * k);
|
||||
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
||||
try {
|
||||
if (codeStyle === "ring") {
|
||||
const d = decodeFrame(grayOf(ctx.getImageData(0, 0, canvas.width, canvas.height)));
|
||||
if (d.token) onToken(d.token);
|
||||
} else {
|
||||
qrEngine ||= await window.QrScanner.createQrEngine();
|
||||
const r = await window.QrScanner.scanImage(canvas, { qrEngine, returnDetailedScanResult: true }).catch(() => null);
|
||||
if (r) { const tok = tokenOf(r.data); if (tok) onToken(tok); else onOther?.(); }
|
||||
}
|
||||
} catch (e) { console.warn(e); }
|
||||
}
|
||||
// ~15 fps cap, saves battery on fast phones
|
||||
setTimeout(loop, Math.max(0, 66 - (performance.now() - t0)));
|
||||
}
|
||||
return {
|
||||
async start() {
|
||||
stream = await navigator.mediaDevices.getUserMedia({
|
||||
audio: false, video: { facingMode: { ideal: "environment" }, width: { ideal: 1280 }, height: { ideal: 720 } },
|
||||
});
|
||||
video.srcObject = stream;
|
||||
await video.play();
|
||||
running = true;
|
||||
loop();
|
||||
},
|
||||
});
|
||||
stop() {
|
||||
running = false;
|
||||
stream?.getTracks().forEach((t) => t.stop());
|
||||
video.srcObject = null;
|
||||
},
|
||||
destroy() {},
|
||||
};
|
||||
}
|
||||
|
||||
// token of a code shown for this poll, null for anything else
|
||||
@@ -97,12 +133,7 @@ function stopScanner() {
|
||||
$("#cam-box").hidden = true;
|
||||
}
|
||||
|
||||
function onScan(data) {
|
||||
const token = tokenOf(data);
|
||||
if (!token) {
|
||||
scanMsg("QR d'un autre scrutin.", "bad");
|
||||
return;
|
||||
}
|
||||
function onScan(token) {
|
||||
if (sent.has(token)) return;
|
||||
sent.add(token);
|
||||
submitToken(token);
|
||||
@@ -202,6 +233,7 @@ function sel(qid) {
|
||||
|
||||
function renderVote(st) {
|
||||
lastState = st;
|
||||
if (st.code_style) codeStyle = st.code_style;
|
||||
const box = $("#questions");
|
||||
box.replaceChildren();
|
||||
let pending = 0;
|
||||
@@ -293,9 +325,8 @@ function collectPresence(n) {
|
||||
paint();
|
||||
$("#p-msg").textContent = "";
|
||||
dlg.showModal();
|
||||
const sc = makeScanner($("#p-video"), (data) => {
|
||||
const t = tokenOf(data);
|
||||
if (!t || got.includes(t)) return;
|
||||
const sc = makeScanner($("#p-video"), (t) => {
|
||||
if (got.includes(t)) return;
|
||||
got.push(t);
|
||||
paint();
|
||||
navigator.vibrate?.(15);
|
||||
@@ -366,6 +397,7 @@ async function boot() {
|
||||
}
|
||||
if (!me.enrollment_open) return block("Inscriptions closes.");
|
||||
required = me.scans_required;
|
||||
codeStyle = me.code_style || "qr";
|
||||
if (me.need_code) { show("#v-code"); return; }
|
||||
startEnroll(me.count);
|
||||
if (firstToken && !sent.has(firstToken)) { sent.add(firstToken); submitToken(firstToken); }
|
||||
|
||||
Reference in New Issue
Block a user