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'); 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'; // This is a utility function I'm using to try and debut why Safari is claiming that "NotSupportedError: mimeType is not supported" const getSupportedMimeType = possibleTypes => { for (let i = 0; i < possibleTypes.length; i++) { if (MediaRecorder.isTypeSupported(possibleTypes[i])) { return possibleTypes[i]; } } return false; } // console.log(getSupportedMimeType(['video/webm;codecs=vp8', 'video/webm', 'video/mp4'])); toggleCameraButton.addEventListener('click', async () => { if (!isCameraOn) { try { let constraints = { video: true }; if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) { constraints = { video: { width: { ideal: 1280 }, height: { ideal: 720 }, facingMode: 'environment' } }; } stream = await navigator.mediaDevices.getUserMedia(constraints); video.srcObject = stream; video.style.display = 'block'; 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; requestAnimationFrame(processFrame); }); } catch (err) { console.error('Failed to access the camera: ', err); } } else { stream.getTracks().forEach(track => track.stop()); video.style.display = 'none'; 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 = 'dithered-frame.png'; link.click(); }); captureVideoButton.addEventListener('click', () => { const startRecording = (canvas, duration, frameRate) => { return new Promise((resolve, reject) => { const stream = canvas.captureStream(frameRate); if (!stream) { const errtxt = 'Stream capture from canvas failed.'; console.error(errtxt); return reject(errtxt); } const mimeType = getSupportedMimeType(['video/webm;codecs=vp8', 'video/webm', 'video/mp4']); const mediaRecorder = new MediaRecorder(stream, { mimeType: mimeType }); let chunks = []; mediaRecorder.ondataavailable = (event) => { if (event.data.size > 0) { chunks.push(event.data); } }; mediaRecorder.onstop = () => { const blob = new Blob(chunks, { type: mimeType }); resolve(blob); }; mediaRecorder.onerror = (event) => { console.error('MediaRecorder error:', event.error); reject(event.error); }; // Set the frame rate mediaRecorder.onstart = () => { const options = mediaRecorder.videoBitsPerSecond; mediaRecorder.videoBitsPerSecond = options * frameRate; }; mediaRecorder.start(); captureVideoButton.disabled = true; captureVideoButton.textContent = 'Recording...'; setTimeout(() => { mediaRecorder.stop(); captureVideoButton.disabled = false; captureVideoButton.textContent = 'Capture Video'; }, duration); }); }; const handleCaptureVideo = async () => { if (!isCameraOn) { console.error('Camera is not active.'); return; } try { captureVideoButton.disabled = true; const frameRate = 10; // Frame rate const videoBlob = await startRecording(ditheredOutput, videoLength, frameRate); const videoURL = URL.createObjectURL(videoBlob); const videoLink = document.createElement('a'); videoLink.href = videoURL; if (videoBlob.type === 'video/mp4') { videoLink.download = 'dithered-video.mp4'; } else { 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() {