forked from processing-js/processing-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoisewave.html
More file actions
125 lines (101 loc) · 3.11 KB
/
noisewave.html
File metadata and controls
125 lines (101 loc) · 3.11 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<!DOCTYPE html>
<html>
<head>
<script src="../../processing.js"></script>
<link rel="stylesheet" href="../style.css"/></head>
<body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
<h2>NoiseWave</h2>
<p>by Daniel Shiffman.
Using Perlin Noise to generate a wave-like pattern.</p>
<p><a href="http://processing.org/learning/basics/noisewave.html"><b>Original Processing.org Example:</b> NoiseWave</a><br>
<script type="application/processing">
int xspacing = 8; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float yoff = 0.0f; // 2nd dimension of perlin noise
float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
void setup() {
size(200,200);
frameRate(30);
colorMode(RGB,255,255,255,100);
smooth();
w = width+16;
yvalues = new float[w/xspacing];
}
void draw() {
background(0);
calcWave();
renderWave();
}
void calcWave() {
float dx = 0.05f;
float dy = 0.01f;
float amplitude = 100.0f;
// Increment y ('time')
yoff += dy;
//float xoff = 0.0; // Option #1
float xoff = yoff; // Option #2
for (int i = 0; i < yvalues.length; i++) {
// Using 2D noise function
//yvalues[i] = (2*noise(xoff,yoff)-1)*amplitude; // Option #1
// Using 1D noise function
yvalues[i] = (2*noise(xoff)-1)*amplitude; // Option #2
xoff+=dx;
}
}
void renderWave() {
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
noStroke();
fill(255,50);
ellipseMode(CENTER);
ellipse(x*xspacing,width/2+yvalues[x],16,16);
}
}
</script><canvas width="200" height="200"></canvas></p>
<div style="height:0px;width:0px;overflow:hidden;"></div>
<pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
// unless otherwise stated.</b>
int xspacing = 8; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float yoff = 0.0f; // 2nd dimension of perlin noise
float[] yvalues; // Using an array to store height values for the wave (not entirely necessary)
void setup() {
size(200,200);
frameRate(30);
colorMode(RGB,255,255,255,100);
smooth();
w = width+16;
yvalues = new float[w/xspacing];
}
void draw() {
background(0);
calcWave();
renderWave();
}
void calcWave() {
float dx = 0.05f;
float dy = 0.01f;
float amplitude = 100.0f;
// Increment y ('time')
yoff += dy;
//float xoff = 0.0; // Option #1
float xoff = yoff; // Option #2
for (int i = 0; i < yvalues.length; i++) {
// Using 2D noise function
//yvalues[i] = (2*noise(xoff,yoff)-1)*amplitude; // Option #1
// Using 1D noise function
yvalues[i] = (2*noise(xoff)-1)*amplitude; // Option #2
xoff+=dx;
}
}
void renderWave() {
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
noStroke();
fill(255,50);
ellipseMode(CENTER);
ellipse(x*xspacing,width/2+yvalues[x],16,16);
}
}</pre>
</body>
</html>