I'm trying to play a video to HTML Canvas. Am having no issues except when I attempt to hide the video source and only display canvas. In this case I just get a 'white screen'. If the video element isn't hidden everything works as expected. I don't want to display the video element and the canvas element, I only want to display the canvas element that has the video feed 'streaming' to it. Any suggestions? Code example attached...
let sourcevideo = document.getElementById("sourcevideo");
let outputcanvas = document.getElementById("outputcanvas");
let canvasctx = outputcanvas.getContext("2d");
function initCanvas() {
outputcanvas.width = sourcevideo.videoWidth;
outputcanvas.height = sourcevideo.videoHeight;
console.log(outputcanvas.width, outputcanvas.height);
drawVideo();
}
function drawVideo() {
canvasctx.drawImage(
sourcevideo,
0,
0,
outputcanvas.width,
outputcanvas.height
);
requestAnimationFrame(drawVideo);
}
sourcevideo.addEventListener("loadeddata", initCanvas);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="videocontainer">
<video id="sourcevideo" controls src="https://upload.wikimedia.org/wikipedia/commons/transcoded/c/c4/Physicsworks.ogv/Physicsworks.ogv.240p.vp9.webm"></video>
</div>
<canvas id="outputcanvas"></canvas>
</body>
</html>