xxxxxxxxxx
34
// Sin wave length height
//
// Draws three sin waves with varying
// amplitudes and wavelengths.
// Adapted from Processing handbook.
function setup() {
createCanvas(400, 100);
background(220);
noStroke();
fill(0);
// We can substitute variables for the fixed numbers in
// the previous example to change the shape of the wave.
drawWave(PI / 10, 35);
fill(100);
drawWave(PI / 20, 25);
fill(255);
drawWave(PI / 40, 10);
}
// Draws a wave across the screen
function drawWave(angleInc, scaleVal){
let angle = 0;
for (let x = 0; x <= width; x += 5) {
let y = 50 + (sin(angle) * scaleVal);
rect(x, y, 2, 4);
angle += angleInc;
}
}