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

cellular-automata-react

v1.5.0

Published

Cellular Automata grid component capable of running your own automata variants, includes a library containing Conways game of life preset and its methods.

Downloads

21

Readme

cellular-automata-react

This is the result of a walkthrough I wrote on http://craigwh.it/posts/conways-game-of-life-in-react.

Prerequisites

  • React (v18 onwards)

Installation

Once installed you can import this module into your React components and get using it quickly.

For some implementation examples see:

NPM

npm i cellular-automata-react

Yarn

yarn add cellular-automata-react

What is Cellular Automata?

A cellular automaton is a collection of "colored" cells on a grid of specified shape that evolves through a number of discrete time steps according to a set of rules based on the states of neighboring cells. The rules are then applied iteratively for as many time steps as desired.

Reference: Wolfram Mathworld

What can I do?

  • Run fun experiments by writing javascript using the API provided.
  • Make interesting "living" graphics on your website.
  • Run the Conways preset and play around with the initial conditions.

This package provides an <AutomataGrid /> component from which you can:

  • Set the initial pixels via coordinates.
  • Set how fast the iterations happen.
  • Set the size of the grid.
  • Set the rules of how the pixels will behave.
  • Set the style of the pixels or grid within.

Example: Conway's game of life with a randomized pixel soup

Example: Random Soup - Generates an N randomized pixel distribution.

import './App.css';
import {
    AutomataGrid,
    conwaysGameOfLifePreset,
    generateSoup,
} from 'cellular-automata-react';

function App() {
    const gridDimensions = {
        xWidth: 4,
        yWidth: 4,
    };

    return (
        <div className="App">
            <header className="App-header">
                <AutomataGrid
                    pixelsActive={generateSoup(gridDimensions)}
                    iterationTimeInMs={1000}
                    size={gridDimensions}
                    rules={conwaysGameOfLifePreset}
                    className="conway-grid"
                />
            </header>
        </div>
    );
}

export default App;

Setting rules

type Pixel = [number, number];

type RuleCallback = (
    // Every active pixel [x,y] is passed
    // Through this callback per iteration.
    pixel: Pixel,
    // The current state of the grid.
    pixels: Array<Pixel>,
    // The size of the board
    size: number,
    // Setter for the next iteration state of the board.
    setPixelsActive: (pixelsActive: Array<Pixel>) => void,
    // Removing a group of pixels from state by
    // passing in an array of coordinates.
    removeActivePixels: (pixel: Array<Pixel>) => void
) => void;

You use this callback as a prop within <AutomataGrid />.

Then use the data params to apply the setters to either kill or create more pixels.

You can use either the predefined utilities within the package or pass a callback and make your own rules, if they're interesting let me know and I'll add them to the presets :)

Example: Random activity rule callback

import './App.css';
import { AutomataGrid, pixelsNearby } from 'cellular-automata-react';

function App() {
    return (
        <AutomataGrid
            rules={(
                pixel,
                pixels,
                size,
                setPixelsActive,
                removeActivePixels
            ) => {
                // All pixels brought in this callback are alive
                // rather than iterating on each grid element.

                const pixelsNearbyArray = pixelsNearby(pixel, size);
                if (pixelsNearbyArray.length) {
                    const randomPixel =
                        pixelsNearbyArray[
                            Math.floor(Math.random() * pixelsNearbyArray.length)
                        ];
                    setPixelsActive(randomPixel);
                }
            }}
        />
    );
}

export default App;

Setting styles

In 1.0.8 you can now pass down a className, it adds the className to the grid wrapper, so you can style from parent downwards.

1.0.9 allows you to set any css-in-js pixel styles for the pixel by passing in an appropriate style object such as the below:

<AutomataGrid
    pixelStyles={{
        activeColor: 'red',
        inactiveColor: 'blue',
        width: 25,
        height: 25,
        border: 1,
        borderColor: 'black',
    }}
/>

OR via CSS:

.automata-grid-custom .automata-grid-element {
    width: 10px !important;
    height: 10px !important;
    background: #e2e8f0 !important;
}

.automata-grid-custom .automata-grid-element-alive {
    background: #38a169 !important;
}

with the prop:

<AutomataGrid className="automata-grid-custom" />

Example rule preset: Conway's game of life

Feel free to take this apart and create your own unique cellular automata algorithm, if you find any cool ones, open a PR with them as a rule.

import {
    nearbyAlivePixelsInState,
    nearbyDeadPixels,
    Pixel,
    AutomataGridSizeProp,
} from 'cellular-automata-react';

export const conwaysGameOfLifePreset = (
    pixel: Pixel,
    pixels: Pixel[],
    size: AutomataGridSizeProp,
    setPixelsActive: (pixelsActive: Array<Pixel>) => void,
    removeActivePixels: (pixel: Array<Pixel>) => void
) => {
    // All the neighbors, so we can generate nearby.

    // Return alive neighbors for current pixel being checked in callback.
    const aliveNeighborPixels = nearbyAlivePixelsInState(pixel, size, pixels);
    const aliveNeighbors = aliveNeighborPixels.length;

    /**
     * DEAD CELL ACTION:
     */

    // We need to only run if has any dead neighbors
    if (aliveNeighbors) {
        // Get an array of the coordinates of the dead pixels around an alive pixel.
        const deadNeighbors = nearbyDeadPixels(pixel, size, pixels);

        // From these dead pixels from neighborhood, return back the ones that have
        // 3 alive neighbors around them.
        const deadPixelsWith3AliveNeighbors = deadNeighbors.filter(
            (deadPixel: Pixel) => {
                return (
                    nearbyAlivePixelsInState(deadPixel, size, pixels).length ===
                    3
                );
            }
        );

        if (deadPixelsWith3AliveNeighbors.length) {
            setPixelsActive(deadPixelsWith3AliveNeighbors);
        }
    }

    /**
     * ALIVE CELL ACTIONS:
     */

    // Any live cell with two or three live neighbours survives.
    if (aliveNeighbors === 3 || aliveNeighbors === 2) {
        return;
    }

    // All other live cells die in the next generation. Similarly, all other dead cells stay dead.
    else {
        removeActivePixels([pixel]);
    }
};

Opportunities/Inspiration

FAQs

  • Does this support SSR?
    • Yes this has been tested on SSR/CSR & SSG

Contribute

Feel free to open pull requests for presets or suggestions and I'll happily look over them.

Also if you make anything cool with this let me know and I'll feature it here.

https://www.npmjs.com/package/cellular-automata-react