summaryrefslogtreecommitdiffstats
path: root/cells1/slider.js
blob: 2c504389c2ceb8835d9edaeafa8b2d853aafa57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// ----- Slider ----- //

( function() {

function Slider() {
  this.x = 0;
  this.accel = 0;
  this.velocity = 0;
  this.friction = 0.3;
}

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;

})();