溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用html2canvas怎么截圖瀏覽器

發(fā)布時(shí)間:2021-04-12 16:14:44 來源:億速云 閱讀:198 作者:Leah 欄目:web開發(fā)

本篇文章給大家分享的是有關(guān)使用html2canvas怎么截圖瀏覽器,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

作用

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)值描述
allowTaintbooleanfalseWhether to allow cross-origin images to taint the canvas---允許跨域
backgroundstring#fffCanvas background color, if none is specified in DOM. Set undefined for transparent---canvas的背景顏色,如果沒有設(shè)定默認(rèn)白色此處被坑,我改為backgroundColor可用
heightnumbernullDefine the heigt of the canvas in pixels. If null, renders with full height of the window.---canvas高度設(shè)定
letterRenderingbooleanfalseWhether to render each letter seperately. Necessary if letter-spacing is used.---在設(shè)置了字間距的時(shí)候有用
loggingbooleanfalseWhether to log events in the console.---在console.log()中輸出信息
proxystringundefinedUrl to the proxy which is to be used for loading cross-origin images. If left empty, cross-origin images won't be loaded.---代理地址
taintTestbooleantrueWhether to test each image if it taints the canvas before drawing them---是否在渲染前測(cè)試圖片
timeoutnumber0Timeout for loading images, in milliseconds. Setting it to 0 will result in no timeout.---圖片加載延遲,默認(rèn)延遲為0,單位毫秒
widthnumbernullDefine the width of the canvas in pixels. If null, renders with full width of the window.---canvas寬度
useCORSbooleanfalseWhether 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);

以上就是使用html2canvas怎么截圖瀏覽器,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI