-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
89 lines (72 loc) · 2.62 KB
/
index.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
(function () {
var video = document.querySelector('.camera__video'),
canvas = document.querySelector('.camera__canvas');
var getVideoStream = function (callback) {
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (navigator.getUserMedia) {
navigator.getUserMedia({video: true},
function (stream) {
video.src = window.URL.createObjectURL(stream);
video.onloadedmetadata = function (e) {
video.play();
callback();
};
},
function (err) {
console.log("The following error occured: " + err.name);
}
);
} else {
console.log("getUserMedia not supported");
}
};
var applyFilterToPixel = function (pixel) {
var filters = {
invert: function (pixel) {
pixel[0] = 255 - pixel[0];
pixel[1] = 255 - pixel[1];
pixel[2] = 255 - pixel[2];
return pixel;
},
grayscale: function (pixel) {
var r = pixel[0];
var g = pixel[1];
var b = pixel[2];
var v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
pixel[0] = pixel[1] = pixel[2] = v;
return pixel;
},
threshold: function (pixel) {
var r = pixel[0];
var g = pixel[1];
var b = pixel[2];
var v = (0.2126 * r + 0.7152 * g + 0.0722 * b >= 128) ? 255 : 0;
pixel[0] = pixel[1] = pixel[2] = v;
return pixel;
}
};
var filterName = document.querySelector('.controls__filter').value;
return filters[filterName](pixel);
};
var applyFilter = function () {
for (var x = 0; x < canvas.width; x++) {
for (var y = 0; y < canvas.height; y++) {
var pixel = canvas.getContext('2d').getImageData(x, y, 1, 1);
pixel.data = applyFilterToPixel(pixel.data);
canvas.getContext('2d').putImageData(pixel, x, y);
}
}
};
var captureFrame = function () {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
canvas.getContext('2d').drawImage(video, 0, 0);
applyFilter();
};
getVideoStream(function () {
captureFrame();
setInterval(captureFrame, 16);
});
})();