Vote page: fall back to the main back lens when the browser picks the selfie or ultra wide camera, camera switch button
This commit is contained in:
@@ -69,3 +69,11 @@ dialog .actions button { flex: 1; }
|
|||||||
.celebrate, .celebrate svg, .celebrate p { animation: none; }
|
.celebrate, .celebrate svg, .celebrate p { animation: none; }
|
||||||
.celebrate .tick { animation: none; stroke-dashoffset: 0; }
|
.celebrate .tick { animation: none; stroke-dashoffset: 0; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* next camera: some phones hand over the selfie lens or the ultra wide */
|
||||||
|
.cam-switch {
|
||||||
|
position: absolute; right: 10px; bottom: 10px; width: 44px; height: 44px; padding: 0; border-radius: 50%;
|
||||||
|
display: grid; place-items: center; border: 0; color: #fff; background: rgba(20, 22, 36, .55);
|
||||||
|
-webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
|
||||||
|
}
|
||||||
|
.cam-switch:hover { border: 0; background: rgba(20, 22, 36, .75); }
|
||||||
|
|||||||
@@ -149,6 +149,7 @@ const SVGNS = "http://www.w3.org/2000/svg";
|
|||||||
const ICONS = {
|
const ICONS = {
|
||||||
screen: ["M3 5h18v11H3z", "M8 20h8", "M12 16v4"],
|
screen: ["M3 5h18v11H3z", "M8 20h8", "M12 16v4"],
|
||||||
check: ["M5 12.5l4.5 4.5L19 7.5"],
|
check: ["M5 12.5l4.5 4.5L19 7.5"],
|
||||||
|
swap: ["M4 9h13l-3.5-3.5", "M20 15H7l3.5 3.5"],
|
||||||
};
|
};
|
||||||
|
|
||||||
export function icon(name, size = 16) {
|
export function icon(name, size = 16) {
|
||||||
|
|||||||
+60
-10
@@ -103,17 +103,41 @@ function makeScanner(video, onToken, onOther) {
|
|||||||
// ~15 fps cap, saves battery on fast phones
|
// ~15 fps cap, saves battery on fast phones
|
||||||
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));
|
||||||
|
video.parentElement.append(swap);
|
||||||
|
async function attach(s) {
|
||||||
|
stream = s;
|
||||||
|
// not every browser knows focusMode, those ignore it
|
||||||
|
s.getVideoTracks()[0]?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {});
|
||||||
|
video.srcObject = s;
|
||||||
|
await video.play();
|
||||||
|
}
|
||||||
|
swap.onclick = async () => {
|
||||||
|
const cams = rankCameras(await videoInputs());
|
||||||
|
if (cams.length < 2) return;
|
||||||
|
const cur = stream?.getVideoTracks()[0]?.getSettings().deviceId;
|
||||||
|
const next = cams[(cams.findIndex((d) => d.deviceId === cur) + 1) % cams.length];
|
||||||
|
stream?.getTracks().forEach((t) => t.stop());
|
||||||
|
try { await attach(await openCamera(next.deviceId)); lsSet(CAM_KEY, next.deviceId); }
|
||||||
|
catch { await attach(await openCamera(null)); }
|
||||||
|
};
|
||||||
return {
|
return {
|
||||||
async start() {
|
async start() {
|
||||||
// rings from the back of a room are a few dozen pixels wide at 720p, ask for the full sensor
|
const saved = lsGet(CAM_KEY);
|
||||||
const [w, h] = codeStyle === "ring" ? [1920, 1080] : [1280, 720];
|
let s = null;
|
||||||
stream = await navigator.mediaDevices.getUserMedia({
|
if (saved) s = await openCamera(saved).catch(() => null);
|
||||||
audio: false, video: { facingMode: { ideal: "environment" }, width: { ideal: w }, height: { ideal: h } },
|
s ||= await openCamera(null);
|
||||||
});
|
// some browsers ignore facingMode or hand over the ultra wide: switch to the main back lens when labels say so
|
||||||
// not every browser knows focusMode, those ignore it
|
const track = s.getVideoTracks()[0], cams = await videoInputs();
|
||||||
stream.getVideoTracks()[0]?.applyConstraints({ advanced: [{ focusMode: "continuous" }] }).catch(() => {});
|
if (!saved && cams.length > 1 && (track.getSettings().facingMode === "user" || !isMainBack(track.label))) {
|
||||||
video.srcObject = stream;
|
const best = rankCameras(cams)[0];
|
||||||
await video.play();
|
if (best && best.deviceId !== track.getSettings().deviceId) {
|
||||||
|
s.getTracks().forEach((t) => t.stop());
|
||||||
|
s = await openCamera(best.deviceId).catch(() => null) || await openCamera(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
swap.hidden = cams.length < 2;
|
||||||
|
await attach(s);
|
||||||
running = true;
|
running = true;
|
||||||
loop();
|
loop();
|
||||||
},
|
},
|
||||||
@@ -122,10 +146,36 @@ function makeScanner(video, onToken, onOther) {
|
|||||||
stream?.getTracks().forEach((t) => t.stop());
|
stream?.getTracks().forEach((t) => t.stop());
|
||||||
video.srcObject = null;
|
video.srcObject = null;
|
||||||
},
|
},
|
||||||
destroy() {},
|
destroy() { swap.remove(); },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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];
|
||||||
|
const base = { width: { ideal: w }, height: { ideal: h } };
|
||||||
|
return navigator.mediaDevices.getUserMedia({
|
||||||
|
audio: false, video: deviceId ? { ...base, deviceId: { exact: deviceId } } : { ...base, facingMode: { ideal: "environment" } },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function videoInputs() {
|
||||||
|
try { return (await navigator.mediaDevices.enumerateDevices()).filter((d) => d.kind === "videoinput"); }
|
||||||
|
catch { return []; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// without a label we can't tell, so trust what facingMode gave
|
||||||
|
const isMainBack = (label) => !label || (!FRONT.test(label) && !ODD.test(label));
|
||||||
|
|
||||||
|
// main back lens first, other back lenses next, front last. android lists its main back camera first
|
||||||
|
function rankCameras(cams) {
|
||||||
|
const score = (d) => (FRONT.test(d.label) ? -4 : 0) + (ODD.test(d.label) ? -2 : 0) + (BACK.test(d.label) ? 1 : 0);
|
||||||
|
return cams.map((d, i) => ({ d, i, s: score(d) })).sort((a, b) => b.s - a.s || a.i - b.i).map((x) => x.d);
|
||||||
|
}
|
||||||
|
|
||||||
// token of a code shown for this poll, null for anything else
|
// token of a code shown for this poll, null for anything else
|
||||||
function tokenOf(data) {
|
function tokenOf(data) {
|
||||||
let u;
|
let u;
|
||||||
|
|||||||
Reference in New Issue
Block a user