GithubHelp home page GithubHelp logo

worley-noise's Introduction

worley-noise

Worley noise in JavaScript.

What is it?

Worley noise (also called Voronoi or Cell noise) is a type of noise where the value of a point is based on its distance to a set of previously placed points. By using the distance to the closest point it produces images like this:

But we don't have to use the closest point, we can choose the second, third, etc. We can even combine these values by performing mathematical operations on them like addition and subtraction:

The previous images were generated by using the Euclidean distance for the calculations. If we use Manhattan distance we get quite different results:

The formula for generating these images can be found in the advanced example.

Getting started

Download the development or the minified version.

Example usage:

<script src="worley-noise.dev.js"></script>
<script>
// Creates a new noise instance with 10 randomly placed points.
// Takes seed as a second argument (optional, defaults to 10000).
// Coordinates range from (0, 0) to (1, 1).
var noise = new WorleyNoise(10, 1000);

// Manually adds a point to the center.
noise.addPoint(0.5, 0.5);

// Gets Euclidean noise value at (0.3, 0.4).
// The third argument (k) defines which point should be chosen when calculating the distance.
// As k=1 in this case, the closest point is chosen.
console.log(noise.getEuclidean(0.3, 0.4, 1));

// Gets Manhattan noise value at (0.3, 0.4).
// As k=2 in this case, the 2nd closest point is chosen.
console.log(noise.getManhattan(0.3, 0.4, 2));

// Creates an 5x5 array with the computed noise values.
var width = 5;
var map = noise.getMap(width);

// Gets value at (3, 2).
// (3, 2) corresponds to (3 / (5 - 1), 2 / (5 - 1)) -> (0.75, 0.5).
console.log(map[2 * width + 3]);

// Creates a normalized array where values have been scaled to be between 0 and 1.
map = noise.getNormalizedMap(width);

// Uses custom function for noise value calculation.
// It sums the Euclidean distance to the closest point
// and the Manhattan distance to the second closest point.
map = noise.getNormalizedMap(width, function (e, m) {
    return e(1) + m(2);
});
</script>

Canvas examples can be found in the project.

Development

Install dependencies:

npm install

Create a dev build:

npm run dev

Create a minified build:

npm run build

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.