/**
* Created with JetBrains WebStorm.
* User: zhy
* Date: 14-6-18
* Time: 上午12:24
* To change this template use File | Settings | File Templates.
*/
varZhangHongyang = {};
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);
vardataUrl = _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);
varoffset = _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)
{
varpos = {x: elem.offsetLeft, y: elem.offsetTop};
varoffsetParent = elem.offsetParent;
while(offsetParent)
{
pos.x += offsetParent.offsetLeft;
pos.y += offsetParent.offsetTop;
offsetParent = offsetParent.offsetParent;
}
returnpos;
}
return{init: _init}
})();