xxxxxxxxxx
25
// Create an array of y-positions
let ys = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// Push a new mouseY position into array every frame
ys.push(mouseY);
// Calculate average Y position over 150 frames
let avgY = 0;
for(let y of ys) {
avgY += y;
}
avgY /= ys.length;
// Get rid of oldest position once we pass 150
if(ys.length > 150) ys.shift();
// Draw a ball at average Y
ellipse(width/2, avgY, 50, 50);
}