Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Bouncing Ball Animation | Animation and Mini Projects
JavaScript Canvas Drawing and Animation

bookBouncing Ball Animation

Animating a bouncing ball on the canvas is a classic project that combines your knowledge of drawing, animation loops, and simple physics. In this project, you will create a ball that moves under the influence of gravity and bounces off the edges of the canvas. The animation should appear smooth, with the ball accelerating downwards and reversing its direction when it hits the canvas boundaries. The core requirements are to draw a ball, animate its motion, apply gravity, and handle wall collisions so the ball bounces realistically.

index.html

index.html

copy

To understand how the bouncing ball animation works, start with the animation loop. The animate function uses requestAnimationFrame to repeatedly clear the canvas, draw the ball, and update its position. The ball's position is updated each frame by adding its velocity values (vx and vy) to its x and y coordinates. Gravity is simulated by increasing the vertical velocity (vy) every frame, making the ball accelerate downwards.

When the ball reaches the bottom of the canvas, a collision is detected if the ball's bottom edge passes the canvas height. The vertical velocity is reversed and reduced by multiplying it by the bounce factor, making the ball bounce back up but not as high as before. Similar checks handle collisions with the top, left, and right edges, reversing the appropriate velocity and ensuring the ball stays within the visible area. This creates the effect of a ball bouncing around the canvas, influenced by gravity and losing energy with each bounce.

1. Which property is typically updated each frame to move the ball in the bouncing ball animation?

2. What happens when the ball collides with the edge of the canvas in the bouncing ball animation?

question mark

Which property is typically updated each frame to move the ball in the bouncing ball animation?

Select the correct answer

question mark

What happens when the ball collides with the edge of the canvas in the bouncing ball animation?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain how to implement the animation loop in code?

How does the bounce factor affect the ball's motion?

What happens if the ball hits the side walls instead of the bottom?

Awesome!

Completion rate improved to 5.88

bookBouncing Ball Animation

Desliza para mostrar el menú

Animating a bouncing ball on the canvas is a classic project that combines your knowledge of drawing, animation loops, and simple physics. In this project, you will create a ball that moves under the influence of gravity and bounces off the edges of the canvas. The animation should appear smooth, with the ball accelerating downwards and reversing its direction when it hits the canvas boundaries. The core requirements are to draw a ball, animate its motion, apply gravity, and handle wall collisions so the ball bounces realistically.

index.html

index.html

copy

To understand how the bouncing ball animation works, start with the animation loop. The animate function uses requestAnimationFrame to repeatedly clear the canvas, draw the ball, and update its position. The ball's position is updated each frame by adding its velocity values (vx and vy) to its x and y coordinates. Gravity is simulated by increasing the vertical velocity (vy) every frame, making the ball accelerate downwards.

When the ball reaches the bottom of the canvas, a collision is detected if the ball's bottom edge passes the canvas height. The vertical velocity is reversed and reduced by multiplying it by the bounce factor, making the ball bounce back up but not as high as before. Similar checks handle collisions with the top, left, and right edges, reversing the appropriate velocity and ensuring the ball stays within the visible area. This creates the effect of a ball bouncing around the canvas, influenced by gravity and losing energy with each bounce.

1. Which property is typically updated each frame to move the ball in the bouncing ball animation?

2. What happens when the ball collides with the edge of the canvas in the bouncing ball animation?

question mark

Which property is typically updated each frame to move the ball in the bouncing ball animation?

Select the correct answer

question mark

What happens when the ball collides with the edge of the canvas in the bouncing ball animation?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 3
some-alt