html2 canvas svg不能识别的解决方案

发布时间:2022-04-12 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了html2 canvas svg不能识别的解决方案脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_304_0@  最新有个功能需要截取网页成图片,于是用到比较流行的htML2canvas,本来以为能顺顺利利的搞定,后来发现网页上的流程图连接线不在截图中。于是各种百度、bing,也搜到好多,但是感觉没有一个完整的代码,现在自己解决了,分享下代码。

  首先需要下载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码,可以转图片

                            //...

                            $('<img>',{src:base64Str}).appendTo($('body'));//直接在原网页显示
                         }                     });                }            }

到此这篇关于html2+canvas svg不能识别的解决方案文章就介绍到这了,更多相关html2 canvas svg不能识别内容请搜索脚本宝典以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本宝典!

脚本宝典总结

以上是脚本宝典为你收集整理的html2 canvas svg不能识别的解决方案全部内容,希望文章能够帮你解决html2 canvas svg不能识别的解决方案所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:canvascanvas