Skip to content

Commit fe89dba

Browse files
committed
Draft of a new tree shakeable API
1 parent 37b3125 commit fe89dba

18 files changed

+6044
-14158
lines changed

README.md

+32-35
Original file line numberDiff line numberDiff line change
@@ -22,59 +22,56 @@ Created something awesome with simplex-noise? Let me know so I can add it to the
2222

2323
```npm i -S simplex-noise```
2424

25-
## Usage
25+
## Usage Examples
26+
27+
### ES Module Import
2628

2729
```javascript
28-
// when using es modules
29-
import SimplexNoise from 'simplex-noise';
30-
// when using commonjs
31-
const {SimplexNoise} = require('simplex-noise');
30+
// when using ES modules
31+
import { noiseFunction2D } from 'simplex-noise';
3232
```
3333

34-
By default simplex-noise.js will use Math.random() to seed the noise.
34+
### CommonJS Require
35+
3536
```javascript
36-
// initializing a new simplex instance
37-
// do this only once as it is relatively expensive
38-
const simplex = new SimplexNoise(),
39-
value2d = simplex.noise2D(x, y),
40-
value3d = simplex.noise3D(x, y, z),
41-
value4d = simplex.noise4D(x, y, z, w);
37+
const { noiseFunction2D } = require('simplex-noise');
4238
```
4339

44-
You can also pass in a seed string which will then be used to initialize
45-
the noise using the built in alea PRNG.
46-
```javascript
47-
const simplex = new SimplexNoise('seed'),
48-
value2d = simplex.noise2D(x, y),
49-
sameSeed = new SimplexNoise('seed'),
50-
differentSeed = new SimplexNoise('different seed');
40+
### 2D
5141

52-
sameSeed.noise2D(x, y) === value2d
53-
differentSeed.noise2D(x, y) !== value2d
42+
```
43+
const noise2D = noiseFunction2D();
44+
console.log(noise2D(x, y));
5445
```
5546

56-
You can also pass an alternative random function to the constructor that is
57-
used to build the permutation table.
58-
This can be used with a custom pseudo random number generator:
47+
### 3D
5948

60-
```javascript
61-
const random = new Alea(seed),
62-
simplex = new SimplexNoise(random),
63-
value2d = simplex.noise2D(x, y);
49+
```
50+
const noise3D = noiseFunction3D();
51+
console.log(noise3D(x, y, z));
6452
```
6553

66-
The ALEA PRNG can be found in the npm package [alea](https://npmjs.org/package/alea).
54+
### 4D
6755

68-
## node.js
56+
```
57+
const noise4D = noiseFunction4D();
58+
console.log(noise4D(x, y, z, w));
59+
```
6960

70-
Node.js is also supported, you can install the package using [npm](https://npmjs.org/package/simplex-noise).
61+
### Using a seed value
7162

72-
```javascript
73-
const SimplexNoise = require('simplex-noise'),
74-
simplex = new SimplexNoise(Math.random),
75-
value2d = simplex.noise2D(x, y);
63+
By default simplex-noise.js will use Math.random() to seed the noise.
64+
You can pass in a PRNG function to use your own seed value.
65+
66+
```
67+
import Alea from 'alea';
68+
const alea = new Alea('seed');
69+
const noise2D = noiseFunction2D(alea);
70+
console.log(noise2D(x, y));
7671
```
7772

73+
The ALEA PRNG used in the example above can be found in the [alea](https://npmjs.org/package/alea) npm package.
74+
7875
## Benchmarks
7976

8077
simplex-noise.js is reasonably quick.

commonjs-wrapper.ts

-5
This file was deleted.

examples/plasma.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import SimplexNoise from 'simplex-noise';
1+
import {noiseFunction3D} from 'simplex-noise';
22

33
const canvas = document.createElement('canvas');
44
canvas.width = 512;
55
canvas.height = 512;
66
document.body.appendChild(canvas);
77

8-
const simplex = new SimplexNoise(),
8+
const noise3D = noiseFunction3D(),
99
ctx = canvas.getContext('2d'),
1010
imgdata = ctx.getImageData(0, 0, canvas.width, canvas.height),
1111
width = imgdata.width,
@@ -16,8 +16,8 @@ function render() {
1616
const t = performance.now()/1000;
1717
for (var x = 0; x < width; x++) {
1818
for (var y = 0; y < height; y++) {
19-
var r = simplex.noise3D(x / 16, y / 16, t) * 0.5 + 0.5;
20-
var g = simplex.noise3D(x / 8, y / 8, t) * 0.5 + 0.5;
19+
var r = noise3D(x / 16, y / 16, t) * 0.5 + 0.5;
20+
var g = noise3D(x / 8, y / 8, t) * 0.5 + 0.5;
2121
data[(x + y * width) * 4 + 0] = r * 255;
2222
data[(x + y * width) * 4 + 1] = (r + g) * 200;
2323
data[(x + y * width) * 4 + 2] = 0;

0 commit comments

Comments
 (0)