Skip to content

API Reference

createAvatar

The main function for generating avatars.

import { createAvatar } from "@avatar-generator/core";
import { initials } from "@avatar-generator/style-initials";
const avatar = createAvatar(initials, { seed: "Hugo GB" });

Parameters

ParameterTypeDescription
styleStyle<T>The avatar style to use
optionsT extends AvatarOptionsStyle-specific options

Returns

PropertyTypeDescription
svgstringThe generated SVG string
toDataUri()() => stringReturns a data URI for use in img src

Common Options

All styles accept these base options:

OptionTypeDefaultDescription
seedstring(required)Seed for deterministic generation
sizenumber64Avatar size in pixels
colorsstring[](style default)Custom color palette
squarebooleanfalseUse square shape instead of circle
transparentbooleanfalseMake background transparent
border{ width: number, color: string }-Border configuration
rotatenumber0Rotation in degrees
flipbooleanfalseHorizontal flip
scalenumber1Scale factor

Style-Specific Options

InitialsOptions

For @avatar-generator/style-initials:

OptionTypeDefaultDescription
namestringseedName to extract initials from
fontFamilystring"sans-serif"Font family
fontWeightnumber600Font weight
textColorstring"#fff"Text color

GeometricOptions

For @avatar-generator/style-geometric (Identicon):

OptionTypeDefaultDescription
gridSizenumber5Grid size (odd recommended for symmetry)
paddingnumber1Padding cells around the pattern
foregroundColorstring(from palette)Override foreground color

PixelsOptions

For @avatar-generator/style-pixels (Pixel Faces):

OptionTypeDefaultDescription
pixelSizenumber8Pixel grid size
skinTonesstring[]SKIN_TONESCustom skin tone palette
accessoriesbooleantrueEnable accessories like glasses (15% chance)
featureColorstring"#2C1810"Override eye/mouth color

RingsOptions

For @avatar-generator/style-rings:

OptionTypeDefaultDescription
ringCountnumber4Number of rings
ringGapnumber2Gap between rings
segmentedbooleantrueAllow segmented pie-slice rings
dashedbooleantrueAllow dashed rings
centerStylestring"solid"Center decoration: "solid", "dot", "ring", "diamond", "none"

FacesOptions

For @avatar-generator/style-faces:

OptionTypeDefaultDescription
skinTonesstring[]SKIN_TONESCustom skin tone palette
featureColorstring"#2C1810"Override feature color
eyebrowsbooleantrueEnable eyebrows (50% chance)
earsbooleantrueEnable ears (40% chance)
nosebooleantrueEnable nose (30% chance)
mouthStylestring(random)Override mouth: "smile", "neutral", "open", "smirk", "small"
eyeStylestring(random)Override eyes: "dots", "circles", "closed", "happy"
hairStylestring(random)Override hair: "none", "short", "flat", "parted", "long", "curly", "mohawk", "buzz"

IllustratedOptions

For @avatar-generator/style-illustrated:

OptionTypeDefaultDescription
skinTonesstring[]SKIN_TONESCustom skin tone palette
eyeColorsstring[]EYE_COLORSCustom eye color palette
hairStylestring(random)Override hair (12 styles): "bald", "buzz", "short", "medium", "long", "curly", "wavy", "mohawk", "afro", "ponytail", "bangs", "sidepart"
eyeStylestring(random)Override eyes (8 styles): "round", "almond", "narrow", "wide", "sleepy", "winking", "looking", "glasses"
eyebrowStylestring(random)Override eyebrows (6 styles): "natural", "thick", "thin", "raised", "furrowed", "unibrow"
noseStylestring(random)Override nose (5 styles): "small", "pointed", "round", "long", "button"
mouthStylestring(random)Override mouth (8 styles): "smile", "bigSmile", "neutral", "frown", "open", "smirk", "tongue", "teeth"
glassesbooleantrueEnable glasses (20% chance, 3 types)
hatbooleantrueEnable hat (10% chance, 3 types)
earringsbooleantrueEnable earrings (8% chance)
facialHairbooleantrueEnable facial hair (15% chance, 3 types)

AnimeOptions

For @avatar-generator/style-anime:

OptionTypeDefaultDescription
skinTonesstring[]SKIN_TONESCustom skin tone palette
eyeColorsstring[]EYE_COLORSCustom eye color palette
hairStylestring(random)Override hair (10 styles): "short-spiky", "medium-messy", "long-straight", "twin-tails", "ponytail", "side-swept", "wild", "bob", "hime-cut", "shaggy"
eyeStylestring(random)Override eyes (8 styles): "normal", "sparkly", "determined", "gentle", "cat", "half-closed", "closed-happy", "surprised"
mouthStylestring(random)Override mouth (6 styles): "small-smile", "open-small", "cat-mouth", "line", "pout", "grin"
noseStylestring(random)Override nose (3 styles): "dot", "line", "shadow"
expressionstring(random)Override expression
bangsboolean(random, 60%)Enable bangs over forehead
ahogeboolean(random, 40%)Enable ahoge hair strand on top
blushboolean(random, 35%)Enable blush on cheeks
accessoriesbooleantrueEnable accessories (bandaid 5%, headband 8%)

Utilities

createRandom

Creates a deterministic random number generator from a seed string.

import { createRandom } from "@avatar-generator/core";
const random = createRandom("my-seed");
random.next(); // Float between 0 and 1
random.int(0, 10); // Integer between 0 and 9
random.pick(["a", "b"]); // Random item from array
random.bool(0.7); // Boolean with 70% true probability
random.shuffle([1,2,3]); // Shuffle array in place

DEFAULT_COLORS

The default color palette used by styles:

import { DEFAULT_COLORS } from "@avatar-generator/core";
// ["#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4", "#FFEAA7",
// "#DDA0DD", "#98D8C8", "#F7DC6F", "#BB8FCE", "#85C1E9"]

SKIN_TONES

Skin tone palette for face-based styles (Pixel Faces, Faces, Illustrated, Anime):

import { SKIN_TONES } from "@avatar-generator/core";
// ["#FFDBB4", "#EAC086", "#C68B59", "#A0674B", "#8D5524", "#613915"]

EYE_COLORS

Eye color palette for the Illustrated and Anime styles:

import { EYE_COLORS } from "@avatar-generator/core";
// ["#634E34", "#2E536F", "#3D6B45", "#89724B", "#3B3024"]

SVG Utilities

Low-level SVG building functions:

import {
escapeXml,
createSvgOpen,
createBackground,
createBorder,
buildSvg,
} from "@avatar-generator/core";
// escapeXml(text) - Escapes XML special characters
// createSvgOpen(size) - Creates opening SVG tag
// createBackground(size, color, transparent) - Creates background rect
// createBorder(size, width, color, square) - Creates border
// buildSvg(content, options, backgroundColor) - Builds complete SVG

Types

Style Interface

interface Style<T extends AvatarOptions = AvatarOptions> {
name: string;
create(options: T): AvatarResult;
}

AvatarResult

interface AvatarResult {
svg: string;
toDataUri(): string;
}

Random

interface Random {
next(): number;
int(min: number, max: number): number;
pick<T>(array: T[]): T;
bool(probability?: number): boolean;
shuffle<T>(array: T[]): T[];
}