0.1.2 • Published 3 years ago

@basementuniverse/marble-identicons v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Marble Identicons

Generate random marble-style identicons.

Identicons Preview

Installation

npm install @basementuniverse/marble-identicons

Usage

API:

function identicon(
  name: string,
  settings: Partial<IdenticonSettings>
) => HTMLCanvasElement

See below for more information on configuration settings.

In a browser environment

Include the script in your page and call the identicon function. It will return a HTMLCanvasElement node which can be appended to the DOM or drawn into another canvas.

Example:

<script src="build/identicon.js"></script>
<script>

const myIdenticon = identicon('Alan Turing');

document.body.appendChild(myIdenticon);

</script>

In React

This example shows one way of wrapping the HTMLCanvasElement inside a JSX element:

import { useRef, useLayoutEffect } from 'react';
import { identicon } from '@basementuniverse/marble-identicons';

export function MarbleIdenticon({ name }: { name: string }) {
  const ref = useRef<HTMLDivElement>(null);

  useLayoutEffect(() => {
    if (ref.current) {
      ref.current.innerHTML = '';
      ref.current.appendChild(identicon(name));
    }
  });

  return (
    <div className="identicon" ref={ref}></div>
  );
}

This component can then be used like so:

ReactDOM.render(
  <MarbleIdenticon name="Alan Turing" />,
  document.getElementById('root')
);

Settings

type IdenticonSettings = {
  size: number;
  baseSeed: string;
  font: string;
  fontStyle: FontStyle;
  fontSize: number;
  backgroundColours: string[];
  initialsColours: string[];
  initialsOffset: vec;
  initialsAlpha: number;
  initialsCompositeOperation: CompositeOperation;
  stripeColours: string[];
  stripeAlpha: number;
  stripeCompositeOperation: CompositeOperation;
  stripes: [number, number];
  stripeWidth: [number, number];
  stripeDeviation: [number, number];
  curveAmount: [number, number];
  curveOffset: [number, number];
  startWidthSign: WidthSign[];
  endWidthSign: WidthSign[];
};
SettingTypeDescriptionDefault value
sizenumberThe size of the identicon in pixels100
baseSeedstringA string that will be prefixed onto the name when seeding the RNG''
fontstringThe font family used for the initials'Helvetica'
fontStyleFontStyleThe font style used for the initials. Must be one of '', 'normal', 'bold', 'italic''bold'
fontSizenumberThe font size used for the initials, as a factor of size0.4
backgroundColoursstring[]An array of background colours. One will be picked at random from this list.['#16a085', '#1abc9c', '#2ecc71', '#3498db', '#1970b9', '#9b59b6', '#e67e22', '#e74c3c', '#e0395b']
initialsColoursstring[]An array of colours used for the initials. The index of the randomly-chosen background colour will be used to select from this list, clamped to the size of the array.['#ffffff']
initialsOffsetvecThe initials offset position. { x: number, y: number }{ x: 0, y: 0 }
initialsAlphanumberThe alpha opacity of the initials1
initialsCompositeOperationCompositeOperationThe global composite operation used for the initials. Must be one of 'source-over', 'lighter', 'multiply', 'screen', 'overlay', 'color-dodge', 'color-burn', 'hard-light', 'soft-light''source-over'
stripeColoursstring[]An array of colours used for the stripes. The index of the randomly-chosen background colour will be used to select from this list, clamped to the size of the array.['#f1c40f']
stripeAlphanumberThe alpha opacity of the stripes0.15
stripeCompositeOperationCompositeOperationThe global composite operation used for the stripes. Must be one of 'source-over', 'lighter', 'multiply', 'screen', 'overlay', 'color-dodge', 'color-burn', 'hard-light', 'soft-light''lighter'
stripes[number, number]A tuple containing the minimum and maximum number of stripes to draw[3, 8]
stripeWidth[number, number]A tuple containing the minimum and maximum stripe width, measured in radians.[0.2, 0.7]
stripeDeviation[number, number]A tuple containing minimum and maximum distances between successive stripes, measured in radians.[-0.5, 0.5]
curveAmount[number, number]A tuple containing the minimum and maximum radius multiplier for the control point circle.[0.2, 0.4]
curveOffset[number, number]A tuple containing the minimum and maximum offset angle for the control point measured in radians.[0, 0.5]
startWidthSignWidthSign[]An array of signs ('positive' or 'negative') for the starting edge of each stripe. If startWidthSign is ['positive'] and endWidthSign is ['negative'] (or vice-versa) then the stripe side edges will not cross over.['positive', 'negative']
endWidthSignWidthSign[]An array of signs ('positive' or 'negative') for the ending edge of each stripe.['positive', 'negative']