UNPKG

@lueur/avatar

Version:

A TypeScript package to generate customizable SVG user avatars.

47 lines (46 loc) 2.13 kB
import { AvatarOptions } from "../types"; /** * Generates an SVG avatar based on the provided username and customization options. * * @param {string} username - The name or identifier used to generate the avatar. * @param {AvatarOptions} [options] - Optional customization settings for the avatar. * @param {number} [options.width=100] - Width of the avatar in pixels. * @param {number} [options.height=100] - Height of the avatar in pixels. * @param {string} [options.backgroundColor] - Background color of the avatar. * If not provided, it is generated from the username hash. * @param {string} [options.textColor] - Color of the text in the avatar. * If not provided, it is derived for proper contrast with the background color. * @param {string} [options.shape="square"] - Shape of the avatar background ("circle" or "square"). * @param {number} [options.initialsLength=2] - Number of characters to use for the avatar's initials. * @param {string} [options.fontFamily="Arial, sans-serif"] - Font family for the text inside the avatar. * @param {number} [options.fontSize] - Font size for the text. If not provided, * it is calculated based on the avatar dimensions. * @param {string} [options.text] - Custom text to display in the avatar. If not provided, * initials are generated from the username. * @param {Object} [options.svgAttributes={}] - Additional attributes to add to the root SVG element. * * @return {string} The generated avatar SVG as a string. * * @example * // Generate a basic square avatar * const svg = generateAvatarSvg('johndoe'); * * @example * // Generate a circular avatar with custom colors * const svg = generateAvatarSvg('janedoe', { * shape: 'circle', * backgroundColor: '#FF0000', * textColor: '#FFFFFF' * }); * * @example * // Generate an avatar with custom size and text * const svg = generateAvatarSvg('bobsmith', { * width: 200, * height: 200, * text: 'BS', * fontSize: 48, * svgAttributes: { class: 'custom-avatar' } * }); */ export declare function generateAvatarSvg(username: string, options?: AvatarOptions): string;