-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
151 lines (114 loc) · 4.53 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
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
const video = document.querySelector('.player');
const canvas = document.querySelector('.photo');
const ctx = canvas.getContext('2d');
const strip = document.querySelector('.strip');
const snap = document.querySelector('.snap');
document.getElementById('original').checked = true;
const greenScreenToggle = document.getElementById('greenscreen');
const sliders = document.getElementById('sliders');
function getVideo(){
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(localMediaStream => {
//console.log(localMediaStream);
video.srcObject = localMediaStream; // different from Wes's tutorial, his suggested code was throwing an error. According to stack overflow CreateObjectURL is deprecated for chrome
video.play();
})
.catch(function(err) {alert(err)});
}
function paintToCanvas(){
const width = video.videoWidth;
const height = video.videoHeight;
canvas.width = width;
canvas.height = height;
return setInterval(() => {
ctx.drawImage(video, 0, 0, width, height);
//takes pixels out
let pixels = ctx.getImageData(0, 0, width, height); // pixels will be reassigned to different colors, variable needs to be 'let'
//mess with pixels
if(document.getElementById('green').checked){
pixels = greenEffect(pixels);
}
if(document.getElementById('rgb').checked){
pixels = rgbSplit(pixels);
ctx.globalAlpha = 0.8; // stacks the frames for a blur effect
}
if(document.getElementById('greenscreen').checked){
pixels = greenScreen(pixels);
}
if(document.getElementById('original').checked){
pixels = clearEffects(pixels);
}
//puts pixels back
ctx.putImageData(pixels, 0, 0);
}, 16);
}
function takePhoto(){
//plays shutter sound
snap.currentTime = 0;
snap.play();
//takes the data out of canvas
const data = canvas.toDataURL('image/JPEG');
console.log(data);
const link = document.createElement('a');
link.href = data;
link.setAttribute('download', 'pretty');
link.innerHTML = `<img src = '${data}'>`;
strip.insertBefore(link, strip.firstChild);
}
function clearEffects(pixels){
for(let i=0; i < pixels.data.length; i+=4){
pixels.data[i+0] = pixels.data[i+0]; // r
pixels.data[i+1] = pixels.data[i+1]; // g
pixels.data[i+2] = pixels.data[i+2]; // b
//don't need to change a
}
document.querySelector('.rgb').style.display = "none"; // greenscreen sliders not visible by default
return pixels;
}
function greenEffect(pixels){
for(let i=0; i < pixels.data.length; i+=4){
pixels.data[i+0] = pixels.data[i+0] * 0.2126; // r
pixels.data[i+1] = pixels.data[i+1] * 0.7152; // g
pixels.data[i+2] = pixels.data[i+2] * 0.0722; // b
let v = pixels.data[i+0] + pixels.data[i+1] + pixels.data[i+2]
pixels.data[i+0] = pixels.data[i+1] = pixels.data[i+2] = v;
}
document.querySelector('.rgb').style.display = "none"; // greenscreen sliders not visible by default
return pixels;
}
function rgbSplit(pixels){
for(let i=0; i < pixels.data.length; i+=4){
pixels.data[i-150] = pixels.data[i+0]; // r
pixels.data[i+500] = pixels.data[i+1]; // g
pixels.data[i-500] = pixels.data[i+2]; // b
//don't need to change a
}
document.querySelector('.rgb').style.display = "none"; // greenscreen sliders not visible by default
return pixels;
}
function greenScreen(pixels){
const levels = {};
document.querySelectorAll('.rgb input').forEach((input) => {
levels[input.name] = input.value;
});
//console.log(levels);
for(let i=0; i < pixels.data.length; i+=4){
red = pixels.data[i+0];
green = pixels.data[i+1];
blue = pixels.data[i+2];
if(red >= levels.rmin
&& green >= levels.gmin
&& blue >= levels.bmin
&& red <= levels.rmax
&& green <= levels.gmax
&& blue <= levels.bmax){
//take it out!
pixels.data[i+3]=0;
}
}
document.querySelector('.rgb').style.display = "block";
return pixels;
}
document.querySelector('.rgb').style.display = "none"; // greenscreen sliders not visible by default
getVideo();
video.addEventListener('canplay',paintToCanvas); //listens for when video in corner is playing and automatically will paint to canvas