Skip to content

Instantly share code, notes, and snippets.

@aduh95
Created March 2, 2024 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aduh95/4443b1bd50d64b9b794ec5e0f290ebda to your computer and use it in GitHub Desktop.
Save aduh95/4443b1bd50d64b9b794ec5e0f290ebda to your computer and use it in GitHub Desktop.
Record canvas to make a video
function recordCanvas(canvas, time) {
const frameRate = 60;
const mimeType = "video/webm";
const chunks = [];
function saveChunks(evt) {
// store our final video's chunks
if (evt.data.size > 0) {
chunks.push(evt.data);
}
}
function exportVideo() {
const a = document.createElement("a");
a.href = URL.createObjectURL(
new Blob(chunks, {
type: mimeType,
})
);
a.download = "images.webm";
a.id = "download_button";
a.append("Download video");
document.body.append(a);
console.log(a);
}
const recorder = new MediaRecorder(canvas.captureStream(frameRate), { mimeType });
recorder.ondataavailable = saveChunks;
recorder.onstop = exportVideo;
recorder.start();
setTimeout(() => {
recorder.stop();
}, time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment