npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

avatar-gen-js

v1.2.0

Published

根据用户昵称生成默认头像,支持自定义颜色和图案

Downloads

10

Readme

avatar-gen-js

一个支持浏览器和Node.js的npm库,用于根据用户昵称(可能是邮箱)生成默认头像。

特性

  • 🎨 随机颜色:使用明亮饱和的颜色,避免过亮过暗或死灰色
  • 🎭 图案支持:生成类似GitHub风格的沿垂直中线对称的图案
  • 📏 自定义尺寸:支持任意尺寸设置
  • 🎨 自定义调色盘:支持传入不定长的数组作为调色盘
  • 🔲 自定义格子数:支持自定义图案的格子数(默认5)
  • 🔄 一致性保证:相同输入始终产生相同输出
  • 🌐 跨平台支持:支持浏览器和Node.js环境
  • 📦 多种格式:支持ES、CJS、UMD三种模块格式
  • 🔢 Base64支持:支持导出为Base64编码的SVG URL

安装

npm install avatar-gen-js

使用

基本使用

// ES模块
import generateAvatar from 'avatar-gen-js';
// CommonJS
const { generateAvatar, generatePatternAvatar } = require('avatar-gen-js');

// 生成带图案的头像(默认)
const avatar = generateAvatar('[email protected]');
console.log(avatar);
// 输出SVG字符串

自定义配置

const { generateAvatar } = require('avatar-gen-js');

// 自定义尺寸、颜色和格子数
const avatar = generateAvatar('[email protected]', {
  size: 200,
  colors: ['#FF5733', '#33FF57', '#3357FF'],
  gridSize: 7
});

// 生成Base64编码的SVG URL
const avatarUrl = generateAvatar('[email protected]', {
  size: 200,
  toBase64: true
});

API

generateAvatar(str: string, options?: AvatarOptions): string | string

生成带图案的SVG头像(默认导出)。

参数

  • str: 输入字符串(昵称或邮箱)
  • options: 配置选项
    • size: 头像大小,默认100
    • colors: 自定义调色盘,默认使用内置的明亮饱和颜色
    • gridSize: 图案格子数,默认5,范围3-10
    • toBase64: 是否返回Base64编码的SVG URL,默认false

返回值

  • SVG字符串或Base64编码的SVG URL

generatePatternAvatar(str: string, options?: AvatarOptions): string | string

生成带图案的SVG头像(命名导出)。

参数

  • str: 输入字符串(昵称或邮箱)
  • options: 配置选项
    • size: 头像大小,默认100
    • colors: 自定义调色盘,默认使用内置的明亮饱和颜色
    • gridSize: 图案格子数,默认5,范围3-10
    • toBase64: 是否返回Base64编码的SVG URL,默认false

返回值

  • SVG字符串或Base64编码的SVG URL

AvatarOptions 接口

interface AvatarOptions {
  size?: number;
  colors?: string[];
  gridSize?: number;
  toBase64?: boolean;
}

svgToBase64(svg: string): string

将SVG字符串转换为Base64编码的SVG URL。

参数

  • svg: SVG字符串

返回值

  • Base64编码的SVG URL

示例

输入

generateAvatar('[email protected]');

输出

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100">
  <rect width="100" height="100" fill="#DDA0DD" />
  <rect x="0" y="0" width="20" height="20" fill="white" opacity="0.3" />
  <rect x="80" y="0" width="20" height="20" fill="white" opacity="0.3" />
  <!-- 其他图案元素 -->
</svg>

浏览器兼容性

支持所有现代浏览器,包括:

  • Chrome
  • Firefox
  • Safari
  • Edge

Node.js兼容性

支持Node.js 14及以上版本。

开发

# 安装依赖
npm install

# 构建
npm run build

# 开发模式
npm run dev

# 启动demo服务器
npm start

测试

npm test

许可证

MIT