Files
rotatyvote/static/ringlab/phone.js
T

90 lines
3.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { makeRingScanner } from "/static/js/ringscan.js";
const rings = makeRingScanner();
const $ = (id) => document.getElementById(id);
const video = $("v"), canvas = document.createElement("canvas"), ctx = canvas.getContext("2d");
let engine, running = false, frames = 0, qr = 0, ring = 0, t0 = 0, fpsT = performance.now(), fpsN = 0;
let tokens = new Set(), mode = "aucun", msAvg = 0;
const history = [];
function reset() {
tokens = new Set(); t0 = performance.now(); frames = qr = ring = 0;
$("state").textContent = "Visez l'écran";
}
async function start() {
engine = await QrScanner.createQrEngine();
// same request as the vote page, so what this page measures is what voters get
const stream = await navigator.mediaDevices.getUserMedia({
video: { facingMode: { ideal: "environment" }, width: { ideal: 1920 }, height: { ideal: 1080 } },
});
const set = stream.getVideoTracks()[0].getSettings();
$("res").textContent = `${set.width}×${set.height} ${set.frameRate ? Math.round(set.frameRate) + " i/s" : ""}`;
video.srcObject = stream;
await video.play();
running = true;
reset();
loop();
}
async function loop() {
if (!running) return;
if (video.readyState >= 2) {
// the qr reference reads a 960 px copy, the rings read their own crop of the full frame
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);
frames++; fpsN++;
const ts = performance.now(), d = rings.scan(video, video.videoWidth, video.videoHeight);
msAvg = msAvg ? msAvg * 0.9 + (performance.now() - ts) * 0.1 : performance.now() - ts;
$("ms").textContent = `${Math.round(msAvg)} ms`;
if (d.cand) qr++;
if (d.token) { ring++; mode = "anneaux"; tokens.add(d.token); }
else if (mode !== "anneaux") {
// reference mode: the classic rotating qr
let res = null;
try { res = await QrScanner.scanImage(canvas, { qrEngine: engine, returnDetailedScanResult: true }); } catch { /* none */ }
if (res && res.data.includes("#")) { mode = "QR classique"; tokens.add(res.data.split("#")[1]); }
}
if (tokens.size >= 5 && t0) {
const s = (performance.now() - t0) / 1000;
history.push(s.toFixed(1));
$("state").textContent = `5 codes en ${s.toFixed(1)} s`;
$("hist").textContent = `Essais : ${history.join(" s, ")} s`;
t0 = 0;
}
const now = performance.now();
if (now - fpsT > 1000) { $("fps").textContent = (fpsN * 1000 / (now - fpsT)).toFixed(1); fpsT = now; fpsN = 0; }
$("frames").textContent = frames;
$("qr").textContent = `${Math.round((100 * qr) / Math.max(1, frames))} %`;
$("ring").textContent = `${Math.round((100 * ring) / Math.max(1, frames))} %`;
$("count").textContent = `${Math.min(tokens.size, 5)} / 5`;
$("mode").textContent = mode;
}
setTimeout(loop, 0);
}
$("go").addEventListener("click", () => start().catch((e) => { $("state").textContent = `Caméra : ${e.message}`; }));
$("again").addEventListener("click", reset);
// one raw frame of the stream as the decoder sees it, to send back when a phone can't read the ring.
// built and clicked in the same tap: android drops a download started after an async step (toBlob)
$("snap").addEventListener("click", () => {
if (!video.videoWidth) return;
const c = document.createElement("canvas");
c.width = video.videoWidth; c.height = video.videoHeight;
c.getContext("2d").drawImage(video, 0, 0);
const url = c.toDataURL("image/jpeg", 0.95);
const a = document.createElement("a");
a.href = url;
a.download = `rotatyvote-frame-${video.videoWidth}x${video.videoHeight}-${Date.now()}.jpg`;
document.body.append(a);
a.click();
a.remove();
// fallback: long press the picture to save it
$("shot").src = url;
$("shot").hidden = false;
});