Skip to content

Instantly share code, notes, and snippets.

@mcmoe
Last active March 15, 2021 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcmoe/d61e6f930f68c1999f8dd6c8a46f2600 to your computer and use it in GitHub Desktop.
Save mcmoe/d61e6f930f68c1999f8dd6c8a46f2600 to your computer and use it in GitHub Desktop.
Convert an SVG to a base64 data image
let svgElement = document.getElementById('your_svg_id');
let svgString = new XMLSerializer().serializeToString(svgElement); // Serialize the svg to string
let decoded = unescape(encodeURIComponent(svgString)); // Remove any characters outside the Latin1 range
let base64 = btoa(decoded); // Use btoa to convert the svg to base64
let imgSource = `data:image/svg+xml;base64,${base64}`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment