HTML5 Game - Canvas Video

Draw video frame by frame on canvas

Demo

ResultView the demo in separate window

<!DOCTYPE HTML> 
<html>
    <head>
        <script>
            window.requestAnimFrame = (function(callback){
                return window.requestAnimationFrame ||
                window.webkitRequestAnimationFrame ||
                window.mozRequestAnimationFrame ||
                window.oRequestAnimationFrame ||
                window.msRequestAnimationFrame ||
                function(callback){
                    window.setTimeout(callback, 1000 / 60);
                };/*from www .  j  a  v  a  2 s.  c o  m*/
            })();
            
            function drawFrame(context, video){
                context.drawImage(video, 0, 0);
                requestAnimFrame(function(){
                    drawFrame(context, video);
                });
            }
            
            window.onload = function(){
                let canvas = document.getElementById("myCanvas");
                let context = canvas.getContext("2d");
                let video = document.getElementById("myVideo");
                drawFrame(context, video);
            };
        </script>
    </head>
    <body>
        <video id="myVideo" autoplay="true" loop="true">
            <source src="http://java2s.com/style/demo/your.webm" type="video/webm"/>
        </video>
        <canvas id="myCanvas" width="600" height="360" style="border:1px solid black;">
        </canvas>
    </body>
</html>

Note

To draw a video on an HTML5 canvas, embed a video tag in the HTML document.

Related Topics