JavaScript 视频画布
我正在测试画布和视频,并且已经阅读了很多示例,但是当我按下视频画布上的播放按钮时,我遇到了一个小问题,它不能播放视频,它只显示快照。
我不明白为什么它不能同时播放。这是我的 JavaScript 代码。
function draw()
{
var ctx3 = document.getElementById('c3');
var ctx1 = document.getElementById('c1');
var conts = ctx3.getContext('2d');
var videos = document.getElementById('video');
var frames = conts.getImageData(0,0,300,300);
videos.addEventListener("play", function ()
{
conts.drawImage(videos, 0, 0, 300, 300);
var l = frames.data.length/4;
for (var i = 0; i<1; i++){
var r = frames.data(i * 4 + 0);
var g = frames.data(i * 4 + 1);
var b = frames.data(i * 4 + 2);
if (g > 100 && r > 100 && b < 43)
frames.data(i * 4 + 3) = 0;
ctx1.putImageData(frames,300,300);
return;
}
});
}
</script>
<body onload="draw()">
<canvas id="c1" width="300" height="300"></canvas>
<canvas id="c2" width="300" height="300" ></canvas>
<canvas id="c3" width="300" height="300"></canvas>
<video id="video" src="https://developer.mozilla.org/samples/video/chroma-key/video.ogv" width=video.width height= video.height controls="true"></video>
</body>
I am testing out canvas and video and I have read up on alot of examples, but I am running into a slight problem when I press the play button on the video canvas doesn't play the video as well it just shows a snapshot.
I can not figure out why it doesn't play simultaneously. Here is my code in javascript.
function draw()
{
var ctx3 = document.getElementById('c3');
var ctx1 = document.getElementById('c1');
var conts = ctx3.getContext('2d');
var videos = document.getElementById('video');
var frames = conts.getImageData(0,0,300,300);
videos.addEventListener("play", function ()
{
conts.drawImage(videos, 0, 0, 300, 300);
var l = frames.data.length/4;
for (var i = 0; i<1; i++){
var r = frames.data(i * 4 + 0);
var g = frames.data(i * 4 + 1);
var b = frames.data(i * 4 + 2);
if (g > 100 && r > 100 && b < 43)
frames.data(i * 4 + 3) = 0;
ctx1.putImageData(frames,300,300);
return;
}
});
}
</script>
<body onload="draw()">
<canvas id="c1" width="300" height="300"></canvas>
<canvas id="c2" width="300" height="300" ></canvas>
<canvas id="c3" width="300" height="300"></canvas>
<video id="video" src="https://developer.mozilla.org/samples/video/chroma-key/video.ogv" width=video.width height= video.height controls="true"></video>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须连续调用 conts.drawImage
示例: http://jsfiddle.net/Dmy2E/
You have to continuously call conts.drawImage
Example: http://jsfiddle.net/Dmy2E/