video视频转成canvas(兼容至IE8+,全原生JS)

这里写图片描述
video视频转成canvas(兼容至IE8+,全原生JS)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
<!-- video说明:属性应用
 width="100%"
 height="240px"  /*如果有封面,请设置高度*/
 controls  /*这个属性规定浏览器为该视频提供播放控件*/  
 style="object-fit:fill"  /*加这个style会让 Android / web 的视频在微信里的视频全屏,如果是在手机上预览,会让视频的封面同视频一样大小*/
 webkit-playsinline="true"  /*这个属性是ios 10中设置可以让视频在小窗内播放,也就是不是全屏播放*/  
 x-webkit-airplay="true"  /*这个属性还不知道作用*/ 
 playsinline="true"  /*IOS微信浏览器支持小窗内播放*/ 
 x5-video-player-type="h5" /*启用H5播放器,是wechat安卓版特性*/
 x5-video-orientation="h5" /*播放器支付的方向,landscape横屏,portraint竖屏,默认值为竖屏*/
 x5-video-player-fullscreen="true" /*全屏设置,设置为 true 是防止横屏*/
 preload="auto" /*这个属性规定页面加载完成后载入视频*/ 
 -->
<video id="video" controls loop autoplay="" width='300' webkit-playsinline="true" x-webkit-airplay="true" playsinline="true" x5-video-player-type="h5" src='http://www.w3school.com.cn/example/html5/mov_bbb.mp4'></video>
<button id="play" class="play">播放</button>
<button id="pause" class="pause">暂停</button>
<!-- <script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script> -->
<script>
/*
 * video视频转成canvas(兼容至IE8+)
 * Author: tc   Created On: 2018-08-08
 * 
 *  使用方法:
 *      var videoCanvas = new VideoToCanvas(videoDom);
 *
 *  对象的属性:
 *      暂无。
 *
 *  对象的方法:
 *      play       播放视频
 *      pause      暂停视频
 *      playPause  播放或暂停视频
 *      change(src) 切换视频。参数src为切换的视频地址
 */
var VideoToCanvas = (function(window, document) {
  function VideoToCanvas(videoElement) {
    if(!videoElement) {return;}

    var canvas = document.createElement('canvas');
    canvas.width = videoElement.offsetWidth;
    canvas.height = videoElement.offsetHeight;
    ctx = canvas.getContext('2d');

    var newVideo = videoElement.cloneNode(false);

    var timer = null;

    var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
                                window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

    var cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;

    function drawCanvas() {
      ctx.drawImage(newVideo, 0, 0, canvas.width, canvas.height);
      timer = requestAnimationFrame(drawCanvas);
    }

    function stopDrawing() {
      cancelAnimationFrame(timer);
    }

    newVideo.addEventListener('play', function() {
      drawCanvas();
    },false);
    newVideo.addEventListener('pause', stopDrawing, false);
    newVideo.addEventListener('ended', stopDrawing, false);

    videoElement.parentNode.replaceChild(canvas, video);

    this.play = function() {
      newVideo.play();
    };

    this.pause = function() {
      newVideo.pause();
    };

    this.playPause = function() {
      if(newVideo.paused) {
        this.play();
      } else {
        this.pause();
      }
    };

    this.change = function(src) {
      if(!src) {return;}
      newVideo.src = src;
    };

    this.drawFrame = drawCanvas;
  }

  return VideoToCanvas;

})(window, document); 
</script> 
<script>
window.onload=function(){
  var video = document.getElementById('video');
  var videoCanvas = new VideoToCanvas(video);
  var playClass = document.getElementById('play');
  var pauseClass = document.getElementById('pause');

  playClass.addEventListener("click",function(){
    videoCanvas.play();
  },false);
  pauseClass.addEventListener("click",function(){
    videoCanvas.pause();
  },false);
}  
</script>
</body>
</html>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值