Spaces:
Build error
Build error
Update script.js
Browse files
script.js
CHANGED
|
@@ -2,22 +2,29 @@ const video = document.getElementById("video");
|
|
| 2 |
const canvas = document.getElementById("canvas");
|
| 3 |
const output = document.getElementById("output");
|
| 4 |
let capturedBlob = null;
|
|
|
|
| 5 |
|
| 6 |
// Start the camera
|
| 7 |
async function startCamera() {
|
| 8 |
try {
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
video.srcObject = stream;
|
| 12 |
video.play();
|
| 13 |
console.log("Camera started successfully.");
|
| 14 |
} catch (err) {
|
| 15 |
-
// Handle errors
|
| 16 |
alert("Camera access denied or unavailable.");
|
| 17 |
console.error("Error accessing camera:", err);
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
// Capture the image
|
| 22 |
document.getElementById("capture").addEventListener("click", () => {
|
| 23 |
if (!video.srcObject) {
|
|
|
|
| 2 |
const canvas = document.getElementById("canvas");
|
| 3 |
const output = document.getElementById("output");
|
| 4 |
let capturedBlob = null;
|
| 5 |
+
let currentFacingMode = "user"; // Default camera mode
|
| 6 |
|
| 7 |
// Start the camera
|
| 8 |
async function startCamera() {
|
| 9 |
try {
|
| 10 |
+
const stream = await navigator.mediaDevices.getUserMedia({
|
| 11 |
+
video: { facingMode: currentFacingMode },
|
| 12 |
+
});
|
| 13 |
video.srcObject = stream;
|
| 14 |
video.play();
|
| 15 |
console.log("Camera started successfully.");
|
| 16 |
} catch (err) {
|
|
|
|
| 17 |
alert("Camera access denied or unavailable.");
|
| 18 |
console.error("Error accessing camera:", err);
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
+
// Flip the camera
|
| 23 |
+
document.getElementById("flipCamera").addEventListener("click", () => {
|
| 24 |
+
currentFacingMode = currentFacingMode === "user" ? "environment" : "user";
|
| 25 |
+
startCamera();
|
| 26 |
+
});
|
| 27 |
+
|
| 28 |
// Capture the image
|
| 29 |
document.getElementById("capture").addEventListener("click", () => {
|
| 30 |
if (!video.srcObject) {
|