佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 使用html2canvas實(shí)現(xiàn)瀏覽器截圖的示例代碼

使用html2canvas實(shí)現(xiàn)瀏覽器截圖的示例代碼

熱門標(biāo)簽:檢查注冊(cè)表項(xiàng) 智能手機(jī) 百度競價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 網(wǎng)站建設(shè) 美圖手機(jī) 阿里云 使用U盤裝系統(tǒng) 硅谷的囚徒呼叫中心

使用html2canvas實(shí)現(xiàn)瀏覽器截圖,必須在服務(wù)器環(huán)境下才能實(shí)現(xiàn)。

作用

html2canvas可以通過純JS對(duì)瀏覽器端經(jīng)行截屏,但截圖的精確度還有待提高,部分css不可識(shí)別,所以在canvas中不能完美呈現(xiàn)原畫面樣式

/*多行溢出省略就不行,只能超出隱藏了*/
     .book_inf{
            position: relative; 
            overflow : hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            -webkit-box-orient: vertical;
        }

支持的瀏覽器

  1. Firefox 3.5+
  2. Google Chrome
  3. Opera 12+
  4. IE9+
  5. Safari 6+

基本語法

/*參數(shù):
* #screenshots 所需要截圖的元素id,截圖后要執(zhí)行的函數(shù),
* backgroundColor 配置項(xiàng)背景色
* canvas為截圖后返回的最后一個(gè)canvas
*/
function screenshotsImg(){
       html2canvas(document.querySelector("#screenshots"),{
            backgroundColor: 'transparent',// 設(shè)置背景透明
        }).then(canvas => {
            canvasTurnImg(canvas) //保存的圖片格式轉(zhuǎn)換方法
        });
    }

可用配置項(xiàng)

參數(shù)名稱 類型 默認(rèn)值 描述
allowTaint boolean false Whether to allow cross-origin images to taint the canvas---允許跨域
background string #fff Canvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景顏色,如果沒有設(shè)定默認(rèn)白色此處被坑,我改為backgroundColor可用
height number null Define the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度設(shè)定
letterRendering boolean false Whether to render each letter seperately. Necessary if letter-spacing is used.---在設(shè)置了字間距的時(shí)候有用
logging boolean false Whether to log events in the console.---在console.log()中輸出信息
proxy string undefined Url to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.---代理地址
taintTest boolean true Whether to test each image if it taints the canvas before drawing them---是否在渲染前測試圖片
timeout number 0 Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---圖片加載延遲,默認(rèn)延遲為0,單位毫秒
width number null Define the width of the canvas in pixels. If null, renders with full width of the window.---canvas寬度
useCORS boolean false Whether to attempt to load cross-origin images as CORS served, before reverting back to proxy--跨域代理

設(shè)置圖片格式

1、從canvas中直接提取圖片元數(shù)據(jù)

  // 圖片導(dǎo)出為 png 格式
        var type = 'png';
        var imgData = canvas.toDataURL(type);

2、將mime-type改為image/octet-stream,強(qiáng)制讓瀏覽器直接download

/**
 * 獲取mimeType
 * @param  {String} type the old mime-type
 * @return the new mime-type
 */
var _fixType = function(type) {
    type = type.toLowerCase().replace(/jpg/i, 'jpeg');
    var r = type.match(/png|jpeg|bmp|gif/)[0];
    return 'image/' + r;
};
   
// 加工image data,替換mime type
imgData = imgData.replace(_fixType(type),'image/octet-stream');

3、圖片download到本地

/**
 * 在本地進(jìn)行文件保存
 * @param  {String} data     要保存到本地的圖片數(shù)據(jù)
 * @param  {String} filename 文件名
 */
var saveFile = function(data, filename){
    var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
    save_link.href = data;
    save_link.download = filename;
   
    var event = document.createEvent('MouseEvents');
    event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    save_link.dispatchEvent(event);
};
   
// 下載后的文件名
var filename = 'baidufe_' + (new Date()).getTime() + '.' + type;
// download
saveFile(imgData,filename);

案例

案例

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:通遼 懷化 煙臺(tái) 賀州 山南 湖北 黃山 湘潭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用html2canvas實(shí)現(xiàn)瀏覽器截圖的示例代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    察雅县| 岳西县| 宜城市| 襄樊市| 天等县| 济宁市| 多伦县| 庆阳市| 古蔺县| 天长市| 唐山市| 桑植县| 宁德市| 富源县| 松桃| 全州县| 武强县| 南安市| 筠连县| 灵寿县| 天水市| 疏附县| 怀集县| 福鼎市| 凭祥市| 冷水江市| 高台县| 陕西省| 蒙山县| 碌曲县| 定南县| 卓尼县| 阿拉善盟| 神木县| 青田县| 荆门市| 外汇| 抚松县| 太仓市| 寿阳县| 汪清县|