• Stars
    star
    1,461
  • Rank 30,995 (Top 0.7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A fast simplex noise implementation in Javascript / Typescript.

Tests TypeScript

simplex-noise.js header API Documentation

simplex-noise.js is a simplex noise implementation in Javascript/TypeScript. It works in the browser and Node.js, using CommonJS and ES Modules. It is self-contained (dependency-free), relatively small (about 2k minified and gzipped) and fairly fast (about 20 nanoseconds for a sample of 2d noise) and tree shakeable.

Demos & Real World Examples

Created something awesome with simplex-noise? Let me know so I can add it to the list.

Installation

npm i -S simplex-noise

Usage Examples

ES Module Import

// import the noise functions you need
import { createNoise2D } from 'simplex-noise';

CommonJS Require

// import the noise functions you need
const { createNoise2D } = require('simplex-noise');

2D

// initialize the noise function
const noise2D = createNoise2D();
// returns a value between -1 and 1
console.log(noise2D(x, y));

3D

const noise3D = createNoise3D();
console.log(noise3D(x, y, z));

4D

const noise4D = createNoise4D();
console.log(noise4D(x, y, z, w));

Using a seed value

By default simplex-noise.js will use Math.random() to seed the noise. You can pass in a PRNG function to use your own seed value.

# install the alea prng
npm install -S alea
import alea from 'alea';
// create a new random function based on the seed
const prng = alea('seed');
// use the seeded random function to initialize the noise function
const noise2D = createNoise2D(prng);
console.log(noise2D(x, y));

The ALEA PRNG used in the example above can be found in the alea npm package.

Benchmarks

simplex-noise.js is reasonably quick. According to perf/index.js I can perform about 70 million noise2D() calls/second on a single thread on my desktop (Ryzen 5950X).

$ node perf/index.js
noise2D: 72,916,215 ops/sec Β±1%
noise3D: 47,855,199 ops/sec Β±0%
noise4D: 35,564,111 ops/sec Β±0%

Migrating from 3.x to 4.x

random initialization

// 3.x
import SimplexNoise from 'simplex-noise';
const simplex = new SimplexNoise();
const value2d = simplex.noise2D(x, y);
// 4.x
// import the functions you need
import { createNoise2D } from 'simplex-noise';
const noise2D = createNoise2D();
const value2d = noise2D(x, y);

Initialization with a seed

// 3.x
import SimplexNoise from 'simplex-noise';
const simplex = new SimplexNoise('seed');
const value2d = simplex.noise2D(x, y);
// 4.x
// npm install -S alea
import { createNoise2D } from 'simplex-noise';
import alea from 'alea';
const noise2D = createNoise2D(alea('seed'));
const value2d = noise2D(x, y);

// IMPORTANT: If you use multiple noise functions (for example 2d and 3d)
// and want compatible output with 3.x you will need to pass a fresh instance
// of alea to each create call. If you reuse the alea instance you will
// get different outputs compared to simplex-noise 3.x.
const seed = 'seed';
const noise2D = createNoise2D(alea(seed));
const noise3D = createNoise3D(alea(seed));

Emulating the 3.x and older API

const simplex = {
  noise2D: createNoise2D(alea(seed)),
  noise3D: createNoise3D(alea(seed)),
  noise4D: createNoise4D(alea(seed)),
};

Changelog

4.0.1

  • Explicitly defined the return type of createNoise4D to be NoiseFunction4D. Contributed by satelllte.

4.0.0

⚠️ This release changes the API and the output of the noise functions. ⚠️

  • Reworked the API so that the noise functions can be imported individually. When combined with tree shaking this helps with build sizes.
  • Removed the built in version of the ALEA PRNG to focus the library to do only one thing. If you want to continue to use it you'll have to install and import it separately.
  • Noise functions are a bit faster (~ 20 - 30%).
  • Noise values can be different from previous versions
  • Input coordinates bigger than 2^31 may not result in a noisy output anymore. If you have a use case that is affected by this change, please file an issue.
  • Test coverage is now at 100%.
  • A big thank you to @mreinstein, @redblobgames and everyone else involved for their comments and PRs which motivated me to create this new version.

3.0.1

  • Include simplex-noise.ts as source file, fixes sourcemap warnings.

3.0.0

  • Changed module structure. When using bundlers that import the es module even using require() the import might need to be updated.
  • Dependency update
  • Setting sideEffects: false in package.json
  • Added snapshot tests
  • Code converted to typescript, the package can of course still be used from regular JS
  • Dropped bower
  • Added support for es modules

2.4.0

  • Included a PRNG based on ALEA to directly allow seeding
  • Included typescript definitions

2.3.0

⚠️ This release changes the output of the noise functions. ⚠️

In the future such changes will be released as a new major version.

  • Corrected generation of permutation table
  • Moved tests to mocha/chai
  • Cleanup

2.2.0

  • Small performance improvement for 2D noise

2.1.1

  • Increased entropy by fixing a little initialization issue.

2.1.0

  • AMD support

2.0.0

  • Changed node.js api, SimplexNoise is now exported directly.
  • Added unit tests

1.0.0

  • Initial Release

License

Copyright (c) 2022 Jonas Wagner, licensed under the MIT License (enclosed)

Credits

This is mostly a direct javascript port of the Java implementation by Stefan Gustavson and Peter Eastman.

The initial typescript definition has been provided by Neonit.

More Repositories

1

smartcrop.js

Content aware image cropping
JavaScript
12,766
star
2

smartcrop-cli

Command line interface for the smartcrop library to provide content aware image cropping.
JavaScript
316
star
3

analog-film-emulator

A web based analog film emulator/photo editor.
JavaScript
236
star
4

normalmap.js

normalmap.js is a library for creating simple interactive lighting effects using normal maps.
JavaScript
176
star
5

fluidwebgl

WebGL Fluid Simulation
JavaScript
170
star
6

Neonflames

Neon flames is a crazy online HTML5 drawing tool.
JavaScript
142
star
7

dont-crop

A small, dependency free javascript library to fit a gradient to an image or extract it's primary colors.
TypeScript
122
star
8

smartcrop-sharp

Node module for using smartcrop via sharp/libvips
JavaScript
109
star
9

playitslowly

Play it slowly is a software to play back audio files at a different speed or pitch.
Python
98
star
10

box2d2-js

Automatic port of box2dAS 2.0 to javascript
ActionScript
87
star
11

voxelworlds

WebGL Voxel World Generation Demo
JavaScript
73
star
12

terrain

WebGL Terrain, ocean, fog
JavaScript
71
star
13

webglice

A webgl demo, showing of an iceberg featuring hdr rendering water reflections etc.
JavaScript
36
star
14

httpripper

HTTP Ripper is a tool to rip content out of the web.
Python
34
star
15

simplex-noise-demo-synthwave

Just a little demo for simplex-noise.js 4.0
TypeScript
27
star
16

smartcrop-gm

Node module for using smartcrop via image magick
JavaScript
27
star
17

space-break

Space Break is a html5 acrade 'ball and paddle' game implemented in coffee script
CoffeeScript
23
star
18

kinect-experiments

kinect usb rocket launcher controll
Python
18
star
19

Frontendconf-2011

Source code from a live coding talk on canvas particle systems I gave at frontendconf
JavaScript
13
star
20

jquery.textCloud

jQuery plugin to create text clouds
JavaScript
12
star
21

addresscloud

Visualization of 3.7 million swiss addresses using WebGL
JavaScript
12
star
22

fluidcanvas

Fluid simulation experiment using HTML5 Canvas
JavaScript
12
star
23

guitarTrainer

A web based guitar practice tool
JavaScript
10
star
24

lanshark

P2P Filesharing Tool
Python
8
star
25

musicviz-code

Coded during my talk @takeoffconf
JavaScript
6
star
26

littlemetronome

a little metronome written using python, gtk and gstreamer
Python
5
star
27

wildwebgl

Raymarching distance fields with wild results
JavaScript
5
star
28

webrtc-pong

A simple WebRTC multiplayer game example
JavaScript
4
star
29

kalman-ts

A simplistic library for implementing kalman filters using javascript/typescript.
TypeScript
4
star
30

box2dlite

Box2Dlite changed to run on linux
C++
3
star
31

jack-pipe

Pipes a wav file through jack into another wav file.
C
2
star
32

schwermetall

3D Printed Guitar Pedal Enclosure
2
star
33

bluenoise-rs

A bit of fun generating blue noise using void and cluster
Rust
2
star
34

swirly-lens-hoods

A 3D printable lens hood that creates swirly bokeh.
1
star
35

parcel-namer-29a

probably not of interest to anyone
TypeScript
1
star