GithubHelp home page GithubHelp logo

dujunhui007 / identicon.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stewartlord/identicon.js

0.0 1.0 0.0 64 KB

GitHub-style identicons as PNGs or SVGs in JS

License: BSD 2-Clause "Simplified" License

HTML 26.58% JavaScript 73.42%

identicon.js's Introduction

identicon.js

Screenshot

CDNJS version

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.

Demo

View Demo

Installation

npm install identicon.js --save

NPM Stats

Options

  • hash - A hexadecimal string of 15+ characters that will be used to generate the image.
  • options - [Optional] An options object used to customize the generated image.
    • size - The size in pixels of the height and width of the generated (square) image. Defaults to 64 pixels.
    • 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.
    • foreground - The foreground color is automatically derived from the hash value. Use this option to override that behavior and provide a rgba value array instead (e.g. [255,0,0,255] for red).
    • 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].
    • saturation - The saturation of the derived foreground color as a value from 0-1. Defaults to 0.7.
    • brightness - The brightness of the derived foreground color as a value from 0-1. Defaults to 0.5.

Usage

Simple

Generate the Identicon by supplying a hash string and size.

// 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="data:image/png;base64,' + data + '">');
Advanced

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="data:image/svg+xml;base64,' + data + '">');

PNG output requires PNGLib

Copyright 2017, Stewart Lord Released under the BSD license

identicon.js's People

Contributors

stewartlord avatar buildbreakdo avatar luciditydesign avatar akhra avatar justerhan avatar refset avatar extend1994 avatar mitar avatar ppuleo avatar hai-ko avatar lukasz-karolewski avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.