html2canvas 使用注意點
最近在搞一個截圖的東西,遇到了一些問題奸鬓,所以貼出代碼焙畔,方便大家一次性解決開發(fā)中會遇到的問題。本篇文章解決了截圖不完整串远,圖片跨域宏多,截圖空白,截圖帶背景顏色(沒有背景圖片的代碼)澡罚,分辨率低伸但,截圖與實際元素位置偏差等問題
首先貼出 插件基礎(chǔ)配置參數(shù)(請仔細(xì)閱讀注釋)
html2canvas 配置參數(shù)
名稱 | 默認(rèn) | 描述 |
---|---|---|
useCORS | false | 是否嘗試使用CORS從服務(wù)器加載圖像 |
allowTaint | false | 是否允許跨域圖像。會污染畫布留搔,導(dǎo)致無法使用canvas.toDataURL 方法 |
proxy | null | 代理將用于加載跨域圖像的網(wǎng)址更胖。如果保留為空,則不會加載跨域圖像。 |
backgroundColor | #ffffff | 畫布背景色(如果未在DOM中指定)却妨。設(shè)置null為透明 |
canvas | null | 現(xiàn)有canvas元素用作繪圖的基礎(chǔ) |
foreignObjectRendering | false | 如果瀏覽器支持饵逐,是否使用ForeignObject渲染 |
imageTimeout | 15000 | 加載圖像的超時時間(以毫秒為單位)。設(shè)置0為禁用超時管呵。 |
ignoreElements | (element) => false | 謂詞功能梳毙,可從渲染中刪除匹配的元素。 |
logging | true | 啟用日志以進(jìn)行調(diào)試 |
onclone | null | 克隆文檔以進(jìn)行渲染時調(diào)用的回調(diào)函數(shù)可用于修改將要渲染的內(nèi)容捐下,而不會影響原始源文檔账锹。 |
removeContainer | true | 是否清除html2canvas臨時創(chuàng)建的克隆DOM元素 |
scale | window.devicePixelRatio | 用于渲染的比例。默認(rèn)為瀏覽器設(shè)備像素比率坷襟。 |
width | Element 寬度 | canvas的寬度 |
height | Element 高度 | canvas的高度 |
X | Element X偏移 | 裁剪畫布X坐標(biāo) |
? | Element y偏移 | 裁剪畫布y坐標(biāo) |
scrollX | Element 滾動X | 渲染元素時要使用的x滾動位置(例如奸柬,如果Element使用position: fixed) |
scrollY | Element 滾動Y | 呈現(xiàn)元素時要使用的y-scroll位置(例如,如果Element使用position: fixed) |
windowWidth | Window.innerWidth | 渲染時使用的窗口寬度Element婴程,這可能會影響媒體查詢之類的內(nèi)容 |
windowHeight | Window.innerHeight | 渲染時要使用的窗口高度Element廓奕,這可能會影響媒體查詢之類的內(nèi)容 |
插件二次封裝,詳細(xì)請看注釋
插件部分
// 獲取截圖
export const getHtmlImage = async (options={}) => {
let { canvasBox, canvasBoxOffsetLeft, canvasBoxOffsetTop, canvasBoxOffsetHeight } = options
// 大概估算出档叔,圖片最大面積為maxPixels桌粉,超出只能舍棄一些分辨率,判斷請轉(zhuǎn)到scale配置
const maxPixels = 880 * 3270 * 0.85;
console.log('getHtmlImage 配置項=》》》》》》',options)
return new Promise (async (resolve, reject) => {
if (!canvasBox) {
resolve(null)
return
}
let domWidth = canvasBox.offsetWidth
let domHeight = canvasBoxOffsetHeight || canvasBox.offsetHeight
console.log(domWidth, domHeight)
//定時為了解決頁面太長衙四,未加載出來時就截圖铃肯,導(dǎo)致圖片不完整
setTimeout(() => {
html2canvas(canvasBox, {
// allowTaint: true, //是否允許跨域,會對canvas造成污染传蹈,導(dǎo)致無法使用canvas.toDataURL 方法
useCORS: true, //是否允許跨域
// proxy: 'https://test.test.com',
backgroundColor: '#fff', //解決生成圖片有白色背景方法押逼,設(shè)置為null
width: domWidth,
height: domHeight,
x: canvasBoxOffsetLeft || canvasBox.offsetLeft,
y: canvasBoxOffsetTop || canvasBox.offsetTop,
// x: 397,
// y: 490,
scale: maxPixels / (domWidth*domHeight) > 1 ? 1 : maxPixels / (domWidth*domHeight),
}).then((canvas)=>{
if(canvas){
let dataUrl = canvas.toDataURL('image/png'); // image/png , image/jpg
console.log('dataUrl=>>>>>>>>',dataUrl)
// let newImg = document.createElement("img");
// // //newImg.style = "display:none;"; //如果要導(dǎo)出惦界,這兒可以隱藏挑格,然后用canvas2image搞定
// newImg.src = dataUrl;
// document.body.appendChild(newImg);
resolve(dataUrl)
}else{
resolve(null)
}
});
}, 500);
})
}
普通直接截取某個元素,直接調(diào)用方法
// 截圖模塊
let canvasBox = this.document.querySelector('.canvasBox') || null
// await接收圖片地址
let url = await getHtmlImage({canvasBox})
解決截圖元素是定位的沾歪,導(dǎo)致截圖位置偏差漂彤,高度計算不準(zhǔn),截圖空白的問題灾搏,position: fixed
// 截圖模塊
let canvasBox = this.document.querySelector('.canvasBox') || null
let scrollTop = document.documentElement.scrollTop
// 左側(cè)計算方法挫望, 元素距離屏幕左側(cè)的距離
let canvasBoxOffsetLeft = canvasBox ? canvasBox.getBoundingClientRect().left : 0;
// 頂部計算方法, 元素距離頂部距離 + body滾動高度
let canvasBoxOffsetTop = canvasBox ? canvasBox.offsetTop + scrollTop : 0;
// await接收圖片地址
let url = await getHtmlImage({
canvasBox,
canvasBoxOffsetLeft,
canvasBoxOffsetTop
})
如有寫得錯誤/改進(jìn)的地方确镊,可回復(fù)評論