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

pf-value-noise

v1.0.2

Published

N-Dimensional Value Noise Generator

Downloads

8

Readme

pf-value-noise

N-Dimensional Value Noise Generator - A value noise generator for any number of dimensions. Similar to, but faster than Perlin noise.

Rainbow Value Noise

Examples

// Require the module to use it.
const ValueNoise = require('pf-value-noise')

// Create a 3D value noise generator.
const noise3D = new ValueNoise({ dimensions: 3 })

// Use it to make a 100×100×100 grid of values
const resolution = 100
let data = []
for (let x = 0; x < resolution; ++x) {
  for (let y = 0; y < resolution; ++y) {
    for (let z = 0; z < resolution; ++z) {
      data.push(noise3D.get([ x / resolution, y / resolution, z / resolution ]))
    }
  }
}

const _ = require('lodash')
data = _(data).chunk(resolution).chunk(resolution).value()
data[5][62][17]
// 0.6594545530358533

The following example creates the above picture.

// Create the canvas
const { createCanvas } = require('canvas')
const [ width, height ] = [ 800, 200 ]
const canvas = createCanvas(width, height)
const ctx = canvas.getContext('2d', { alpha: false })

// Create the image data
const ValueNoise = require('pf-value-noise')
const noise3D = new ValueNoise({ dimensions: 3, seed: 'pillow' })
const resolution = 100
const imageData = ctx.createImageData(width, height)
let dataIndex = 0
for (let row = 0; row < height; ++row) {
  for (let col = 0; col < width; ++col) {
    imageData.data[dataIndex++] = noise3D.get([ row / resolution, col / resolution, 0 ]) * 256 | 0
    imageData.data[dataIndex++] = noise3D.get([ row / resolution, col / resolution, 1 ]) * 256 | 0
    imageData.data[dataIndex++] = noise3D.get([ row / resolution, col / resolution, 2 ]) * 256 | 0
    ++dataIndex
  }
}

// Export the image data
const fs = require('fs')
ctx.putImageData(imageData, 0, 0)
canvas.createPNGStream()
  .pipe(fs.createWriteStream('rainbow-value-noise.png'))

API

ValueNoise

({Class}): Represents a value noise generator.

const ValueNoise = require('pf-value-noise')
const noiseGenerator = new ValueNoise()

ValueNoise.constructor([options])

Arguments

  1. [options] (Object): An objects of options.

| Option | Type | Default | Description | |:---------------:|:--------:|:--------:|:-------------------------------| | seed | String | null | RNG's seed | | dimensions | Number | 2 | Number of dimensions | | min | Number | 0 | Minimum value returned | | max | Number | 1 | Maximum value returned | | wavelength | Number | 1 | Size of the first octave | | octaves | Number | 8 | Number of octaves to sample | | octaveScale | Number | 1/2 | Scaling for successive octaves | | persistence | Number | 1/2 | Weight for successive octaves | | interpolation | Function | cosine | Interpolation function used |

Note that even with the same seed, a different order of <Perlin>.get() calls can change the overall noise function since its values are generated lazily.

wavelength sets the size of the first octave, and each successive octave will be octaveScale times the previous. The octaves are centered about the origin and added together according to their weight. The first octave has a weight of 1, and each successive octave will be persistence times the previous.

The octaves are sampled using the interpolation function with signature function(a, b, t) that returns a value between a and b according to the parameter 0 <= t <= 1. The default interpolation function used is cosine interpolation.

interpolation: function (a, b, t) {
  return (1 - Math.cos(Math.PI * t)) / 2 * (b - a) + a
}

After the octaves are sampled and added together, the values are adjusted to fall between min and max. Note that the value distribution is roughly Gaussian depending on the number of octaves.

ValueNoise.prototype.get(coordinates)

Arguments

  1. coordinates (Array): The data point to get. Its length should match dimensions.

Returns

  • (Number): The value at those coordinates.
const noise4D = new ValueNoise({ dimensions: 4 })

noise4D.get([ 1, 2, 3, 4 ])
// 0.538503118881535