Jdenticon
Jdenticon is a JavaScript library for generating unique, recognizable identicons, often used for user avatars or cryptographic hashing visualization. It supports rendering these icons into both HTML5 Canvas (raster) and SVG (vector) elements, providing flexibility for different display needs. The library offers distinct bundles for browser and Node.js environments, enabling client-side display or server-side image generation, including PNG outputs. The current stable version is 3.3.0. Release cadence is somewhat ad-hoc, with several minor releases per year addressing bugs, improving TypeScript support, and enhancing features. Key differentiators include its dual Canvas/SVG rendering capabilities, a wide range of configurable aesthetic properties (padding, saturation, hues), and a CLI tool. Since version 3.0.0, automatic icon rendering on page load requires explicit opt-in.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'update')
cause After v3.0.0, `jdenticon` no longer automatically initializes or exposes top-level methods for direct access via a script tag without explicit import or assignment to a global.fixIf using a module system, import named exports like `import { update } from 'jdenticon';`. If using a script tag and relying on auto-rendering, ensure you are importing `jdenticon/standalone` or explicitly call `update()`. -
Module not found: Can't resolve 'jdenticon'
cause The module resolver (e.g., Webpack, TypeScript) cannot locate the package or is configured incorrectly for your environment (browser vs. Node.js).fixCheck your `node_modules` for `jdenticon`. If using a bundler with specific environment targets or Node16 module resolution, try `import 'jdenticon/browser'` or `import 'jdenticon/node'`. -
Property 'toPng' does not exist on type 'typeof import("jdenticon")'.cause The `toPng` function is specific to the Node.js environment and is not part of the main (browser-oriented) `jdenticon` export.fixExplicitly import `toPng` from the Node.js specific bundle: `import { toPng } from 'jdenticon/node';`. -
Error: Jdenticon: Canvas element must be passed when rendering on Node.js.
cause When using `drawIcon` on Node.js, it typically expects a canvas-compatible object (e.g., from `node-canvas`) as the first argument, unlike in the browser where it can automatically find elements based on `data-jdenticon-value`.fixEnsure you are passing a valid canvas context or element from a library like `node-canvas` when calling `drawIcon` in a Node.js environment.
Warnings
- breaking Starting with v3.0.0, the main 'jdenticon' bundle no longer automatically renders icons on page startup. You must explicitly call `update()` or `drawIcon()` from your code to render icons.
- breaking In v3.0.0, the default `padding` for the `drawIcon` method was changed from `0` to `0.08` to improve visual consistency.
- deprecated Directly setting `jdenticon_config.saturation` is deprecated since v2.1.0. Use `jdenticon_config.saturation.color` or `jdenticon_config.saturation.grayscale` instead.
- gotcha When using bundlers or TypeScript with Node16 module resolution, you might need to explicitly import specific bundles (`jdenticon/browser` or `jdenticon/node`) to ensure the correct environment-specific code is used.
- gotcha Older TypeScript configurations (pre-v3.0.1) might have required adding `"dom"` to `lib` in `tsconfig.json` or experienced Node typings leaking into browser contexts. These issues were largely addressed in v3.0.1.
Install
-
npm install jdenticon -
yarn add jdenticon -
pnpm add jdenticon
Imports
- update
const jdenticon = require('jdenticon'); jdenticon.update();import { update } from 'jdenticon'; - drawIcon
import jdenticon from 'jdenticon'; jdenticon.drawIcon(...);
import { drawIcon } from 'jdenticon'; - toPng
import { toPng } from 'jdenticon';import { toPng } from 'jdenticon/node'; - Standalone auto-rendering
import 'jdenticon/standalone';
Quickstart
import { update } from 'jdenticon';
document.addEventListener('DOMContentLoaded', () => {
// Assume HTML elements with data-jdenticon-value are present
// e.g., <svg width="80" height="80" data-jdenticon-value="my-icon-seed"></svg>
// or <canvas width="80" height="80" data-jdenticon-value="my-icon-seed"></canvas>
// To render all icons on the page with data-jdenticon-value attribute:
update();
// To dynamically create and render an icon on a specific element:
const svgElement = document.createElement('svg');
svgElement.setAttribute('width', '100');
svgElement.setAttribute('height', '100');
// The `update` function can take an element and a value
update(svgElement, 'dynamic-icon-seed');
document.body.appendChild(svgElement);
// Example of using a specific value for an existing element:
const existingCanvas = document.querySelector('canvas[data-jdenticon-value="icon value"]');
if (existingCanvas) {
update(existingCanvas, 'new-seed-for-canvas');
}
});