html5/css3 诱人的实例 -模仿优酷视频截图功能的详解:
一般的视频网站对于用户上传的视频,在用户上传完成后,可以对播放的视频进行截图,然后作为视频的展示图。项目中也可以引入这样的功能给用户一种不错的体验,而不是让用户额外上传一张展示图。
效果图:
看起来还是很不错,下面我给大家分析下,极其核心代码很简单:
_canvas = document.createelement("canvas");
_ctx = _canvas.getcontext("2d");
_ctx.fillstyle = '#ffffff';
_ctx.fillrect(0, 0, _videowidth, _videowidth);
_ctx.drawimage(_video, 0, 0, _videowidth, _videoheight, 0, 0, _videowidth, _videoheight);
var dataurl = _canvas.todataurl("image/png");
核心代码就这几行,利用了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的代码:
/**
* created with jetbrains webstorm.
* user: zhy
* date: 14-6-18
* time: 上午12:24
* to change this template use file | settings | file templates.
*/
var zhanghongyang = {};
zhanghongyang.click2shot = (function ()
{
var _id_video = "video";
var _id_shotbar = "shotbar";
var _videowidth = 0;
var _videoheight = 0;
var _canvas = null;
var _ctx = null;
var _video = null;
function _init()
{
_canvas = document.createelement("canvas");
_ctx = _canvas.getcontext("2d");
_video = document.getelementbyid(_id_video);
_video.addeventlistener("canplay", function ()
{
_canvas.width = _videowidth = _video.videowidth;
_canvas.height = _videoheight = _video.videoheight;
console.log(_videowidth + " , " + _videoheight);
_ctx.fillstyle = '#ffffff';
_ctx.fillrect(0, 0, _videowidth, _videowidth);
$("#" + _id_shotbar).click(_click2shot);
_video.removeeventlistener("canplay", arguments.callee);
});
}
function _click2shot(event)
{
_video.pause();
_ctx.drawimage(_video, 0, 0, _videowidth, _videoheight, 0, 0, _videowidth, _videoheight);
var dataurl = _canvas.todataurl("image/png");
//创建一个和video相同位置的图片
var $imgbig = $("<img/>");
$imgbig.width(_videowidth).height(_videoheight).css({position: "absolute", left: _video.offsetleft, top: _video.offsettop,
width: _videowidth + "px", height: _videowidth + "px"}).attr("src", dataurl);
$("body").append($imgbig);
//创建缩略图,准备加到shotbar
var $img = $("<img>");
$img.attr("src", dataurl);
$(this).append($img);
var offset = _getoffset($img[0]);
$img.hide();
//添加动画效果
$imgbig.animate({left: offset.x + "px", top: offset.y + "px", width: $img.width() + "px", height: $img.height() + "px"}, 200, function ()
{
$img.attr("src", dataurl).show();
$imgbig.remove();
_video.play();
});
}
/**
* 获取元素在显示区域的leftoffset和topoffset
* @param elem
* @returns {{x: (number|number), y: (number|number)}}
* @private
*/
function _getoffset(elem)
{
var pos = {x: elem.offsetleft, y: elem.offsettop};
var offsetparent = elem.offsetparent;
while (offsetparent)
{
pos.x += offsetparent.offsetleft;
pos.y += offsetparent.offsettop;
offsetparent = offsetparent.offsetparent;
}
return pos;
}
return {init: _init}
})();
需要注意的是,video.canplay事件中获取完属性和一些操作后,一定要removeeventlinstener,否则暂停播放会一直调用此方法。点击事件时,会暂停video,然后在video的位置生成一张图片,使用jquery动画移动到缩略图的位置,然后移除文档,缩略图显示,造成的动画效果。
得到图片之后的上传之类的操作,大家可以自己添加。还有很重要的一点:canvas.todataurl(image/png);可能需要在服务器中访问才能正常使用,我把写好的页面拖到了tomcat中,大家可以随便启动个什么服务器,不然会报安全问题。
以上就是html5/css3 诱人的实例 -模仿优酷视频截图功能的详解的详细内容。