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.

122 lines (96 loc) 4.7 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Script-based animation using requestAnimationFrame</title> <style> body { background-color: black; color: rgb(77, 77, 77); } div { position: absolute; left: 10px; padding: 50px; background: rgb(31, 31, 31); color: rgb(77, 77, 77); top: 100px; } </style> <script> /* Opera engineer Erik Möller wrote about rAF and developed a polyfill that better handles browsers without native support. You can read about it, but basically his code will choose a delay of between 4ms and 16ms in order to more closely match 60fps. Here it is, in case you’d like to use it. Note it uses the standard method name. I have also fixed the cancel* method’s name, as it has changed in WebKit. */ /* (function() { var lastTime = 0; var vendors = ['webkit', 'moz']; for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); */ (function() { var lastTime = 0, vendors = ['ms', 'moz', 'webkit', 'o'], x, length, currTime, timeToCall; for(x = 0, length = vendors.length; x < length && !window.requestAnimationFrame; ++x) { window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; } if (!window.requestAnimationFrame) window.requestAnimationFrame = function(callback, element) { currTime = new Date().getTime(); timeToCall = Math.max(0, 16 - (currTime - lastTime)); lastTime = currTime + timeToCall; return window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); }; if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function(id) { clearTimeout(id); }; }()); // requestAnimationFrame polyfill ends here. </script> </head> <body> <!-- <div id="root"></div> --> <div id="animated">Hello there.</div> <b>Check the console and reload to see this example working</b> <br> <!-- <button onclick="start()">Click me to start!</button> <button onclick="stop()">Click me to stop!</button> --> <br> https://www.w3.org/TR/animation-timing/ <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script type="text/javascript" src="../../src/animation-stepper.js"></script> <script type="text/javascript" src="./demo-1.js"></script> </body> </html>