Skip to content

Commit e2c770d

Browse files
authored
Merge pull request #38 from jwagner/typescript
Convert simplex-noise.js to typescript
2 parents 634a758 + d9fa53d commit e2c770d

29 files changed

+21969
-4214
lines changed

.eslintrc.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
module.exports = {
2-
'env': {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: [
5+
'@typescript-eslint',
6+
],
7+
env: {
38
'browser': true,
49
'node': true,
510
'mocha': true,
611
'es2017': true
712
},
8-
'extends': 'eslint:recommended',
13+
'extends': ['eslint:recommended','plugin:@typescript-eslint/recommended'],
914
'globals': {
1015
'Uint8Array': false,
1116
'define': false,
1217
'Float32Array': false
1318
},
19+
'parserOptions': {
20+
'sourceType': 'module'
21+
},
1422
'rules': {
1523
'indent': [
1624
'error',
@@ -28,5 +36,8 @@ module.exports = {
2836
'error',
2937
'always'
3038
]
31-
}
39+
},
40+
'ignorePatterns': [
41+
'/dist'
42+
]
3243
};

.github/workflows/tests.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ jobs:
2222
with:
2323
node-version: ${{ matrix.node-version }}
2424
- run: npm ci
25-
- run: npm test
25+
- run: npm test
26+
- run: npm run-script build

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
node_modules
1+
/node_modules
2+
/.parcel-cache
3+
/dist
4+
/e2e
5+
/public

.mocharc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extension": [
3+
"ts"
4+
],
5+
"spec": "test/simplex-noise-test.ts",
6+
"require": "ts-node/register"
7+
}

README.md

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
# simplex-noise.js
2-
[![Tests](https://github.com/jwagner/simplex-noise.js/actions/workflows/tests.yml/badge.svg)](https://github.com/jwagner/simplex-noise.js/actions/workflows/tests.yml)
32

4-
simplex-noise.js is a fast simplex noise implementation in Javascript. It works in the browser and on nodejs.
3+
[API Documentation](https://29a.ch/simplex-noise/docs/classes/SimplexNoise.html)
4+
5+
[![Tests](https://github.com/jwagner/simplex-noise.js/actions/workflows/tests.yml/badge.svg)](https://github.com/jwagner/simplex-noise.js/actions/workflows/tests.yml) [![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
6+
7+
8+
simplex-noise.js is a simplex noise implementation in Javascript/TypeScript.
9+
It works in the browser and nodejs. Using Commonjs and ES Modules.
10+
It is self contained (dependency free), relatively small (about 2k minified and gzipped)
11+
and fairly fast (about 20 nanoseconds for a sample of 2d noise).
512

613
## Demos
714

815
- Simple 2D plasma on [codepen.io](http://codepen.io/jwagner/pen/BNmpdm/?editors=001).
916
- [3D voxel world generation](http://29a.ch/sandbox/2012/voxelworld/) example.
1017
- Film grain in [analog film emulator](http://29a.ch/film-emulator/).
1118

19+
Created something awesome with simplex-noise? Let me know so I can add it to the list.
20+
1221
## Installation
1322

1423
```npm i -S simplex-noise```
1524

1625
## Usage
1726

27+
```javascript
28+
// when using es modules
29+
import SimplexNoise from 'simplex-noise';
30+
// when using commonjs
31+
const {SimplexNoise} = require('simplex-noise');
32+
```
33+
1834
By default simplex-noise.js will use Math.random() to seed the noise.
1935
```javascript
2036
// initializing a new simplex instance
@@ -61,12 +77,18 @@ const SimplexNoise = require('simplex-noise'),
6177

6278
## Benchmarks
6379

64-
- [Comparison between 2D and 3D noise](http://jsperf.com/simplex-noise/4)
65-
- [Comparison with simplex implementation in three.js](http://jsperf.com/simplex-noise-comparison/3)
80+
simplex-noise.js is reasonably quick.
81+
According to `perf/benchmark.js` I can perform about 50 million `noise2D()` calls/second on a single thread on my desktop (Ryzen 5950X).
82+
So ~20 nanoseconds per call.
6683

67-
For development you can open `perf/index.html` and watch the console or run `node perf/benchmark.js` in a shell.
68-
There is also a rake task for comparing your current changes can also run `make compare`.
69-
The command works using git stash.
84+
```
85+
$ node perf/index.js
86+
27745787.933336906
87+
init: 192,590 ops/sec ±1%
88+
noise2D: 57,928,891 ops/sec ±1%
89+
noise3D: 34,159,230 ops/sec ±0%
90+
noise4D: 24,589,786 ops/sec ±0%
91+
```
7092

7193
## Tests
7294

@@ -78,8 +100,13 @@ npm install && npm test
78100
## Changelog
79101

80102
### main
103+
- Changed module structure. When using bundlers that import the es module even using require() the import might need to be updated.
81104
- Dependency update
82105
- Setting sideEffects: false in package.json
106+
- Added snapshot tests
107+
- Code converted to typescript, the package can of course still be used from regular JS
108+
- Dropped bower
109+
- Added support for es modules
83110

84111
### 2.4.0
85112
- Included a PRNG based on ALEA to directly allow seeding
@@ -119,12 +146,12 @@ you will need to use a polyfill like [typedarray.js](http://www.calormen.com/pol
119146

120147

121148
## License
122-
Copyright (c) 2015 Jonas Wagner, licensed under the MIT License (enclosed)
149+
Copyright (c) 2021 Jonas Wagner, licensed under the MIT License (enclosed)
123150

124151
## Credits
125152
This is mostly a direct javascript port of the [Java implementation](http://webstaff.itn.liu.se/~stegu/simplexnoise/SimplexNoise.java)
126153
by Stefan Gustavson and Peter Eastman.
127154

128155
The integrated pseudo random generator is based on code by by Johannes Baagøe.
129156

130-
The typescript definition has been provided by [Neonit](https://github.com/Neonit).
157+
The initial typescript definition has been provided by [Neonit](https://github.com/Neonit).

bower.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
mkdir -p dist
3+
tsc --build tsconfig-commonjs.json
4+
tsc --build tsconfig-esm.json
5+
echo '{"type": "module"}' > dist/esm/package.json

commonjs-wrapper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import SimplexNoise from './simplex-noise.js';
2+
// dumb hack so there is a consistent way to import using commonjs
3+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4+
(SimplexNoise as any)['SimplexNoise'] = SimplexNoise;
5+
export = SimplexNoise;

index.d.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)