前端使用domtoimage生成截圖的詳細(xì)步驟
前端生成截圖有多種方式:
- 使用html2canvas,在之前文章中已有具體介紹(使用html2canvas生成截圖)這個(gè)插件在生成截圖的時(shí)候有一些弊端,在canvas繪制時(shí)耗時(shí)長(zhǎng),且繪制時(shí)屏幕會(huì)阻塞無法操作
- 使用domtoimage(官方文檔)這個(gè)插件在使用時(shí)會(huì)相對(duì)絲滑很多,一下簡(jiǎn)單介紹使用方法(具體可結(jié)合使用html2canvas生成截圖食用)
npm install dom-to-image //下載插件 import domtoimage from 'dom-to-image'; //引入
const screenImage = (screen) => { //screen為要下載的dom元素 return new Promise((resolve, reject) => { const formData = new FormData() domtoimage.toBlob(screen, { //直接轉(zhuǎn)化為二進(jìn)制格式,可以直接將圖片下載 style: { backgroundColor: '#17496D' //給它一個(gè)背景色 } }) .then(function(blob) { formData.append('image', blob, 'image.png') resolve(formData) }) .catch(function(error) { console.error('截圖生成失敗', error) }) }) }
domtoimage方法
domtoimage.toPng(…);將節(jié)點(diǎn)轉(zhuǎn)化為png格式的圖片 domtoimage.toJpeg(…);將節(jié)點(diǎn)轉(zhuǎn)化為jpg格式的圖片
domtoimage.toSvg(…);將節(jié)點(diǎn)轉(zhuǎn)化為svg格式的圖片,生成的圖片的格式都是base64格式
domtoimage.toBlob(…);將節(jié)點(diǎn)轉(zhuǎn)化為二進(jìn)制格式,這個(gè)可以直接將圖片下載
domtoimage.toPixelData(…);獲取原始像素值,以Uint8Array
數(shù)組的形式返回,每4個(gè)數(shù)組元素表示一個(gè)像素點(diǎn),即rgba值。這個(gè)方法也是挺實(shí)用的,可以用于WebGL中編寫著色器顏色。
domtoimage屬性
filter : 過濾器節(jié)點(diǎn)中默寫不需要的節(jié)點(diǎn);
bgcolor : 圖片背景顏色;
height, width : 圖片寬高;
style:傳入節(jié)點(diǎn)的樣式,可以是任何有效的樣式;
quality : 圖片的質(zhì)量,也就是清晰度;一個(gè)介于 0 和 1 之間的數(shù)字,表示 JPEG圖像的圖像質(zhì)量(例如 0.92 => 92%)。默認(rèn)為 1.0 (100%)
cacheBust :將時(shí)間戳加入到圖片的url中,相當(dāng)于添加新的圖片;
imagePlaceholder :圖片生成失敗時(shí),在圖片上面的提示,相當(dāng)于img標(biāo)簽的alt;
附:dom-to-image實(shí)現(xiàn)的網(wǎng)頁(yè)截圖
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>echarts 測(cè)試</title> <script type="text/javascript" src="dom-to-image.js"></script> </head> <body style="position:relative;"> <div style="width:100px;height:100px;background-color:#000;color:white;"> 尾氣請(qǐng)問氣味哦平均氣溫哦啤酒去i </div> <div style="width:100px;height:100px;background-color:#ccc;"></div> <div style="width:100px;height:100px;background-color:#666;color:#ccc;"> 請(qǐng)我陪考去請(qǐng)問哦 </div> <div style="width:100px;height:100px;background-color:#000;color:white;"> 尾氣請(qǐng)問氣味哦平均氣溫哦啤酒去i </div> <div style="width:100px;height:100px;background-color:#ccc;"></div> <div style="width:100px;height:100px;background-color:#000;color:white;"> 尾氣請(qǐng)問氣味哦平均氣溫哦啤酒去i </div> <div style="width:100px;height:100px;background-color:#ccc;"></div> <div style="width:100px;height:100px;background-color:#ddd;"></div> <div style="width:900px;height:100px;background-color:#666;"> 橋文件額期望寄哦IQ叫我我就群毆我為奇偶去叫哦我IQ寄哦 </div> <div οnclick="jt()">點(diǎn)擊截圖</div> <script> var pointInfo= {}; document.οnmοusedοwn=function(e){ if(!pointInfo.bool)return; pointInfo.startInfo={ x:e.clientX+window.scrollX, y:e.clientY+window.scrollY }; pointInfo.eleArr[1].style.left=e.clientX+window.scrollX+"px"; pointInfo.eleArr[1].style.top=e.clientY+window.scrollY+"px"; } document.οnmοusemοve=function(e){ if(!pointInfo.bool)return; if(!pointInfo.startInfo)return; pointInfo.eleArr[1].style.width=e.clientX-pointInfo.startInfo.x+window.scrollX+"px"; pointInfo.eleArr[1].style.height=e.clientY-pointInfo.startInfo.y+window.scrollY+"px"; } document.οnmοuseup=function(e){ if(!pointInfo.bool)return; if(!pointInfo.startInfo)return; pointInfo.bool=false; var c = document.createElement("canvas"); node=document.body; document.body.removeChild( pointInfo.eleArr[0]); var promise = domtoimage.toPng(node); promise.then(function(v){ var img =new Image(); img.src=v; c.width=parseInt(pointInfo.eleArr[1].style.width); c.height=parseInt(pointInfo.eleArr[1].style.height); var ctx = c.getContext("2d"); img.οnlοad=function(){ ctx.drawImage(img,pointInfo.startInfo.x,pointInfo.startInfo.y,c.width,c.height,0,0,c.width,c.height); var imgS=document.createElement("img"); imgS.src=c.toDataURL(); document.body.appendChild(imgS); pointInfo.startInfo=null; } }); } function jt(){ var d= document.createElement("div"); var d2=document.createElement("div"); d.style.cssText="width:100%;height:100%;background-color:rgba(0,0,0,0.2);position:absolute;top:0;left:0;"; d2.style.cssText="position:absolute;background:rgba(255,255,255,0.2);"; pointInfo.eleArr= [d,d2]; pointInfo.bool=true; d.appendChild(d2); document.body.appendChild(d); } </script> </body> </html>
總結(jié)
到此這篇關(guān)于前端使用domtoimage生成截圖的文章就介紹到這了,更多相關(guān)前端domtoimage生成截圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
js trim函數(shù) 去空格函數(shù)與正則集錦
在javascript中處理文本框輸入值的時(shí)候,經(jīng)常要用到"去掉前后空白"的功能。用過jQuery的朋友都知道,jQuery提供了一個(gè)trim()這樣的功能函數(shù),可以很輕松幫我們實(shí)現(xiàn)這樣的效果。2009-11-11JavaScript JSON.stringify()的使用總結(jié)
JSON是一種輕量級(jí)數(shù)據(jù)格式,可以方便地表示復(fù)雜數(shù)據(jù)結(jié)構(gòu)。JSON對(duì)象有兩個(gè)方法:stringify()和parse()。在簡(jiǎn)單的情況下,這兩個(gè)方法分別可以將JavaScript序列化為JSON字符串,以及將JSON解析為原生JavaScript值。本文著重介紹JSON.stringify()的使用方法和注意事項(xiàng)。2021-05-05Javascript自執(zhí)行匿名函數(shù)(function() { })()的原理淺析
匿名函數(shù)就是沒有函數(shù)名的函數(shù)。這篇文章主要介紹了Javascript自執(zhí)行匿名函數(shù)(function() { })()的原理淺析的相關(guān)資料,需要的朋友可以參考下2016-05-05js+canvas實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了js+canvas實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02通過實(shí)例了解Render Props回調(diào)地獄解決方案
這篇文章主要介紹了通過實(shí)例了解Render Props回調(diào)地獄解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11一文徹底搞懂JavaScrip中的call、apply、arguments
在學(xué)習(xí)JavaScript時(shí),你可能會(huì)遇到call、arguments和apply這三個(gè)方法,這篇文章主要介紹了關(guān)于JavaScrip中的call、apply、arguments的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04element-ui?對(duì)話框dialog使用echarts報(bào)錯(cuò)'dom沒有獲取到'的問題
這篇文章主要介紹了element-ui?對(duì)話框dialog里使用echarts,報(bào)錯(cuò)'dom沒有獲取到'的問題,在這個(gè)事件里邊進(jìn)行echarts的初始化,執(zhí)行數(shù)據(jù),本文結(jié)合實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-11-11