-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.js
50 lines (46 loc) · 1.24 KB
/
video.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//todo: the code should refactor
let VIDEO=null;
let CANVAS=null;
let CONTEXT=null;
let SCALER=0.8;
let SIZE={
x:0,
y:0,
width:0,
height:0
};
function main(){
CANVAS=document.getElementById('theCanvas');
CONTEXT=CANVAS.getContext('2d');
navigator.mediaDevices.getUserMedia({video:true})
.then(function(signal){
VIDEO=document.createElement('video');
VIDEO.srcObject=signal;
VIDEO.play();
VIDEO.onloadeddata=function(){
handleResize();
window.addEventListener('resize', handleResize);
updateCanvas();
};
}).catch(function(err){
alert(`Camera Error ${err}`);
})
}
function handleResize() {
CANVAS.width = window.innerWidth;
CANVAS.height= window.innerHeight;
let resizer= SCALER* Math.min(
window.innerWidth/VIDEO.videoWidth,
window.innerHeight/VIDEO.videoHeight
);
SIZE.width=resizer*VIDEO.videoWidth;
SIZE.height=resizer*VIDEO.videoHeight;
SIZE.x=window.innerWidth/2-SIZE.width/2;
SIZE.y=window.innerHeight/2-SIZE.height/2;
}
function updateCanvas(){
CONTEXT.drawImage(VIDEO,
SIZE.x,SIZE.y, SIZE.width, SIZE.height);
window.requestAnimationFrame(updateCanvas);
}
main();