about summary refs log tree commit diff stats
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/pico-cam/index.html5
-rw-r--r--js/pico-cam/pico-cam.js79
-rw-r--r--js/pico-cam/service-worker.js4
3 files changed, 84 insertions, 4 deletions
diff --git a/js/pico-cam/index.html b/js/pico-cam/index.html
index 0948b04..05b8335 100644
--- a/js/pico-cam/index.html
+++ b/js/pico-cam/index.html
@@ -46,6 +46,10 @@
             color: #FFFFFF;
         }
 
+        .capture-frame#captureVideo.active {
+            background-color: #FF0000;
+        }
+
         .media-container {
             display: flex;
             justify-content: center;
@@ -76,6 +80,7 @@
     <div class="footer">
         <button id="toggleCamera">Turn Camera On</button>
         <button id="captureFrame" class="capture-frame" disabled>Capture Frame</button>
+        <button id="captureVideo" class="capture-frame" disabled>Capture Video</button>
     </div>
     <div class="media-container">
         <video id="webcam" autoplay playsinline style="display:none;"></video>
diff --git a/js/pico-cam/pico-cam.js b/js/pico-cam/pico-cam.js
index f51179e..3dc7646 100644
--- a/js/pico-cam/pico-cam.js
+++ b/js/pico-cam/pico-cam.js
@@ -1,5 +1,6 @@
 const toggleCameraButton = document.getElementById('toggleCamera');
 const captureFrameButton = document.getElementById('captureFrame');
+const captureVideoButton = document.getElementById('captureVideo');
 const video = document.getElementById('webcam');
 const canvas = document.getElementById('ditheredOutput');
 const context = canvas.getContext('2d');
@@ -8,6 +9,7 @@ let stream = null;
 let isCameraOn = false;
 const lowResWidth = 160; // Gameboy camera resolution: 160×144
 const lowResHeight = 144;
+const videoLength = 3000; // 3 seconds
 video.style.width = lowResWidth + 'px';
 video.style.height = lowResHeight + 'px';
 
@@ -24,7 +26,9 @@ toggleCameraButton.addEventListener('click', async () => {
             isCameraOn = true;
             toggleCameraButton.textContent = 'Turn Camera Off';
             captureFrameButton.disabled = false;
+            captureVideoButton.disabled = false;
             captureFrameButton.classList.add('active');
+            captureVideoButton.classList.add('active');
             video.addEventListener('loadeddata', () => {
                 canvas.width = lowResWidth;
                 canvas.height = lowResHeight;
@@ -39,19 +43,90 @@ toggleCameraButton.addEventListener('click', async () => {
         isCameraOn = false;
         toggleCameraButton.textContent = 'Turn Camera On';
         captureFrameButton.disabled = true;
+        captureVideoButton.disabled = true;
         captureFrameButton.classList.remove('active');
+        captureVideoButton.classList.remove('active');
     }
 });
 
 captureFrameButton.addEventListener('click', () => {
     const link = document.createElement('a');
     link.href = canvas.toDataURL('image/png');
-    link.download = 'frame.png';
+    link.download = 'dithered-frame.png';
     link.click();
 });
 
+captureVideoButton.addEventListener('click', () => {
+    const startRecording = (canvas, duration) => {
+        return new Promise((resolve, reject) => {
+            const stream = canvas.captureStream();
+            if (!stream) {
+                const errtxt = 'Stream capture from canvas failed.';
+                console.error(errtxt);
+                return reject(errtxt);
+            }
+
+            const mediaRecorder = new MediaRecorder(stream, { mimeType: 'video/webm' });
+            let chunks = [];
+
+            mediaRecorder.ondataavailable = (event) => {
+                if (event.data.size > 0) {
+                    chunks.push(event.data);
+                }
+            };
+
+            mediaRecorder.onstop = () => {
+                const blob = new Blob(chunks, { type: 'video/webm' });
+                resolve(blob);
+            };
+
+            mediaRecorder.onerror = (event) => {
+                console.error('MediaRecorder error:', event.error);
+                reject(event.error);
+            };
+
+            // console.log('Starting recording...');
+            mediaRecorder.start();
+            setTimeout(() => {
+                // console.log('Stopping recording...');
+                mediaRecorder.stop();
+            }, duration);
+        });
+    };
+
+    const handleCaptureVideo = async () => {
+        if (!isCameraOn) {
+            console.error('Camera is not active.');
+            return;
+        }
+
+        try {
+            captureVideoButton.disabled = true;
+            const videoBlob = await startRecording(ditheredOutput, videoLength);
+            const videoURL = URL.createObjectURL(videoBlob);
+
+            const videoLink = document.createElement('a');
+            videoLink.href = videoURL;
+            videoLink.download = 'dithered-video.webm';
+            videoLink.click();
+
+            // Enable the button again after recording
+            captureVideoButton.disabled = false;
+        } catch (error) {
+            console.error('Error recording video:', error);
+            captureVideoButton.disabled = false;
+        }
+    };
+
+    handleCaptureVideo();
+});
+
+
 function processFrame() {
-    if (!isCameraOn) return;
+    if (!isCameraOn) {
+        console.error('Camera is not active.');
+        return;
+    }
 
     // Crop and draw the middle part of the video frame
     const videoAspect = video.videoWidth / video.videoHeight;
diff --git a/js/pico-cam/service-worker.js b/js/pico-cam/service-worker.js
index a64353c..ce5271b 100644
--- a/js/pico-cam/service-worker.js
+++ b/js/pico-cam/service-worker.js
@@ -1,4 +1,4 @@
-var CACHE_NAME = 'pico-cam-v1';
+var CACHE_NAME = 'pico-cam-v2';
 var urlsToCache = [
   './',
   './index.html',
@@ -30,7 +30,7 @@ self.addEventListener('fetch', function(event) {
 });
 
 self.addEventListener('activate', function(event) {
-    var cacheWhitelist = ['pico-cam-v1'];
+    var cacheWhitelist = ['pico-cam-v2'];
     event.waitUntil(
         caches.keys().then(function(cacheNames) {
             return Promise.all(
boothj5@gmail.com> 2015-05-29 01:01:34 +0100 committer James Booth <boothj5@gmail.com> 2015-05-29 01:01:34 +0100 Added regex output matcher, presence test' href='/danisanti/profani-tty/commit/functionaltests/test_presence.c?id=a522d0225d7de9124ebbd18188ef9758c60cc8f5'>a522d022 ^
7f98e013 ^
272e9f83 ^

7f98e013 ^
272e9f83 ^
a522d022 ^



4ec78920 ^



a48b9fce ^
4ec78920 ^



7f98e013 ^
272e9f83 ^
7f98e013 ^
272e9f83 ^
4ec78920 ^







a48b9fce ^
4ec78920 ^



7f98e013 ^
272e9f83 ^

7f98e013 ^
272e9f83 ^
4ec78920 ^







a48b9fce ^
4ec78920 ^



7f98e013 ^
272e9f83 ^
7f98e013 ^
272e9f83 ^
4ec78920 ^







a48b9fce ^
4ec78920 ^



7f98e013 ^
272e9f83 ^

7f98e013 ^
272e9f83 ^
4ec78920 ^







a48b9fce ^
4ec78920 ^



7f98e013 ^
272e9f83 ^
7f98e013 ^
272e9f83 ^
4ec78920 ^







a48b9fce ^
4ec78920 ^



7f98e013 ^
272e9f83 ^

7f98e013 ^
272e9f83 ^
4ec78920 ^



272e9f83 ^



a48b9fce ^
272e9f83 ^



7f98e013 ^
272e9f83 ^
7f98e013 ^
272e9f83 ^








a48b9fce ^
272e9f83 ^


7f98e013 ^
272e9f83 ^
7f98e013 ^
272e9f83 ^





7f98e013 ^
272e9f83 ^


7f98e013 ^
272e9f83 ^



a5cf83d2 ^



a48b9fce ^
a5cf83d2 ^

7f98e013 ^
a5cf83d2 ^






2cb2f83c ^
ac3e7dd9 ^
2cb2f83c ^





7f98e013 ^
2cb2f83c ^










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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263