Created
January 24, 2017 11:42
-
-
Save queckezz/c97991ce6ab198afebf8bc9bfeb54e0e to your computer and use it in GitHub Desktop.
simplex noise demo, sampling values from black to white
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const FastSimplexNoise = require('fast-simplex-noise').default | |
const fill2d = | |
(rows, cols, fn) => Array(rows) | |
.fill(true) | |
.map((_, x) => { | |
return Array(cols) | |
.fill(true) | |
.map((_, y) => { | |
return fn(x, y) | |
}) | |
}) | |
const main = | |
(ctx, width, height) => { | |
const gen = new FastSimplexNoise({ | |
frequency: 0.01, | |
octaves: 10 | |
}) | |
const grid = fill2d( | |
width, | |
height, | |
(x, y) => gen.scaled([x, y]) | |
) | |
for (let x = 0; x < width; x++) { | |
for (let y = 0; y < height; y++) { | |
ctx.fillStyle = `rgba(0, 0, 0, ${grid[x][y]})` | |
ctx.fillRect(x, y, 1, 1) | |
} | |
} | |
} | |
const canvas = document.getElementById('canvas') | |
const ctx = canvas.getContext('2d') | |
main(ctx, canvas.width, canvas.height) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment