The noise() function is used to return a number generated by Perlin noise at the given coordinates. This value is semi-random, which means that the value would be fixed for a coordinate for the lifespan of the program.
The Perlin noise value is different from the value returned by the random() function as this noise has a more natural and harmonic succession compared to the standard one.
Syntax:
noise(x, [y], [z])
Parameter: This function accept three parameters as mentioned above and described below:
- x: This is a number which represents the x-coordinate in the noise space.
- y: This is a number which represents the y-coordinate in the noise space. It is an optional parameter.
- z: This is a number which represents the z-coordinate in the noise space. It is an optional parameter.
Return Value: It returns the Perlin noise value between 0 and 1.
Below examples illustrates the noise() function in p5.js:
Example 1: Plotting the noise values of the y-coordinate of the moving point.
- Program:
let x_coordinate = 100.0;
let plot_x = 10.0;
 Â
Âfunction
setup() {
   Â
createCanvas(400, 200);
}
 Â
Âfunction
draw() {
 Â
ÂÂ Â Â Â
// Get noise with x coordinate
   Â
x_noise = noise(x_coordinate) * 100;
 Â
ÂÂ Â Â Â
// Plot the point with random noise
   Â
strokeWeight(10);
   Â
point(plot_x, x_noise);
 Â
ÂÂ Â Â Â
// Increment the x coordinate
   Â
x_coordinate++;
 Â
ÂÂ Â Â Â
// Increase the x coordinate
   Â
// for plotting
   Â
plot_x++;
}
- Output:
Example 2: This example demonstrates the semi-random property of a function.
- Program:
let x_coordinate = 0.0;
let plot_y = 0.0;
 Â
Âfunction
setup() {
   Â
createCanvas(400, 200);
}
 Â
Âfunction
draw() {
    Â
ÂÂ Â Â Â
if
(x_coordinate < 5) {
 Â
ÂÂ Â Â Â Â Â Â Â
// Get noise with x coordinate
       Â
x_noise = noise(x_coordinate);
   Â
ÂÂ Â Â Â Â Â Â Â
// Output the noise along with
       Â
// its corresponding coordinate
       Â
coord_text =
"Noise for x coordinate "
           Â
+ x_coordinate +
" is "
+ x_noise;
        Â
ÂÂ Â Â Â Â Â Â Â
text(coord_text, 10, plot_y);
 Â
ÂÂ Â Â Â Â Â Â Â
// Increment the x coordinate
       Â
x_coordinate++;
 Â
ÂÂ Â Â Â Â Â Â Â
// Increase the y coordinate
       Â
// for plotting
       Â
plot_y = plot_y + 15;
   Â
}
   Â
else
       Â
x_coordinate = 0;
}
- Output:
Example 3: This example uses two parameters for defining a point in the noise space.
- Program:
let x_coordinate = 0.0;
let y_coordinate = 0.0;
let plot_y = 0.0;
 Â
Âfunction
setup() {
   Â
createCanvas(400, 200);
}
 Â
Âfunction
draw() {
    Â
ÂÂ Â Â Â
if
(x_coordinate < 10) {
 Â
ÂÂ Â Â Â Â Â Â Â
// Get noise with x and y coordinates
       Â
xy_noise = noise(x_coordinate, y_coordinate);
 Â
ÂÂ Â Â Â Â Â Â Â
// Output the noise along with
       Â
// its corresponding coordinate
       Â
coord_text =
"Noise for coordinates: "
           Â
+ x_coordinate +
", "
+ y_coordinate
           Â
+
" is "
+ xy_noise;
       Â
text(coord_text, 10, plot_y);
 Â
ÂÂ Â Â Â Â Â Â Â
// Increment the x and y
       Â
// coordinates
       Â
x_coordinate++;
       Â
y_coordinate++;
 Â
ÂÂ Â Â Â Â Â Â Â
// Increase the y coordinate
       Â
// for plotting
       Â
plot_y = plot_y + 15;
   Â
}
}
- Output:
Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
Reference: https://p5js.org/reference/#/p5/noise