-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
112 lines (95 loc) · 3.73 KB
/
script.js
File metadata and controls
112 lines (95 loc) · 3.73 KB
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
const videoElement = document.querySelector("video");
const canvasElement = document.querySelector("canvas");
const context = canvasElement.getContext("2d", { willReadFrequently: true });
const downloadArea = document.querySelector(".download-area");
const snapSound = document.querySelector("audio");
var timer;
const zoom = document.querySelector("#zoom");
const posX = document.querySelector("#zoom-offset-x")
const posY = document.querySelector("#zoom-offset-y")
function getVideo() {
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(stream => videoElement.srcObject = stream);
}
function paintToCanvas() {
let height = videoElement.videoHeight;
let width = videoElement.videoWidth;
canvasElement.height = height;
canvasElement.width = width;
timer = setInterval(() => {
context.clearRect(0, 0, width, height);
context.drawImage(videoElement,
width * posX.value, height * posY.value, width * (1 - posX.value), height * (1 - posY.value),
0, 0, width * Number(zoom.value) * (1 - posX.value), height * Number(zoom.value) * (1 - posY.value));
const image = context.getImageData(0, 0, width, height);
context.putImageData(filter(image), 0, 0);
}, 16);
}
function takePhoto() {
snapSound.play();
const data = canvasElement.toDataURL("image/png");
const downloadLink = document.createElement("a");
downloadLink.href = data;
downloadLink.innerHTML = `<img src=${data}>`
downloadLink.setAttribute("download", "photo.png");
downloadArea.prepend(downloadLink);
}
videoElement.addEventListener("canplay", paintToCanvas);
document.querySelector(".takePhoto").addEventListener("click", takePhoto);
document.querySelector(".startBtn").addEventListener("click", getVideo);
document.querySelector(".stopBtn").addEventListener("click", () => {
clearInterval(timer);
videoElement.srcObject.getTracks().forEach(track => {
track.stop();
})
})
const greyScale = document.querySelector("#greyscale");
const blackWhite = document.querySelector("#blackWhite");
const color = document.querySelector("#color");
const rmColor = document.querySelector("#rm-color");
const rmColorLevel = document.querySelector("#rm-color-level");
function greyScaleEffect(pixels) {
for (let i = 0; i < pixels.data.length; i += 4) {
const avg = (pixels.data[i] + pixels.data[i + 1] + pixels.data[i + 2]) / 3;
pixels.data[i] = avg;
pixels.data[i + 1] = avg;
pixels.data[i + 2] = avg;
}
return pixels;
}
function removeColor(pixels, r, g, b) {
const level = Number(rmColorLevel.value);
for (let i = 0; i < pixels.data.length; i += 4) {
if (
(pixels.data[i] < (r+level) && pixels.data[i] > (r-level))
&& (pixels.data[i+1] < (g + level) && pixels.data[i+1] > (g - level))
&& (pixels.data[i+2] < (b + level) && pixels.data[i+2] > (b -level))
) {
pixels.data[i+3] = 0;
}
}
return pixels;
}
const hexToRgb = hex => hex.replace("#", "").match(/.{2}/g).map(x => parseInt(x, 16));
function filter(pixels) {
if (rmColor.checked) {
const [r, g, b] = hexToRgb(color.value);
pixels = removeColor(pixels, r, g, b);
}
if (blackWhite.checked) {
pixels = blackWhiteEffect(pixels);
} else if (greyScale.checked) {
pixels = greyScaleEffect(pixels);
}
return pixels;
}
function blackWhiteEffect(pixels) {
for (let i = 0; i < pixels.data.length; i += 4) {
let avg = (pixels.data[i] + pixels.data[i + 1] + pixels.data[i + 2]) / 3;
avg = avg > 128 ? 255 : 0;
pixels.data[i] = avg;
pixels.data[i + 1] = avg;
pixels.data[i + 2] = avg;
}
return pixels;
}