Skip to content

Instantly share code, notes, and snippets.

@latentflip
Last active December 23, 2015 04:29
Show Gist options
  • Save latentflip/6580472 to your computer and use it in GitHub Desktop.
Save latentflip/6580472 to your computer and use it in GitHub Desktop.

SVGify

Compiles a directory of svgs into a js file of base64 pngs which can be accessed from window.svgs.

###Usage:

  • Put svgify into your directory of svgs, and run it with node svgify.js.
  • Include the resulting svgs.js in your page
  • Insert images like: <img data-src-svg='#{filename-without-extension}' />
  • Add replaceSvgs.js to your page.
  • Profit.

There's probably a better way to do this ;)

$(function() {
$('[data-src-svg]').each(function () {
$(this).attr('src', logos[$(this).attr('data-src-svg')]);
});
});
var fs = require('fs'),
path = require('path');
var json = {}
fs.readdir(__dirname, function (err, files) {
files.forEach(function(p) {
if (path.extname(p) === '.svg') {
var data = fs.readFileSync(p)
var base64data = new Buffer(data).toString('base64');
json[path.basename(p, '.svg')] = "data:image/svg+xml;base64," + base64data;
}
});
fs.writeFile('svgs.js', "window.svgs = "+JSON.stringify(json))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment