Skip to content

Commit 08dac3f

Browse files
committed
Finished day 19
1 parent b5904ec commit 08dac3f

File tree

2 files changed

+95
-102
lines changed

2 files changed

+95
-102
lines changed

19 - Webcam Fun/scripts-FINISHED.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

19 - Webcam Fun/scripts.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,98 @@ const canvas = document.querySelector('.photo');
33
const ctx = canvas.getContext('2d');
44
const strip = document.querySelector('.strip');
55
const snap = document.querySelector('.snap');
6+
7+
function getVideo () {
8+
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
9+
.then(localMediaStream => {
10+
video.src = window.URL.createObjectURL(localMediaStream);
11+
video.play();
12+
})
13+
.catch(err => {
14+
console.error(`Please allow your webcam!`, err);
15+
});
16+
}
17+
18+
function paintToCanvas () {
19+
const width = video.videoWidth;
20+
const height = video.videoHeight;
21+
canvas.width = width;
22+
canvas.height = height;
23+
24+
return setInterval(() => {
25+
ctx.drawImage(video, 0, 0, width, height);
26+
//take pixels out
27+
let pixels = ctx.getImageData(0, 0, width, height);
28+
29+
//mess with them
30+
//pixels = redEffect(pixels);
31+
// pixels = rgbSplit(pixels);
32+
pixels = greenScreen(pixels);
33+
34+
// ctx.globalAlpha = 0.1;
35+
//put them out
36+
ctx.putImageData(pixels, 0, 0);
37+
}, 16);
38+
}
39+
40+
function takePhoto () {
41+
snap.currentTime = 0;
42+
snap.play();
43+
44+
//take the data out of the canvas
45+
const data = canvas.toDataURL('image/jpeg');
46+
//this is base64 - text based representation of the photo
47+
const link = document.createElement('a');
48+
link.href = data;
49+
link.setAttribute('download', 'handsome');
50+
link.innerHTML =`<img src="${data}" alt="Hansome man" />`
51+
strip.insertBefore(link, strip.firstChild)
52+
}
53+
54+
function redEffect(pixels) {
55+
for(let i = 0; i < pixels.data.length; i+=4) {
56+
pixels.data[i + 0] = pixels.data[i + 0] * 100; //r
57+
pixels.data[i + 1] = pixels.data[i + 1] - 50; //g
58+
pixels.data[i + 2] = pixels.data[i + 2] * 0.5; //b
59+
}
60+
return pixels;
61+
}
62+
63+
function rgbSplit(pixels) {
64+
for(let i = 0; i < pixels.data.length; i+=4) {
65+
pixels.data[i - 150] = pixels.data[i + 0]; //r
66+
pixels.data[i + 100] = pixels.data[i + 1]; //g
67+
pixels.data[i - 150] = pixels.data[i + 2]; //b
68+
}
69+
return pixels;
70+
}
71+
72+
function greenScreen(pixels) {
73+
const levels = {};
74+
75+
document.querySelectorAll('.rgb input').forEach((input) => {
76+
levels[input.name] = input.value;
77+
});
78+
79+
for (i = 0; i < pixels.data.length; i = i + 4) {
80+
red = pixels.data[i + 0];
81+
green = pixels.data[i + 1];
82+
blue = pixels.data[i + 2];
83+
alpha = pixels.data[i + 3];
84+
85+
if (red >= levels.rmin
86+
&& green >= levels.gmin
87+
&& blue >= levels.bmin
88+
&& red <= levels.rmax
89+
&& green <= levels.gmax
90+
&& blue <= levels.bmax) {
91+
// take it out!
92+
pixels.data[i + 3] = 0;
93+
}
94+
}
95+
96+
return pixels;
97+
}
98+
getVideo();
99+
100+
video.addEventListener('canplay', paintToCanvas);

0 commit comments

Comments
 (0)