css3截图

日期:2021-01-20 类型:科技新闻 

关键词:微信小程序商城,免费微信小程序,小程序设计软件,小程序生成平台,可查询的小程序

1般的视頻网站针对客户提交的视頻,在客户提交进行后,能够对播发的视頻开展截图,随后做为视頻的展现图。新项目中还可以引进这样的作用给客户1种非常好的体验,而并不是让客户附加提交1张展现图。

_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时,第1个主要参数能够为video目标,随后便是根据canvas拿到DataUrl,取值给Img标识了。重要点就这些。

下面看来全部事例:

HTML:

<!DOCTYPE html>  
<html>  
<head>  
    <title></title>  
    <meta charset="utf⑻">  
    <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: ⑸px ⑸px 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⑴.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>  
<div id="shotBar">  
</div>  
</body>  
</html>  

html和css全是非常简易的。

关键看Js的编码:

/** 
 * Created with JetBrains WebStorm. 
 * User: zhy 
 * Date: 16⑹⑴8 
 * 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");  
        //建立1个和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恶性事件中获得完特性和1些实际操作后,1定要removeEventLinstener,不然中止播发会1直启用此方式。点一下恶性事件时,会中止video,随后在video的部位转化成1张照片,应用jquery动漫挪动到缩略图的部位,随后移除文本文档,缩略图显示信息,导致的动漫实际效果。

获得照片以后的提交之类的实际操作,大伙儿能够自身加上。也有很关键的1点:canvas.toDataURL("image/png");将会必须在服务器中浏览才可以一切正常应用,我把写好的网页页面拖到了tomcat中,大伙儿能够随意起动个甚么服务器,要不然会报安全性难题。