diff --git a/gulp/util/bundle.js b/gulp/util/bundle.js index 7c5de51..17ea220 100644 --- a/gulp/util/bundle.js +++ b/gulp/util/bundle.js @@ -49,6 +49,8 @@ bundle.ignore('./' + exclude[i]); } + bundle.add('./src/polyfill/requestAnimationFrame.js'); + bundle.require(paths.jsEntry, { expose: 'pixi.js' }); return bundle; diff --git a/gulp/util/bundle.js b/gulp/util/bundle.js index 7c5de51..17ea220 100644 --- a/gulp/util/bundle.js +++ b/gulp/util/bundle.js @@ -49,6 +49,8 @@ bundle.ignore('./' + exclude[i]); } + bundle.add('./src/polyfill/requestAnimationFrame.js'); + bundle.require(paths.jsEntry, { expose: 'pixi.js' }); return bundle; diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 5008c8e..69ab4e2 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -7,7 +7,6 @@ _uid: 0, _saidHello: false, - RAFramePolyfill:require('./requestAnimationFramePolyfill'), EventData: require('./EventData'), eventTarget: require('./eventTarget'), pluginTarget: require('./pluginTarget'), diff --git a/gulp/util/bundle.js b/gulp/util/bundle.js index 7c5de51..17ea220 100644 --- a/gulp/util/bundle.js +++ b/gulp/util/bundle.js @@ -49,6 +49,8 @@ bundle.ignore('./' + exclude[i]); } + bundle.add('./src/polyfill/requestAnimationFrame.js'); + bundle.require(paths.jsEntry, { expose: 'pixi.js' }); return bundle; diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 5008c8e..69ab4e2 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -7,7 +7,6 @@ _uid: 0, _saidHello: false, - RAFramePolyfill:require('./requestAnimationFramePolyfill'), EventData: require('./EventData'), eventTarget: require('./eventTarget'), pluginTarget: require('./pluginTarget'), diff --git a/src/core/utils/requestAnimationFramePolyfill.js b/src/core/utils/requestAnimationFramePolyfill.js deleted file mode 100644 index 3e14a07..0000000 --- a/src/core/utils/requestAnimationFramePolyfill.js +++ /dev/null @@ -1,27 +0,0 @@ -(function(window) { - var lastTime = 0; - var vendors = ['ms', 'moz', 'webkit', 'o']; - 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) { - 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); - }; - } - - })(window); diff --git a/gulp/util/bundle.js b/gulp/util/bundle.js index 7c5de51..17ea220 100644 --- a/gulp/util/bundle.js +++ b/gulp/util/bundle.js @@ -49,6 +49,8 @@ bundle.ignore('./' + exclude[i]); } + bundle.add('./src/polyfill/requestAnimationFrame.js'); + bundle.require(paths.jsEntry, { expose: 'pixi.js' }); return bundle; diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 5008c8e..69ab4e2 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -7,7 +7,6 @@ _uid: 0, _saidHello: false, - RAFramePolyfill:require('./requestAnimationFramePolyfill'), EventData: require('./EventData'), eventTarget: require('./eventTarget'), pluginTarget: require('./pluginTarget'), diff --git a/src/core/utils/requestAnimationFramePolyfill.js b/src/core/utils/requestAnimationFramePolyfill.js deleted file mode 100644 index 3e14a07..0000000 --- a/src/core/utils/requestAnimationFramePolyfill.js +++ /dev/null @@ -1,27 +0,0 @@ -(function(window) { - var lastTime = 0; - var vendors = ['ms', 'moz', 'webkit', 'o']; - 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) { - 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); - }; - } - - })(window); diff --git a/src/polyfill/requestAnimationFrame.js b/src/polyfill/requestAnimationFrame.js new file mode 100644 index 0000000..1ce9076 --- /dev/null +++ b/src/polyfill/requestAnimationFrame.js @@ -0,0 +1,66 @@ +// References: +// https://github.com/Financial-Times/polyfill-service/tree/master/polyfills/requestAnimationFrame +// https://gist.github.com/timhall/4078614 +// http://paulirish.com/2011/requestanimationframe-for-smart-animating/ +// https://gist.github.com/1579671 +// http://updates.html5rocks.com/2012/05/requestAnimationFrame-API-now-with-sub-millisecond-precision + +// Expected to be used with Browserfiy +// Browserify automatically detects the use of `global` and passes the +// correct reference of `global`, `self`, and finally `window` + +// Date.now +if (!(Date.now && Date.prototype.getTime)) { + Date.now = function now() { + return new Date().getTime(); + }; +} + +// performance.now +if (!(global.performance && global.performance.now)) { + var startTime = Date.now(); + if (!global.performance) { + global.performance = {}; + } + global.performance.now = function () { + return Date.now() - startTime; + }; +} + +// requestAnimationFrame +var lastTime = Date.now(); +var vendors = ['ms', 'moz', 'webkit', 'o']; + +for(var x = 0; x < vendors.length && !global.requestAnimationFrame; ++x) { + global.requestAnimationFrame = global[vendors[x] + 'RequestAnimationFrame']; + global.cancelAnimationFrame = global[vendors[x] + 'CancelAnimationFrame'] || + global[vendors[x] + 'CancelRequestAnimationFrame']; +} + +if (!global.requestAnimationFrame) { + global.requestAnimationFrame = function (callback) { + if (typeof callback !== 'function') { + throw new TypeError(callback + 'is not a function'); + } + + var currentTime = Date.now(), + delay = 16 + lastTime - currentTime; + + if (delay < 0) { + delay = 0; + } + + lastTime = currentTime; + + return setTimeout(function () { + lastTime = Date.now(); + callback(performance.now()); + }, delay); + }; +} + +if (!global.cancelAnimationFrame) { + global.cancelAnimationFrame = function(id) { + clearTimeout(id); + }; +}