-
Notifications
You must be signed in to change notification settings - Fork 33
/
noise.js
44 lines (34 loc) · 1.09 KB
/
noise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
function makeNoise(width, height){
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
var imgData = ctx.getImageData(0, 0, width, height),
data = imgData.data,
pixels = data.length;
for(var i = 0; i < pixels; i+=4){
data[i] = Math.random()*255;
data[i+1] = Math.random()*255;
data[i+2] = Math.random()*255;
// data[i+1] = data[i];
// data[i+2] = data[i];
data[i+3] = 255;
}
ctx.putImageData(imgData, 0, 0);
return canvas;
}
function makeOctaveNoise(width, height, octaves){
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d');
canvas.width = width;
canvas.height = height;
ctx.fillStyle = 'black';
ctx.fillRect(0,0,width,height);
ctx.globalAlpha = 1/octaves;
ctx.globalCompositeOperation = 'lighter';
for(var i = 0; i < octaves; i++){
var octave = makeNoise(width>>i, height>>i);
ctx.drawImage(octave, 0, 0, width, height);
}
return canvas;
}