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:
+10
-49
@@ -88,7 +88,7 @@ function makeScanner(video, onToken, onOther) {
|
||||
// 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);
|
||||
steer(d, performance.now());
|
||||
steer(d);
|
||||
} else {
|
||||
// 960 px is plenty for a qr and keeps low-end phones near 7 fps
|
||||
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)));
|
||||
}
|
||||
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
|
||||
// the button is touched, then it's the person's call
|
||||
// sensor zoom, by hand only: an automatic one made the preview jump and shook too much on cheap phones.
|
||||
// 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×");
|
||||
video.parentElement.append(swap, zoom);
|
||||
let zs = null;
|
||||
@@ -117,13 +117,9 @@ function makeScanner(video, onToken, onOther) {
|
||||
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) return;
|
||||
// people keep their seat between votes: the zoom that read the ring is where the next scan starts
|
||||
if (d.token) lsSet(ZOOM_KEY, String(zs.now));
|
||||
if (!zs.auto) return;
|
||||
const z = zoomPlan(zs, d, now);
|
||||
if (z != null) setZoom(z);
|
||||
function steer(d) {
|
||||
// people keep their seat between votes: a zoom that read the ring is where the next scan starts
|
||||
if (zs && d.token) lsSet(ZOOM_KEY, String(zs.now));
|
||||
}
|
||||
async function attach(s) {
|
||||
stream = s;
|
||||
@@ -131,7 +127,7 @@ function makeScanner(video, onToken, onOther) {
|
||||
// not every browser knows focusMode, those ignore it
|
||||
track?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {});
|
||||
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.textContent = "1×";
|
||||
video.srcObject = s;
|
||||
@@ -141,7 +137,6 @@ function makeScanner(video, onToken, onOther) {
|
||||
}
|
||||
zoom.onclick = () => {
|
||||
if (!zs) return;
|
||||
zs.auto = false;
|
||||
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);
|
||||
};
|
||||
@@ -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 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;
|
||||
|
||||
function openCamera(deviceId) {
|
||||
// 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];
|
||||
// rings from the back of a room are a few dozen pixels wide at 720p: ask for up to 4k, the browser hands over
|
||||
// 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 } };
|
||||
return navigator.mediaDevices.getUserMedia({
|
||||
audio: false, video: deviceId ? { ...base, deviceId: { exact: deviceId } } : { ...base, facingMode: { ideal: "environment" } },
|
||||
|
||||
Reference in New Issue
Block a user