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.

115 lines (92 loc) 4.82 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> <b>Check the console to see this working</b> <button onclick="window.myFunction('CANCEL-PREVIOWS')">Launch! (Cancel previous)</button> <button onclick="window.myFunction('IF-PREVIOUS-FINISHED')">Launch if previous finished</button> <button onclick="window.myFunction('ADD-TO-QUEUE')">Add to queue.</button> <button onclick="window.myFunction('ADD-TO-QUEUE-IF-ABOUT-TO-FINISH')">Add to queue IF THE MOVEMENT IS ABOUT TO FINISH, 70% or 85%, Step 4... etc </button> <div><p id="thing" style="margin-left: 20px;">thing</p></div> <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> <script type="text/javascript" src="./demo-0.js"></script> </body> </html>