Created
April 1, 2020 07:31
-
-
Save hashrock/e55e497620b77900d098fb8c736cf31f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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