Skip to content

Commit

Permalink
Merge pull request #38 from jwagner/typescript
Browse files Browse the repository at this point in the history
Convert simplex-noise.js to typescript
  • Loading branch information
jwagner committed Aug 22, 2021
2 parents 634a758 + d9fa53d commit e2c770d
Show file tree
Hide file tree
Showing 29 changed files with 21,969 additions and 4,214 deletions.
17 changes: 14 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
module.exports = {
'env': {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
env: {
'browser': true,
'node': true,
'mocha': true,
'es2017': true
},
'extends': 'eslint:recommended',
'extends': ['eslint:recommended','plugin:@typescript-eslint/recommended'],
'globals': {
'Uint8Array': false,
'define': false,
'Float32Array': false
},
'parserOptions': {
'sourceType': 'module'
},
'rules': {
'indent': [
'error',
Expand All @@ -28,5 +36,8 @@ module.exports = {
'error',
'always'
]
}
},
'ignorePatterns': [
'/dist'
]
};
3 changes: 2 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
- run: npm test
- run: npm run-script build
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
node_modules
/node_modules
/.parcel-cache
/dist
/e2e
/public
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": [
"ts"
],
"spec": "test/simplex-noise-test.ts",
"require": "ts-node/register"
}
45 changes: 36 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
# simplex-noise.js
[![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)

simplex-noise.js is a fast simplex noise implementation in Javascript. It works in the browser and on nodejs.
[API Documentation](https://29a.ch/simplex-noise/docs/classes/SimplexNoise.html)

[![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/)


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

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

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

## Installation

```npm i -S simplex-noise```

## Usage

```javascript
// 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.
```javascript
// initializing a new simplex instance
Expand Down Expand Up @@ -61,12 +77,18 @@ const SimplexNoise = require('simplex-noise'),

## Benchmarks

- [Comparison between 2D and 3D noise](http://jsperf.com/simplex-noise/4)
- [Comparison with simplex implementation in three.js](http://jsperf.com/simplex-noise-comparison/3)
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.

For development you can open `perf/index.html` and watch the console or run `node perf/benchmark.js` in a shell.
There is also a rake task for comparing your current changes can also run `make compare`.
The command works using git stash.
```
$ 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%
```

## Tests

Expand All @@ -78,8 +100,13 @@ npm install && npm test
## Changelog

### main
- 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
Expand Down Expand Up @@ -119,12 +146,12 @@ you will need to use a polyfill like [typedarray.js](http://www.calormen.com/pol


## License
Copyright (c) 2015 Jonas Wagner, licensed under the MIT License (enclosed)
Copyright (c) 2021 Jonas Wagner, licensed under the MIT License (enclosed)

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

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

The typescript definition has been provided by [Neonit](https://github.com/Neonit).
The initial typescript definition has been provided by [Neonit](https://github.com/Neonit).
34 changes: 0 additions & 34 deletions bower.json

This file was deleted.

5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
mkdir -p dist
tsc --build tsconfig-commonjs.json
tsc --build tsconfig-esm.json
echo '{"type": "module"}' > dist/esm/package.json
5 changes: 5 additions & 0 deletions commonjs-wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import SimplexNoise from './simplex-noise.js';
// dumb hack so there is a consistent way to import using commonjs
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(SimplexNoise as any)['SimplexNoise'] = SimplexNoise;
export = SimplexNoise;
49 changes: 0 additions & 49 deletions index.d.ts

This file was deleted.

Loading

0 comments on commit e2c770d

Please sign in to comment.