xxxxxxxxxx
50
var waves = [];
var amplitude = 0; //? how to release key and change amplitude to 0;
function setup() {
createCanvas(800, 800);
for (i = 0; i < 8; i++) {
waves[i] = new Wave((i + 1) * 0.05, amplitude, (2+1.2*i)*40, 100 + i * height / 10);
}
}
function draw() {
background(0);
console.log(waves)
for (i = 0; i < 8; i++) {
waves[i].renderWave();
}
//noLoop();
}
function Wave(tempTheta, tempAmp, tempPeriod, tempAxis) { //tempTheta, tempAmp, tempPeriod in constructor or in function
this.xspacing = 3;
this.x = 0.02;
this.w = width + 16;
this.theta = tempTheta;
this.amp = tempAmp;
this.period = tempPeriod;
this.axis = tempAxis; //y value of wave axis
this.dx = (TWO_PI / this.period) * this.xspacing; // ?tempPeriod or this.period
this.y = new Array(floor(this.w / this.xspacing));
this.renderWave = function() {
this.theta += tempTheta;
this.x = this.theta;
// console.log(this.x);
for (var i = 0; i < this.y.length; i++) {
this.y[i] = sin(this.x) * this.amp;
this.x += this.dx;
}
noStroke();
fill(255);
// A simple way to draw the wave with an ellipse at each location
for (var j = 0; j < this.y.length; j++) {
ellipse(j * this.xspacing, tempAxis + this.y[j], 5, 5);
}
}
}