UNPKG

animation-stepper

Version:

Javascript library to handle several animations at once using a singleton approach. One window.requestAnimationFrame instance will be running in the background to orchest all animations.

30 lines (28 loc) 850 B
HTML <!DOCTYPE html> <title>Script-based animation using requestAnimationFrame</title> <style> div { position: absolute; left: 10px; padding: 50px; background: crimson; color: white } </style> <script> var requestId = 0; function animate(time) { document.getElementById("animated").style.left = (time - animationStartTime) % 2000 / 4 + "px"; requestId = window.requestAnimationFrame(animate); } function start() { animationStartTime = window.performance.now(); requestId = window.requestAnimationFrame(animate); } function stop() { if (requestId) window.cancelAnimationFrame(requestId); requestId = 0; } </script> <button onclick="start()">Click me to start!</button> <button onclick="stop()">Click me to stop!</button> <div id="animated">Hello there.</div> https://www.w3.org/TR/animation-timing/