How to use @dicebear/avatars - 10 common examples

To help you get started, we’ve selected a few @dicebear/avatars examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DiceBear / avatars / server / server / actions / v2.ts View on Github external
// Check whether the image exists at Gravatar.
          await request.head(gravatarUrl).promise();

          res.redirect(gravatarUrl);

          return;
        } catch (e) {
          // No image available at Gravtar, therefore no forwarding.
        }
      }

      res.status(200);
      res.setHeader('Content-Type', 'image/svg+xml');
      res.setHeader('Cache-Control', 'public, max-age=' + ms(privateConfig.apiHttpCaching) / 1000);
      res.end(new Avatars(spriteCollectionPackage(options.cast(requestOptions))).create(seed));
    } catch (e) {
      console.error(e);

      res.status(500).end('Failed to load package ' + spriteCollection.name);
    }
  }
);
github DiceBear / avatars / packages / avatars-initials-sprites / src / index.ts View on Github external
};

  let backgroundColors: string[] = [];
  let isInternetExplorer =
    options.userAgent &&
    Bowser.getParser(options.userAgent).satisfies({
      ie: '>0',
      edge: '>0'
    });
  let isSafari =
    options.userAgent &&
    Bowser.getParser(options.userAgent).satisfies({
      safari: '>0'
    });

  Object.keys(Color.collection).forEach((backgroundColor: keyof ColorCollection) => {
    if (
      options.backgroundColors === undefined ||
      options.backgroundColors.length === 0 ||
      options.backgroundColors.indexOf(backgroundColor) !== -1
    ) {
      backgroundColors.push(Color.collection[backgroundColor][options.backgroundColorLevel]);
    }
  });

  return function(random: Random) {
    let backgroundColor = random.pickone(backgroundColors);
    let seedInitials = (initials(random.seed.trim()) as string).toLocaleUpperCase().slice(0, options.chars);
    let fontFamily = "-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,'Noto Sans',sans-serif";

    // prettier-ignore
    return [
github DiceBear / avatars / packages / avatars-identicon-sprites / src / index.ts View on Github external
export default function(options: Options = {}) {
  options = {
    padding: 0,
    background: '#FFF',
    colorLevel: 600,
    ...options
  };

  let colors: string[] = [];

  Object.keys(Color.collection).forEach((color: keyof ColorCollection) => {
    if (options.colors === undefined || options.colors.length === 0 || options.colors.indexOf(color) !== -1) {
      colors.push(Color.collection[color][options.colorLevel]);
    }
  });

  return function(random: Random) {
    let color = random.pickone(colors);

    // prettier-ignore
    return [
      `<svg shape-rendering="crispEdges" version="1.1" viewBox="${0-options.padding} ${0-options.padding} ${5+options.padding*2} ${5+options.padding*2}" style="isolation:isolate" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://www.w3.org/2000/svg">`,
      // Background
      `<path fill="${options.background}" d="M${0-options.padding} ${0-options.padding}h${5+options.padding*2}v${5+options.padding*2}H${0-options.padding}V0z"></path>`,
      // Row 1
      random.pickone([
        `<path fill="${color}" fill-rule="evenodd" d="M0 4h1v1H0V4zm4 0h1v1H4V4z"></path>`,</svg>
github oipwg / oip-platform-react / src / components / PublisherIcon.js View on Github external
import React, { Component } from 'react';

import Avatars from '@dicebear/avatars';
import MaleSpriteCollection from '@dicebear/avatars-male-sprites';
import FemaleSpriteCollection from '@dicebear/avatars-female-sprites';

let maleAvatars = new Avatars(MaleSpriteCollection);
let femaleAvatars = new Avatars(FemaleSpriteCollection);

class PublisherIcon extends Component {
	constructor(props){
		super(props);

		this.state = {
			avatarSrc: ""
		}

		this.updateAvatar = this.updateAvatar.bind(this);
		this.setDiceBearAvatar = this.setDiceBearAvatar.bind(this);
	}
	componentDidMount(){
		// Every time the state changes, log it
		this.updateAvatar(this.props)
	}
github oipwg / oip-platform-react / src / components / PublisherIcon.js View on Github external
import React, { Component } from 'react';

import Avatars from '@dicebear/avatars';
import MaleSpriteCollection from '@dicebear/avatars-male-sprites';
import FemaleSpriteCollection from '@dicebear/avatars-female-sprites';

let maleAvatars = new Avatars(MaleSpriteCollection);
let femaleAvatars = new Avatars(FemaleSpriteCollection);

class PublisherIcon extends Component {
	constructor(props){
		super(props);

		this.state = {
			avatarSrc: ""
		}

		this.updateAvatar = this.updateAvatar.bind(this);
		this.setDiceBearAvatar = this.setDiceBearAvatar.bind(this);
	}
	componentDidMount(){
		// Every time the state changes, log it
		this.updateAvatar(this.props)
github DiceBear / avatars / packages / avatars-bottts-sprites / src / index.ts View on Github external
return function(random: Random) {
    let primaryColorCollection = random.pickone(colorsCollection);
    let secondaryColorCollection = random.pickone(colorsCollection);

    let primaryColor = new Color(primaryColorCollection[options.primaryColorLevel]);
    let secondaryColor = new Color(primaryColorCollection[options.secondaryColorLevel]);

    if (options.colorful) {
      secondaryColor = new Color(secondaryColorCollection[options.secondaryColorLevel]);
    }

    let eyes = random.pickone(eyesCollection);
    let face = random.pickone(faceCollection);
    let mouth = random.pickone(mouthCollection);
    let sides = random.pickone(sidesCollection);
    let texture = random.pickone(textureCollection);
    let top = random.pickone(topCollection);

    // prettier-ignore
    return [
      '<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 180">',
      group(random, sides(secondaryColor), options.sidesChance, 0, 66),</svg>
github DiceBear / avatars / packages / avatars-bottts-sprites / src / index.ts View on Github external
return function(random: Random) {
    let primaryColorCollection = random.pickone(colorsCollection);
    let secondaryColorCollection = random.pickone(colorsCollection);

    let primaryColor = new Color(primaryColorCollection[options.primaryColorLevel]);
    let secondaryColor = new Color(primaryColorCollection[options.secondaryColorLevel]);

    if (options.colorful) {
      secondaryColor = new Color(secondaryColorCollection[options.secondaryColorLevel]);
    }

    let eyes = random.pickone(eyesCollection);
    let face = random.pickone(faceCollection);
    let mouth = random.pickone(mouthCollection);
    let sides = random.pickone(sidesCollection);
    let texture = random.pickone(textureCollection);
    let top = random.pickone(topCollection);

    // prettier-ignore
    return [
      '<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 180">',</svg>
github DiceBear / avatars / packages / avatars-bottts-sprites / src / index.ts View on Github external
return function(random: Random) {
    let primaryColorCollection = random.pickone(colorsCollection);
    let secondaryColorCollection = random.pickone(colorsCollection);

    let primaryColor = new Color(primaryColorCollection[options.primaryColorLevel]);
    let secondaryColor = new Color(primaryColorCollection[options.secondaryColorLevel]);

    if (options.colorful) {
      secondaryColor = new Color(secondaryColorCollection[options.secondaryColorLevel]);
    }

    let eyes = random.pickone(eyesCollection);
    let face = random.pickone(faceCollection);
    let mouth = random.pickone(mouthCollection);
    let sides = random.pickone(sidesCollection);
    let texture = random.pickone(textureCollection);
    let top = random.pickone(topCollection);

    // prettier-ignore
    return [
      '<svg fill="none" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 180 180">',
      group(random, sides(secondaryColor), options.sidesChance, 0, 66),
      group(random, top(secondaryColor), options.topChange, 41, 0),
      group(random, face(primaryColor, random.bool(options.textureChance) ? texture() : undefined), 100, 25, 44),
      group(random, mouth(), options.mouthChance, 52, 124),</svg>
github google / wikiloop-battlefield / server / routes / avatar.js View on Github external
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

const Avatars = require('@dicebear/avatars').default;
const sprites = require('@dicebear/avatars-identicon-sprites').default;
const avatars = new Avatars(sprites, {});
const { logger } = require('../common');

module.exports = async (req, res) => {
    logger.debug(`avatar requested with seed`, req.params.seed);
    let svg = avatars.create(req.params.seed);
    res.send(svg);
    req.visitor
        .event({ ec: "api", ea: "/avatar/:seed" })
        .send();
}
github DiceBear / avatars / packages / avatars-bottts-sprites / src / index.ts View on Github external
Object.keys(Color.collection).forEach((color: keyof ColorCollection) => {
    if (options.colors === undefined || options.colors.length === 0 || options.colors.indexOf(color) !== -1) {
      colorsCollection.push(Color.collection[color]);
    }
  });

@dicebear/avatars

An avatar library for designers and developers.

MIT
Latest version published 2 years ago

Package Health Score

53 / 100
Full package analysis