Created
March 2, 2024 15:38
-
-
Save aduh95/4443b1bd50d64b9b794ec5e0f290ebda to your computer and use it in GitHub Desktop.
Record canvas to make a video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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