这段代码可以将Canvas录制为webm视频文件
var canvas = document.getElementById("mycanvas");
var stream = canvas.captureStream();
var recorder = new MediaRecorder(stream, { mimeType: 'video/webm' });
console.log("开始录制");
const data = [];
recorder.ondataavailable = function (event) {
if (event.data && event.data.size) {
data.push(event.data);
}
};
recorder.onstop = () => {
//结束录制时下载视频
const url = URL.createObjectURL(new Blob(data, { type: 'video/webm' }));
var element = document.createElement('a');
element.setAttribute('href', url);
element.setAttribute('download', "");
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
//录制开始
recorder.start();
//5秒后录制结束
setTimeout(() => {
recorder.stop();
console.log("结束录制");
}, 5000);