Last active
February 3, 2021 16:17
-
-
Save TBD/76b2fefd0eca6192cb430356d8cc481c to your computer and use it in GitHub Desktop.
canvas recorder (use theRecorder.stop() to stop and save as .webm 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
// from https://github.com/yellowdoge/canvas-recorder/blob/master/capturer.js | |
var canvases = document.querySelectorAll(".drawingArea"); | |
console.log("# canvases " + canvases.length); | |
var theCanvas = canvases[0]; | |
var theStream = theCanvas.captureStream(); | |
var theRecorder; | |
var recordedChunks = []; | |
try { | |
theRecorder = new MediaRecorder(theStream); | |
} catch (e) { | |
console.assert(false, 'Exception while creating MediaRecorder: ' + e); | |
} | |
theRecorder.ondataavailable = function(event) { | |
recordedChunks.push(event.data); | |
} | |
theRecorder.onstop = function() { | |
console.log('stopping!'); | |
theStream.getVideoTracks()[0].stop(); | |
saveByteArray(recordedChunks, 'test.webm'); | |
} | |
theRecorder.start(); | |
console.log('recording!'); | |
function saveByteArray(data, name) { | |
var blob = new Blob(data, {type: "video/webm"}); | |
var url = URL.createObjectURL(blob); | |
var a = document.createElement("a"); | |
document.body.appendChild(a); | |
a.style = "display: none"; | |
a.href = url; | |
a.download = name; | |
a.click(); | |
URL.revokeObjectURL(url); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment