xxxxxxxxxx
28
function setup() {
createCanvas(400, 400);
}
let rect_x = 0; // rectangle position
let rect_speed = 1; // rectangle speed
function draw() {
background(220);
console.log(rect_speed);
fill(255, 0, 0)
rect(rect_x, 100, 30, 30);
rect_x = rect_x + rect_speed;
//rect_x = rect_x + 1
if (rect_x > 400) { // if rect_x is greater than 400
rect_speed = rect_speed * -1; // multiply the speed by -1 (reverse direction)
}
/* resetting the animation
if (rect_x > 400) { // if rect_x is greater than 400
rect_x = 0; // reset the loop
}
*/
}