-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (38 loc) · 1.02 KB
/
index.js
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
46
47
48
49
'use strict';
var TinyEmitter = require('tiny-emitter');
var Downup = module.exports = function(init) {
this.updating = false;
this.value = init || 0;
this.test = init || 0;
};
Downup.prototype = Object.create(TinyEmitter.prototype);
Downup.prototype.set = function(value) {
this.value = value;
};
Downup.prototype.update = function(value) {
this.test = value;
if (this.updating && value === this.value) return;
this.updating = true;
if (requestAnimationFrame)
requestAnimationFrame(this._update.bind(this)); // Browser
else
this._update(); // Node
};
// Internal update
Downup.prototype._update = function(){
var offset = this.test - this.value,
min = Math.min(this.test, this.value),
max = Math.max(this.test, this.value);
for (var evt in this.e) {
evt = Number(evt);
if (
(evt > min && evt < max) ||
(evt === min && offset < 0) ||
(evt === max && offset > 0)
) {
this.emit(evt, offset);
}
}
this.value = this.test;
this.updating = false;
}