-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.js
More file actions
58 lines (53 loc) · 2.16 KB
/
camera.js
File metadata and controls
58 lines (53 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Get the video element, buttons, canvas, and download link
const video = document.getElementById('video');
const startButton = document.getElementById('startButton');
const snapButton = document.getElementById('snapButton');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const audio1 = document.getElementById('audio1');
const audio2 = document.getElementById('audio2');
// Function to format the current date and time for the file name
function getFormattedFileName() {
const today = new Date();
const formattedDate = today.toLocaleDateString().replace(/\//g, '-'); // Format the date as a string
const formattedTime = today.toLocaleTimeString().replace(/:/g, '-'); // Format the time as a string
return formattedDate + '_' + formattedTime;
}
// Function to start the camera
async function startCamera() {
try {
// Request access to the user's webcam
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
// Set the video source to the stream from the webcam
video.srcObject = stream;
// Play the start audio and update the UI
startt();
} catch (error) {
console.error('Error accessing the webcam', error);
}
}
// Function to take a photo and trigger download
function takePhotoAndDownload() {
// Draw the current frame from the video on the canvas
context.drawImage(video, 0, 0, canvas.width, canvas.height);
// Create a data URL from the canvas
const dataURL = canvas.toDataURL('image/png');
// Create an anchor element to trigger download
const link = document.createElement('a');
link.href = dataURL;
link.download = getFormattedFileName() + '.png';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
// Play the shutter sound
audio2.play();
}
// Function to update the UI when starting the camera
function startt() {
startButton.style.display = "none";
snapButton.style.display = "block";
audio1.play();
}
// Add event listeners to the buttons
startButton.addEventListener('click', startCamera);
snapButton.addEventListener('click', takePhotoAndDownload);