-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patheasing.js
More file actions
114 lines (86 loc) · 3.5 KB
/
Copy patheasing.js
File metadata and controls
114 lines (86 loc) · 3.5 KB
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
const _ = require('lodash');
const EasingFunctions = require("./easing-functions");
function stopInterval(interval) {
if (interval != null)
clearInterval(interval)
interval = null;
}
module.exports = function(RED) {
function Easing(config) {
RED.nodes.createNode(this,config);
var node = this;
// validate settings
if (config.interval <= 0 || config.duration <= 0) {
node.error("duration and interval need to be bigger than 0.");
return;
}
if (config.numberOfValues <= 0) {
node.error("array size must be bigger than 0.");
return;
}
if (!_.has(EasingFunctions,config.easingType)) {
node.error("easing function does not exists.");
return;
}
config.interval = _.toNumber(config.interval);
config.duration = _.toNumber(config.duration);
config.numberOfValues = _.toNumber(config.numberOfValues);
// holds interval
var interval = null;
var lastValue = 0;
node.on('input', function(msg) {
var startValue, endValue;
// check if reset is send
if (msg.topic == 'reset') {
stopInterval(interval)
return
// if payload is a number, use that as endValue
} else if (_.isNumber(msg.payload)) {
startValue = lastValue;
endValue = msg.payload;
// else check if payload has to and from values
} else if (_.isObject(msg.payload)) {
startValue = _.has(msg.payload,'from') ? msg.payload.from : 0.0;
endValue = _.has(msg.payload,'to') ? msg.payload.to : 1.0;
} else {
startValue = 0.0;
endValue = 1.0
}
if (config.outputType === "asArray") {
let size = _.has(msg.payload, 'size') ? msg.payload.size : config.numberOfValues;
let values = _.map(_.range(0,1.0, 1.0/size), (t) => {
return startValue + EasingFunctions[config.easingType](t) * (endValue - startValue);
});
values.push(endValue);
lastValue = endValue;
msg.payload = values;
node.send(msg);
} else if (config.outputType === "overTime") {
let duration = _.has(msg.payload, 'duration') ? msg.payload.duration : config.duration;
let intervallTime = _.has(msg.payload, 'interval') ? msg.payload.interval : config.interval
let elapsed = 0;
// clear previous interval
stopInterval(interval);
//send start value
msg.payload = startValue
node.send(msg);
// start interval
interval = setInterval( () => {
elapsed += intervallTime;
let t = Math.min(1.0, elapsed / duration)
let val = startValue + EasingFunctions[config.easingType](t) * (endValue - startValue);
lastValue = val;
msg.payload = val
node.send(msg);
if (t >= 1.0) {
stopInterval(interval);
}
}, intervallTime)
}
});
node.on('close', () => {
stopInterval(interval);
})
}
RED.nodes.registerType("easing", Easing);
}