GitHub-style identicons as PNGs or SVGs in JS.
This little library will produce the same shape and (nearly) the same color as GitHub when given the same hash value. Supports PNG and SVG output formats. Note that GitHub uses an internal database identifier for the hash, so you can't simply md5 the username and get the same result. The creative visual design is borrowed from Jason Long of Git and GitHub fame.
npm install identicon.js --save
Generate the Identicon by supplying a hash string and size.
Note: SVGs will produce clearer images (see below). To make PNGs crisper, you can try adding the CSS rule image-rendering: pixelated;
// create a base64 encoded PNG
var data = new Identicon('d3b07384d113edec49eaa6238ad5ff00', 420).toString();
// write to a data URI
document.write('<img width=420 height=420 src="https://github.com/stewartlord/identicon.js/raw/master/data:image/png;base64,' + data + '">');
To customize additional properties, generate the Identicon by supplying a hexadecimal string and an options object.
// set up options
var hash = "c157a79031e1c40f85931829bc5fc552"; // 15+ hex chars
var options = {
foreground: [0, 0, 0, 255], // rgba black
background: [255, 255, 255, 255], // rgba white
margin: 0.2, // 20% margin
size: 420, // 420px square
format: 'svg' // use SVG instead of PNG
};
// create a base64 encoded SVG
var data = new Identicon(hash, options).toString();
// write to a data URI
document.write('<img width=420 height=420 src="https://github.com/stewartlord/identicon.js/raw/master/data:image/svg+xml;base64,' + data + '">');
Copyright 2018, Stewart Lord Released under the BSD license