Trying to use Canvas and DrawImage to display low-rate video at 90 degrees

风流意气都作罢 提交于 2019-12-08 07:56:47

问题


I would like to display rotated camera video at a rate of 2 frames/second. The following code displays a 240 x 320 image rotated at 90 degrees. The image does not update unless I refresh the browser. The source that is changing is cam_1.jpg. It seems that once DrawImage has a src image, it does look for an update.

What have I not learned yet? Thank You for your time and expertise.

<title>CamDisplay</title></head><body>
<canvas height="320" width="240"></canvas>
<script type="text/javascript" charset="utf-8">
    var cam = new Image;
    window.onload = function(){
        var c = document.getElementsByTagName('canvas')[0];
        var ctx = c.getContext('2d');
        setInterval(function(){
            ctx.save();
            ctx.clearRect( 0, 0, 240, 320 );
            ctx.translate( 240, 0);

            cam.src = 'cam_1.jpg?uniq='+Math.random();

            ctx.rotate( 1.57);
            ctx.drawImage( cam, 0, 0 );
                            ctx.restore();
        },500);
    };
</script>
</body>
</html>

回答1:


the image is not getting time to load. try

var cam = new Image;
var c = document.getElementsByTagName('canvas')[0];
var ctx = c.getContext('2d');

cam.onload = function() {
        ctx.save();
         ctx.clearRect( 0, 0, 240, 320 );
         ctx.translate( 240, 0);
         ctx.rotate( 1.57);
         ctx.drawImage( this, 0, 0 );
        ctx.restore();  
    }

window.onload = function(){
    setInterval(function(){
        cam.src = 'cam_1.jpg?uniq='+Math.random();
    },500);
}


来源:https://stackoverflow.com/questions/9138617/trying-to-use-canvas-and-drawimage-to-display-low-rate-video-at-90-degrees

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!