Last active
November 16, 2017 22:17
-
-
Save xjamundx/5278562 to your computer and use it in GitHub Desktop.
Update Base64 SVG background image.
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
function updateSVG() { | |
var el = document.querySelector('.icon-user'); | |
var style = window.getComputedStyle(el); | |
var uri = style.backgroundImage; | |
var svg64 = uri.replace(/^.+base64,/, "").replace(/\"?\)$/, "") | |
var xml = window.atob(svg64); | |
var hex = '#' + Math.floor(Math.random()*16777215).toString(16); | |
var color = xml.replace(/fill="#[A-Za-z0-9]+"/, 'fill="' + hex + '"'); | |
var color64 = window.btoa(color); | |
var colorUri = "url('data:image/svg+xml;base64," + color64 + "')"; | |
el.style.backgroundImage = colorUri; | |
} | |
document.querySelector('#changeme').onclick = updateSVG; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment