Skip to content

Instantly share code, notes, and snippets.

@xjamundx
Last active November 16, 2017 22:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xjamundx/5278562 to your computer and use it in GitHub Desktop.
Save xjamundx/5278562 to your computer and use it in GitHub Desktop.
Update Base64 SVG background image.
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