Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function saveSvgAsPng(el) {
const svgData = new XMLSerializer().serializeToString(el);
const canvas = document.createElement("canvas");
canvas.width = el.width.baseVal.value;
canvas.height = el.height.baseVal.value;
const ctx = canvas.getContext("2d");
const image = new Image();
image.onload = function() {
ctx.drawImage(image, 0, 0);
const a = document.createElement("a");
a.href = canvas.toDataURL("image/png");
a.setAttribute("download", "image.png");
a.dispatchEvent(new MouseEvent("click"));
image.src =
"data:image/svg+xml;charset=utf-8;base64," +
function download() {
const svg = document.querySelector("svg");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.