I have a sketch in P5.js that is close to working as intended, except that I can't figure out how to control the period or wavelength. As you'll see if you run the code, the animation creates long sweeping waves, but I'd like to be able to set the wavelength based on a parameter. This is just a coding exercise - any help would be appreciated!
Note, the code isn't running directly on S.O. but can be run here, https://editor.p5js.org/knectar/sketches/ONxJGvmJH
var arr = []; //array that holds rectangles
var rectNum; //number of rectangles drawn
var xPos; // horizontal position of rectangle
var yPos; // vertical position of rectangle
var w = 5; // rectangle width
var h; // rectangle height
var rPadding = 4; //space between bars
var amplitude = 40;
var frequency = 2;
function setup() {
createCanvas(1000, 400);
angleMode(DEGREES); //slows the animation down (maybe an issue?)
rectNum = floor(width/(w+rPadding)); //limit array size to width of canvas
yPos = height*.6; // positions objects in vertical center
for (var i = 0; i < rectNum+1; i++){
var bar = new Bar(i*(w+rPadding), yPos, w, -h); //builds out the objects
arr.push(bar); //loads objects into array
}
}
function draw() {
background(1);
for (var i = 0; i < arr.length; i++){
arr[i].make();
arr[i].move(i);
}
}
function Bar(xPos, yPos, w, h){
this.xPos = xPos;
this.yPos = yPos;
this.width = w;
this.height = h;
this.move = function(i){
this.height = sin(frameCount*frequency+i)*amplitude+(amplitude*2); // sine function to control rectange height
}
this.make = function(){
strokeWeight(0);
stroke(51);
fill(160, 0, 124);
rect(this.xPos, this.yPos, this.width, -this.height); // negative height sets wave to point up
}
}