@basementuniverse/marble-identicons

中文文档
0.1.2发布于 2022/04/14MITTypeScriptCommonJS近 30 天下载 1.5万

Generate marble-style identicons

原始使用文档
文档目录5 节
  1. Installation
  2. Usage
  3. In a browser environment
  4. In React
  5. Settings

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']

版本与发布记录

@basementuniverse/marble-identicons 共发布 3 个版本,首次发布于 2021/08/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.2latest2022/04/144 年前
0.1.12022/03/064 年前
0.1.02021/08/205 年前

关于 @basementuniverse/marble-identicons 的常见问题

@basementuniverse/marble-identicons 是什么?

Generate marble-style identicons

如何安装 @basementuniverse/marble-identicons?

在项目目录运行 npm install @basementuniverse/marble-identicons,或使用 pnpm add / yarn add / bun add。

@basementuniverse/marble-identicons 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@basementuniverse/marble-identicons 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@basementuniverse/marble-identicons 可以商用吗?

@basementuniverse/marble-identicons 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@basementuniverse/marble-identicons 的下载量有多少?

根据 npm 官方下载统计,@basementuniverse/marble-identicons 在 2026/10/08 – 2026/11/06 这 30 天内被下载 14,549 次,日均约 485 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@basementuniverse/marble-identicons 还在维护吗?

最新版本 0.1.2 发布于 2022/04/14(4 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/08/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。