-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
35 lines (30 loc) · 1.26 KB
/
camera.js
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
window.addEventListener('DOMContentLoaded', () => {
const videoElement = document.getElementById('video-background');
const canvasElement = document.getElementById('canvas');
const context = canvasElement.getContext('2d');
let mediaStream;
// Verificar si el navegador es compatible con getUserMedia
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia({ video: { facingMode: 'environment' } })
.then((stream) => {
mediaStream = stream;
videoElement.srcObject = mediaStream;
videoElement.play();
})
.catch((error) => {
console.error('Error al acceder a la cámara: ', error);
});
} else {
console.error('getUserMedia no es soportado por el navegador');
}
videoElement.addEventListener('play', () => {
const drawFrame = () => {
if (videoElement.paused || videoElement.ended) {
return;
}
context.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
requestAnimationFrame(drawFrame);
};
requestAnimationFrame(drawFrame);
});
});