Skip to content

Instantly share code, notes, and snippets.

@hvianna
Created September 22, 2023 13:22
Show Gist options
  • Save hvianna/8a44ac7b88509e2f7f3f22d8b96e4261 to your computer and use it in GitHub Desktop.
Save hvianna/8a44ac7b88509e2f7f3f22d8b96e4261 to your computer and use it in GitHub Desktop.
Draw an SVG element into a canvas
// thanks https://levelup.gitconnected.com/draw-an-svg-to-canvas-and-download-it-as-image-in-javascript-f7f7713cf81f
const canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d'),
svgEl = document.getElementById('mysvg');
const image = new Image();
image.src = URL.createObjectURL( new Blob( [ svgEl.outerHTML ], { type: 'image/svg+xml;charset=utf-8' } ) );
image.onload = () => {
ctx.drawImage( image, 0, 0 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment