Skip to content

Instantly share code, notes, and snippets.

@nealnote
Created August 1, 2014 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealnote/b22db26217b4531fa4f2 to your computer and use it in GitHub Desktop.
Save nealnote/b22db26217b4531fa4f2 to your computer and use it in GitHub Desktop.
requestAnimationFrame.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset='utf-8'>
<title> requestAnimationFrame </title>
<style type="text/css">
.mover {
position: absolute;
height: 100px;
width: 100px;
background-color: #27AE60;
}
</style>
</head>
<body>
<script type="text/javascript">
var rAF = (function(){
return window.requestAnimationFrame || function( callback ){
window.setTimeout(callback, 1000/60)
}
})();
for (var i = 0; i<50 ; i++ ){
document.write("<div class='mover'></div>");
}
var movers = document.querySelectorAll('.mover');
(function(){
for (var m = 0, l = movers.length; m < l ; m ++){
movers[m].style.top = ( m * 20) + 'px';
}
})();
function update(timestamp){
if ( timestamp < 5000 ) {
console.log( timestamp )
}
for (var m = 0, l = movers.length; m < l ; m ++){
movers[m].style.left = (Math.sin(m + timestamp/1000) + 1) * 500 + 'px';
}
rAF(update);
}
rAF(update);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment