Giter Club home page Giter Club logo

blo's Introduction

blo
blo is a small and fast library to generate Ethereum identicons.

npm version bundle size License

Features

  • πŸ₯ Small: 0.67 KB gzipped.
  • πŸ’₯ Fast: 3.5x faster than the second fastest solution.
  • πŸ” Optimized: Leverages SVG to generate compact and sharp images at any size.
  • πŸ’† Simple: Focuses on Ethereum identicons only, allowing for a simpler API.
  • πŸ—‚ Typed: Ships with types included.
  • πŸ‘« Works everywhere: browsers, Bun, Node.js.
  • ☁️ Zero dependencies.

Library Comparison

Library Renders/sec1 Size Types Environment2 Rendering
blo πŸ’₯ 8,197 SVG
ethereum-blockies-base64 807 PNG
blockies-react-svg 1,749 SVG
@download/blockies 334 Canvas
blockies-ts 342 Canvas
react-blockies 2,361 Canvas

Getting Started

npm i -S blo
pnpm add blo
yarn add blo
import { blo } from "blo";

img.src = blo("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");

React / Vue / Others

blo is fast enough to not require memoization or async rendering for common use cases.

function AddressIcon({ address }: { address: `0x${string}` }) {
  return (
    <img
      alt={address}
      src={blo(address)}
    />
  );
}

API

blo(address: Address, size = 64): string

Get a data URI string representing the identicon as an SVG image.

The size paramater shouldn’t usually be needed, as the image will stay sharp no matter what the size of the img element is.

Example:

import { blo } from "blo";

img.src = blo(address); // size inside the SVG defaults to 64px
img2.src = blo(address, 24); // set it to 24px
bloSvg(address: Address, size = 64): string

Same as above except it returns the SVG code instead of a data URI string.

bloImage(address: Address): BloImage

Get a BloImage data structure that can be used to render the image in different formats.

See src/svg.ts for an example of how to use it.

Types

The library ships with TypeScript types included.

// BloImage contains the data needed to render an icon.
export type BloImage = [BloImageData, Palette];

// 4x8 grid of the image left side, as 32 PaletteIndex items.
// The right side is omitted as it's a mirror of the left side.
export type BloImageData = Uint8Array;

// Colors used by a given icon.
export type Palette = [
  Hsl, // background
  Hsl, // color
  Hsl, // spot
];

// Points to one of the three Palette colors.
export type PaletteIndex =
  | 0 // background
  | 1 // color
  | 2; // spot

// A color in the HSL color space.
// [0]: 0-360 (hue)
// [1]: 0-100 (saturation)
// [2]: 0-100 (lightness)
export type Hsl = Uint16Array;

// An Ethereum address.
export type Address = `0x${string}`;

Acknowledgements

FAQ

Does it follow the exact same algorithm as Etherscan, MetaMask and others?

Yes.

Does it work with ENS names?

No it only works with Ethereum addresses, but you can resolve the ENS name to an address (e.g. with wagmi) and pass the result to blo.

Can blo render other formats than SVG?

You can render to any format you want by using the bloImage() function, which returns a data structure (see API above). Check out the Bun and Node demos for examples of rendering an identicon in the terminal.

Ethereum identicon rendered in the terminal

Can it be used to generate other types of identicons?

blo only focuses on the Ethereum identicons algorithm but you can use it with any data, just prefix it with 0x to fulfill the expected Address type if you are using TypeScript.

Why is it named blo?

blo is short for blockies, which is the name of the original library it is based on.

License

MIT

Footnotes

  1. The number of renders per second. It was measured on Chrome 117 Linux with an AMD Ryzen 7 PRO 4750U. See ./benchmark for the methodology. ↩

  2. The term β€œall” refers to libraries that are framework agnostic and that run in browsers, Bun and Node.js. ↩

blo's People

Contributors

bpierre avatar erjanmx avatar technophile-04 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

blo's Issues

error while using the lib

blo version: 1.1.1

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/abhi/code/explorer-web/node_modules/blo/dist/index.cjs.js from /Users/abhi/code/explorer-web/.next/server/pages/accounts.js not supported.
index.cjs.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.cjs.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/abhi/code/explorer-web/node_modules/blo/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

circular icons

hey @bpierre instead of the default square shaped icons, i want to generate circular icons. i tried changing the path from square to a circle in svg.ts but the circle isnt getting rendered. any help would be appreciated.

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.