Spaces:
Runtime error
Runtime error
Create static/js/script.js
Browse files- static/js/script.js +98 -0
static/js/script.js
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
let video = document.getElementById('video');
|
2 |
+
let canvas = document.getElementById('canvas');
|
3 |
+
let captureButton = document.getElementById('capture-btn');
|
4 |
+
let switchButton = document.getElementById('switch-btn');
|
5 |
+
let retakeButton = document.getElementById('retake-btn');
|
6 |
+
let uploadButton = document.getElementById('upload-btn');
|
7 |
+
let cameraIcon = document.getElementById('camera-icon');
|
8 |
+
let status = document.getElementById('status');
|
9 |
+
let stream = null;
|
10 |
+
let facingMode = 'environment'; // Default to back camera
|
11 |
+
|
12 |
+
// Start camera when clicking the camera icon
|
13 |
+
cameraIcon.addEventListener('click', async () => {
|
14 |
+
try {
|
15 |
+
if (stream) {
|
16 |
+
stream.getTracks().forEach(track => track.stop());
|
17 |
+
}
|
18 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
19 |
+
video: { facingMode: facingMode }
|
20 |
+
});
|
21 |
+
video.srcObject = stream;
|
22 |
+
video.style.display = 'block';
|
23 |
+
cameraIcon.style.display = 'none';
|
24 |
+
captureButton.style.display = 'inline-block';
|
25 |
+
switchButton.style.display = 'inline-block';
|
26 |
+
status.textContent = 'Camera started. Click Capture to take a photo.';
|
27 |
+
} catch (err) {
|
28 |
+
status.textContent = 'Error accessing camera: ' + err.message;
|
29 |
+
}
|
30 |
+
});
|
31 |
+
|
32 |
+
// Capture image
|
33 |
+
captureButton.addEventListener('click', () => {
|
34 |
+
canvas.width = video.videoWidth;
|
35 |
+
canvas.height = video.videoHeight;
|
36 |
+
canvas.getContext('2d').drawImage(video, 0, 0);
|
37 |
+
video.style.display = 'none';
|
38 |
+
canvas.style.display = 'block';
|
39 |
+
captureButton.style.display = 'none';
|
40 |
+
switchButton.style.display = 'none';
|
41 |
+
retakeButton.style.display = 'inline-block';
|
42 |
+
uploadButton.style.display = 'inline-block';
|
43 |
+
status.textContent = 'Image captured. Choose to retake or upload.';
|
44 |
+
});
|
45 |
+
|
46 |
+
// Switch camera view
|
47 |
+
switchButton.addEventListener('click', async () => {
|
48 |
+
facingMode = facingMode === 'environment' ? 'user' : 'environment';
|
49 |
+
if (stream) {
|
50 |
+
stream.getTracks().forEach(track => track.stop());
|
51 |
+
}
|
52 |
+
try {
|
53 |
+
stream = await navigator.mediaDevices.getUserMedia({
|
54 |
+
video: { facingMode: facingMode }
|
55 |
+
});
|
56 |
+
video.srcObject = stream;
|
57 |
+
status.textContent = `Switched to ${facingMode === 'environment' ? 'back' : 'front'} camera.`;
|
58 |
+
} catch (err) {
|
59 |
+
status.textContent = 'Error switching camera: ' + err.message;
|
60 |
+
}
|
61 |
+
});
|
62 |
+
|
63 |
+
// Retake photo
|
64 |
+
retakeButton.addEventListener('click', () => {
|
65 |
+
canvas.style.display = 'none';
|
66 |
+
video.style.display = 'block';
|
67 |
+
captureButton.style.display = 'inline-block';
|
68 |
+
switchButton.style.display = 'inline-block';
|
69 |
+
retakeButton.style.display = 'none';
|
70 |
+
uploadButton.style.display = 'none';
|
71 |
+
status.textContent = 'Camera restarted. Click Capture to take a new photo.';
|
72 |
+
});
|
73 |
+
|
74 |
+
// Upload to Instagram
|
75 |
+
uploadButton.addEventListener('click', () => {
|
76 |
+
let imageData = canvas.toDataURL('image/jpeg');
|
77 |
+
status.textContent = 'Uploading to Instagram...';
|
78 |
+
fetch('/upload', {
|
79 |
+
method: 'POST',
|
80 |
+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
81 |
+
body: 'image=' + encodeURIComponent(imageData)
|
82 |
+
})
|
83 |
+
.then(response => response.json())
|
84 |
+
.then(data => {
|
85 |
+
if (data.error) {
|
86 |
+
status.textContent = 'Error: ' + data.error + (data.details ? ' - ' + JSON.stringify(data.details) : '');
|
87 |
+
} else {
|
88 |
+
status.textContent = data.message;
|
89 |
+
canvas.style.display = 'none';
|
90 |
+
retakeButton.style.display = 'none';
|
91 |
+
uploadButton.style.display = 'none';
|
92 |
+
cameraIcon.style.display = 'block';
|
93 |
+
}
|
94 |
+
})
|
95 |
+
.catch(err => {
|
96 |
+
status.textContent = 'Upload failed: ' + err.message;
|
97 |
+
});
|
98 |
+
});
|