简体   繁体   中英

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.

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM