Add countdown to the webcam, and fix flipping

This commit is contained in:
n07070
2026-05-17 16:40:00 +02:00
parent 002dc2eb8e
commit 16c1ef4d72
2 changed files with 57 additions and 17 deletions

View File

@@ -186,18 +186,18 @@ function setup_events(){
}, false); }, false);
switch_cameras.addEventListener('click', function(ev) { switch_cameras.addEventListener('click', function(ev) {
flip_cameras(camera_options); flip_cameras();
}, false ); }, false );
printButton.addEventListener('click', function(ev){ printButton.addEventListener('click', async function(ev) {
data = take_picture(); ev.preventDefault();
try { try {
await countDownToPicture(); // wait for countdown to finish
const data = await take_picture(); // take_picture can be async
print_picture(data); print_picture(data);
} catch (e) { } catch (e) {
alert("Failed to print a picture because : " + e); alert("Failed to print a picture because : " + e);
} }
ev.preventDefault();
}, false); }, false);
} }
@@ -220,19 +220,18 @@ function dataURLtoFile(dataurl, filename) {
return new File([u8arr], filename, {type:mime}); return new File([u8arr], filename, {type:mime});
} }
function take_picture(){ async function take_picture() {
var context = canvas.getContext('2d'); const context = canvas.getContext('2d');
let data = null;
if (width && height) { if (width && height) {
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
context.drawImage(video, 0, 0, width, height); context.drawImage(video, 0, 0, width, height);
data = canvas.toDataURL('image/png');
var data = canvas.toDataURL('image/png'); photo.setAttribute('src', data);
photo.setAttribute('src', data);
} else { } else {
clear_canvas(); clear_canvas();
} }
return data; return data;
} }
@@ -257,10 +256,18 @@ function print_picture(data){
} }
function flip_cameras(camera_options){ function flip_cameras(){
console.log("Current facing mode : " + camera_options.video.facingMode)
if ( supports_facing_mode ) { if ( supports_facing_mode ) {
get_webcam((camera_options.video.facingMode === 'user' ? 'environment' : 'user')); console.log("Support facing mode, trying to switch !");
if (camera_options.video.facingMode == 'user'){
camera_options = { audio: false, video: { facingMode: "environment" },};
} else {
camera_options = { audio: false, video: { facingMode: "user" },};
}
} }
console.log("Switching to " + camera_options.video.facingMode );
get_webcam(camera_options);
} }
function stop_video_streams(){ function stop_video_streams(){
@@ -300,4 +307,33 @@ function no_webcam_error(){
throw new Error("Unable to get a video device, stopping the photobooth."); throw new Error("Unable to get a video device, stopping the photobooth.");
} }
async function countDownToPicture(){
return new Promise((resolve) => {
console.log("Starting countdown");
// We create full-page overlay that displays a number to countdown until the picture is taken.
const countdown_div = document.getElementById("countdown");
countdown_div.style.display = "block";
// Set the different styling attributes of the element
countdown_div.setAttribute("class", "fullscreen-countdown");
// The loop must run for 3 seconds
interval = 1000;
var loops = 3;
var x = setInterval(() => {
console.log(loops);
// Update the content of the div
document.getElementById("countdown-number").innerHTML = loops;
if (loops == 0) {
countdown_div.style.display = "none";
// The timer has finished
clearInterval(x);
resolve();
}
loops--;
}, interval);
});
}
window.addEventListener('load', startup, false); window.addEventListener('load', startup, false);

View File

@@ -54,6 +54,10 @@
</div> </div>
<div id="countdown">
<div id="countdown-number"></div>
</div>
<style media="screen"> <style media="screen">
canvas { canvas {
display: none; display: none;