html2 canvas svg不能識(shí)別的解決方案
發(fā)布時(shí)間:2021-06-03 17:28:00 作者:風(fēng)一樣的大叔
我要評(píng)論

最近很多朋友聯(lián)系小編在截取網(wǎng)頁(yè)成圖片的時(shí)候,html2+canvas不能識(shí)別 svg的問(wèn)題,正好這個(gè)問(wèn)題我也遇到過(guò),下面我把處理思路分享到腳本之家平臺(tái),對(duì)html2 canvas svg不能識(shí)別問(wèn)題感興趣的朋友參考下吧
最新有個(gè)功能需要截取網(wǎng)頁(yè)成圖片,于是用到比較流行的html2canvas,本來(lái)以為能順順利利的搞定,后來(lái)發(fā)現(xiàn)網(wǎng)頁(yè)上的流程圖連接線不在截圖中。于是各種百度、bing,也搜到好多,但是感覺沒(méi)有一個(gè)完整的代碼,現(xiàn)在自己解決了,分享下代碼。
首先需要下載canvg.js,github地址:https://github.com/canvg/canvg
function showQRCode() { scrollTo(0, 0); if (typeof html2canvas !== 'undefined') { //以下是對(duì)svg的處理 var nodesToRecover = []; var nodesToRemove = []; var svgElem = $("#divReport").find('svg');//divReport為需要截取成圖片的dom的id svgElem.each(function (index, node) { var parentNode = node.parentNode; var svg = node.outerHTML.trim(); var canvas = document.createElement('canvas'); canvg(canvas, svg); if (node.style.position) { canvas.style.position += node.style.position; canvas.style.left += node.style.left; canvas.style.top += node.style.top; } nodesToRecover.push({ parent: parentNode, child: node }); parentNode.removeChild(node); nodesToRemove.push({ parent: parentNode, child: canvas }); parentNode.appendChild(canvas); }); html2canvas(document.querySelector("#divReport"), { onrendered: function(canvas) { var base64Str =canvas.toDataURL();//base64碼,可以轉(zhuǎn)圖片 //... $('<img>',{src:base64Str}).appendTo($('body'));//直接在原網(wǎng)頁(yè)顯示 } }); } }
到此這篇關(guān)于html2+canvas svg不能識(shí)別的解決方案的文章就介紹到這了,更多相關(guān)html2 canvas svg不能識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
- 這篇文章主要介紹了html2canvas生成的圖片偏移不完整的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小2020-05-19
- 這篇文章主要介紹了html2canvas截圖空白問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)2020-03-24
使用html2canvas實(shí)現(xiàn)將html內(nèi)容寫入到canvas中生成圖片
這篇文章主要介紹了使用html2canvas實(shí)現(xiàn)將html內(nèi)容寫入到canvas中生成圖片,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下2020-01-03html2 canvas生成清晰的圖片實(shí)現(xiàn)打印功能
html2canvas是一個(gè)非常強(qiáng)大的截圖插件,很多生成圖片和打印的場(chǎng)景會(huì)用到它。接下來(lái)通過(guò)本文給大家介紹html2 canvas生成清晰的圖片實(shí)現(xiàn)打印功能,感興趣的朋友跟隨小編一起2019-09-23html2canvas.js 實(shí)現(xiàn)頁(yè)面截圖
html2canvas是一可以在網(wǎng)頁(yè)上實(shí)現(xiàn)頁(yè)面截圖的js,給大家提供包括html2canvas.js 和html2canvas.min.js以及應(yīng)用示例2019-06-18