HTML5 CSS3 誘人的執行個體 :模仿優酷視頻截圖功能

來源:互聯網
上載者:User

一般的視頻網站對於使用者上傳的視頻,在使用者上傳完成後,可以對播放的視頻進行,然後作為視頻的展示圖。項目中也可以引入這樣的功能給使用者一種不錯的體驗,而不是讓使用者額外上傳一張展示圖。

:


看起來還是很不錯,下面我給大家分析下,極其核心代碼很簡單:

_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><div id="shotBar"></div></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中,大家可以隨便啟動個什麼伺服器,不然會報安全問題。


好了,如果這篇文章對你有協助請頂一個,同時也歡迎大家留言~




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.