Skip to content

Instantly share code, notes, and snippets.

@nakasyou
Created April 15, 2023 06:42
Show Gist options
  • Save nakasyou/3f2e90108eb1743914ce62bea217f068 to your computer and use it in GitHub Desktop.
Save nakasyou/3f2e90108eb1743914ce62bea217f068 to your computer and use it in GitHub Desktop.
SVGをPNGに変換するJavaScriptコード
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