-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
119 lines (106 loc) · 3.85 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
let recordingState = false;
let videoEle = document.querySelector("video");
let chunks = [];
let recordBtn = document.querySelector("#record");
let mediaRecorder;
let appliedFilter;
const maxZoom = 3;
const minZoom = 1;
let currZoom = 1;
let img = document.querySelector("#capture");
let body = document.querySelector("body");
let filters = document.querySelectorAll(".filter");
let zoomIn = document.querySelector(".zoom-in");
let zoomOut = document.querySelector(".zoom-out");
let galleryBtn = document.querySelector("#gallery");
galleryBtn.addEventListener("click", function () {
location.assign("gallery.html");
});
zoomIn.addEventListener("click", function (e) {
if (currZoom < maxZoom) {
currZoom = currZoom + 0.1;
videoEle.style.transform = `scale(${currZoom})`;
}
});
zoomOut.addEventListener("click", function (e) {
if (currZoom > minZoom) {
currZoom = currZoom - 0.1;
videoEle.style.transform = `scale(${currZoom})`;
}
});
for (let i = 0; i < filters.length; i++) {
filters[i].addEventListener("click", function (e) {
appliedFilter = e.currentTarget.style.backgroundColor;
let div = document.createElement("div");
removeFilter();
div.style.backgroundColor = appliedFilter;
div.classList.add("filter-div");
body.append(div);
});
}
function removeFilter() {
let filterDiv = document.querySelector(".filter-div");
if (filterDiv) filterDiv.remove();
}
//navigator is a object in browser with many functions.
//mediaDevices is one of them
//getUserMedia is defined in mediaDevces
//it takes arguements like below and you cannot change their names;
//mediaStream is a object that we get as data which is returned if the promise of getUserMedia is resolved
//vedio.scrObj takes that object to play back our video;
//browser js cannot communicate with OS features(mic ,camera etc)so browser is the medium of communication between them.
//search about cache.delete();
//if multiple cameras or mics then userMedia return a array;
img.addEventListener("click", function (e) {
let innerDiv = img.querySelector("div");
innerDiv.classList.add("imgCaptureAnimation");
setTimeout(() => {
innerDiv.classList.remove("imgCaptureAnimation");
}, 1200);
let canvas = document.createElement("canvas");
canvas.width = videoEle.videoWidth;
canvas.height = videoEle.videoHeight;
let tool = canvas.getContext("2d");
tool.translate(canvas.width / 2, canvas.height / 2);
tool.scale(currZoom, currZoom);
tool.translate(-canvas.width / 2, -canvas.height / 2);
tool.drawImage(videoEle, 0, 0);
if (appliedFilter) {
tool.fillStyle = appliedFilter;
tool.fillRect(0, 0, canvas.width, canvas.height);
}
let url = canvas.toDataURL();
addMedia(url, "image");
alert("saved in galary");
});
recordBtn.addEventListener("click", function (e) {
let innerDiv = recordBtn.querySelector("div");
currZoom = 1;
videoEle.style.transform = `scale(${currZoom})`;
removeFilter();
appliedFilter = "";
if (!recordingState) {
innerDiv.classList.add("recordAnimation");
recordingState = true;
mediaRecorder.start();
} else {
innerDiv.classList.remove("recordAnimation");
recordingState = false;
mediaRecorder.stop();
}
});
navigator.mediaDevices
.getUserMedia({ video: true, audio: true })
.then(function (mediaStream) {
mediaRecorder = new MediaRecorder(mediaStream);
videoEle.srcObject = mediaStream;
mediaRecorder.addEventListener("dataavailable", function (e) {
chunks.push(e.data);
});
mediaRecorder.addEventListener("stop", function (e) {
let blob = new Blob(chunks, { type: "video/mp4" });
chunks = [];
addMedia(blob, "video");
alert("saved in galary");
});
});