-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
51 lines (44 loc) · 1.57 KB
/
script.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
51
const canvas = document.querySelector("#output");
var imageCapture;
const totalFrames = 10;
const frames = new Array();
function init() {
navigator.mediaDevices.getUserMedia(
{video: true, audio: false})
.then(gotMedia)
.catch(error => console.error(error));
}
function gotMedia(mediaStream) {
const mediaStreamTrack = mediaStream.getVideoTracks()[0];
imageCapture = new ImageCapture(mediaStreamTrack);
console.log(imageCapture);
setInterval(takeStill, 1000);
}
function takeStill() {
imageCapture.takePhoto()
.then(blob => createImageBitmap(blob))
.then(imageBitmap => {
frames.push(imageBitmap)
if (frames.length == totalFrames + 1) frames.shift();
drawCanvas();
})
.catch(error => console.error(error));
}
function drawCanvas() {
canvas.width = getComputedStyle(canvas).width.split('px')[0];
canvas.height = getComputedStyle(canvas).height.split('px')[0];
let ratio = Math.min(canvas.width / frames[0].width, canvas.height / frames[0].height);
let x = (canvas.width - frames[0].width * ratio) / 2;
let y = (canvas.height - frames[0].height * ratio) / 2;
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
frames.forEach(frame => {
ctx.globalCompositeOperation = "screen"
ctx.drawImage(frame, 0, 0, frame.width, frame.height,
x, y, frame.width * ratio, frame.height * ratio);
});
}
function getFrame(index) {
return document.querySelector("#frame" + index);
}
window.addEventListener('load', init, false);