Docs

Cat avatars for your site

Give every user a cat. Use one image link, add a small script, or let people build their own cat with the picker and save it to their account.

Getting started

Pick the way that fits your site. All three draw the same cats.

Every way starts from the same rule: the same text always makes the same cat. Give it a user id and that user gets their own cat, with nothing to store. If you want people to choose their cat, the picker gives you a short cat code to save instead.

Script tag

Add one script and draw cats right in the browser. Nothing is sent anywhere.

HTML
<script src="https://cdn.jsdelivr.net/npm/catvatar@1/dist/catvatar.min.js"></script>

<!-- a tag that draws a cat -->
<cat-avatar name="user-42" size="48"></cat-avatar>
<cat-avatar code="v1-012803203330001100" size="48" shape="squircle"></cat-avatar>

The <cat-avatar> tag takes name or code, plus size (pixels or any CSS length), shape, colors and label (the text screen readers hear). It redraws when you change an attribute.

JavaScript
// an <img> with a user's cat
document.querySelector("#avatar").src = Catvatar.dataUri(user.id);

// or the SVG markup itself
el.innerHTML = Catvatar.svg(user.catCode || user.id, { size: 64 });

The script is also served by this site at https://catvatar.mocat.app/catvatar.min.js, if you would rather not use jsDelivr.

npm and React

The same package for bundlers, with TypeScript types.

Terminal
npm install catvatar
React
import { svg } from "catvatar";

export function Avatar({ user, size = 40 }) {
  const markup = svg(user.catCode || user.id, { size });
  return <span role="img" aria-label={user.name} dangerouslySetInnerHTML={{ __html: markup }} />;
}

The markup comes from Catvatar itself, never from what users type, so it is safe to put in the page. Importing the package also defines <cat-avatar>, so you can use the tag in any framework.

Avatar picker

Let people make their own cat instead of uploading a photo. It opens on top of your page and gives you the cat to save.

Live demoA pretend profile form. The cat is saved in this browser only.
Sam

No cat saved yet, so the name draws one. That works as a default avatar.

JavaScript
<script src="https://cdn.jsdelivr.net/npm/catvatar@1/dist/catvatar.min.js"></script>
<button id="pick">Choose your cat</button>

<script>
  document.querySelector("#pick").onclick = async () => {
    const cat = await Catvatar.pick({ code: user.catCode });
    if (!cat) return; // they closed it

    // save it with the account, like any other profile field
    await fetch("/api/profile", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ catCode: cat.code }),
    });
  };
</script>

Options

OptionWhat it does
codeOpen a cat saved earlier, so people can change it
nameStart from the cat this text makes (a random cat if left out)
titleThe heading. "Choose your cat" if left out
saveLabelThe save button. "Use this cat" if left out
theme"light", "dark" or "auto" (follows the visitor's setting)
shapesWhich shapes people can pick, like ["circle"] if your site always shows round avatars

What you get back

pick() gives null if the person closes it, or:

FieldWhat it is
codeThe cat code. Save this: 21 characters, or 52 with custom colors
svgThe picture as SVG markup, if you would rather store the image
dataUriThe picture as a data: URL, ready for <img src>
png(size)Makes a PNG file (a Blob) to upload to your own storage. 256 pixels if left out

Match your site

The picker keeps its own styles, so your CSS cannot break it. Change its colors with these variables:

CSS
catvatar-picker {
  --cv-accent: #7a5af8;     /* buttons and selected parts */
  --cv-on-accent: #ffffff;  /* text on the accent */
  --cv-radius: 14px;
  --cv-font: inherit;
  --cv-bg: #ffffff;  --cv-text: #1d1b22;  --cv-muted: #6b6675;  --cv-line: #e3e0e8;
  /* dark theme: the same names ending in -dark, like --cv-bg-dark */
}

Cat codes

One short line of text that holds a whole cat. Store it like any other text field.

Catvatar

v1-012803203330001100 is Momo: a black tuxedo with a nose streak, round eyes and an open mouth, on the Momo palette.

  • A code holds every part, the palette and the shape. It does not depend on any name, so people can rename themselves and keep their cat.
  • Codes start with a version (v1-). A saved code always opens the same cat.
  • Check a code before you save it with Catvatar.isCode(text). It is safe to show in pages and links: only letters, numbers and dashes.
  • Show it with <cat-avatar code="…">, Catvatar.svg(code) or the image link with ?code=.

On your server

The package has no dependencies and runs anywhere JavaScript does: Node, Bun, Deno and Cloudflare Workers.

Node
import { svg, isCode } from "catvatar";

// check what the browser sent before saving it
if (!isCode(req.body.catCode)) return res.status(400).send("Not a cat code");

// draw an avatar for an email, a PDF or an image API of your own
const markup = svg(user.catCode || user.id, { size: 128 });

All functions

FunctionWhat it does
svg(textOrCode, options)The cat as SVG markup. Options: size, shape, colors, background, title
dataUri(textOrCode, options)The same as a data: URL for <img src>
png(textOrCode, size)A PNG file (Blob). Browser only
pick(options)Opens the avatar picker. Browser only
codeFor(text)The cat code of the cat a text makes, so the picker can open it for editing
isCode(text)True when the text is a valid cat code
decode(code)Reads a code into its parts (traits, shape, colors), or null
defineElement(tag)Registers the avatar tag under another name, if cat-avatar is taken

Privacy and versions

  • The script never sends anything. Cats are drawn on the visitor's device. There is no tracking and no account.
  • Do not put emails in image links. Links show up in logs and browser history. Use a user id, or a hash of the email.
  • Cat codes do not change. A v1- code opens the same cat for as long as this exists.
  • Pin the major version. catvatar@1 on jsDelivr gets fixes but never a change that redraws your users' cats.