Avatar

The low-level <Avatar> component lets you swap rendering algorithms or plug in your own.

Usage

<script setup>
import { Avatar, gradientAvatarAlgorithm } from '@maxnth/gestalt'
</script>

<template>
  <Avatar
    seed="user-123"
    :size="80"
    :algorithm="gradientAvatarAlgorithm"
  />
</template>

Props

PropTypeDefaultDescription
seedstring | numberAny value; passed through to the algorithm.
sizenumber32Rendered size in pixels.
radiusnumber | string"9999px"Corner radius. Number is pixels; string is CSS.
algorithmAvatarAlgorithmAlgorithm object with name and render.
optionsunknownAlgorithm-specific options.
renderSizenumber256Internal canvas resolution.
ariaLabelstringAccessible label for the canvas.
ringboolean | number | stringfalseOptional outline. true = white 2px ring.
styleobjectExtra inline styles merged onto the wrapper.

Writing a custom algorithm

An algorithm is just an object with name and render:

import type { AlgorithmRenderContext, AvatarAlgorithm } from '@maxnth/gestalt'

const stripeAlgorithm: AvatarAlgorithm = {
  name: 'stripe',
  render({ ctx, size }: AlgorithmRenderContext, options) {
    const opts = options as { color?: string }
    const segments = 4
    const segmentHeight = size / segments
    ctx.fillStyle = opts?.color ?? '#3b82f6'
    for (let i = 0; i < segments; i += 2) {
      ctx.fillRect(0, i * segmentHeight, size, segmentHeight)
    }
  },
}

Then pass it to <Avatar>:

<Avatar
  seed="stripe-demo"
  :size="96"
  :algorithm="stripeAlgorithm"
  :options="{ color: '#10b981' }"
/>