在canvas中使用video播放视频

<!DOCTYPE html>
<meta charset="utf-8"> 
<style>
	.zIndex1{
		z-index:1;
	}
	.zIndex-1{
		z-index:-1;
	}
</style>
<div style="border:1px solid #eee;position: absolute;top: 0;z-index:0">
	<div id="play_stop" style="position: absolute;width: 100%;height: 100%;background-image:url(934.png);background-size: 50%;">
		<input type="button" value="播放"  id="play" />
		<input type="button" value="暂停"  id="stop" />
	</div>
	<div style="z-index:1;background: white;">
		<canvas id="canvas-1" width="500" height="600"></canvas>
	</div>
</div>
<script>
var timer = null;
var video = document.createElement("video");
var canvas = document.getElementById("canvas-1");
var ctx = canvas.getContext("2d");
var play_stop = document.getElementById("play_stop");

function drawTip(text) {
    canvas.width  = canvas.width;
    canvas.height = canvas.height;
    ctx.font = "24px Microsoft YaHei";
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
    ctx.fillText(text,canvas.width/2,canvas.height/2);
}

function init() {
    //drawTip("正在缓冲中....");
    video.src = "00KO-1.webm";
}

video.oncanplay = function() {
    //drawTip("加载完毕,开始播放")
}

function play() {
    init();
    video.play();
	play_stop.style.zIndex = -1;
    timer = setInterval(function(){
      if(video.currentTime >= video.duration){
		video.src = "00KO-2.webm";
		video.play();
        //stop();
      };
	  // 针对webm格式的视频每一次绘制前,都清除上一次画布的内容,防止重影
	  ctx.clearRect(0, 0, canvas.width, canvas.height);
      ctx.drawImage(video, (canvas.width-video.videoWidth)/2, (canvas.height-video.videoHeight)/2, video.videoWidth, video.videoHeight);//绘制视频
   },16);
}

function stop(){
    clearInterval(timer);
    video.pause();
}

//drawTip("你可以点击播放按钮播放视频....")
document.getElementById("play").onclick = function(){ play();}
document.getElementById("stop").onclick = function(){ stop();}
</script>
  • 20
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值