-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfader.js
122 lines (106 loc) · 3.47 KB
/
fader.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"use strict"
var ease = require('./easing.js').ease
/**
* Fading effect with adaption of target while animating. Smooth delay of input values from one channel.
* Runs only on a single specific channel.
* Adaption of delay possible while animation is possible as well as aborting an animation.
*
* @param dmx dmx.js instance
* @param universe
* @param channel
* @constructor
*/
function Fader(dmx, universe, channel) {
this.dmx = dmx;
this.universe = universe;
this.channel = channel;
this.fadingGoal = 0;
this.finished = false;
this.aborted = false;
this.intervalId = null;
this.onFinish = false;
this.onUpdate = false;
this.speedUpdated = false;
}
Fader.speed = 1;
/**
* Set the new speed of this animation - possible while animatoin is running
* @param newspeed
*/
Fader.prototype.updateSpeed = function (newspeed) {
Fader.speed = newspeed;
if (this.intervalId != null && this.finished == false && this.speedUpdated == false) {
this.speedUpdated = true;
clearInterval(this.intervalId);
this.run(this.fadingGoal, Fader.speed, this.onFinish, this.onUpdate);
}
};
/**
* Modify speed values for "higher accuracy" at lower values
* @returns {number}
*/
Fader.prototype.getModifiedSpeed = function() {
// see function also in dmx-web for static animation -> socket_on-fading
return (0.1 * Math.exp((Fader.speed/13)+1)*1000 - 0.15)/ 256; //*1000 for millisec and div by 256 for size per step
};
/**
* Abort this single animation
*/
Fader.prototype.abort = function () {
//console.log("Aborting single animation");
this.aborted = true;
}
/**
* Set new target value where to end the animation - possible while animation is running.
* @param fadingGoal
*/
Fader.prototype.updateValue = function(fadingGoal) {
this.fadingGoal = fadingGoal;
};
/**
* Setup animation and start it
* Modification of parameters possible while running - see updateValue, updateSpeed
* Aborting with abort possible
*
* @param fadingGoal initial goal where it should finish
* @param speed initial how fast
* @param onFinish
* @param onUpdate
*/
Fader.prototype.run = function(fadingGoal, speed, onFinish, onUpdate) {
Fader.speed = speed;
this.fadingGoal = fadingGoal;
this.onFinish = onFinish;
this.onUpdate = onUpdate;
var self = this;
var lastUpdate = new Date().getTime();
var singleStep = function () {
var currentValue = self.dmx.get(self.universe, self.channel);
if (currentValue == self.fadingGoal || self.aborted) {//finished
clearInterval(self.intervalId);
self.finished = true;
var singleUpdate = {}; //creating new object with one single channel target value
singleUpdate[self.channel] = currentValue;
if(onFinish) onFinish(singleUpdate);
} else {
var newvalue = currentValue;
if (currentValue < self.fadingGoal) {
newvalue++;
} else { //current bigger
newvalue--;
}
var singleUpdate = {}; //creating new object with one single channel target value
singleUpdate[self.channel] = newvalue;
self.dmx.update(self.universe, singleUpdate, false);
if(onUpdate) onUpdate(singleUpdate);
// comment line above and uncomment lines below for better performance on display-slider update in browser
// if(onUpdate && new Date().getTime() - lastUpdate > 100){ //update display slider only every 100 ms
// onUpdate(singleUpdate);
// lastUpdate = new Date().getTime();
// };
}
self.speedUpdated = false;
};
self.intervalId = setInterval(singleStep, self.getModifiedSpeed());
};
module.exports = Fader