时间:2021-07-01 10:21:17 帮助过:20人阅读
核心代码就这几行,利用了ctx.drawImage时,第一个参数可以为video对象,然后就是通过canvas拿到DataUrl,赋值给Img标签了。关键点就这些。
下面来看整个例子:
HTML:
- <!DOCTYPE html>
- <html>
- <head>
- <title></title>
- <meta charset="utf-8">
- <style type="text/css">
- html
- {
- overflow: hidden;
- }
- body
- {
- background-color: #999;
- }
- video
- {
- display: block;
- margin: 60px auto 0;
- }
- #shotBar
- {
- position: absolute;
- bottom: 5px;
- height: 120px;
- width: 98%;
- background-color: #000;
- box-shadow: -5px -5px 10px #fff;
- border-radius: 5px;
- padding: 2px;
- overflow: auto;
- }
- #shotBar img
- {
- border: 3px solid #fff;
- border-radius: 5px;
- height: 110px;
- width: 210px;
- margin-left: 4px;
- }
- </style>
- <script type="text/javascript" src="../../../jquery-1.8.3.js"></script>
- <script type="text/javascript" src="videoshot.js"></script>
- <script type="text/javascript">
- $(function ()
- {
- ZhangHongyang.click2shot.init();
- });
- </script>
- </head>
- <body>
- <video src="media/style.mp4" controls id="video">
- </video>
- <p id="shotBar">
- </p>
- </body>
- </html>
html和css都是相当简单的。
主要看Js的代码:
需要注意的是,video.canplay事件中获取完属性和一些操作后,一定要removeEventLinstener,否则暂停播放会一直调用此方法。点击事件时,会暂停video,然后在video的位置生成一张图片,使用jquery动画移动到缩略图的位置,然后移除文档,缩略图显示,造成的动画效果。
得到图片之后的上传之类的操作,大家可以自己添加。还有很重要的一点:canvas.toDataURL("image/png");可能需要在服务器中访问才能正常使用,我把写好的页面拖到了tomcat中,大家可以随便启动个什么服务器,不然会报安全问题。
以上就是HTML5 CSS3模仿优酷视频截图功能示例 的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!