Comparing version 0.0.4 to 0.0.5
{ | ||
"name": "rebound", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A simple library for modeling spring dynamics", | ||
@@ -19,3 +19,9 @@ "main": "rebound.min.js", | ||
"animation" | ||
] | ||
], | ||
"scripts": { | ||
"test": "jasmine-node spec" | ||
}, | ||
"devDependencies": { | ||
"jasmine-node": "^1.14.5" | ||
} | ||
} |
143
rebound.js
@@ -129,12 +129,11 @@ // Rebound | ||
// create a new SpringSystem and then add springs to it. | ||
var SpringSystem = rebound.SpringSystem = function SpringSystem() { | ||
var SpringSystem = rebound.SpringSystem = function SpringSystem(looper) { | ||
this._springRegistry = {}; | ||
this._activeSprings = []; | ||
this._listeners = []; | ||
this.listeners = []; | ||
this._idleSpringIndices = []; | ||
this._boundFrameCallback = bind(this._frameCallback, this); | ||
this.looper = looper || new AnimationLooper(); | ||
this.looper.springSystem = this; | ||
}; | ||
extend(SpringSystem, {}); | ||
extend(SpringSystem.prototype, { | ||
@@ -150,12 +149,16 @@ | ||
_listeners: null, | ||
listeners: null, | ||
_idleSpringIndices: null, | ||
_frameCallback: function() { | ||
this.loop(); | ||
// A SpringSystem is iterated by a looper. The looper is responsible | ||
// for executing each frame as the SpringSystem is resolved to idle. | ||
// There are three types of Loopers described below AnimationLooper, | ||
// SimulationLooper, and SteppingSimulationLooper. AnimationLooper is | ||
// the default as it is the most useful for common UI animations. | ||
setLooper: function(looper) { | ||
this.looper = looper | ||
looper.springSystem = this; | ||
}, | ||
_frameCallbackId: null, | ||
// Create and register a new spring with the SpringSystem. This | ||
@@ -195,3 +198,9 @@ // Spring will now be solved for during the physics iteration loop. By default | ||
getAllSprings: function() { | ||
return Object.values(this._springRegistry); | ||
var vals = []; | ||
for (var id in this._springRegistry) { | ||
if (this._springRegistry.hasOwnProperty(id)) { | ||
vals.push(this._springRegistry[id]); | ||
} | ||
} | ||
return vals; | ||
}, | ||
@@ -245,5 +254,4 @@ | ||
// SpringSystem. | ||
loop: function() { | ||
loop: function(currentTimeMillis) { | ||
var listener; | ||
var currentTimeMillis = Date.now(); | ||
if (this._lastTimeMillis === -1) { | ||
@@ -255,5 +263,5 @@ this._lastTimeMillis = currentTimeMillis -1; | ||
var i = 0, len = this._listeners.length; | ||
var i = 0, len = this.listeners.length; | ||
for (i = 0; i < len; i++) { | ||
var listener = this._listeners[i]; | ||
var listener = this.listeners[i]; | ||
listener.onBeforeIntegrate && listener.onBeforeIntegrate(this); | ||
@@ -269,10 +277,8 @@ } | ||
for (i = 0; i < len; i++) { | ||
var listener = this._listeners[i]; | ||
var listener = this.listeners[i]; | ||
listener.onAfterIntegrate && listener.onAfterIntegrate(this); | ||
} | ||
compatCancelAnimationFrame(this._frameCallbackId); | ||
if (!this._isIdle) { | ||
this._frameCallbackId = | ||
compatRequestAnimationFrame(this._boundFrameCallback); | ||
this.looper.run(); | ||
} | ||
@@ -291,5 +297,3 @@ }, | ||
this._isIdle = false; | ||
compatCancelAnimationFrame(this._frameCallbackId); | ||
this._frameCallbackId = | ||
compatRequestAnimationFrame(this._boundFrameCallback); | ||
this.looper.run(); | ||
} | ||
@@ -302,3 +306,3 @@ }, | ||
addListener: function(listener) { | ||
this._listeners.push(listener); | ||
this.listeners.push(listener); | ||
}, | ||
@@ -308,3 +312,3 @@ | ||
removeListener: function(listener) { | ||
removeFirst(this._listeners, listener); | ||
removeFirst(this.listeners, listener); | ||
}, | ||
@@ -314,3 +318,3 @@ | ||
removeAllListeners: function() { | ||
this._listeners = []; | ||
this.listeners = []; | ||
} | ||
@@ -335,5 +339,5 @@ | ||
var Spring = rebound.Spring = function Spring(springSystem) { | ||
this._id = Spring._ID++; | ||
this._id = 's' + Spring._ID++; | ||
this._springSystem = springSystem; | ||
this._listeners = []; | ||
this.listeners = []; | ||
this._currentState = new PhysicsState(); | ||
@@ -377,3 +381,3 @@ this._previousState = new PhysicsState(); | ||
_listeners: null, | ||
listeners: null, | ||
@@ -386,3 +390,3 @@ _timeAccumulator: 0, | ||
destroy: function() { | ||
this._listeners = []; | ||
this.listeners = []; | ||
this._springSystem.deregisterSpring(this); | ||
@@ -440,4 +444,4 @@ }, | ||
this._currentState.position = currentValue; | ||
for (var i = 0, len = this._listeners.length; i < len; i++) { | ||
var listener = this._listeners[i]; | ||
for (var i = 0, len = this.listeners.length; i < len; i++) { | ||
var listener = this.listeners[i]; | ||
listener.onSpringUpdate && listener.onSpringUpdate(this); | ||
@@ -482,4 +486,4 @@ } | ||
this._springSystem.activateSpring(this.getId()); | ||
for (var i = 0, len = this._listeners.length; i < len; i++) { | ||
var listener = this._listeners[i]; | ||
for (var i = 0, len = this.listeners.length; i < len; i++) { | ||
var listener = this.listeners[i]; | ||
listener.onSpringEndStateChange && listener.onSpringEndStateChange(this); | ||
@@ -660,4 +664,4 @@ } | ||
for (var i = 0, len = this._listeners.length; i < len; i++) { | ||
var listener = this._listeners[i]; | ||
for (var i = 0, len = this.listeners.length; i < len; i++) { | ||
var listener = this.listeners[i]; | ||
if (notifyActivate) { | ||
@@ -716,4 +720,8 @@ listener.onSpringActivate && listener.onSpringActivate(this); | ||
getListeners: function() { | ||
return this.listeners; | ||
}, | ||
addListener: function(newListener) { | ||
this._listeners.push(newListener); | ||
this.listeners.push(newListener); | ||
return this; | ||
@@ -723,3 +731,3 @@ }, | ||
removeListener: function(listenerToRemove) { | ||
removeFirst(this._listeners, listenerToRemove); | ||
removeFirst(this.listeners, listenerToRemove); | ||
return this; | ||
@@ -729,3 +737,3 @@ }, | ||
removeAllListeners: function() { | ||
this._listeners = []; | ||
this.listeners = []; | ||
return this; | ||
@@ -765,2 +773,55 @@ }, | ||
// Loopers | ||
// ------- | ||
// **AnimationLooper** resolves the SpringSystem on an animation timing loop. | ||
var AnimationLooper = rebound.AnimationLooper = function AnimationLooper() { | ||
this.springSystem = null; | ||
var _run = function() { | ||
this.springSystem.loop(Date.now()); | ||
}; | ||
this.run = function() { | ||
compatRequestAnimationFrame(_run); | ||
} | ||
}; | ||
// **SimulationLooper** resolves the SpringSystem to a resting state in a | ||
// blocking loop. This is useful for synchronously generating pre-recorded | ||
// animations that can then be played on a timing loop later. | ||
var SimulationLooper = rebound.SimulationLooper = function SimulationLooper(timestep) { | ||
this.springSystem = null; | ||
var time = 0; | ||
var running = false; | ||
timestep=timestep || 16.667; | ||
this.run = function() { | ||
if (running) { | ||
return; | ||
} | ||
running = true; | ||
while(!this.springSystem.getIsIdle()) { | ||
this.springSystem.loop(time+=timestep); | ||
} | ||
running = false; | ||
} | ||
}; | ||
// **SteppingSimulationLooper** resolves the SpringSystem one step at a time controlled | ||
// by an outside loop. This is useful for testing. | ||
var SteppingSimulationLooper = rebound.SteppingSimulationLooper = function(timestep) { | ||
this.springSystem = null; | ||
var time = 0; | ||
var running = false; | ||
// this.run is NOOP'd here to allow control from the outside using this.step. | ||
this.run = function(){}; | ||
// Perform one step toward resolving the SpringSystem. | ||
this.step = function(timestep) { | ||
this.springSystem.loop(time+=timestep); | ||
} | ||
} | ||
// Math for converting from | ||
@@ -836,8 +897,2 @@ // [Origami](http://facebook.github.io/origami/) to | ||
function compatCancelAnimationFrame(id) { | ||
return typeof window != 'undefined' && | ||
window.cancelAnimationFrame && | ||
cancelAnimationFrame(id); | ||
} | ||
// Cross browser/node timer functions. | ||
@@ -844,0 +899,0 @@ function compatRequestAnimationFrame(func) { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
490380
22
1684
1