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

  发布时间:2021-06-03 17:28:00   作者:风一样的大叔   我要评论
最近很多朋友联系小编在截取网页成图片的时候,html2+canvas不能识别 svg的问题,正好这个问题我也遇到过,下面我把处理思路分享到脚本之家平台,对html2 canvas svg不能识别问题感兴趣的朋友参考下吧

  最新有个功能需要截取网页成图片,于是用到比较流行的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不能识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

相关文章

  • html2canvas生成的图片偏移不完整的解决方法

    这篇文章主要介绍了html2canvas生成的图片偏移不完整的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小
    2020-05-19
  • html2canvas截图空白问题的解决

    这篇文章主要介绍了html2canvas截图空白问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习
    2020-03-24
  • 使用html2canvas实现将html内容写入到canvas中生成图片

    这篇文章主要介绍了使用html2canvas实现将html内容写入到canvas中生成图片,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下
    2020-01-03
  • html2 canvas生成清晰的图片实现打印功能

    html2canvas是一个非常强大的截图插件,很多生成图片和打印的场景会用到它。接下来通过本文给大家介绍html2 canvas生成清晰的图片实现打印功能,感兴趣的朋友跟随小编一起
    2019-09-23
  • html2canvas.js 实现页面截图

    html2canvas是一可以在网页上实现页面截图的js,给大家提供包括html2canvas.js 和html2canvas.min.js以及应用示例
    2019-06-18

最新评论