-
-
Save gustavohenke/9073132 to your computer and use it in GitHub Desktop.
var svg = document.querySelector( "svg" ); | |
var svgData = new XMLSerializer().serializeToString( svg ); | |
var canvas = document.createElement( "canvas" ); | |
var ctx = canvas.getContext( "2d" ); | |
var img = document.createElement( "img" ); | |
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) ); | |
img.onload = function() { | |
ctx.drawImage( img, 0, 0 ); | |
// Now is done | |
console.log( canvas.toDataURL( "image/png" ) ); | |
}; |
@philfreo , I found your function useful, but it was still partially cut off, so as a hack, I did this to ensure it wasn't cut off. Won't work in all cases. Just a heads up that getBoundingClientRect() doesn't seem to always work.
canvas.width = svgSize.width * 2;
canvas.height = svgSize.height * 2;
By the way, this gist is a lifesaver!
Here's what i'm using. Works fine in Chrome but not on Firefox... Any ideas?
function svg_to_png(container) {
var wrapper = document.getElementById(container);
var svg = wrapper.querySelector("svg");
if (typeof window.XMLSerializer != "undefined") {
var svgData = (new XMLSerializer()).serializeToString(svg);
} else if (typeof svg.xml != "undefined") {
var svgData = svg.xml;
}
var canvas = document.createElement("canvas");
var svgSize = svg.getBoundingClientRect();
canvas.width = svgSize.width;
canvas.height = svgSize.height;
var ctx = canvas.getContext("2d");
var img = document.createElement("img");
img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData))) );
img.onload = function() {
ctx.drawImage(img, 0, 0);
var imgsrc = canvas.toDataURL("image/png");
var a = document.createElement("a");
a.download = container+".png";
a.href = imgsrc;
a.click();
};
}
@Guillemp
put your img.setAttribute(...) after img.onload
@Guillemp
you should append in the body!
document.body.appendChild(a);
@abishekrsrikaanth "Is there a way to convert PNG to SVG?"
Yes, there is now: https://github.com/jankovicsandras/imagetracerjs
Seems a little odd to base64 encode it.... I went with
var img = new Image()
img.src = "data:image/svg+xml;utf8," + svgData
@toastal Using utf8 didn't seem to work. Did it work for you? I am Using Chrome.
var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );
var canvas = document.createElement( "canvas" );
var svgSize = svg.getBoundingClientRect();
canvas.width = svgSize.width;
canvas.height = svgSize.height;
var ctx = canvas.getContext( "2d" );
var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );
img.onload = function() {
ctx.drawImage( img, 0, 0 );
// Now is done
console.log( canvas.toDataURL( "image/png" ) );
};
Worked with this code. but for some SVG files it is showing "NETWORK ERROR". Can u help?
When I convert SVG to PNG it only export graph plot not graph data to PNG .
Please Advice or suggest any solution.
I'm trying to export angular-d3-charts to PDF as an image but the chart is not complete the x-axis are hidden as it's not inline style is there any workaround to solve this ???
You save my life!!!!!!!!!!!!!!!!!!!! @Guillemp This code works well! Thank you very much!
I used snap JS to draw svg, here what I do:
var s = Snap('#svg_id');
var svg = document.querySelector( "#svg_id" );
var svgData = new XMLSerializer().serializeToString( svg );
var canvas = document.createElement( "canvas" );
canvas.width = 1500;
canvas.height = 1450;
var ctx = canvas.getContext( "2d" );
var img = document.createElement( "img" );
img.setAttribute( "src", s.toDataURL());
console.log(s.toDataURL());
img.onload = function() {
ctx.drawImage( img, 0, 0 );
console.log( canvas.toDataURL( "image/png" ) );
var imgsrc = canvas.toDataURL( "image/png" );
var a = document.getElementById("download_png");
a.download = "filename.png"
a.href = imgsrc;
};
Here's what i'm using. Works fine in Chrome but not on Firefox... Any ideas?
function svg_to_png(container) { var wrapper = document.getElementById(container); var svg = wrapper.querySelector("svg"); if (typeof window.XMLSerializer != "undefined") { var svgData = (new XMLSerializer()).serializeToString(svg); } else if (typeof svg.xml != "undefined") { var svgData = svg.xml; } var canvas = document.createElement("canvas"); var svgSize = svg.getBoundingClientRect(); canvas.width = svgSize.width; canvas.height = svgSize.height; var ctx = canvas.getContext("2d"); var img = document.createElement("img"); img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData))) ); img.onload = function() { ctx.drawImage(img, 0, 0); var imgsrc = canvas.toDataURL("image/png"); var a = document.createElement("a"); a.download = container+".png"; a.href = imgsrc; a.click(); }; }
Thanks
var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );var canvas = document.createElement( "canvas" );
var svgSize = svg.getBoundingClientRect();
canvas.width = svgSize.width;
canvas.height = svgSize.height;
var ctx = canvas.getContext( "2d" );var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );img.onload = function() {
ctx.drawImage( img, 0, 0 );// Now is done console.log( canvas.toDataURL( "image/png" ) );
};
Worked with this code. but for some SVG files it is showing "NETWORK ERROR". Can u help?
Here's what i'm using. Works fine in Chrome but not on Firefox... Any ideas?
function svg_to_png(container) { var wrapper = document.getElementById(container); var svg = wrapper.querySelector("svg"); if (typeof window.XMLSerializer != "undefined") { var svgData = (new XMLSerializer()).serializeToString(svg); } else if (typeof svg.xml != "undefined") { var svgData = svg.xml; } var canvas = document.createElement("canvas"); var svgSize = svg.getBoundingClientRect(); canvas.width = svgSize.width; canvas.height = svgSize.height; var ctx = canvas.getContext("2d"); var img = document.createElement("img"); img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData))) ); img.onload = function() { ctx.drawImage(img, 0, 0); var imgsrc = canvas.toDataURL("image/png"); var a = document.createElement("a"); a.download = container+".png"; a.href = imgsrc; a.click(); }; }
worked for me, thanks
sorry can somebody tell me what is container hier ? image Tag?
function svg_to_png(container) {
var wrapper = document.getElementById(container);
var svg = wrapper.querySelector("svg");
if (typeof window.XMLSerializer != "undefined") {
var svgData = (new XMLSerializer()).serializeToString(svg);
} else if (typeof svg.xml != "undefined") {
var svgData = svg.xml;
}
var canvas = document.createElement("canvas");
var svgSize = svg.getBoundingClientRect();
canvas.width = svgSize.width;
canvas.height = svgSize.height;
var ctx = canvas.getContext("2d");
var img = document.createElement("img");
img.setAttribute("src", "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData))) );
img.onload = function() {
ctx.drawImage(img, 0, 0);
var imgsrc = canvas.toDataURL("image/png");
var a = document.createElement("a");
a.download = container+".png";
a.href = imgsrc;
a.click();
};
}
If your SVG styling isn't inline, you can use this package: https://github.com/lukehorvat/computed-style-to-inline-style