Skip to content
Advertisement

How can I perform a reset on the loop I have when pressing a button?

I’m creating a code where 3 balls will bounce when a button is clicked. I have a code I found in the internet and added the start button but clicking it multiple times speed up the balls. Also, I tried adding a reset button that will clear out the canvas but can’t make it work. Sharing both the HTML and JS code.

JavaScript
JavaScript

Advertisement

Answer

You need to store the requestAnimationFrame in a var and use cancelAnimationFrame to stop

I added a div to hold the buttons and now delegate from that div so only one event handler is needed.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement