Vote page: no visible auto zoom (preview jumped, 4x shook on cheap phones); up to 4k stream and a centre search at twice the detail instead

This commit is contained in:
2026-11-17 13:52:22 +01:00
parent 4e63c73904
commit 3c1f897f7a
2 changed files with 32 additions and 63 deletions
+15 -7
View File
@@ -40,20 +40,28 @@ export function makeRingScanner(opts = {}) {
if (d) return d; if (d) return d;
} }
track = null; track = null;
// phones held upright hand over a 1080x1920 stream const r = search(src, sw, sh, now, 0, 0, sw, sh);
const k = Math.min(1, WIDE / Math.max(sw, sh)); if (r.token || r.found) return r;
wide.width = Math.round(sw * k); wide.height = Math.round(sh * k); // nothing in the whole view: the back rows see a ring too small to find on the reduced copy. look at the
// middle, where people aim, at twice the detail. same as zooming in, without moving the preview
return search(src, sw, sh, now, sw / 4, sh / 4, sw / 2, sh / 2);
},
};
// finds discs in the region x0 y0 w h of the frame (on a copy of at most WIDE px), reads them at full resolution
function search(src, sw, sh, now, x0, y0, w, h) {
const k = Math.min(1, WIDE / Math.max(w, h));
wide.width = Math.round(w * k); wide.height = Math.round(h * k);
// plain bilinear at 3x reduction skips pixels and keeps the noise // plain bilinear at 3x reduction skips pixels and keeps the noise
wctx.imageSmoothingQuality = "high"; wctx.imageSmoothingQuality = "high";
wctx.drawImage(src, 0, 0, wide.width, wide.height); wctx.drawImage(src, x0, y0, w, h, 0, 0, wide.width, wide.height);
const cands = locate(grayOf(wctx.getImageData(0, 0, wide.width, wide.height)), { max: 5 }); const cands = locate(grayOf(wctx.getImageData(0, 0, wide.width, wide.height)), { max: 5 });
let last = null; let last = null;
for (const c of cands) { for (const c of cands) {
const d = readAt(src, sw, sh, { cx: (c.cx + 0.5) / k, cy: (c.cy + 0.5) / k, unit: c.r / CENTER / k, A: c.A.map((v) => v / k) }, now); const d = readAt(src, sw, sh, { cx: x0 + (c.cx + 0.5) / k, cy: y0 + (c.cy + 0.5) / k, unit: c.r / CENTER / k, A: c.A.map((v) => v / k) }, now);
if (d && d.token) return d; if (d && d.token) return d;
last = d || last; last = d || last;
} }
return last || { token: null, found: cands.length }; return last || { token: null, found: cands.length };
}, }
};
} }
+10 -49
View File
@@ -88,7 +88,7 @@ function makeScanner(video, onToken, onOther) {
// reads a crop at sensor resolution: a far code keeps all its pixels // reads a crop at sensor resolution: a far code keeps all its pixels
const d = rings.scan(video, video.videoWidth, video.videoHeight); const d = rings.scan(video, video.videoWidth, video.videoHeight);
if (d.token) onToken(d.token); if (d.token) onToken(d.token);
steer(d, performance.now()); steer(d);
} else { } else {
// 960 px is plenty for a qr and keeps low-end phones near 7 fps // 960 px is plenty for a qr and keeps low-end phones near 7 fps
const k = Math.min(1, 960 / video.videoWidth); const k = Math.min(1, 960 / video.videoWidth);
@@ -105,8 +105,8 @@ function makeScanner(video, onToken, onOther) {
setTimeout(loop, Math.max(0, 66 - (performance.now() - t0))); setTimeout(loop, Math.max(0, 66 - (performance.now() - t0)));
} }
const swap = h("button", { class: "cam-switch", type: "button", "aria-label": "Changer de caméra", hidden: true }, icon("swap", 20)); const swap = h("button", { class: "cam-switch", type: "button", "aria-label": "Changer de caméra", hidden: true }, icon("swap", 20));
// sensor zoom: a far ring gets real pixels instead of an upscaled crop. it steers itself (zoomPlan) until // sensor zoom, by hand only: an automatic one made the preview jump and shook too much on cheap phones.
// the button is touched, then it's the person's call // the far ring gets its pixels from the full sensor resolution instead (openCamera, ringscan)
const zoom = h("button", { class: "cam-zoom", type: "button", "aria-label": "Zoom", hidden: true }, "1×"); const zoom = h("button", { class: "cam-zoom", type: "button", "aria-label": "Zoom", hidden: true }, "1×");
video.parentElement.append(swap, zoom); video.parentElement.append(swap, zoom);
let zs = null; let zs = null;
@@ -117,13 +117,9 @@ function makeScanner(video, onToken, onOther) {
zoom.textContent = `${(Math.round(z * 10) / 10).toLocaleString("fr-FR")}×`; zoom.textContent = `${(Math.round(z * 10) / 10).toLocaleString("fr-FR")}×`;
track.applyConstraints({ advanced: [{ zoom: z }] }).catch(() => { zs = null; zoom.hidden = true; }); track.applyConstraints({ advanced: [{ zoom: z }] }).catch(() => { zs = null; zoom.hidden = true; });
} }
function steer(d, now) { function steer(d) {
if (!zs) return; // people keep their seat between votes: a zoom that read the ring is where the next scan starts
// people keep their seat between votes: the zoom that read the ring is where the next scan starts if (zs && d.token) lsSet(ZOOM_KEY, String(zs.now));
if (d.token) lsSet(ZOOM_KEY, String(zs.now));
if (!zs.auto) return;
const z = zoomPlan(zs, d, now);
if (z != null) setZoom(z);
} }
async function attach(s) { async function attach(s) {
stream = s; stream = s;
@@ -131,7 +127,7 @@ function makeScanner(video, onToken, onOther) {
// not every browser knows focusMode, those ignore it // not every browser knows focusMode, those ignore it
track?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {}); track?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {});
const cap = track?.getCapabilities?.().zoom; const cap = track?.getCapabilities?.().zoom;
zs = cap && cap.max >= 1.5 ? newZoomState(Math.max(1, cap.min), Math.min(cap.max, 4)) : null; zs = cap && cap.max >= 1.5 ? { base: Math.max(1, cap.min), max: Math.min(cap.max, 3), now: Math.max(1, cap.min) } : null;
zoom.hidden = !zs; zoom.hidden = !zs;
zoom.textContent = "1×"; zoom.textContent = "1×";
video.srcObject = s; video.srcObject = s;
@@ -141,7 +137,6 @@ function makeScanner(video, onToken, onOther) {
} }
zoom.onclick = () => { zoom.onclick = () => {
if (!zs) return; if (!zs) return;
zs.auto = false;
const steps = [zs.base, 2, 3].filter((z) => z >= zs.base && z <= zs.max); const steps = [zs.base, 2, 3].filter((z) => z >= zs.base && z <= zs.max);
setZoom(steps.find((z) => z > zs.now + 0.05) ?? zs.base); setZoom(steps.find((z) => z > zs.now + 0.05) ?? zs.base);
}; };
@@ -183,48 +178,14 @@ function makeScanner(video, onToken, onOther) {
}; };
} }
// auto zoom. a code that is seen (sync ring found) but not read, small and near the middle gets zoomed toward
// ~40 % of the frame, never so far that it would leave the frame. a code filling the frame zooms back out,
// and losing it for a while goes back to the widest view
function newZoomState(base, max) {
const t = performance.now();
return { base, max, now: base, auto: true, lastRead: 0, lastSeen: t, lastMove: t };
}
function zoomPlan(zs, d, now) {
if (d.token) zs.lastRead = now;
// a zoom too many costs nothing, so a weak sync ring is enough to steer on
const w = d.where, seen = !!w && (d.score ?? 0) >= 0.4;
if (seen) zs.lastSeen = now;
if (now - zs.lastMove < 600) return null;
let z = null;
if (seen && !d.token && now - zs.lastRead > 900 && w.d < 0.24 && zs.now < zs.max) {
const off = Math.max(Math.abs(w.x - 0.5), Math.abs(w.y - 0.5));
const fit = (zs.now * 0.45) / (off + w.d / 2);
z = Math.min(zs.max, fit, zs.now * Math.min(2, 0.4 / w.d));
if (z < zs.now + 0.15) z = null;
} else if (seen && w.d > 0.8 && zs.now > zs.base) {
z = Math.max(zs.base, zs.now * 0.6);
} else if (!seen && now - zs.lastSeen > 1800 && zs.now > zs.base) {
z = zs.base;
zs.lastSeen = now;
} else if (!seen && zs.now === zs.base && now - zs.lastSeen > 800 && now - zs.lastMove > 800) {
// from the back rows the ring is too small to be found at all: look closer where the person aims, the
// middle, and fall back to wide again (branch above) when nothing shows up there either
z = Math.min(zs.max, zs.base * 2);
zs.lastSeen = now;
}
if (z != null) zs.lastMove = now;
return z;
}
const CAM_KEY = "rv_cam"; const CAM_KEY = "rv_cam";
const ZOOM_KEY = `rv_zoom_${pid}`; const ZOOM_KEY = `rv_zoom_${pid}`;
const FRONT = /front|user|avant|facetime|selfie/i, ODD = /ultra|wide|grand.angle|macro|tele|télé|depth|infra|dual|triple|double/i, BACK = /back|rear|arri[eè]re|environment/i; const FRONT = /front|user|avant|facetime|selfie/i, ODD = /ultra|wide|grand.angle|macro|tele|télé|depth|infra|dual|triple|double/i, BACK = /back|rear|arri[eè]re|environment/i;
function openCamera(deviceId) { function openCamera(deviceId) {
// rings from the back of a room are a few dozen pixels wide at 720p, ask for the full sensor // rings from the back of a room are a few dozen pixels wide at 720p: ask for up to 4k, the browser hands over
const [w, h] = codeStyle === "ring" ? [1920, 1080] : [1280, 720]; // the closest it has. only a crop around the ring is read at that size, so the cost per frame stays flat
const [w, h] = codeStyle === "ring" ? [3840, 2160] : [1280, 720];
const base = { width: { ideal: w }, height: { ideal: h } }; const base = { width: { ideal: w }, height: { ideal: h } };
return navigator.mediaDevices.getUserMedia({ return navigator.mediaDevices.getUserMedia({
audio: false, video: deviceId ? { ...base, deviceId: { exact: deviceId } } : { ...base, facingMode: { ideal: "environment" } }, audio: false, video: deviceId ? { ...base, deviceId: { exact: deviceId } } : { ...base, facingMode: { ideal: "environment" } },