Last active
July 7, 2024 12:19
-
-
Save mcmoe/d61e6f930f68c1999f8dd6c8a46f2600 to your computer and use it in GitHub Desktop.
Convert an SVG to a base64 data image
This file contains 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
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
commented
May 12, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment