Javascript 为什么在MediaRecorder中使用drawImage时canvas.captureStream中的视频为空

Javascript 为什么在MediaRecorder中使用drawImage时canvas.captureStream中的视频为空,javascript,html,google-chrome,web-mediarecorder,Javascript,Html,Google Chrome,Web Mediarecorder,我在canvas元素中有一个完美工作的演示动画,我可以使用MediaRecorder和captureStream将其录制为webm视频文件 2d上下文api中的动画在生成的视频中表现良好,但当我尝试使用drawImage向画布添加图像时,我似乎无法使其正常工作。在后一种情况下,MediaRecorder.ondataavailable处理程序不接收有效数据,生成的视频文件为0字节文件 我甚至实现了一个演示,在那里我可以切换是否执行drawImage调用。在下面的代码中,如果drawImage=f

我在canvas元素中有一个完美工作的演示动画,我可以使用MediaRecorder和captureStream将其录制为webm视频文件

2d上下文api中的动画在生成的视频中表现良好,但当我尝试使用drawImage向画布添加图像时,我似乎无法使其正常工作。在后一种情况下,MediaRecorder.ondataavailable处理程序不接收有效数据,生成的视频文件为0字节文件

我甚至实现了一个演示,在那里我可以切换是否执行drawImage调用。在下面的代码中,如果drawImage=false,则生成视频时不会出现问题,但如果将drawImage切换为true,则会生成一个0字节的文件

为了演示,我将这个jsfiddle放在一起

我在Chrome版本75.0.3770.100的MacOS上运行这段代码官方版本64位-甚至不确定它是否应该在Firefox等上运行,因为MediaRecorder API在FF上抛出看似无关的错误

请参见此处的完整代码:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<canvas id="drawing_canvas" width="1280" height="720"></canvas>

<script>
    const image = new Image();
    image.src = 'https://66.media.tumblr.com/84d332cafeb1052c477c979281e5713b/tumblr_owe3l0tkCj1wxdq3zo1_1280.jpg';

    window.requestAnimationFrame(animation);

    const drawImage = false;  // toggle to 'true' to make this example fail
    const canvas = document.getElementById('drawing_canvas');

    const allChunks = [];
    const recorder = initMediaRecorder(canvas);
    recorder.start();

    setTimeout(function (e) {
        console.log("Video ended");
        recorder.stop();
    }, 5000);

    function initMediaRecorder(canvasElement) {
        const stream = canvasElement.captureStream(60);
        const recorder = new MediaRecorder(stream, {mimeType: 'video/webm'});
        recorder.ondataavailable = function (e) {
            console.log("data handler called");
            if (e.data) {
                console.log("data available: " + e.data.size)
                if (e.data.size > 0) {
                    console.log("data added");
                    allChunks.push(e.data);
                }
            } else {
                console.error("Data handler received no data in event: " + JSON.stringify(e))
            }
        };

        recorder.onstop = function (e) {
            const fullBlob = new Blob(allChunks);
            const link = document.createElement('a');
            link.style.display = 'none';

            link.href = window.URL.createObjectURL(fullBlob);
            link.download = 'media.webm';

            document.body.appendChild(link);
            link.click();
            link.remove();
        };
        return recorder;
    }


    function animation() {
        const now = new Date();
        const ctx = document.getElementById('drawing_canvas').getContext('2d');

        if (drawImage) {
            ctx.drawImage(image, 0, 0);
        }

        ctx.clearRect(0, 0, 150, 150);

        ctx.strokeStyle = 'white';
        ctx.fillStyle = 'white';
        ctx.rect(0, 0, 1280, 720);
        ctx.stroke();

        ctx.save();

        ctx.translate(75, 75);
        ctx.scale(0.4, 0.4);
        ctx.rotate(-Math.PI / 2);
        ctx.strokeStyle = 'black';
        ctx.fillStyle = 'white';
        ctx.lineWidth = 8;
        ctx.lineCap = 'round';

        // Hour marks
        ctx.save();
        for (var i = 0; i < 12; i++) {
            ctx.beginPath();
            ctx.rotate(Math.PI / 6);
            ctx.moveTo(100, 0);
            ctx.lineTo(120, 0);
            ctx.stroke();
        }
        ctx.restore();

        // Minute marks
        ctx.save();
        ctx.lineWidth = 5;
        for (i = 0; i < 60; i++) {
            if (i % 5 != 0) {
                ctx.beginPath();
                ctx.moveTo(117, 0);
                ctx.lineTo(120, 0);
                ctx.stroke();
            }
            ctx.rotate(Math.PI / 30);
        }
        ctx.restore();

        const sec = now.getSeconds();
        const min = now.getMinutes();
        let hr = now.getHours();
        hr = hr >= 12 ? hr - 12 : hr;

        ctx.fillStyle = 'black';

        // write Hours
        ctx.save();
        ctx.rotate(hr * (Math.PI / 6) + (Math.PI / 360) * min + (Math.PI / 21600) * sec);
        ctx.lineWidth = 14;
        ctx.beginPath();
        ctx.moveTo(-20, 0);
        ctx.lineTo(80, 0);
        ctx.stroke();
        ctx.restore();

        // write Minutes
        ctx.save();
        ctx.rotate((Math.PI / 30) * min + (Math.PI / 1800) * sec);
        ctx.lineWidth = 10;
        ctx.beginPath();
        ctx.moveTo(-28, 0);
        ctx.lineTo(112, 0);
        ctx.stroke();
        ctx.restore();

        // Write seconds
        ctx.save();
        ctx.rotate(sec * Math.PI / 30);
        ctx.strokeStyle = '#D40000';
        ctx.fillStyle = '#D40000';
        ctx.lineWidth = 6;
        ctx.beginPath();
        ctx.moveTo(-30, 0);
        ctx.lineTo(83, 0);
        ctx.stroke();
        ctx.beginPath();
        ctx.arc(0, 0, 10, 0, Math.PI * 2, true);
        ctx.fill();
        ctx.beginPath();
        ctx.arc(95, 0, 10, 0, Math.PI * 2, true);
        ctx.stroke();
        ctx.fillStyle = 'rgba(0, 0, 0, 0)';
        ctx.arc(0, 0, 3, 0, Math.PI * 2, true);
        ctx.fill();
        ctx.restore();

        ctx.beginPath();
        ctx.lineWidth = 14;
        ctx.strokeStyle = '#325FA2';
        ctx.arc(0, 0, 142, 0, Math.PI * 2, true);
        ctx.stroke();

        ctx.restore();

        window.requestAnimationFrame(animation);
    }

</script>
</body>
</html>
更新:上述行为已确认至少可在这些Chrome/ium版本上复制:

版本75.0.3770.100在MacOS Mojave 10.14.5上正式构建64位

版本77.0.3849.0 MacOS Mojave 10.14.5上的官方构建64位金丝雀


版本77.0.3770.100 Ubuntu 19.04 Disco Dingo上的官方构建快照64位

,这是因为您的图像来自跨域资源并污染了您的画布。 污染正在捕获媒体流的画布将阻止该媒体流捕获任何新图像

此外,尝试从这样一个受污染的画布捕获媒体流将抛出一个SecurityError

const ctx=canvas.getContext'2d'; const stream=canvas.captureStream; vid.srcObject=流; const img=新图像; img.onload=e=>{ console.log“将污染画布” ctx.drawImageimg,0,0; //如果我们现在尝试捕获一个新的流,我们有一个明显的错误 const stream2=canvas.captureStream; } img.src=https://66.media.tumblr.com/84d332cafeb1052c477c979281e5713b/tumblr_owe3l0tkCj1wxdq3zo1_1280.jpg; ctx.fillRect0,0,20,20;