gpt4 book ai didi

javascript - html 5 Canvas 中使用的预加载视频

转载 作者:行者123 更新时间:2023-11-28 03:42:27 31 4
gpt4 key购买 nike

我得到了以下代码以在 Canvas 中使用视频,但我想知道是否可以预加载视频。完全下载,完成后播放。更好的是,如何以一定的百分比逐步下载,比如 80%。

<!DOCTYPE html>

<html land="en">
<head>
<meta charset="utf-8">
<title>Using video in the canvas tag</title>
<script type="text/javascript">

function init(){
var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');

var vid = document.createElement('video');
vid.src = 'http://upload.wikimedia.org/wikipedia/commons/8/8c/Anthropoides_paradisea1.ogg';
vid.autoplay = true;
vid.loop = true;

setInterval(function() {
ctx.drawImage(vid, 0, 0);
}, 60);
}
</script>

</head>


<body onLoad="init();">
<canvas id="canvas1" width="500" height="500"></canvas>

</body>


</html>

最佳答案

我相信您会对 canplaythrough 事件感兴趣。当它看起来已经足够加载时触发,如果以当前速率播放,它将在播放完成之前完全加载。

var vid, ctx;

function play() {
vid.play();
setInterval(function() {
ctx.drawImage(vid, 0, 0);
}, 60);
}

function init() {
var can = document.getElementById('canvas1');
ctx = can.getContext('2d');
vid = document.createElement('video');
vid.src = "http://upload.wikimedia.org/wikipedia/commons/8/8c/Anthropoides_paradisea1.ogg";
vid.autoplay = false;
vid.loop = true;
vid.addEventListener("canplaythrough", play, false);
}

Example jsFiddle

这是处理所有不同媒体元素事件的好资源:http://www.w3.org/2010/05/video/mediaevents.html .

关于javascript - html 5 Canvas 中使用的预加载视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9540035/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com