A fast simplex noise implementation in Javascript.

Overview

simplex-noise.js

API Documentation

Tests TypeScript

simplex-noise.js is a simplex noise implementation in Javascript/TypeScript. It works in the browser and nodejs. 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).

Demos

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

Installation

npm i -S simplex-noise

Usage

// when using es modules
import SimplexNoise from 'simplex-noise';
// when using commonjs
const {SimplexNoise} = require('simplex-noise');

By default simplex-noise.js will use Math.random() to seed the noise.

// initializing a new simplex instance
// do this only once as it is relatively expensive
const simplex = new SimplexNoise(),
    value2d = simplex.noise2D(x, y),
    value3d = simplex.noise3D(x, y, z),
    value4d = simplex.noise4D(x, y, z, w);

You can also pass in a seed string which will then be used to initialize the noise using the built in alea PRNG.

const simplex = new SimplexNoise('seed'),
    value2d = simplex.noise2D(x, y),
    sameSeed = new SimplexNoise('seed'),
    differentSeed = new SimplexNoise('different seed');

sameSeed.noise2D(x, y) === value2d
differentSeed.noise2D(x, y) !== value2d

You can also pass an alternative random function to the constructor that is used to build the permutation table. This can be used with a custom pseudo random number generator:

const random = new Alea(seed),
    simplex = new SimplexNoise(random),
    value2d = simplex.noise2D(x, y);

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

node.js

Node.js is also supported, you can install the package using npm.

const SimplexNoise = require('simplex-noise'),
    simplex = new SimplexNoise(Math.random),
    value2d = simplex.noise2D(x, y);

Benchmarks

simplex-noise.js is reasonably quick. According to perf/benchmark.js I can perform about 50 million noise2D() calls/second on a single thread on my desktop (Ryzen 5950X). So ~20 nanoseconds per call.

$ node perf/index.js
27745787.933336906
init: 192,590 ops/sec ±1%
noise2D: 57,928,891 ops/sec ±1%
noise3D: 34,159,230 ops/sec ±0%
noise4D: 24,589,786 ops/sec ±0%

At least at a glance it also seems to be faster than 'fast-simplex-noise':

simplex-noise noise2D: 53,429,815 ops/sec ±0%
fast-simplex-noise noise2D: 6,239,845 ops/sec ±0%

simplex-noise noise4D: 22,578,593 ops/sec ±0%
fast-simplex-noise noise4D: 5,292,975 ops/sec ±0%

Tests

There are some simple unit tests for this library to run them

npm install && npm test

Changelog

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

Requirements

It requires typed arrays. If you want to use it in browsers without support you will need to use a polyfill like typedarray.js.

License

Copyright (c) 2021 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 integrated pseudo random generator is based on code by by Johannes Baagøe.

The initial typescript definition has been provided by Neonit.

Comments
  • importing alea as a module

    importing alea as a module

    https://www.npmjs.com/package/alea is tiny and could probably be leveraged inside of simplex-noise.

    This would be valuable because other parts of my simulation rely on alea, and if simplex-noise is pulling this in as a dependency I'll have less duplicated code.

    @jwagner would you be open to a PR for this? I'm happy to submit one.

    opened by mreinstein 12
  • optimizations proposed by Perlin, corrections proposed by Gustavson

    optimizations proposed by Perlin, corrections proposed by Gustavson

    Factored out gradient look up and applied Gustavson's corrections. Minor pre calculation tweaks. Comments and references are in the code, still needs to be cleaned up.

    opened by EricBalingit 10
  • Tree shakeable v4.0

    Tree shakeable v4.0

    Release plan

    • [x] Finish writing article
    • [x] Finish demo
    • [x] Release docs
    • [x] Merge this branch & Trigger NPM Release
    • [x] Release synth wave demo to 29a.ch
    • [x] Make synth wave demo repo public
    • [x] Fix up code pens
    • [x] Publish article & update on 29a.ch
    • [x] Announce new version (twitter, ?)
    • [x] Clean up related issues & PRs
    opened by jwagner 9
  • MIME type ('application/octet-stream') is not executable

    MIME type ('application/octet-stream') is not executable

    Hi there,

    Thanks for putting this awesome resource out there. I was hoping to use the 3.0.1 version in codepen but getting a MIME type ('application/octet-stream'). Here's the link. Version 2.4.0 seems to work fine, given a nearly identical implementation.

    opened by olgageletina 6
  • upd to even faster floor method

    upd to even faster floor method

    x | 0 does exactly the same as Math.floor(x) but much faster. Don't know why you got ~10% speedup by combining both approaches, but definitely it would be even faster without calling Math.floor at all.

    opened by foretoo 5
  • ENOENT Error

    ENOENT Error

    What

    Forgive me if I'm wrong but I am using this package in a create-react-app 5.0.0 and React 17.X.X setting and with simplex-noise 3.0.0 I recieve this error.

    WARNING in ./node_modules/simplex-noise/dist/esm/simplex-noise.js
    Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
    Failed to parse source map from '/Users/matt-jarrett/Developer/js-pollock/node_modules/simplex-noise/simplex-noise.ts' file: Error: ENOENT: no such file or directory, open '/Users/matt-jarrett/Developer/js-pollock/node_modules/simplex-noise/simplex-noise.ts'
     @ ./src/lib/create-renderer.js 6:0-41 15:22-34
     @ ./src/lib/index.js 8:0-50 28:25-39
     @ ./src/App.js 13:0-25 87:39-42
     @ ./src/index.js 8:0-27 11:38-41
    
    1 warning has detailed information that is not shown.
    Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.
    
    webpack 5.65.0 compiled with 1 warning in 1864 ms
    

    My use

    import SimplexNoise from "simplex-noise"
    ...
    export default (opt = {}) => {
      ...
      const simplex = new SimplexNoise(randFunc)
      ...
    }
    

    Am I doing something wrong?

    I searched for simplex-noise.ts as it's referenced in the error and I see it in this package's package.json.

    ...
    "source": "simplex-noise.ts",
    ...
    
    opened by cujarrett 5
  • use pure es module, use alea from npm, modernize module. fixes #31

    use pure es module, use alea from npm, modernize module. fixes #31

    a number of changes:

    • replace the alea internal implementation with the one in npm
    • convert the module from UMD to a pure es module (API Breaking change)
    • remove bower package
    • update all dependencies
    • use let and const instead of var everywhere
    • update travis ci to test against maintained node versions (10 through 14)
    • style change: function() -> function ()
    opened by mreinstein 5
  • experimental es module support

    experimental es module support

    hello friend,

    i wanted to load simplex-noise as a hip new web-ready es module

    i created this branch and also published [email protected] to npm for my own usage

    the es module is available now via cdn here: https://unpkg.com/[email protected]/dist-esm/simplex-noise.js

    i used babel-plugin-transform-commonjs to generate the es module distribution

    codepen example demonstrates loading simplex-noise-esm as an es module

    opened by chase-moskal 4
  • Move permutation table creation to a separate class method when using own PRNG

    Move permutation table creation to a separate class method when using own PRNG

    Feature request

    Move the following into a SimplexNoise.init() (or other name) method: https://github.com/jwagner/simplex-noise.js/blob/37b3125a48ec3347c7a66d6e12ce2e78447ada99/simplex-noise.ts#L80-L87

    Use case

    When updating the random number generator, we are forced to recreate an instance of SimplexNoise. With this, we could create a single instance and reinit it if the PRNG changed:

    // Use default Math.random (non predictable)
    let simplex = new SimplexNoise(Math.random);
    
    function setSeed(s) {
      // Update seed to make Math.random predictable
      seedMathRandom(s)
      
      // Before:
      // Recreate noise instance
      simplex = new SimplexNoise(Math.random);
    
      // After proposed change:
      // Reset permutation table
      simplex.init(Math.random);
    }
    
    setSeed('0')
    

    Let me know how that sounds and if you need a PR.

    enhancement 
    opened by dmnsgn 3
  • `package.json` not exported for Sapper

    `package.json` not exported for Sapper

    Whenever I build dev or build in sapper, rollup-plugin-svelte shows this warning.

    [rollup-plugin-svelte] The following packages did not export their `package.json` file so we could not check the "svelte" field. If you had difficulties importing svelte components from a package, then please contact the author and ask them to export the package.json file.
    

    Just to bring this to your attention, everything still works even though the warning is shown. All builds still are successful and functionality is not affected.

    opened by EvasiveXkiller 3
  • Fixing falsy seed values

    Fixing falsy seed values

    If you set seed to zero or empty string, then SimplexNoise will generate random noise which might not be expected as the seed value was set. Now it will only be random if 'randomOrSeed' not set at all.

    opened by tiborsaas 3
  • Analytical derivatives?

    Analytical derivatives?

    Any chance you might add an (optional) way to get analytical derivatives for the noise result?

    Derivatives are handy because they tell you the ”slope” of the noise, which is incredibly useful for use cases like terrain generation. It’s possible to manually calculate them by sampling the noise 3+ times, but (as I understand it) analytical derivatives are much cheaper to compute.

    As a reference, I’ve found these GLSL implementations:

    • https://iquilezles.org/articles/gradientnoise/
    • https://github.com/BrianSharpe/Wombat (the _Deriv variants)

    I’ve explored adding them myself and making a PR, but honestly I’m a bit out of my depth here. :)

    feature 
    opened by jonikorpi 1
  • `noise()` generic function

    `noise()` generic function

    what about a "generic" noise function, with variable-length arguments, ie:

    |generic |implied |comment| |-|-|-| |noise()|noise2D(0, 0)| 0 by default| |noise(1)|noise2D(1,1)| same value for 2nd arg| |noise(1,2)|noise2D(1,2)| | |noise(1,2,3)|noise3D(1,2,3)| | |noise(1,2,3,4)|noise4D(1,2,3,4)| | |noise(1,2,3,4,5)|noise4D(1,2,3,4,5)|NB: when more than 4 args => 5th, 6th... will just be ignored by noise4D|

    This is inspired from https://processing.org/reference/noise_.html

    function noise(...args) {
      let dim // 2 or 3 or 4
      let args2 = [...args] // copy of args
      
      if (args.length < 2) {
        // 0 or 1 arg => noise2d
        const arg = args[0] || 0
        args2 = [arg, arg]
        dim = 2
      } else {
        // 2 or 3 or 4 or more args => noise2D or noise3D or noise4D
        dim = Math.min(args.length, 4)
      }
      
      return simplex[`noise${dim}D`](...args2) /2 + 0.5 // normalize [0;1]
    }
    

    NB: I've chosen to normalise the value to ]0;1[ but maybe you prefer staying ]-1;1[

    working demo: https://codepen.io/abernier/pen/ExvqNyj

    enhancement 
    opened by abernier 4
  • higher dimensions

    higher dimensions

    Would you consider supporting dimensions beyond 4D, specifically 5D and 6D?

    For me this would simplify use-cases for looping / tiling animations such as #29 which can be implemented by using two of the dimensions to sample along a circle.

    enhancement 
    opened by dribnet 1
Releases(4.0.1)
  • 4.0.1(Oct 14, 2022)

  • 4.0.0(Jul 23, 2022)

    This new version completely changes the API

    • 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.
    Source code(tar.gz)
    Source code(zip)
  • 3.0.1(Jan 4, 2022)

    What's Changed

    • Updated examples to es6 by @amilajack in https://github.com/jwagner/simplex-noise.js/pull/26
    • Fix source map warnings by @jwagner in https://github.com/jwagner/simplex-noise.js/pull/44

    New Contributors

    • @amilajack made their first contribution in https://github.com/jwagner/simplex-noise.js/pull/26

    Full Changelog: https://github.com/jwagner/simplex-noise.js/compare/3.0.0...3.0.1

    Source code(tar.gz)
    Source code(zip)
  • 3.0.0(Aug 23, 2021)

    • 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
    Source code(tar.gz)
    Source code(zip)
  • 2.4.0(Feb 10, 2018)

Owner
Jonas Wagner
Jonas Wagner
Mute email noise from people you don't know.

All the love to supporters ❤️ You are fantastic if you're using emailgurus.xyz. Thank you so much for your support. Welcome to Emailgurus! Hi! Here is

Aymane Sennnoussi 16 Oct 28, 2022
Fast & minimal implementation of bech32, base64, base32, base16 & base58

micro-base Fast and minimal implementation of bech32, base64, base58, base32 & base16. Matches following specs: Bech32, Bech32m: BIP173, BIP350 Base16

Paul Miller 45 Jan 4, 2023
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022
A flexible, memory compact, fast and dynamic CSG implementation on top of three-mesh-bvh

three-bvh-csg An experimental, in progress, flexible, memory compact, fast and dynamic CSG implementation on top of three-mesh-bvh. More than 100 time

Garrett Johnson 208 Jan 5, 2023
A quotaless, partially limitless, and fast Node.js Multiplayer Piano server implementation that efficiently makes use of the protocol and uWebSockets.js

speedymppserver A quotaless, partially limitless, and fast Node.js Multiplayer Piano server implementation that efficiently makes use of the protocol

Lapis 4 Oct 14, 2022
Simple Library implemented using HTML, CSS and JavaScript. This is a simple implementation of JavaScript Modules of ES6.

Awesome-books A single page project with the porpuse of storing books' titles and authors. Built With CSS, HTML & Javascript. How to run in your local

Saadat Ali 7 Feb 21, 2022
Javascript implementation of flasher tool for Espressif chips, running in web browser using WebSerial.

Javascript implementation of esptool This repository contains a Javascript implementation of esptool, a serial flasher utility for Espressif chips. Un

Espressif Systems 103 Dec 22, 2022
A JavaScript implementation of Michael Baker's Miner2149

miner2149-js A JavaScript implementation of Michael Baker's Miner2149. This repository is for personal use only. Not for commercial use or distributio

Gabrien Symons 1 Jan 11, 2022
A javascript text differencing implementation.

jsdiff A javascript text differencing implementation. Based on the algorithm proposed in "An O(ND) Difference Algorithm and its Variations" (Myers, 19

Kevin Decker 6.8k Jan 7, 2023
javascript implementation of logistic regression/c4.5 decision tree

LearningJS: A Javascript Implementation of Logistic Regression and C4.5 Decision Tree Algorithms Author: Yandong Liu. Email: yandongl @ cs.cmu.edu Upd

Yandong Liu 67 Aug 19, 2022
ParkyDB - block based, linkable and verifiable document database -- javascript reference implementation

Ancon ParkyDB A data mesh database using Web 3.0 technology Note: Requires Node v17.7.2 and up for development More about data mesh architecture Block

Ancon Protocol 6 Aug 16, 2022
An implementation of (Untyped) Lambda Calculus in JavaScript.

Lambda Calculus More restraint and more pure, so functional and so reduced. -- Anonymous Bauhaus Student An implementation of (Untyped) Lambda Calculu

Cicada Language 25 Nov 17, 2022
An implementation of Interaction Nets in JavaScript.

Interaction Nets An implementation of Interaction Nets in JavaScript. Use S-expression as overall syntax. Use Forth-like postfix stack-based syntax to

Cicada Language 48 Dec 23, 2022
An Open-Source JavaScript Implementation of Bionic Reading.

bionic-reading Try on Runkit or Online Sandbox An Open-Source JavaScript Implementation of Bionic Reading API. ⚙️ Install npm i bionic-reading yarn ad

shj 127 Dec 16, 2022
A tiny CRDT implementation in Javascript.

Tiny Merge A tiny CRDT implemented in Javascript. The philosophy behind Tiny Merge is to strategically reduce the functionality of CRDT's in favour of

James Addison 10 Dec 2, 2022
An Open-Source JavaScript Implementation of Bionic Reading.

TextVide (vide; Latin for "see") Support all languages that separate words with spaces Try on Runkit or Online Sandbox An Open-Source JavaScript Imple

shj 127 Dec 16, 2022
Charm implementation in JavaScript (TypeScript)

charm.js A tiny, self-contained cryptography library, implementing authenticated encryption and keyed hashing. Any number of hashing and authenticated

Frank Denis 12 Nov 2, 2022
A JavaScript implementation of the Spring 83 protocol

An implementation of the Spring 83 protocol. Very much a work-in-progress. This was built in reference to draft-20220616.md@0f63d3d2. Setup Requires n

Ryan Joseph 10 Aug 22, 2022
A native-like JavaScript pull to refresh implementation for the web.

Pull to Refresh for the Web 1.1 This is a pull to refresh implementation for the web. It focuses on buttery UX performance and responsiveness to feel

Andy Peatling 536 Dec 19, 2022