xxxxxxxxxx
52
let xspacing = 100; // Distance between each horizontal location
let w; // Width of entire wave
let theta = 0.0; // Start angle at 0
let amplitude = 75.0; // Height of wave
let period = 500.0; // How many pixels before the wave repeats
let dx; // Value for incrementing x
let yvalues; // Using an array to store height values for the wave
let offset = 25;
function setup() {
createCanvas(710, 400);
w = width + 16;
dx = (TWO_PI / period) * xspacing;
yvalues = new Array(floor(w / xspacing));
}
function draw() {
background(0);
calcWave();
renderWave();
}
function calcWave() {
// Increment theta (try different values for
// 'angular velocity' here)
theta += 0.04;
// For every x value, calculate a y value with sine function
let x = theta;
console.log(yvalues.length)
for (let i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x) * amplitude;
x += dx;
}
}
function renderWave() {
// A simple way to draw the wave with an ellipse at each location
stroke(237, 34, 93);
fill(255, 0,0,100)
beginShape();
for (let x = 0; x < yvalues.length; x++) {
vertex(x * xspacing + offset, height / 2 + yvalues[x] + offset);
}
for (let x = yvalues.length; x > 0; x--) {
vertex(x * xspacing - offset + offset, height / 2 + yvalues[x] - offset);
}
endShape();
}