xxxxxxxxxx
77
// Example based on the ones in The Nature of Code by Daniel Shiffman, with a few tweaks.
// http://natureofcode.com
let drawingMode = "gaussian";
let modeSelector;
function setup() {
createCanvas(360, 200);
modeSelector = createSelect();
modeSelector.position(10, 10);
modeSelector.option("Random", "random");
modeSelector.option("Gaussian", "gaussian");
modeSelector.option("Perlin Noise", "perlin");
modeSelector.selected(drawingMode);
modeSelector.changed(onModeChange);
}
function draw() {
background(255);
switch (drawingMode) {
case "random":
drawRandom();
break;
case "gaussian":
drawGaussian();
break;
case "perlin":
drawPerlinNoise();
break;
}
}
function drawRandom() {
noFill();
stroke(0);
strokeWeight(2);
beginShape();
for (let i = 0; i < width; i++) {
let y = random(height);
vertex(i, y);
}
endShape();
}
function drawGaussian() {
noFill();
stroke(0);
strokeWeight(2);
beginShape();
for (let i = 0; i < width; i++) {
let y = randomGaussian(height / 2, height/8);
vertex(i, y);
}
endShape();
}
function drawPerlinNoise() {
let xoff = frameCount * 0.01;
noFill();
stroke(0);
strokeWeight(2);
beginShape();
for (let i = 0; i < width; i++) {
let y = noise(xoff) * height;
xoff += 0.01;
vertex(i, y);
}
endShape();
}
function onModeChange() {
drawingMode = modeSelector.value();
}