summaryrefslogtreecommitdiffstats
path: root/prevnext1/slider.js
diff options
context:
space:
mode:
Diffstat (limited to 'prevnext1/slider.js')
-rw-r--r--prevnext1/slider.js36
1 files changed, 36 insertions, 0 deletions
diff --git a/prevnext1/slider.js b/prevnext1/slider.js
new file mode 100644
index 0000000..f3673c5
--- /dev/null
+++ b/prevnext1/slider.js
@@ -0,0 +1,36 @@
+( function() {
+
+function Slider() {
+ this.x = 0;
+ this.accel = 0;
+ this.velocity = 0;
+ this.friction = 0.25;
+}
+
+Slider.prototype.update = function() {
+ this.velocity += this.accel;
+ this.velocity *= ( 1 - this.friction );
+ this.x += this.velocity;
+ // reset acceleration
+ this.accel = 0;
+};
+
+Slider.prototype.applyForce = function( force ) {
+ this.accel += force;
+};
+
+Slider.prototype.getRestingPosition = function() {
+ // little simulation where thing will rest
+ var restingVelo = 0.07;
+ var velo = this.velocity;
+ var restX = this.x;
+ while ( Math.abs( velo ) > restingVelo ) {
+ velo *= 1 - this.friction;
+ restX += velo;
+ }
+ return restX;
+};
+
+window.Slider = Slider;
+
+})();