Skip to content

Commit

Permalink
add example code
Browse files Browse the repository at this point in the history
  • Loading branch information
b6pzeusbc54tvhw5jgpyw8pwz2x6gs committed Apr 27, 2016
1 parent 6ebb7ac commit 663fbf5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
24 changes: 24 additions & 0 deletions example/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var rafUC = require('../raf-uc.js');

var stateList = [{pos: 0}];

function step() {

console.log( 'hi' );

var lastState = stateList[ stateList.length-1 ];
newPos = lastState.pos+1;
stateList.push({ pos: newPos });

if( newPos < 10 ) {

rafUC.raf( step );

} else {

console.log('count hi: ' + stateList.length );
console.log( stateList );
}
}

rafUC.raf( step );
23 changes: 12 additions & 11 deletions raf-uc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
var root = (typeof self == 'object' && self.self == self && self) ||
(typeof global == 'object' && global.global == global && global);

// Set up Backbone appropriately for the environment. Start with AMD.
// Set up raf-uc appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {

define(['exports'], function(exports) {
Expand All @@ -41,36 +41,37 @@
root.rafUC = factory( root, {} );
}

}( function( root, rafUC ) {
}( function( root, exports ) {

'use strict';

var previousRafUC = root.rafUC;
rafUC.noConflict = function() {
exports.noConflict = function() {
root.rafUC = previousRafUC;
return this;
};

var lastTime = 0;
var prefixes = 'webkit moz ms o'.split(' ');
var prefixList = 'webkit moz ms o'.split(' ');
// get unprefixed rAF and cAF, if present
var requestAnimationFrame = root.requestAnimationFrame;
var cancelAnimationFrame = root.cancelAnimationFrame;
// loop through vendor prefixes and get prefixed rAF and cAF
// loop through vendor prefixList and get prefixed rAF and cAF
var prefix;
for( var i = 0; i < prefixes.length; i++ ) {
for( var i = 0; i < prefixList.length; i++ ) {
if ( requestAnimationFrame && cancelAnimationFrame ) {
break;
}
prefix = prefixes[i];
prefix = prefixList[i];
requestAnimationFrame = requestAnimationFrame || root[ prefix + 'RequestAnimationFrame' ];
cancelAnimationFrame = cancelAnimationFrame || root[ prefix + 'CancelAnimationFrame' ] ||
root[ prefix + 'CancelRequestAnimationFrame' ];
}

// fallback to setTimeout and clearTimeout if either request/cancel is not supported
if ( !requestAnimationFrame || !cancelAnimationFrame ) {
requestAnimationFrame = function( callback, element ) {

requestAnimationFrame = function( callback ) {
var currTime = new Date().getTime();
var timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
var id = root.setTimeout( function() {
Expand All @@ -89,8 +90,8 @@
root.requestAnimationFrame = requestAnimationFrame;
root.cancelAnimationFrame = cancelAnimationFrame;

rafUC.raf = requestAnimationFrame;
rafUC.caf = requestAnimationFrame;
exports.raf = requestAnimationFrame;
exports.caf = requestAnimationFrame;

return rafUC;
return exports;
}));

0 comments on commit 663fbf5

Please sign in to comment.