Skip to content

Instantly share code, notes, and snippets.

@willbailey
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willbailey/4d99ecc3d0d963d0d9bc to your computer and use it in GitHub Desktop.
Save willbailey/4d99ecc3d0d963d0d9bc to your computer and use it in GitHub Desktop.
'use strict';
var SpringChain = function(attachmentSpringConfig, mainSpringConfig) {
this._springSystem = new rebound.SpringSystem();
this._listeners = [];
this._springs = [];
this._controlSpringIndex = -1;
this._attachmentSpringConfig = attachmentSpringConfig ||
SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG;
this._mainSpringConfig = mainSpringConfig ||
SpringChain.DEFAULT_MAIN_SPRING_CONFIG;
};
SpringChain.DEFAULT_MAIN_SPRING_CONFIG =
rebound.SpringConfig.fromOrigamiTensionAndFriction(255, 11);
SpringChain.DEFAULT_ATTACHMENT_SPRING_CONFIG =
rebound.SpringConfig.fromOrigamiTensionAndFriction(40, 7);
SpringChain.prototype.addSpring = function(listener) {
var spring = this._springSystem.createSpring()
.addListener(this)
.setSpringConfig(this._attachmentSpringConfig);
this._springs.push(spring);
this._listeners.push(listener);
return this;
};
SpringChain.prototype.getControlSpring = function() {
return this._springs[this._controlSpringIndex];
}
SpringChain.prototype.setControlSpringIndex = function(idx) {
this._controlSpringIndex = idx;
var controlSpring = this._springs[idx];
if (!controlSpring) {
return null;
}
for (var i = 0, len = this._springs.length; i < len; i++) {
var spring = this._springs[i];
spring.setSpringConfig(this._attachmentSpringConfig);
}
this.getControlSpring().setSpringConfig(this._mainSpringConfig);
return this;
};
SpringChain.prototype.getControlSpring = function() {
return this._springs[this._controlSpringIndex];
};
SpringChain.prototype.getSpringSystem = function() {
return this._springSystem;
};
SpringChain.prototype.getAllSprings = function() {
return this._springs;
};
SpringChain.prototype._proxyEvent = function(evt, spring) {
var idx = this._springs.indexOf(spring);
var listener = this._listeners[idx];
if (!listener) {
return;
}
var handler = listener[evt];
if (!handler) {
return;
}
handler.call(listener, spring);
};
SpringChain.prototype.onSpringActivate = function(spring) {
this._proxyEvent('onSpringActivate', spring);
};
SpringChain.prototype.onSpringEndStateChange = function(spring) {
this._proxyEvent('onSpringEndStateChange', spring);
};
SpringChain.prototype.onSpringUpdate = function(spring) {
var idx = this._springs.indexOf(spring);
var listener = this._listeners[idx];
var above = -1, below = -1;
if (idx === this._controlSpringIndex) {
below = idx - 1;
above = idx + 1;
} else if (idx < this._controlSpringIndex) {
below = idx - 1;
} else if (idx > this._controlSpringIndex) {
above = idx + 1;
}
if (above > -1 && above < this._springs.length) {
this._springs[above].setEndValue(spring.getCurrentValue());
}
if (below > -1 && below < this._springs.length) {
this._springs[below].setEndValue(spring.getCurrentValue());
}
listener.onSpringUpdate(spring);
};
SpringChain.prototype.onSpringAtRest = function(spring) {
this._proxyEvent('onSpringAtRest', spring);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment