简体   繁体   English

尝试将SVG转换为PNG图像

[英]Trying to convert an SVG to PNG Image

I have been dealing with this for a bit but I can't seem to get it to work. 我已经处理了一段时间,但似乎无法正常工作。 Onclick to resulting png image is empty. Onclick到生成的png图像为空。 See the code below.... 请参见下面的代码。

function svgToCanvas (targetElem,fileName) { 
    var nodesToRecover = [];
    var nodesToRemove = [];

    var svgElem = targetElem.find('svg');

    console.log(svgElem);

    svgElem.each(function(index, node) {
        var parentNode = node.parentNode;
        var svg = parentNode.innerHTML;

        var canvas = document.createElement('canvas');

        canvg(canvas, svg);

        nodesToRecover.push({
            parent: parentNode,
            child: node
        });

        parentNode.removeChild(node);

        nodesToRemove.push({
            parent: parentNode,
            child: canvas
        });

        parentNode.appendChild(canvas);
    });

    html2canvas(targetElem, {
        allowTaint: true,
        onrendered: function(canvas) {

            var theName = fileName + ".png";
            prev_img = theName;

            var a = document.createElement('a');
            a.href = canvas.toDataURL();
            a.download = theName;
            a.click();

        }

    }); 

}

#Running the function..
var theDiv = $('#divContainingSVG');
var fileNm = "imageName";
svgToCanvas(theDiv, fileNm);

Not sure where to go next. 不知道下一步要去哪里。 I simply need to convert the image to a png and then save it to the server. 我只需要将图像转换为png,然后将其保存到服务器即可。 When testing the download version the resulting png is blank. 测试下载版本时,结果png为空白。 Note that I'm using... canvg.js (including rgbcolor.js) and html2canvas.svg.js 请注意,我正在使用... canvg.js(包括rgbcolor.js)和html2canvas.svg.js

Thanks in advance! 提前致谢!

Actually it turns out that this function works fine. 实际上,事实证明该功能可以正常工作。 The issue was a conflict with another function that was included, by mistake, to get the svg. 问题是与错误地包含在获取svg中的另一个功能发生冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM