-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (80 loc) · 2.97 KB
/
script.js
File metadata and controls
92 lines (80 loc) · 2.97 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Function to start camera preview
function startCamera() {
const videoElement = document.getElementById("video");
// Check if getUserMedia is available in the browser
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia({ video: true })
.then((stream) => {
// Assign the stream to the video element
videoElement.srcObject = stream;
})
.catch((err) => {
console.error("Error accessing camera: ", err);
});
} else {
alert("Your browser does not support camera access.");
}
}
function captureImage() {
const video = document.getElementById("video");
const canvas = document.getElementById("canvas");
const img = document.getElementById("captured-image");
const wrapper = document.querySelector(".camera-wrapper");
wrapper.appendChild(img);
// Set canvas size to match video dimensions
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
const context = canvas.getContext("2d");
context.drawImage(video, 0, 0, canvas.width, canvas.height);
// Convert canvas to image and display it
img.src = canvas.toDataURL("image/png");
downloadImage(img.src);
}
// Download image
const downloadImage = (src) => {
const downloadBtn = document.querySelector(".download-image");
downloadBtn.classList.remove("hidden");
downloadBtn.addEventListener("click", function () {
const link = document.createElement("a");
link.href = src;
link.download = "captured-image.png";
link.click();
});
};
// Open Camera
const openCamera = document.getElementById("start-camera");
openCamera.addEventListener("click", () => {
startCamera();
document.querySelector(".camera-controls").classList.remove("hidden");
});
// Capture image
const imageCapture = document.querySelector(".capture-image");
imageCapture.addEventListener("click", () => {
captureImage();
document.querySelector(".camera-controls").classList.add("hidden");
document.querySelector("#reset-camera").classList.remove("hidden");
});
// Reset state
const resetBtn = document.getElementById("reset-camera");
resetBtn.addEventListener("click", () => {
const capturedImageSrc = document.getElementById("captured-image");
if (capturedImageSrc && capturedImageSrc.src !== "") {
capturedImageSrc.src = "";
resetBtn.classList.add("hidden");
document.querySelector(".camera-controls").classList.remove("hidden");
document.querySelector(".download-image").classList.add("hidden");
}
});
// Start Recording button
const startRecording = document.querySelector(".record-video");
startRecording.addEventListener("click", () => {
startRecording.classList.add("recording-started");
document.getElementById("stop-recording").classList.remove("hidden");
});
// Stop Recording
const stopRecording = document.getElementById("stop-recording");
stopRecording.addEventListener("click", () => {
startRecording.classList.remove("recording-started");
stopRecording.classList.add("hidden");
});