Created
November 5, 2024 04:40
-
-
Save zazaulola/0db35e70a8bea76212c42e6cf6f2d5ef to your computer and use it in GitHub Desktop.
Convert string with SVG code to DataURL
This file contains hidden or 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
// for modern browsers | |
const svgToDataUrl = svg => `data:image/svg+xml;utf8,${svg.replace(/[#%]/g,s=>'%'+s.charCodeAt(0))}`; | |
// for old browsers | |
const svgToDataUrl = svg => `data:image/svg+xml;utf8,${svg.replace(/[<>#%{}"\s]/g,s=>'%'+s.charCodeAt(0))}`; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment