ds-heightmap
Advanced tools
Comparing version 0.2.3 to 1.0.0
{ | ||
"name": "ds-heightmap", | ||
"version": "0.2.3", | ||
"version": "1.0.0", | ||
"description": "Use diamond-square algorithm to generate heightmaps.", | ||
"main": "index.js", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"exports": { | ||
"import": "./index.esm.mjs", | ||
"require": "./lib/index.js" | ||
}, | ||
"files": [ | ||
"lib", | ||
"index.esm.mjs" | ||
], | ||
"scripts": { | ||
"start": "node index.js", | ||
"lint": "eslint *.js --fix", | ||
"jest": "jest", | ||
"test": "npm run lint && npm run jest", | ||
"umd": "NODE_ENV=development browserify index.js -t envify -s DsHeightmap -o ds-heightmap.js", | ||
"umd-min": "NODE_ENV=production browserify index.js -t envify -t babelify --presets [ env react ] -s DsHeightmap -t uglifyify -o | uglifyjs --compress unused,dead_code -o ds-heightmap.min.js", | ||
"build": "npm run umd && npm run umd-min", | ||
"prepublish": "npm run build" | ||
"build": "tsc", | ||
"build:watch": "tsc --watch", | ||
"lint": "tsc --noEmit --noUnusedLocals true --noUnusedParameters true", | ||
"prepublish": "npm run build", | ||
"test": "npm run lint" | ||
}, | ||
@@ -31,21 +37,4 @@ "repository": { | ||
"devDependencies": { | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-react": "^6.24.1", | ||
"babelify": "^8.0.0", | ||
"browserify": "^16.1.1", | ||
"envify": "^4.1.0", | ||
"eslint": "^4.19.1", | ||
"eslint-config-standard": "^11.0.0", | ||
"eslint-plugin-import": "^2.10.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"eslint-plugin-promise": "^3.7.0", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"jest": "^22.4.3", | ||
"pre-commit": "^1.2.2", | ||
"uglify-js": "^3.3.18", | ||
"uglifyify": "^4.0.5" | ||
}, | ||
"jest": { | ||
"testURL": "http://localhost" | ||
"typescript": "^4.6.3" | ||
} | ||
} |
121
README.md
@@ -21,87 +21,58 @@ # ds-heightmap | ||
```javascript | ||
const ds = require('ds-heightmap'); | ||
ds.init(12, { | ||
corner: [1, 1, 5, 5], // determine the heights of four corners | ||
offset: -0.5, // effect the overall height of the map | ||
range: 9, // all the height values in the map will be within -range to range | ||
rough: 0.8 // effect the terrain variability (roughness) | ||
import { ds } from 'ds-heightmap'; | ||
const data = ds({ | ||
width: 129, // the width of the map, must be larger than 1. | ||
height: 129, // the height of the map, must be larger than 1. | ||
depth: 2000, // the value of each pixel will be within 0~depth, default: 2000. | ||
rough: 1, // effect the terrain variability (roughness), default: 1. | ||
}); // pass factors | ||
ds.run(); // generate a new heightmap base on the factors above | ||
const data = ds.out(); // return a 2D-array of numbers | ||
// Or call ds.gen() to do ds.run() and ds.out() together. | ||
console.log(data.data); // you would get a 2D-array of numbers | ||
console.log(data.max); // the maximum number in all pixels | ||
console.log(data.min); // the minimum number in all pixels | ||
``` | ||
Or in another way: | ||
### Render the map | ||
```javascript | ||
const ds = require('ds-heightmap').ds; | ||
Here is an example of how to render the data on canvas: | ||
const data = ds(12, { | ||
corner: [1, 1, 5, 5], | ||
offset: -0.5, | ||
range: 9, | ||
rough: 0.8 | ||
}); | ||
``` | ||
Using ES6: | ||
```javascript | ||
import heightmap, { ds } from 'ds-heightmap'; | ||
import { ds } from 'ds-heightmap' | ||
heightmap.init(9); | ||
// or | ||
const data = ds(7); | ||
``` | ||
const width = 400 | ||
const height = 300 | ||
const { data, max, min } = ds({ | ||
width, | ||
height | ||
}) | ||
Or in **Html**: | ||
```html | ||
<script src="/path/to/ds-heightmap.min.js"></script> | ||
``` | ||
const range = max - min | ||
const colorData = [] | ||
for (let i = 0; i < height; i++) { | ||
for (let j = 0; j < width; j++) { | ||
const level = (data[j][i] - min) / range | ||
if (level > 0.8) { | ||
colorData.push(186, 174, 154, 255) | ||
} else if (level > 0.6) { | ||
colorData.push(222, 214, 163, 255) | ||
} else if (level > 0.4) { | ||
colorData.push(209, 215, 171, 255) | ||
} else if (level > 0.2) { | ||
colorData.push(189, 204, 150, 255) | ||
} else { | ||
colorData.push(148, 191, 139, 255) | ||
} | ||
} | ||
} | ||
### Render the map | ||
const imageData = new ImageData( | ||
Uint8ClampedArray.from(colorData), | ||
width, | ||
height, | ||
); | ||
Once you get the map data, you can render it into an image using an external image processing library. | ||
Here is an example with [jimp](https://github.com/oliver-moran/jimp): | ||
```javascript | ||
const Jimp = require('jimp'); | ||
new Jimp(size, size, (err, image) => { | ||
if (err) throw err; | ||
data.forEach((d, x) => { | ||
d.forEach((v, y) => { | ||
image.setPixelColor(convertValueToColor(v), x, y); | ||
}); | ||
}); | ||
image.write('map.png', (err) => { | ||
if (err) throw err; | ||
}); | ||
}); | ||
const canvas = document.getElementById('canvas') | ||
canvas.width = width | ||
canvas.height = height | ||
const ctx = canvas.getContext('2d') | ||
ctx.putImageData(imageData, 0, 0) | ||
``` | ||
## API | ||
### init (power, option = {}) | ||
Init the library. Where `power` effects the size of the map (If `power` equals `n`, a map of 2<sup>n</sup> * 2<sup>n</sup> will be produced). For `option`, see [below](#options). | ||
### run () | ||
Manually call this function to do the diamond-square algorithm. | ||
### out () => `array` | ||
Return the map data. | ||
### ds (power, option = {}) => `array` | ||
Run `init`, `run`, `out` all together. | ||
### gen () => `array` | ||
Run `run`, `out` all together. | ||
### options | ||
| Option | Description | Type | Default | | ||
| --- | --- | --- | --- | | ||
| corner | Determine the heights of four corners. They are initial values in diamond-square algorithm. Can be an array of four numbers or only one number which means all corners have the same height. | Array, Number | [1, 1, 1, 1] | | ||
| offset | Designed to effect the overall height of the map. Ranged from `-0.9` to `0.9`. | Number | -0.2 | | ||
| range | All the height values in the map will be within `-range` to `range`. The min value is `1`. | Number | 7 | | ||
| rough | Designed to effect the terrain variability (roughness). Ranged from `0.1` to `0.9`. | Number | 0.8 | |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
0
0
15200
7
164
78
1