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 🙏

© 2025 – Pkg Stats / Ryan Hefner

text-to-avatar

v2.2.0

Published

A TypeScript package to generate avatar images

Readme

Text To Avatar

A TypeScript package to generate avatar images based on a user's name with customizable size, colors, and initials.

🚀 Installation

Install the package using npm:

npm install text-to-avatar

or using yarn:

yarn add text-to-avatar

📖 Usage

Import the package and generate an avatar:

import { generateAvatarImage } from "text-to-avatar";
    # Or            
const { generateAvatarImage } = require("text-to-avatar")


const avatar = generateAvatarImage({ name: "John", size: 150, positions: [1, 2] });
console.log(avatar); // Base64 Image URL

🔧 Options

The generateAvatarImage function accepts an object with the following properties:

| Parameter | Type | Default | Description | | ----------- | ---------- | -------- | -------------------------------------------------- | | name | string | 'test' | The name used to generate initials and color. | | size | number | 100 | The size of the avatar (in pixels). | | positions | number[] | [1, 2] | The positions of letters to extract from the name. |

Example Usages

Generate a 200px avatar for "Alice" using the first and third letters:

const avatar = generateAvatarImage({ name: "Alice", size: 200, positions: [1, 3] });
console.log(avatar); // Base64 Image URL

Generate a 100px avatar for "David" using the second and fourth letters:

const avatar = generateAvatarImage({ name: "David", size: 100, positions: [2, 4] });
console.log(avatar); // Base64 Image URL

🎨 Font Support

You can use Google Fonts to customize the avatar font:

import { generateAvatarImage, addFont } from "text-to-avatar";

await addFont({ fontFamily: "Roboto", fontWeight: "700" });

const avatar = generateAvatarImage({ name: "John", size: 150, positions: [1, 2] });

Options for addFont:

fontFamily string —

  1. Google Font family name -- ("Roboto")

fontWeight string —

  1. Font weight (optional)
  2. Default will be 400

Note: In browser, font is injected automatically via a tag. In Node.js, font is downloaded and registered with canvas.

🛠 How It Works

  1. Extracts initials from the specified positions in the name.
  2. Generates a unique background color based on the name.
  3. Draws the initials in the center of the canvas with a white font.
  4. Returns a Base64 encoded image that can be used as an avatar.

📄 License

This project is licensed under the ISC License.