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

@excaliburjs/plugin-perlin

v0.29.0

Published

Perlin noise generator for the Excalibur game engine

Downloads

6

Readme

Excalibur Perlin Noise

This is a small module that implements the original perlin noise implementation in Excalibur given the original Siggraph paper

You may want to use noise if you need "smooth" randomness from point to point. It's super useful for generating "natural" looking structures randomly.

Usage

In your excalibur project run

npm install @excaliburjs/plugin-perlin

import { PerlinGenerator } from '@excaliburjs/plugin-perlin';

const generator = new PerlinGenerator({
    seed: 515, // random seed
    octaves: 2, // number of times noise is laid on itself
    frequency: 24, // number of times the pattern oscillates, higher is like zooming out
    amplitude: 0.91, // [0-1] amplitude determines the relative height of the peaks generated in the noise
    persistance: 0.95 // [0-1] he persistance determines how quickly the amplitude will drop off, a high degree of persistance results in smoother patterns, a low degree of persistance generates spiky patterns.
});

You can use the generator to sample perlin noise with the given configuration.

  • generator.noise(x) - Sample 1D noise, outputs a value [0, 1]
  • generator.noise(x, y) - Sample 2D noise, outputs a value [0, 1]
  • generator.noise(x, y, z) - Sample 3D noise, , outputs a value [0, 1]
  • generator.sequence(length, optionalStep) - Generates a list of 1D noise starting at 0 and ending at 1 of continuous perlin noise, by default the step is 1/length;
  • generator.grid(width, height, optionalStep) - Generates a 2D grid as a packed array of perlin noise given a step value packed into a 1D array i = (x + y*width), by default the step will 1/(min(dimension))

Perlin Drawing

If you want to draw perlin noise directly there is a helper PerlinDrawer2D which can be used to draw to a Canvas or output an HTML Image.

// Create a perlin drawer and pass a generator
// Optionally pass a color function that takes a sample value [0, 1] and produces a Color
const drawer = new PerlinDrawer2D(generator, (value: number) => {
    const rainbow = [
        ex.Color.Red,
        ex.Color.Orange,
        ex.Color.Yellow,
        ex.Color.Green,
        ex.Color.Blue,
        ex.Color.Violet
    ]
    const colorIndex = Math.floor((val * rainbow.length));
    return rainbow[colorIndex];
});

const canvas = new ex.Canvas({
    width: 800,
    height: 600,
    cache: true,
    draw: (ctx) => {
        // Generating perlin noise expensive
        drawer.draw(ctx, 0, 0, 800, 600);
    }
});
const actor = new ex.Actor({x: 0, y: 0, width: 800, height: 600});
actor.graphics.use(canvas);
actor.graphics.anchor = ex.Vector.Zero;
game.add(actor);

Example perlin noise

Here is an example of HTML image output, this can be used as an upload to an Excalibur Material or other ImageSource.

const drawer = new PerlinDrawer2D(generator, (value: number) => {
    const rainbow = [
        ex.Color.Red,
        ex.Color.Orange,
        ex.Color.Yellow,
        ex.Color.Green,
        ex.Color.Blue,
        ex.Color.Violet
    ]
    const colorIndex = Math.floor((val * rainbow.length));
    return rainbow[colorIndex];
});

const image = drawer.image(200, 200);
document.body.appendChild(image);