-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
129 lines (105 loc) · 3.73 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
120
121
122
123
124
125
126
127
128
129
let video = document.querySelector("video");
let recordBtnCont = document.querySelector(".record-btn-cont");
let recordBtn = document.querySelector(".record-btn");
let captureBtnCont = document.querySelector(".capture-btn-cont");
let captureBtn = document.querySelector(".capture-btn");
let recordFlag = false;
let transparentColor = "transparent";
let recorder;
var chunks = []; // to store video data in chunks
let constraints = {
video: true,
audio: true,
};
// navigator -> global, browser info
navigator.mediaDevices.getUserMedia(constraints).then((stream) => {
video.srcObject = stream;
recorder = new MediaRecorder(stream);
// for recording
recorder.addEventListener("start", (e) => {
// when start new recording erasing the previous data
chunks = [];
});
recorder.addEventListener("dataavailable", (e) => {
chunks.push(e.data);
});
recorder.addEventListener("stop", (e) => {
// conversion of media chunks to video
var blob = new Blob(chunks, { type: "video/mp4" });
var videoURL = URL.createObjectURL(blob);
// creating anchor tag to show as a HTML or download
let a = document.createElement("a");
a.href = videoURL;
a.download = "stream.mp4";
a.click();
});
});
// recording start and end
recordBtnCont.addEventListener("click", (e) => {
if (!recorder) return;
recordFlag = !recordFlag; // same button will start and stop recording
if (recordFlag) {
// start recoding
recorder.start();
recordBtn.classList.add("scale-record");
startTimer();
} else {
// stop recoding
recorder.stop();
recordBtn.classList.remove("scale-record");
stopTimer();
}
});
// capture photos
captureBtnCont.addEventListener('click', (e) => {
// creating a canvas element (area) to capture with video hight ans weidth
let canvas = document.createElement("canvas");
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
let tool = canvas.getContext('2d');
// how much area do you want to capture
tool.drawImage(video, 0, 0, canvas.videoWidth, canvas.videoHeight);
tool.fillStyle = transparentColor;
tool.fillRect(0, 0, canvas.width, canvas.height);
let imageURL = canvas.toDataURL("a");
let a = document.createElement("a");
a.href = imageURL;
a.download = "image.jpg";
a.click();
})
// Filtering logic
let filterLayer = document.querySelector(".filter-layer");
let allFilters = document.querySelectorAll(".filter");
allFilters.forEach((filterElem) => {
filterElem.addEventListener("click", (e) => {
// Get style
transparentColor = getComputedStyle(filterElem).getPropertyValue("background-color");
filterLayer.style.backgroundColor = transparentColor;
})
})
// for timer
let timerID;
let counter = 0; // Represents total seconds
let timer = document.querySelector(".timer");
function startTimer() {
timer.style.display = "block";
function displayTimer() {
let totalSeconds = counter;
let hours = Number.parseInt(totalSeconds / 3600);
totalSeconds = totalSeconds % 3600; // remaining value
let minutes = Number.parseInt(totalSeconds / 60);
totalSeconds = totalSeconds % 60; // remaining value
let seconds = totalSeconds;
hours = hours < 10 ? `0${hours}` : hours;
minutes = minutes < 10 ? `0${minutes}` : minutes;
seconds = seconds < 10 ? `0${seconds}` : seconds;
timer.innerText = `${hours}:${minutes}:${seconds}`;
counter++;
}
timerID = setInterval(displayTimer, 1000);
}
function stopTimer() {
clearInterval(timerID);
timer.innerText = "00:00:00";
timer.style.display = "none";
}