Skip to content

Instantly share code, notes, and snippets.

@isaka
Created July 21, 2013 10:54
Show Gist options
  • Save isaka/6048233 to your computer and use it in GitHub Desktop.
Save isaka/6048233 to your computer and use it in GitHub Desktop.
A CodePen by Isaka. SVG background image demo
<!-- Yo, edit me! -->
<!-- This SVG will be encoded as a base64 -->
<!-- image for cross-browser compatibility -->
<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='300' height='300'>
<filter id='n' x='0' y='0'>
<feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='10' stitchTiles='stitch'/>
</filter>
<rect width='300' height='300' fill='#140A00'/>
<rect width='300' height='300' filter="url(#n)" opacity='0.4'/>
</svg>
<!-- This is used for output -->
<div id="demo"><pre id="code">
[base64]
</pre></div>
// Short script to encode our SVG in base64
// This can be reversed using window.atob('base64')
var code = document.getElementById('code');
var demo = document.getElementById('demo');
var svg = document.getElementsByTagName('svg')[0];
// Convert the SVG node to HTML.
var div = document.createElement('div');
div.appendChild(svg.cloneNode(true));
// Encode the SVG as base64
var b64 = 'data:image/svg+xml;base64,'+window.btoa(div.innerHTML);
var url = 'url("' + b64 + '")';
code.innerHTML = 'Base64 CSS (for cross-browser compatibility)\n\nbackground-image: ' + url + ';';
demo.style.backgroundImage = url;
html {
height: 100%; width: 100%;
}
#demo {
position: absolute;
top: 0px; left: 0px;
right: 0px; bottom: 0px;
}
#code {
border: 1px solid #000;
background-color: rgba(255,255,255,0.95);
padding: 0.5em;
margin: 1em;
}
svg {
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment