blob: f3673c5e4267ebd6db71aee907d20404f2e61391 (
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
|
( 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;
})();
|