How i can make onclick to start animation

I got requestAnimationFrame(loop); and I'm trying to make onclick button to start animation, once you press it animation starts, I got button figured out <button type="start" form="form1" value="start onclick="start()">start</button> But i cannot figure out how to make it into a script.

2 answers

  • answered 2022-05-04 10:34 Fabien Aur&#233;jac

    Here is a snippet example :

    <button onclick="start()">start</button>
    <script>
       var animId=false;
       function start() {
          animId=requestAnimationFrame(loop);
       }
       function loop() {
          //your code here
       }
       // I added this in case you wanted to call a stop
       function stop() {
          animId && cancelAnimationFrame(animId);
       }
    </script>
    

  • answered 2022-05-04 10:43 Anas Abdullah Al

    If you want to call requestAnimationFrame(loop) you should do it like this...

    <button type="start" form="form1" value="start" onclick="requestAnimationFrame(loop)">start</button>
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum