html2 canvas svg不能識別的解決方案
發(fā)布時間:2021-06-03 17:28:00 作者:風(fēng)一樣的大叔
我要評論
最近很多朋友聯(lián)系小編在截取網(wǎng)頁成圖片的時候,html2+canvas不能識別 svg的問題,正好這個問題我也遇到過,下面我把處理思路分享到腳本之家平臺,對html2 canvas svg不能識別問題感興趣的朋友參考下吧
最新有個功能需要截取網(wǎng)頁成圖片,于是用到比較流行的html2canvas,本來以為能順順利利的搞定,后來發(fā)現(xiàn)網(wǎng)頁上的流程圖連接線不在截圖中。于是各種百度、bing,也搜到好多,但是感覺沒有一個完整的代碼,現(xiàn)在自己解決了,分享下代碼。
首先需要下載canvg.js,github地址:https://github.com/canvg/canvg
function showQRCode() {
scrollTo(0, 0);
if (typeof html2canvas !== 'undefined') {
//以下是對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)頁顯示
} }); } }
到此這篇關(guān)于html2+canvas svg不能識別的解決方案的文章就介紹到這了,更多相關(guān)html2 canvas svg不能識別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
這篇文章主要介紹了html2canvas生成的圖片偏移不完整的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小2020-05-19
這篇文章主要介紹了html2canvas截圖空白問題的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)2020-03-24
使用html2canvas實現(xiàn)將html內(nèi)容寫入到canvas中生成圖片
這篇文章主要介紹了使用html2canvas實現(xiàn)將html內(nèi)容寫入到canvas中生成圖片,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下2020-01-03html2 canvas生成清晰的圖片實現(xiàn)打印功能
html2canvas是一個非常強(qiáng)大的截圖插件,很多生成圖片和打印的場景會用到它。接下來通過本文給大家介紹html2 canvas生成清晰的圖片實現(xiàn)打印功能,感興趣的朋友跟隨小編一起2019-09-23
html2canvas是一可以在網(wǎng)頁上實現(xiàn)頁面截圖的js,給大家提供包括html2canvas.js 和html2canvas.min.js以及應(yīng)用示例2019-06-18





