npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fractalsoftware/random-avatar-generator

v1.0.11

Published

A random avatar generator with low collision

Downloads

117

Readme

Random Avatar Generator

LICENSE Node.js CI Node.js Package

A random pixel pattern generator with low collision and zero external dependencies.

sample

Demo

https://morra.co/random-avatar-generator

Introduction

As part of an academic publication and a side project, I built this simple library to ilustrate different aspects of the JavaScript language capabilities and ecosystem, mixed with some general software development concepts.

This library can produce a random svg code for a given complexity with the following logic:

  • For a given complexity positive integer number a couple of numbers representing x and y axes random binary numbers are generated.
  • XOR computed value enables one position within the matrix of values.
  • In order to have bigger numeric space to prevent collision, each row within the matrix has a random color number.

Installation

$ npm install @fractalsoftware/random-avatar-generator

Usage

How to generate a random pattern:

In browser

<script src="random-avatar-generator.js"></script>

<div id="randomavatar"></div>

<script>
  document.getElementById(
    "randomavatar"
  ).innerHtml = randomAvatarGenerator.getRandomAvatar(8);
</script>

In NodeJS

const randomAvatarGenerator = require("random-avatar-generator");

// Using default method parameters
fs.writeFileSync("avatar.svg", randomAvatarGenerator.getRandomAvatar());

In React

import { getRandomAvatar } from "@fractalsoftware/random-avatar-generator";

const avatar = getRandomAvatar();
cons inlineAvatar = <img src={`data:image/svg+xml;base64,${btoa(avatar)}`} />;

Controlled use

You can also generate the avatar data definition and then generate the SVG string with the following methods:

const avatarData = randomAvatarGenerator.generateRandomAvatarData();
const svgCode = randomAvatarGenerator.getAvatarFromData(avatarData);

In this way, you can keep track of the avatar code and repaint it when needed.

Options

These are the general parameters available in the library API:

complexity

This parameter determines the bits length of the x and y axes randomly generated numbers.

size

The purpose of this optional parameter is the inner calculations of the SVG paths. As the output is a scalable vector, dimensions are almost irrelevant, but we prefered to keep this parameter available on the the library API.

renderMethod

As each enabled position within the SVG output can be rendered independently, the API allows the selection of the way it will be draw. There are defined two methods for this property: square (default) and circle. But also it's possible to extend it defining a custom callback that must output a string with a valid SVG path.

This is an example of a custom callback that can be used in this parameter:

const drawTriangle = (resolution, indexX, indexY) => `M${(indexX * resolution) + (resolution / 2)},${indexY * resolution} l${resolution / 2} ${resolution} l-${resolution} 0z`;
// Output example: M76.5,0 l25.5 51 l-51 0z

in wich

resolution is function of the relation between size and complexity or the size of each cell to be draw:

const resolution = Math.floor(size / complexity);

and

indexX and indexY are the selected cell coordinates to be draw.

With this parameters you'll be able to define the absolute position within the draw area and the dimensions of each cell.

More information about SVG paths here: Paths - SVG: Scalable Vector Graphics | MDN.

Available methods

generateRandomAvatarData

Returns a randomly generated string representation code of an avatar for a given complexity.

Usage

const avatarData = randomAvatarGenerator.generateRandomAvatarData(3);
// Output example: 0-6-6te25-9d9p0-xd5g

Arguments

| name | type | description | | --------------------- | ------ | ------------------------------ | | complexity | number | (optional) Default value: 16 | | avatarDataSeparator | string | (optional) Default value: - |

getAvatarFromData

Returns a string with a valid SVG markup for a given avatarData.

Usage

randomAvatarGenerator.getAvatarFromData("0-6-6te25-9d9p0-xd5g");
// Output: <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 256 256"><path fill="#aeb26d" d="M0,0 h85 v85 h-85Z M85,0 h85 v85 h-85Z M170,0 h85 v85 h-85Z"/><path fill="#f01b54" d="M0,85 h85 v85 h-85Z M85,85 h85 v85 h-85Z M170,85 h85 v85 h-85Z"/><path fill="#17c0d4" d=""/></svg>

Arguments

| name | type | description | | --------------------- | ------------------ | ----------------------------------- | | avatarData | string | (required) A valid avatar data code | | renderMethod | string or function | (optional) Default value: square | | size | number | (optional) Default value: 256 | | avatarDataSeparator | string | (optional) Default value: - |


getRandomAvatar

A simpler helper that returns a random SVG markup for a give complexity

Usage

const randomAvatar = randomAvatarGenerator.getRandomAvatar(4);
// Output example: <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 256 256"><path fill="#fbdfae" d="M0,0 h64 v64 h-64Z M128,0 h64 v64 h-64Z"/><path fill="#1ad956" d="M0,64 h64 v64 h-64Z M128,64 h64 v64 h-64Z"/><path fill="#f26f0b" d="M64,128 h64 v64 h-64Z M192,128 h64 v64 h-64Z"/><path fill="#38b27d" d="M0,192 h64 v64 h-64Z M128,192 h64 v64 h-64Z"/></svg>

Arguments

| name | type | description | | -------------- | ------------------ | ---------------------------------- | | complexity | number | (optional) Default value: 16 | | renderMethod | string or function | (optional) Default value: square | | size | number | (optional) Default value: 256 |

License

The code is available under the MIT license.