HTML5+CSS3如何模仿優(yōu)酷視頻截圖功能

這篇文章給大家分享的是有關(guān)HTML5+CSS3如何模仿優(yōu)酷視頻截圖功能的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括惠民網(wǎng)站建設(shè)、惠民網(wǎng)站制作、惠民網(wǎng)頁(yè)制作以及惠民網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,惠民網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到惠民省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

效果圖:

HTML5+CSS3如何模仿優(yōu)酷視頻截圖功能

看起來(lái)還是很不錯(cuò),下面我給大家分析下,極其核心代碼很簡(jiǎn)單:

_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時(shí),第一個(gè)參數(shù)可以為video對(duì)象,然后就是通過(guò)canvas拿到DataUrl,賦值給Img標(biāo)簽了。關(guān)鍵點(diǎn)就這些。

下面來(lái)看整個(gè)例子:

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>  
<div id="shotBar">  
</div>  
</body>  
</html>

html和css都是相當(dāng)簡(jiǎn)單的。

主要看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");  
  
        //創(chuàng)建一個(gè)和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);  
  
        //創(chuàng)建縮略圖,準(zhǔn)備加到shotBar  
        var $img = $("<img>");  
        $img.attr("src", dataUrl);  
        $(this).append($img);  
  
        var offset = _getOffset($img[0]);  
        $img.hide();  
        //添加動(dòng)畫(huà)效果  
        $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();  
        });  
  
  
    }  
  
    /** 
     * 獲取元素在顯示區(qū)域的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,否則暫停播放會(huì)一直調(diào)用此方法。點(diǎn)擊事件時(shí),會(huì)暫停video,然后在video的位置生成一張圖片,使用jquery動(dòng)畫(huà)移動(dòng)到縮略圖的位置,然后移除文檔,縮略圖顯示,造成的動(dòng)畫(huà)效果。

得到圖片之后的上傳之類的操作,大家可以自己添加。還有很重要的一點(diǎn):canvas.toDataURL("image/png");可能需要在服務(wù)器中訪問(wèn)才能正常使用,我把寫(xiě)好的頁(yè)面拖到了tomcat中,大家可以隨便啟動(dòng)個(gè)什么服務(wù)器,不然會(huì)報(bào)安全問(wèn)題。

感謝各位的閱讀!關(guān)于“HTML5+CSS3如何模仿優(yōu)酷視頻截圖功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享標(biāo)題:HTML5+CSS3如何模仿優(yōu)酷視頻截圖功能
網(wǎng)址分享:http://muchs.cn/article10/pdhsdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、全網(wǎng)營(yíng)銷推廣建站公司、網(wǎng)站排名、ChatGPT、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)