Skip to content

Commit aaed630

Browse files
committed
19 done
1 parent 52fdd5b commit aaed630

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

19 - Webcam Fun/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="photobooth">
1111
<div class="controls">
1212
<button onClick="takePhoto()">Take Photo</button>
13-
<!-- <div class="rgb">
13+
<div class="rgb">
1414
<label for="rmin">Red Min:</label>
1515
<input type="range" min=0 max=255 name="rmin">
1616
<label for="rmax">Red Max:</label>
@@ -29,7 +29,7 @@
2929
<input type="range" min=0 max=255 name="bmin">
3030
<label for="bmax">Blue Max:</label>
3131
<input type="range" min=0 max=255 name="bmax">
32-
</div> -->
32+
</div>
3333
</div>
3434

3535
<canvas class="photo"></canvas>

19 - Webcam Fun/scripts.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,84 @@ 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 => console.log(err))
14+
}
15+
16+
function paintToCanvas() {
17+
const width = video.videoWidth
18+
const height = video.videoHeight
19+
canvas.width = width
20+
canvas.height = height
21+
22+
return setInterval(() => {
23+
ctx.drawImage(video, 0, 0, width, height)
24+
let pixels = ctx.getImageData(0, 0, width, height)
25+
// pixels.data = redEffect(pixels.data)
26+
// pixels.data = rgbSplit(pixels.data)
27+
// ctx.globalAlpha = 0.1
28+
pixels.data = greenScreen(pixels.data)
29+
ctx.putImageData(pixels, 0, 0)
30+
}, 16)
31+
}
32+
33+
function takePhoto() {
34+
snap.currentTime = 0
35+
snap.play()
36+
37+
const data = canvas.toDataURL('image/jpeg')
38+
const link = document.createElement('a')
39+
link.href = data
40+
link.setAttribute('download', 'handsome')
41+
link.innerHTML = `<img src="${data}" alt="handsome bitch" />`
42+
strip.insertBefore(link, strip.firstChild)
43+
}
44+
45+
function redEffect(pix) {
46+
for(i = 0; i < pix.length; i+=4) {
47+
pix[i] = pix[i] + 100
48+
pix[i + 1] = pix[i + 1] - 50
49+
pix[i + 2] = pix[i + 2] * 0.5
50+
}
51+
return pix
52+
}
53+
54+
function rgbSplit(pix) {
55+
for(i = 0; i < pix.length; i+=4) {
56+
pix[i - 150] = pix[i]
57+
pix[i + 100] = pix[i + 1]
58+
pix[i - 150] = pix[i + 2]
59+
}
60+
return pix
61+
}
62+
63+
function greenScreen(pix) {
64+
const levels = {}
65+
document.querySelectorAll('.rgb input').forEach(input => {
66+
levels[input.name] = input.value
67+
})
68+
69+
for (i = 0; i < pix.length; i+=4) {
70+
red = pix[i]
71+
blue = pix[i + 1]
72+
green = pix[i + 2]
73+
alpha = pix[i + 3]
74+
if (red >= levels.rmin
75+
&& green >= levels.gmin
76+
&& blue >= levels.bmin
77+
&& red <= levels.rmax
78+
&& green <= levels.gmax
79+
&& blue <= levels.bmax) {
80+
pix[i + 3] = 0
81+
}
82+
}
83+
return pix
84+
}
85+
getVideo()
86+
video.addEventListener('canplay', paintToCanvas)

0 commit comments

Comments
 (0)