Multicultural Avatar Generator in JavaScript

Overview

Multiavatar

Multiavatar is a multicultural avatar maker.

Multiavatar represents people from multiple races, multiple cultures, multiple age groups, multiple worldviews and walks of life.

In total, it is possible to generate 12,230,590,464 unique avatars.

Installation and usage

Include the script and pass any string to the 'multiavatar' function. It will return the SVG code for the avatar.

Using npm:

npm i @multiavatar/multiavatar

CommonJS:

const multiavatar = require('@multiavatar/multiavatar')
let svgCode = multiavatar('Binx Bond')

ES Module:

import multiavatar from '@multiavatar/multiavatar/esm'
let svgCode = multiavatar('Binx Bond')

Using the script tag:




Include from CDN:


Info

The initial unique 48 (16x3) avatar characters are designed to work as the source from which all 12 billion avatars are generated.

You can find them in the svg folder. These initial characters can be edited with a vector drawing program, such as Inkscape. They are in grayscale, since the color is applied later by the script, and grayscale is better for shapes design.

Every avatar consists of 6 parts:

  • Environment
  • Clothes
  • Head
  • Mouth
  • Eyes
  • Top

Also, there are different versions of different parts. In some final avatars, additional parts are hidden with transparency (none for the color) to create different shapes by re-using the same code. Also, each avatar has 3 unique color themes that are defined in the script. In total, there are: 16 characters x 3 versions/themes = 48 initial unique avatars. You can see them all by opening the svg/index.html file in your browser.

To create new avatars, the Multiavatar script mixes different parts of different avatars, and different color themes.

The total number of unique avatars: 48^6 = 12,230,590,464

One of the main Multiavatar functions is to work as an identicon. Every unique avatar can be identified by the unique string of characters, associated with the avatar.

The string of characters is also the input for the Multiavatar script, which converts the provided string into a 6 double-digit numbers (range 00-47), each representing an individual part of the final avatar.

000000000000 - this string of numbers represents the very first avatar + its A theme. You can also read it like this: 00 00 00 00 00 00.

474747474747 - this is the 12,230,590,464th avatar (or the 16th initial avatar + its "C" color theme).

More info can be found in the multiavatar.js file comments.

Development

Multiavatar is coded in vanilla JavaScript. Only a single library (SHA-256) is used, integrated into the script. The initial character designs in SVG are also integrated into the script. So there is a single dependency-free JavaScript file that has everything in it (minified 68 KB).

To test different parts manually, you can uncomment them in the code.

Also, the Multiavatar script accepts the following three paramenters: string, sansEnv, ver.

string - The text string that will be converted into the avatar. This parameter is required, the other two are optional.

sansEnv - If this is true, the script returns the final avatar without the circle background (environment part).

ver - Pass an object to force a specific initial version, for example: multiavatar('test', false, {'part': '01', 'theme': 'A'});

While the algorithm generates 12 billion unique avatars, some parts (eyes, mouth) are still similar, so it means that currently there are less than 12 billion unique avatars. It is trivial to make all avatars unique by slightly changing the color, but visually that would not make a difference. That's why instead of forcing a formal solution, some parts are left unfinished intentionally.

The design of shapes, and the multicultural balance of avatars are also open for further polishing, optimization, creative ideas and improvements.

Design

The final SVG files are saved (Inkscape 1.0 as "Optimized SVG") with the _final suffix in the svg folder.

After editing the SVG files with a vector program, overwrite the existing _final.svg files with your new versions. Then run the _build.php script to automatically update the multiavatar-dev.js script with the new SVG code from your _final.svg files.

API

To get an avatar as SVG code, add the avatar's ID to the URL:

https://api.multiavatar.com/Binx Bond

JavaScript API call example to get SVG code:

let avatarId = 'Binx Bond'
fetch('https://api.multiavatar.com/'
+JSON.stringify(avatarId))
  .then(res => res.text())
  .then(svg => console.log(svg))

To get an avatar as SVG file, add .svg to the end of the URL:

https://api.multiavatar.com/Binx Bond.svg

To get an avatar as PNG file, add .png to the end of the URL:

https://api.multiavatar.com/Binx Bond.png

License

You can use Multiavatar for free, as long as the conditions described in the LICENSE are followed.

Screenshots

More info

For additional information and extended functionality, visit the multiavatar.com web-app.

The app is based on static html for the home page, and on Laravel 8 + Vue.js for extended functionality, including the web store.

The product mockup generator for the Merch Maker is based on the ImageMagick library.

Comments
  • Add default size in SVG to prevent rendering problems in canvas

    Add default size in SVG to prevent rendering problems in canvas

    Hello,

    I'm not sure if this PR is relevant but I've encountered a problem when I try to draw the generated SVG into a canvas (with some javascript like this) :

     window.addEventListener("load", function () {
                var myCanvas = document.querySelector('#myOutputCanvas')
                var ctx = myCanvas.getContext('2d')
                var img1 = new Image()
                img1.onload = function () {
                    ctx.drawImage(img1, 0, 0)
                }
                var avatarId = 'Binx Bond';
    // this doesn't work :
                var svgCode = multiavatar(avatarId);
    // this works :
                svgCode = '<svg xmlns="http://www.w3.org/2000/svg" width="231" height="231" viewBox="0 0 231 231"><path .....'
                
                img1.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgCode)
            })
    

    When I copy-paste the content of the generated SVG and add the height and the width attributes into the svg root tag, it's ok.

    I'm not an expert in SVG so if this PR would break other usages for your library, feel free to dismiss it.

    Anyway, thank you for your time.

    opened by Trismegiste 4
  • Support UMD and ES modules

    Support UMD and ES modules

    More and more people are using ES modules because of their static nature. It would be great if Multiavatar supported that. Support for UMD would also be great so then users of AMD and people who just read off the global object could also use it. There a couple different ways this could be implemented, the most organized being a rollup build setup or something, the least being copy and pasting the same code into different files for different formats.

    opened by shadowtime2000 4
  • Generating new new characters

    Generating new new characters

    Hi there ! First of all this is really awesome :) thank you for making this awesome ability! I've managed to run locally and get images generated :D Now starting to study up on svgs and how to make them, would like to draw my own art style and have it generate a new set of results

    Was wondering if you could suggest how you made your images, which program you used, and if there's any specific formatting needed to have it work with the app , like having exactly 6 sections, etc

    Thanks a lot ! Have a good one !

    opened by fire17 2
  • PHP support

    PHP support

    I have just finished rewriting Multiavatar in PHP.

    It is now being used for the https://api.multiavatar.com, which became much faster, because before it was a mix of PHP and Node.js, and now just PHP.

    Further on, I'm planning to build avatar images "on the fly". On user request, the image will be generated and saved on the server. It will be possible to generate and access it directly just by adding .png or .svg at the end of the URL, e.g. https://multiavatar.com/Starcrasher.png

    Additionally, this PHP script will be used in the Multiavatar WordPress plugin, which is planned for the near future.

    For more details, please refer to the PHP repository here: https://github.com/multiavatar/multiavatar-php

    opened by giekaton 0
  • SVG as inline datauri

    SVG as inline datauri

    Hello @giekaton !

    Abstract

    I am building a web with users and didn't want to use the "default_user.svg" since it is boring. Found this package and fell in love with it immediately.

    Problem

    So I went ahead integrated it with my React project and since I don't want to "unsafely render svg text" on my web, I tried inline SVG like the following:

    // React Component
    const Avatar = () => {
      const svgString = useMemo(() => {
        return multiavatar("hello world!", false)
      }, [])
    
      return (
        <img src={`data:image/svg+xml,${svgString}`} alt="avatar" />
      )
    }
    

    The above did not render because # is included in the svgString.

    Solution

    So I tried encoding the string as such:

    // from "https://github.com/tigt/mini-svg-data-uri"
    // "modified" a bit
    const REGEX = {
        whitespace: /\s+/g,
        urlHexPairs: /%[\dA-F]{2}/g,
        quotes: /"/g,
    }
    
    function specialHexEncode(match: string) {
        switch (match) {
            case '%20': return ' ';
            case '%3D': return '=';
            case '%3A': return ':';
            case '%2F': return '/';
            default: return match.toLowerCase();
        }
    }
    
    export const toDataURI = (string: string) =>
        encodeURIComponent(string)
            .trim()
            .replace(REGEX.whitespace, ' ')
            .replace(REGEX.quotes, "'")
            .replace(REGEX.urlHexPairs, specialHexEncode)
    

    The above code was used as return toDataURI(multiavatar("hello world!", false)) and what do you know, it renders!

    Advantages

    For developers using multiavatar on the client side can easily render the avatars with img tag.

    Request

    multiavatar function to include optional parameter asDataURI?: boolean (defaults to false)

    Real Life Usage

    // React Component
    const Avatar = () => {
      const svgString = useMemo(() => {
        // May also support options object?
        // return multiavatar("hello world", {
        //   asDataURI: true,
        //   sansEnv: false,
        // })
        
        // expecting `data:image/svg+xml,{encodedSVGstring}`
        return multiavatar("hello world", false, , true);
      }, [])
    
      return (
        <img src={svgString} alt="avatar" />
      )
    }
    

    Edit:

    • Mentioned @giekaton because I wanted to say hello ✋
    • Added expectation returned from multiavatar @ Real Life Usage
    opened by thejskhan 2
  • Feature request

    Feature request

    Is there any way users on my website could choose from selected options without leaving the site. It would be great if options are only 16 or 48 instead of 12 billion. Need an api which I call to get full list, get avatar characters or generate random avatar.

    opened by yogeshGroony 0
  • Hoping add generating square shape avatar feature

    Hoping add generating square shape avatar feature

    Hi @giekaton your project is excellent, but in some websites and Apps, the avatar is a square shape, so It would be better if the Multiavatar can support generating square shape avatar.

    It seems changing the avatar's shape and spread the background color to the edge can do it, but this change may cost a lot, even rebuild the project.

    So an easier way is adding an option discard background , without the circle background, the avatar can fit not only circle shape but the square shape.

    Thank you very much!

    opened by livrth 3
  • Gender filter (feature request)

    Gender filter (feature request)

    Congrats on the new repo! I love it!

    It would be handy to be able to add a gender filter, or a filter that creates gender-neutral avatars (say, no mustache for female and gender-neutral avatars).

    opened by tigrr 3
Owner
Multiavatar
12 Billion Unique Multicultural Avatars
Multiavatar
Return an optimized avatar image from a domain name input.

IndieWeb Avatar API A runtime service to extract avatar images from: <link rel="apple-touch-icon"> <link rel="apple-touch-icon-precomposed"> <link rel

Eleventy 45 Aug 28, 2022
A simple Discord avatar changer.

Discord Avatar Changer A simple Discord avatar changer. The purpose of use is arbitrary. You can use it to get the job done quickly or out of boredom.

Tau 1 Dec 20, 2021
ENS Avatar resolver library for both nodejs and browser.

ens-avatar Avatar resolver library for both nodejs and browser. Getting started Prerequisites Have your web3 provider ready (web3.js, ethers.js) [Only

Ethereum Name Service (ENS) 27 Dec 30, 2022
Lip-sync VRM avatar client for zero-webcam mic-based vtubing

VU-VRM A lip-sync VRM avatar client for zero-webcam mic-based vtubing: automattic.github.io/VU-VRM/ Why? Because multitasking. Because sometimes you n

Automattic 10 Oct 19, 2022
Avatar classes to extend Bootstrap 5.

Bootstrap Avatar A collection of fixed and responsive CSS for styling avatars. Demo https://ghoststead.github.io/bootstrap-avatar Installation jsDeliv

null 4 May 22, 2022
Obsidian text generator Plugin Text generator using GPT-3 (OpenAI)

is a handy plugin for Obsidian that helps you generate text content using the powerful language model GP

null 356 Dec 29, 2022
a babel plugin that can transform generator function to state machine, which is a ported version of typescript generator transform

Babel Plugin Lite Regenerator intro This babel plugin is a ported version of TypeScript generator transform. It can transform async and generator func

Shi Meng 6 Jul 8, 2022
Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Vineeth.TR 16 Dec 6, 2022
JavaScript OAuth 1.0a signature generator (RFC 5849) for node and the browser

OAuth 1.0a signature generator for node and the browser Compliant with RFC 5843 + Errata ID 2550 and community spec Installation Install with npm: npm

Marco Bettiolo 230 Dec 16, 2022
JavaScript documentation generator for node using markdown and jsdoc

Dox Dox is a JavaScript documentation generator written with node. Dox no longer generates an opinionated structure or style for your docs, it simply

TJ Holowaychuk 2.1k Jan 7, 2023
Simple JavaScript Duckumentation generator.

Warning: JSDuck is no more maintained! If you're looking to adopt a documentation tool, try something else. If you're using JSDuck, consider moving ov

Sencha Labs 1.5k Nov 28, 2022
Javascript Sound Effect Generator

This is a JavaScript library for sound effect generation and is supported on most current browsers. Generation speed is approximately 1s audio = 10ms

Loov 567 Oct 31, 2022
Unique guid generator pure Javascript.

Guid Generator Create unique Guids. Usage For client Javascript import { Guid } from "../src/guid"; Guid.NewGuid(); // 1q6G3w1U-8F0D-8p9R-7m6m-5b5B7G

Yahya Altıntop 11 Nov 1, 2022
Cross-browser QRCode generator for javascript

QRCode.js QRCode.js is javascript library for making QRCode. QRCode.js supports Cross-browser with HTML5 Canvas and table tag in DOM. QRCode.js has no

Sangmin, Shim 12.1k Dec 28, 2022
A tiny (108 bytes), secure, URL-friendly, unique string ID generator for JavaScript

Nano ID English | Русский | 简体中文 | Bahasa Indonesia A tiny, secure, URL-friendly, unique string ID generator for JavaScript. “An amazing level of sens

Andrey Sitnik 19.6k Jan 8, 2023
Binary-encoded serialization of JavaScript objects with generator-based parser and serializer

YaBSON Schemaless binary-encoded serialization of JavaScript objects with generator-based parser and serializer This library is designed to transfer l

Gildas 11 Aug 9, 2022
JSDoc generator for JavaScript, TypeScript using AI. (VSCode extension)

JSDoc generator for JavaScript, TypeScript using AI. (VSCode extension)

Amir Reza Dalir 3 Aug 18, 2022
High-quality QR Code generator library in Java, TypeScript/JavaScript, Python, Rust, C++, C.

QR Code generator library Introduction This project aims to be the best, clearest QR Code generator library in multiple languages. The primary goals a

Nayuki 3.3k Jan 4, 2023
🚀 A RESTful API generator for Node.js

A RESTful API generator rest-hapi is a hapi plugin that generates RESTful API endpoints based on mongoose schemas. It provides a powerful combination

Justin Headley 1.2k Dec 31, 2022