-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.js
94 lines (66 loc) · 2.18 KB
/
main.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function init() {
var main = document.querySelector('main');
var mosaicContainer = document.getElementById('mosaic');
var videoWidth= 0, videoHeight = 0;
var videoElement;
var shooter;
var imagesPerRow = 5;
var maxImages = 20;
window.addEventListener('resize', onResize);
GumHelper.startVideoStreaming(function(error, stream, videoEl, width, height) {
if(error) {
alert('Cannot open the camera. Sad times: ' + error.message);
return;
}
videoElement = videoEl;
videoElement.width = width / 4;
videoElement.height = height / 4;
videoWidth = width;
videoHeight = height;
main.appendChild(videoElement);
shooter = new VideoShooter(videoElement);
onResize();
startCapturing();
});
function startCapturing() {
shooter.getShot(onFrameCaptured, 10, 0.2, function onProgress(progress) {
// Not doing anything in the callback,
// but you could animate a progress bar or similar using the `progress` value
});
}
function onFrameCaptured(pictureData) {
var img = document.createElement('img');
img.src = pictureData;
var imageSize = getImageSize();
img.style.width = imageSize[0] + 'px';
img.style.height = imageSize[1] + 'px';
mosaicContainer.insertBefore(img, mosaicContainer.firstChild);
if(mosaicContainer.childElementCount > maxImages) {
mosaicContainer.removeChild(mosaicContainer.lastChild);
}
setTimeout(startCapturing, 10);
}
function getImageSize() {
var windowWidth = window.innerWidth;
var imageWidth = Math.round(windowWidth / imagesPerRow);
var imageHeight = (imageWidth / videoWidth) * videoHeight;
return [ imageWidth, imageHeight ];
}
function onResize(e) {
// Don't do anything until we have a video element from which to derive sizes
if(!videoElement) {
return;
}
var imageSize = getImageSize();
var imageWidth = imageSize[0] + 'px';
var imageHeight = imageSize[1] + 'px';
for(var i = 0; i < mosaicContainer.childElementCount; i++) {
var img = mosaicContainer.children[i];
img.style.width = imageWidth;
img.style.height = imageHeight;
}
videoElement.style.width = imageWidth;
videoElement.style.height = imageHeight;
}
}
window.addEventListener('DOMContentLoaded', init);