Animation
Animation is just changing images really fast. To draw a circle at a different location for every frame you can use the following code:
let xPosition = 0;
let xVelocity = 1;
function setup(){
size(400, 400);
}
function draw(){
background(220);
circle(xPosition, height/2, 100);
xPosition += xVelocity; // move the circle to the right
}
By changing the velocity in x and y direction you can make the circle move diagonally:
let xPosition;
let yPosition;
let xVelocity = 2;
let yVelocity = 3;
function setup(){
size(800, 600);
xPosition = width/2;
yPosition = height/2;
}
function draw(){
circle(xPosition, yPosition, 20);
xPosition += xVelocity;
yPosition += yVelocity;
// bounce
if (xPosition > width || xPosition < 0) xVelocity = -xVelocity;
if (yPosition > height || yPosition < 0) yVelocity = -yVelocity;
}
Bounce a ball across the canvas |