Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//from https://qiita.com/skryoooo/items/a37455bef54321a6195a
//変更内容:関数化とconst化
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," +
btoa(unescape(encodeURIComponent(svgData)));
}
function download() {
const svg = document.querySelector("svg");
saveSvgAsPng(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.