Created
April 15, 2023 06:42
-
-
Save nakasyou/3f2e90108eb1743914ce62bea217f068 to your computer and use it in GitHub Desktop.
SVGをPNGに変換するJavaScriptコード
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
function svg2png(svgURL){ | |
const img = new Image(); | |
return new Promise((resolve)=>{ | |
img.addEventListener('load',()=>{ | |
const canvas = document.createElement('canvas'); | |
canvas.width = img.width; | |
canvas.height = img.height; | |
const ctx = canvas.getContext('2d'); | |
ctx.drawImage(img, 0, 0, img.width, img.height); | |
fetch(canvas.toDataURL('image/png')) | |
.then(res=>res.blob()) | |
.then(URL.createObjectURL) | |
.then(resolve); | |
}); | |
img.src = svgURL; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment