GitHub-style identicons in JS with no server-side processing.
Go to file
Adrian Tiberius 4cd179daaf fix strict code error 2016-06-21 15:15:53 +03:00
.gitignore added bower, npm packages and semver release automation 2015-06-04 23:06:06 -07:00
LICENSE Added LICENSE file. Removed description from js file; now in README.md. 2013-10-03 21:59:38 -07:00
README.md Fixed typo in advanced options section 2016-06-01 14:17:57 -07:00
bower.json Update bower.json 2016-04-19 21:58:37 -07:00
demo.html [#4] Add a background color option, options argument 2016-02-26 00:50:05 -08:00
gulpfile.js added bower, npm packages and semver release automation 2015-06-04 23:06:06 -07:00
identicon.js Update identicon.js 2016-04-19 21:58:09 -07:00
package.json Update package.json 2016-04-19 21:57:44 -07:00
pnglib.js fix strict code error 2016-06-21 15:15:53 +03:00

README.md

identicon.js

GitHub-style identicons in JS with no server-side processing.

This little library will produce the same shape and (roughly) the same color as GitHub when given the same hash value. 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 hard work is done by Robert Eisele's PNGlib. The creative visual design is borrowed from Jason Long of Git and GitHub fame.

Demo

View Demo

Options

  • hash - [Optional] A unicode string that will be used to generate the image. Defaults to a random hash based on the current time.
  • options - [Optional] An options object used to customize the generated image.
    • background - The background color expressed as an rgba value array to use for the image background. For example, use [255,0,0,255] for red. Defaults to an opaque light gray [240,240,240,255].
    • margin - The decimal fraction of the size to use for margin. For example, use 0.2 for a 20% margin. Defaults to 0.08 for an 8% margin.
    • size - The size in pixels of the height and width of the generated (square) image. Defaults to 64 pixels.

Usage

Simple

Generate the Identicon by supplying a hash string and size.


// create a base64 encoded PNG
var data = new Identicon('hash', 420).toString();

// write to a data URI
document.write('<img width=420 height=420 src="data:image/png;base64,' + data + '">');
Advanced

To customize additional properties, generate the Identicon by supplying a hash string and an options object.

// set up options
var hash = "myUnicodeUsername!";          // Any unicode string
var options = {
      background: [255, 255, 255, 255],  // rgba white
      margin: 0.2,                       // 20% margin
      size: 420                         // 420px square
    };

// create a base64 encoded PNG
var data = new Identicon(hash, options).toString();

// write to a data URI
document.write('<img width=420 height=420 src="data:image/png;base64,' + data + '">');

Requires PNGLib

Copyright 2013, Stewart Lord Released under the BSD license