简体   繁体   English

使用JavaScript从SVG生成低分辨率PNG

[英]Generate a low resolution PNG from SVG using javascript

I wanted to convert an SVG file to PNG file. 我想将SVG文件转换为PNG文件。 But the SVG view box is "0 0 8268 5827" so it will generate a huge file. 但是SVG视图框为“ 0 0 8268 5827”,因此它将生成一个巨大的文件。 I want to generate a small file from that SVG. 我想从该SVG生成一个小文件。 Can anybody tell me how can I do that? 谁能告诉我该怎么做?

I used Canvg like 我用Canvg喜欢

var c = $('#export_canvas')[0];
canvg(canvas, data.svg, {renderCallback: function() {
        var datauri = data.quality ? c.toDataURL('image/' + "png", data.quality) : c.toDataURL('image/' + "png");
        window.open(datauri);
    }});

But it is generating big image. 但这正在产生大印象。

Finally I found the solution. 终于我找到了解决方案。 There is a scale transformation available for SVG. SVG有一个比例转换。

                var svgStr = $(data.svg);
                svgStr.removeAttr("height");
                svgStr.removeAttr("width");
                $(svgStr.find("g")[0]).attr("transform", "scale(0.09)");

where "g" is the top group tag. 其中“ g”是头组标签。

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

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