Make It Move
Discover animation! Use variables to make shapes move, bounce, and follow patterns.
Animation is all about changing things over time. Declare a variable outside of setup() and draw(), then change it inside draw(). Since draw() runs 60 times per second, the variable changes smoothly — creating motion!
Try it!Change the 2 to a bigger number like 5 or 10. What happens to the speed?
The circle flies off the right edge! Let's make it wrap back to the left side when it goes past the edge. We check if x is bigger than the canvas width, and if so, reset it to 0.
Wrapping is cool, but bouncing is even cooler. Use a speed variable that can be positive (moving right) or negative (moving left). When the ball hits an edge, flip the speed!
Try it!Try changing the starting speed to 7. What happens when you change the circle size?
Let's make the ball bounce in both directions — horizontal and vertical! We need separate x/y positions and x/y speeds.
The built-in frameCount variable counts up every frame. You can use it with math functions like Math.sin() to create smooth, looping animations without any if statements!
What to try next
- Make the ball change color each time it bounces
- Add gravity: increase speedY a little bit each frame, and make it bounce off the floor
- Animate multiple balls at once using arrays
- Make a shape that moves in a circle using Math.sin() and Math.cos()