-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
264 lines (220 loc) · 6.71 KB
/
app.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
let streaming = false;
const width = 320;
const height = 240;
const video = document.createElement('video');
video.playsInline = true
video.style.display = 'none';
class StreamContainer {
constructor() {
this.el = document.createElement('div');
this.streams = [];
}
append(stream) {
this.streams.push(stream);
this.el.appendChild(stream.el);
};
}
class Stream {
constructor(id, video) {
this.el = document.createElement('div');
this.el.classList.add('Stream');
this.el.id = id;
this.canvas = document.createElement('canvas');
this.canvas.setAttribute('width', width);
this.canvas.setAttribute('height', height);
this.el.appendChild(this.canvas);
this.context = this.canvas.getContext('2d');
if (video) {
this.video = video;
this.video.setAttribute('width', width);
const line = document.createElement('div');
line.classList.add('Line');
this.el.appendChild(line);
}
this.snapshots = document.createElement('div');
this.snapshots.classList.add('SnapshotContainer');
this.el.appendChild(this.snapshots);
}
prependSnapshot(snapshot) {
this.snapshots.insertBefore(snapshot, this.snapshots.firstChild);
}
takeSnapshot() {
const data = this.canvas.toDataURL('image/png');
const snapshot = new Snapshot(data);
this.prependSnapshot(snapshot.el);
}
}
class Snapshot {
constructor(data) {
this.el = document.createElement('div');
this.el.classList.add('Snapshot');
const onClose = () => {
this.el.parentNode.removeChild(this.el);
}
this.closeButton = new CloseButton(onClose);
this.saveButton = new SaveButton(data);
this.photo = new Photo(data);
this.el.appendChild(this.closeButton.el);
this.el.appendChild(this.saveButton.el);
this.el.appendChild(this.photo.el);
}
}
class CloseButton {
constructor(onClose) {
this.el = document.createElement('div');
this.el.classList.add('Button');
this.el.classList.add('CloseButton');
this.el.setAttribute('title', 'Click to remove');
this.el.innerHTML = "×";
this.el.addEventListener('click', onClose);
}
}
class SaveButton {
constructor(href) {
this.el = document.createElement('a');
this.el.classList.add('Button');
this.el.classList.add('SaveButton');
this.el.setAttribute('href', href);
this.el.setAttribute('target', '_blank');
this.el.setAttribute('download', 'snapshot.png');
this.el.setAttribute('title', 'Click to download');
this.el.innerHTML = '★';
}
}
class PlayButton {
constructor() {
this.el = document.createElement('button');
this.el.classList.add('PlayButton')
this.el.innerHTML = `▶`
this.el.onclick = () => play()
}
hide() {
this.el.style.display = 'none';
}
show() {
this.el.style.display = 'initial';
}
}
class ShutterButton {
constructor() {
this.el = document.createElement('button');
this.el.classList.add('ShutterButton')
this.el.onclick = () => {
takepicture();
}
}
}
class Photo {
constructor(src) {
this.el = document.createElement('img');
this.el.setAttribute('src', src);
}
}
const explanation = document.createElement('div');
const streamContainer = new StreamContainer();
const shutterButton = new ShutterButton();
const playButton = new PlayButton();
function play() {
try {
video.play();
playButton.hide();
shutterButton.show();
} catch (err) {
console.warn(err);
}
}
document.body.appendChild(explanation);
document.body.appendChild(streamContainer.el);
document.body.appendChild(playButton.el)
document.body.appendChild(shutterButton.el);
document.body.appendChild(video);
function init() {
explanation.classList.add('Explanation');
if (navigator.mediaDevices.getUserMedia) {
explanation.innerHTML = 'You need to allow this page to use your camera';
} else {
explanation.innerHTML = 'Sorry, your browser does not support ' +
'<code>window.navigator.getUserMedia</code>';
return;
}
streamContainer.append(new Stream('left'));
streamContainer.append(new Stream('right'));
streamContainer.append(new Stream('middle', video));
navigator.mediaDevices.getUserMedia({ video: true, audio: false }).then(
(stream) => {
video.srcObject = stream
video.onloadedmetadata = () => {
play();
};
explanation.innerHTML = 'Position your face so that the red line ' +
'divides it in half. ' +
'Then press <code><SPACE></code> to take a snapshot';
addEventListener('keydown', onKeydown);
},
function(err) {
console.log("An error occured! " + err);
}
);
video.addEventListener('canplay', function(ev) {
if (!streaming) {
video.setAttribute('width', video.videoWidth * 2);
video.setAttribute('height', video.videoHeight * 2);
streamContainer.streams.forEach(function(stream) {
stream.canvas.setAttribute('width', width);
stream.canvas.setAttribute('height', height);
});
streaming = true;
}
}, false);
setInterval(function() {
render();
}, 30);
}
function render() {
const left = streamContainer.streams[0].context;
left.drawImage(video, 0, 0, width, height);
const middle = streamContainer.streams[2].context;
middle.drawImage(video, 0, 0, width, height);
const right = streamContainer.streams[1].context;
right.drawImage(video, 0, 0, width, height);
const flipdata_left = flip(left.getImageData(0, 0, width, height), 0, width / 2);
const flipdata_right = flip(right.getImageData(0, 0, width, height), width / 2, width);
left.putImageData(flipdata_left, 0, 0);
right.putImageData(flipdata_right, 0, 0);
}
function takepicture() {
streamContainer.streams.forEach(function(stream) {
stream.takeSnapshot();
});
}
function onKeydown(event) {
if (event.key === ' ') {
event.preventDefault();
takepicture();
}
}
function flip(imageData, from, to) {
const data = imageData.data;
const width = imageData.width;
const height = imageData.height;
// http://stackoverflow.com/a/13933017
for (let y = 0; y < height; y++) {
for (let x = from; x < to; x++) { // divide by 2 to only loop through the left half of the image.
const offset = ((width * y) + x) * 4; // Pixel origin
// Get pixel
const r = data[offset];
const g = data[offset + 1];
const b = data[offset + 2];
const a = data[offset + 3];
// Calculate how far to the right the mirrored pixel is
const mirrorOffset = (width - (x * 2)) * 4;
// Get set mirrored pixel's colours
data[offset + mirrorOffset] = r;
data[offset + 1 + mirrorOffset] = g;
data[offset + 2 + mirrorOffset] = b;
data[offset + 3 + mirrorOffset] = a;
}
}
return imageData;
}
init()