This commit is contained in:
2026-11-16 19:32:44 +01:00
parent 0d0f84485a
commit 86ec6adb6b
5 changed files with 30 additions and 20 deletions
+14 -8
View File
@@ -1,5 +1,5 @@
import { $, h, api, pollId, liveSocket, resultsView, toast, ring, icon } from "./common.js";
import { decodeFrame, grayOf } from "./ringcode.js";
import { makeRingScanner } from "./ringscan.js";
const pid = pollId();
const DONE_KEY = `rv_done_${pid}`;
@@ -74,20 +74,22 @@ async function startScanner() {
function makeScanner(video, onToken, onOther) {
let stream = null, running = false;
const canvas = document.createElement("canvas"), ctx = canvas.getContext("2d", { willReadFrequently: true });
const rings = makeRingScanner();
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)));
// reads a crop at sensor resolution: a far code keeps all its pixels
const d = rings.scan(video, video.videoWidth, video.videoHeight);
if (d.token) onToken(d.token);
} else {
// 960 px is plenty for a qr 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);
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?.(); }
@@ -99,9 +101,13 @@ function makeScanner(video, onToken, onOther) {
}
return {
async start() {
// rings from the back of a room are a few dozen pixels wide at 720p, ask for the full sensor
const [w, h] = codeStyle === "ring" ? [1920, 1080] : [1280, 720];
stream = await navigator.mediaDevices.getUserMedia({
audio: false, video: { facingMode: { ideal: "environment" }, width: { ideal: 1280 }, height: { ideal: 720 } },
audio: false, video: { facingMode: { ideal: "environment" }, width: { ideal: w }, height: { ideal: h } },
});
// not every browser knows focusMode, those ignore it
stream.getVideoTracks()[0]?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {});
video.srcObject = stream;
await video.play();
running = true;