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.
145 lines (116 loc) • 5.82 kB
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);
background-color: black;
color: rgb(77, 77, 77);
font-family: sans-serif;
font-size: 12px;
font-weight: 100;
}
.box {
position: relative;
left: 10px;
padding: 0;
background: rgb(31, 31, 31);
color: rgb(77, 77, 77);
width: 100px;
margin-bottom: 10px;
height: 75px;
display: block;
line-height: 75px;
text-align: center;
}
.box.white{ color: whitesmoke; }
.box.yellow{ color: yellow; }
.box.cyan{ color: cyan; }
.box.red{ color: red; }
</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>
https://www.w3.org/TR/animation-timing/ <br>
https://developers.google.com/web/fundamentals/performance/rendering/optimize-javascript-execution?hl=es-419 <br>
<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>
-->
<button onclick="resetByLabel('yellow-label')">Click me to rest by label 'yellow-label'</button>
<br>
<div id="animated-1" class="box cyan">Hello there 1</div>
<div id="animated-2" class="box red">Hello there 2</div>
<div id="animated-3" class="box white">Hello there 3</div>
<div id="animated-4" class="box yellow">Hello there 4</div>
<div id="animated-5" class="box white">Hello there 5</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="./../../src/animation-stepper.js"></script>
<script type="text/javascript" src="./demo-2.js"></script>
<script type="text/javascript" src="./demo-2-animations.js"></script>
</body>
</html>