Skip to content

Instantly share code, notes, and snippets.

@doxas
Last active July 11, 2018 02:48
Show Gist options
  • Save doxas/08112329b8a5f9a8779cb542952c11e2 to your computer and use it in GitHub Desktop.
Save doxas/08112329b8a5f9a8779cb542952c11e2 to your computer and use it in GitHub Desktop.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>noisy</title>
<script src="https://wgld.org/j/noiseX.js" type="text/javascript"></script>
<script type="text/javascript">
let octave = 6;
let offset = 4;
let persistence = 0.5;
let baseWidth = Math.pow(2, octave + offset);
let n = new noiseX(octave, offset, persistence);
n.setSeed(Date.now());
window.addEventListener('load', () => {
let i, j;
let canvas;
let noiseColor = [];
for(i = 0; i < 4; ++i){
noiseColor[i] = genNoise(Date.now());
}
canvas = canvasExport(noiseColor, baseWidth);
document.body.appendChild(canvas);
}, false);
function genNoise(seed){
n.setSeed(seed);
let i, j;
let noiseColor = [];
for(i = 0; i < baseWidth; ++i){
for(j = 0; j < baseWidth; ++j){
noiseColor[i * baseWidth + j] = n.snoise(i, j, baseWidth);
}
}
return noiseColor;
}
function canvasExport(data, width){
let cvs = document.createElement('canvas');
cvs.width = cvs.height = width;
let ctx = cvs.getContext('2d');
let cci = ctx.createImageData(width, width);
for(let i = 0, j = width * width; i < j; ++i){
cci.data[i * 4] = data[0][i] * 255;
cci.data[i * 4 + 1] = data[1][i] * 255;
cci.data[i * 4 + 2] = data[2][i] * 255;
cci.data[i * 4 + 3] = data[3][i] * 255;
}
ctx.putImageData(cci, 0, 0);
return cvs;
};
</script>
<style type="text/css">
canvas {
margin: 5px;
}
</style>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment