By default, camera features are disabled; they're opted into by URL param and browser prompt.

This commit is contained in:
Rezmason
2022-08-02 21:23:27 -07:00
parent 3e301e9e58
commit 8aeb156875
4 changed files with 49 additions and 32 deletions

39
js/camera.js Normal file
View File

@@ -0,0 +1,39 @@
// TODO: switch to video-based texture
// TODO: mipmap?
const video = document.createElement("video");
const cameraCanvas = document.createElement("canvas");
cameraCanvas.width = 1;
cameraCanvas.height = 1;
const context = cameraCanvas.getContext("2d");
let cameraAspectRatio = 1.0;
const drawToCanvas = () => {
requestAnimationFrame(drawToCanvas);
context.drawImage(video, 0, 0);
};
const setupCamera = async () => {
try {
const stream = await navigator.mediaDevices.getUserMedia({video: {
width: { min: 800, ideal: 1280 },
frameRate: { ideal: 60 }
}, audio: false});
const videoTrack = stream.getVideoTracks()[0];
const {width, height} = videoTrack.getSettings();
video.width = width;
video.height = height;
cameraCanvas.width = width;
cameraCanvas.height = height;
cameraAspectRatio = width / height;
video.srcObject = stream;
video.play();
drawToCanvas();
} catch (e) {
console.warn(`Camera not initialized: ${e}`);
}
};
export { cameraCanvas, cameraAspectRatio, setupCamera };