-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecord_canvas.js
More file actions
41 lines (38 loc) · 1.32 KB
/
Copy pathrecord_canvas.js
File metadata and controls
41 lines (38 loc) · 1.32 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
// code from https://dev.to/ndesmic/how-to-record-a-canvas-element-and-make-a-gif-4852
// author ndesmic
const recordBtn = document.getElementById("record-button");
let recording = false;
let mediaRecorder;
let recordedChunks;
recordBtn.addEventListener("click", () => {
recording = !recording;
if(recording){
recordBtn.textContent = "Stop";
const stream = canvas.captureStream(25);
mediaRecorder = new MediaRecorder(stream, {
mimeType: 'video/webm;codecs=vp9',
ignoreMutedMedia: true
});
recordedChunks = [];
mediaRecorder.ondataavailable = e => {
if(e.data.size > 0){
recordedChunks.push(e.data);
}
};
mediaRecorder.start();
} else {
recordBtn.textContent = "Record"
mediaRecorder.stop();
setTimeout(() => {
const blob = new Blob(recordedChunks, {
type: "video/webm"
});
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "recording.webm";
a.click();
URL.revokeObjectURL(url);
},0);
}
});