国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

css3截圖_動力節點Java學院整理

2024-07-11 08:25:29
字體:
來源:轉載
供稿:網友

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

_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: 16-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} })();
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 措勤县| 遵义市| 昔阳县| 台前县| 郁南县| 五大连池市| 孟州市| 库车县| 巴南区| 驻马店市| 贺兰县| 久治县| 谢通门县| 威海市| 平原县| 建宁县| 蛟河市| 汕头市| 巴彦县| 麟游县| 冕宁县| 林西县| 平昌县| 岫岩| 庄河市| 湛江市| 新乡市| 开江县| 油尖旺区| 同仁县| 平乐县| 沛县| 微博| 图木舒克市| 炉霍县| 启东市| 沂南县| 梅州市| 岑巩县| 商洛市| 页游|