Vote page: camera zooms itself onto a small ring it sees but can't read, until the zoom button is touched

This commit is contained in:
2026-11-17 13:33:18 +01:00
parent 7266fe1b47
commit 597c5e5cc2
2 changed files with 55 additions and 10 deletions
+4 -1
View File
@@ -23,9 +23,12 @@ export function makeRingScanner(opts = {}) {
const seed = { cx: (t.cx - x0) * k - 0.5, cy: (t.cy - y0) * k - 0.5, r: t.unit * CENTER * k, A: t.A && t.A.map((v) => v * k) }; const seed = { cx: (t.cx - x0) * k - 0.5, cy: (t.cy - y0) * k - 0.5, r: t.unit * CENTER * k, A: t.A && t.A.map((v) => v * k) };
const d = reader.read(grayOf(cctx.getImageData(0, 0, crop.width, crop.height)), now, seed); const d = reader.read(grayOf(cctx.getImageData(0, 0, crop.width, crop.height)), now, seed);
if (!d.cand) return null; if (!d.cand) return null;
// where the code sits in the frame and how big, as fractions: lets the camera zoom in on a small one
const ux = x0 + (d.cand.cx + 0.5) / k, uy = y0 + (d.cand.cy + 0.5) / k, unit = d.cand.r / CENTER / k;
d.where = { x: ux / sw, y: uy / sh, d: (2 * DISC * unit) / Math.min(sw, sh) };
// only a blob with a sync ring keeps the crop alive, anything else goes back to the whole frame // only a blob with a sync ring keeps the crop alive, anything else goes back to the whole frame
if (!d.token && !d.soft) { track = null; return d; } if (!d.token && !d.soft) { track = null; return d; }
track = { cx: x0 + (d.cand.cx + 0.5) / k, cy: y0 + (d.cand.cy + 0.5) / k, unit: d.cand.r / CENTER / k, A: d.cand.A.map((v) => v / k), at: now }; track = { cx: ux, cy: uy, unit, A: d.cand.A.map((v) => v / k), at: now };
return d; return d;
} }
+51 -9
View File
@@ -88,6 +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());
} 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);
@@ -104,27 +105,40 @@ 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 (mostly android): a far ring gets real pixels instead of an upscaled 720p crop // sensor zoom: a far ring gets real pixels instead of an upscaled crop. it steers itself (zoomPlan) until
// the button is touched, then it's the person's call
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 zooms = [1]; let zs = null;
function setZoom(z) {
const track = stream?.getVideoTracks()[0];
if (!track || !zs) return;
zs.now = z;
zoom.textContent = `${(Math.round(z * 10) / 10).toLocaleString("fr-FR")}×`;
track.applyConstraints({ advanced: [{ zoom: z }] }).catch(() => { zs = null; zoom.hidden = true; });
}
function steer(d, now) {
if (!zs || !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;
const track = s.getVideoTracks()[0]; const track = s.getVideoTracks()[0];
// 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;
zooms = cap && cap.max >= 1.5 ? [1, 2, 3].filter((z) => z >= cap.min && z <= cap.max) : [1]; zs = cap && cap.max >= 1.5 ? newZoomState(Math.max(1, cap.min), Math.min(cap.max, 4)) : null;
zoom.hidden = zooms.length < 2; zoom.hidden = !zs;
zoom.textContent = "1×"; zoom.textContent = "1×";
video.srcObject = s; video.srcObject = s;
await video.play(); await video.play();
} }
zoom.onclick = async () => { zoom.onclick = () => {
const track = stream?.getVideoTracks()[0]; if (!zs) return;
if (!track) return; zs.auto = false;
const next = zooms[(zooms.indexOf(+zoom.textContent.replace("×", "")) + 1) % zooms.length]; const steps = [zs.base, 2, 3].filter((z) => z >= zs.base && z <= zs.max);
try { await track.applyConstraints({ advanced: [{ zoom: next }] }); zoom.textContent = `${next}×`; } catch { zoom.hidden = true; } setZoom(steps.find((z) => z > zs.now + 0.05) ?? zs.base);
}; };
swap.onclick = async () => { swap.onclick = async () => {
const cams = rankCameras(await videoInputs()); const cams = rankCameras(await videoInputs());
@@ -164,6 +178,34 @@ 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) {
return { base, max, now: base, auto: true, lastRead: 0, lastSeen: 0, lastMove: 0 };
}
function zoomPlan(zs, d, now) {
if (d.token) zs.lastRead = now;
const w = d.where, seen = !!w && (d.score ?? 0) >= 0.55;
if (seen) zs.lastSeen = now;
if (now - zs.lastMove < 900) return null;
let z = null;
if (seen && !d.token && now - zs.lastRead > 1200 && 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 > 2500 && zs.now > zs.base) {
z = zs.base;
zs.lastSeen = now;
}
if (z != null) zs.lastMove = now;
return z;
}
const CAM_KEY = "rv_cam"; const CAM_KEY = "rv_cam";
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;