blob: e24507106b7c913d26f9e1b4ce22535bd4f1a033 (
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
39
40
41
42
43
44
45
|
( function() {
function Slider( x, y ) {
this.x = x;
this.y = y;
this.velocity = 0;
this.accel = 0;
this.friction = 0.15;
}
Slider.prototype.update = function() {
this.velocity += this.accel;
this.velocity *= ( 1 - this.friction );
this.x += this.velocity;
this.accel = 0;
};
Slider.prototype.applyForce = function( force ) {
this.accel += force;
};
Slider.prototype.getRestingPosition = function() {
var fFriction = 1 - this.friction;
var restingVelo = 0.07;
var ticks = getBaseLog( fFriction, restingVelo / Math.abs( this.velocity ) );
var frictionSum = ( Math.pow( fFriction, ticks + 1 ) - 1 ) / ( fFriction - 1 );
var restX = this.x + this.velocity * fFriction * frictionSum;
return {
x: restX,
frictionSum: frictionSum
};
};
function getBaseLog( a, b ) {
return Math.log( b ) / Math.log( a );
}
window.Slider = Slider;
})();
|