Skip to content

Instantly share code, notes, and snippets.

@mcmoe
Last active May 12, 2024 02:36
Show Gist options
  • 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}`;
@VitorBhmm
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment