Skip to content

Instantly share code, notes, and snippets.

@kirbysayshi
Created October 1, 2011 03:02
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 kirbysayshi/1255534 to your computer and use it in GitHub Desktop.
Save kirbysayshi/1255534 to your computer and use it in GitHub Desktop.
requestAnimationFrame support for ImpactJS
From 205a52304f57df249a17d839ae92d50ff3a70f46 Mon Sep 17 00:00:00 2001
From: KirbySaysHi <kirbysayshi@gmail.com>
Date: Fri, 30 Sep 2011 22:57:21 -0400
Subject: [PATCH] requestAnimationFrame. disable by setting ig.system.raf to
false.
---
lib/impact/impact.js | 14 +++++++++++++-
lib/impact/system.js | 20 +++++++++++++++++---
2 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/lib/impact/impact.js b/lib/impact/impact.js
index 2d42dce..c1e3578 100644
--- a/lib/impact/impact.js
+++ b/lib/impact/impact.js
@@ -55,7 +55,19 @@ Function.prototype.bind = function(bind) {
};
};
+// -----------------------------------------------------------------------------
+// requestAnimationFrame polyfill, paul irish.
+window.requestAnimFrame = (function(){
+ return window.requestAnimationFrame ||
+ window.webkitRequestAnimationFrame ||
+ window.mozRequestAnimationFrame ||
+ window.oRequestAnimationFrame ||
+ window.msRequestAnimationFrame ||
+ function(/* function */ callback, /* DOMElement */ element){
+ window.setTimeout(callback, 1000 / 60);
+ };
+})();
// -----------------------------------------------------------------------------
// ig Namespace
diff --git a/lib/impact/system.js b/lib/impact/system.js
index 7c2ac2d..6df229f 100644
--- a/lib/impact/system.js
+++ b/lib/impact/system.js
@@ -14,11 +14,12 @@ ig.System = ig.Class.extend({
realWidth: 320,
realHeight: 240,
scale: 1,
+ raf: true,
tick: 0,
- intervalId: 0,
newGameClass: null,
running: false,
+ intervalId: 0,
delegate: null,
clock: null,
@@ -76,15 +77,24 @@ ig.System = ig.Class.extend({
stopRunLoop: function() {
- clearInterval( this.intervalId );
this.running = false;
+ if(this.raf !== true){
+ clearInterval( this.intervalId );
+ }
},
startRunLoop: function() {
this.stopRunLoop();
- this.intervalId = setInterval( this.run.bind(this), 1000 / this.fps );
+
+ this.run = this.run.bind(this);
this.running = true;
+
+ if(this.raf !== true){
+ this.intervalId = setInterval(this.run, 1000 / this.fps);
+ } else {
+ this.run();
+ }
},
@@ -105,6 +115,10 @@ ig.System = ig.Class.extend({
this.setGameNow( this.newGameClass );
this.newGameClass = null;
}
+
+ if( this.raf === true && this.running === true ){
+ requestAnimFrame( this.run, this.canvas );
+ }
},
--
1.7.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment