-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (86 loc) · 3.23 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
window.addEventListener('DOMContentLoaded', function() {
const video = document.getElementById('video');
const canvasInput = document.getElementById('canvasInput');
const ctx = canvasInput.getContext('2d');
const canvasOutput = document.getElementById('canvasOutput');
//if navigator.maxTouchPoints > 1 assume it's mobile and use front camera
//else is desktop and use regular user facing camera
const videoMode = navigator.maxTouchPoints > 1 ? { 'facingMode': { 'exact': 'environment' } } : true;
const mediaConfig = {
'video': videoMode
};
let image = new MarvinImage();
let width, height;
let typeFilter = 1;
function setupView() {
width = window.innerWidth;
height = window.innerHeight;
canvasInput.setAttribute('width', width);
canvasInput.setAttribute('height', height);
canvasOutput.setAttribute('width', width);
canvasOutput.setAttribute('height', height);
video.setAttribute('width', width);
video.setAttribute('height', height);
}
function loop() {
let loopFrame = requestAnimationFrame(loop);
ctx.save();
//ctx.globalAlpha = 0.05;
ctx.drawImage(video, 0, 0, width, height);
ctx.restore();
image.load(canvasInput.toDataURL('image/png'), function() {
//Marvin.colorChannel(image, image, 14, 0, -8);
switch (typeFilter) {
case 1:
Marvin.grayScale(image, image);
break;
case 2:
Marvin.blackAndWhite(image, image, 30);
break;
case 3:
Marvin.sepia(image, image, 30);
break;
case 4:
Marvin.invertColors(image, image);
break;
}
image.draw(canvasOutput);
});
}
const selectFilter = document.getElementById('filter');
selectFilter.addEventListener('change', e => {
typeFilter = parseInt(e.target.value, 10);
});
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
setupView();
canvasOutput.addEventListener('click', function() {
if (!document.fullscreenElement) {
canvasOutput
.requestFullscreen()
.then({})
.catch(err => {
alert(`Error attempting to enable full-screen mode: ${err.message} (${err.name})`);
});
} else {
document.exitFullscreen();
}
//https://stackoverflow.com/a/25322717/343900
window.setTimeout(function() {
setupView();
}, 500);
});
window.addEventListener('orientationchange', function() {
// https://stackoverflow.com/a/25322717/343900
window.setTimeout(function() {
setupView();
}, 300);
});
navigator.mediaDevices.getUserMedia(mediaConfig).then(function(stream) {
video.srcObject = stream;
});
//invoked once
video.addEventListener('loadedmetadata', function() {
loop();
});
}
});