forked from kaelzhang/neuron.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmotion.js
More file actions
executable file
·163 lines (122 loc) · 3.74 KB
/
motion.js
File metadata and controls
executable file
·163 lines (122 loc) · 3.74 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* module fx/motion
*
* unlike Robert Penner's easing equations, fx/motion deal with the offset concerning to the time.
* not only animations, this class is a time plan for any functions
* y-axis is the argument of the function, x-axis is time
*/
module.exports = NR.Class({
Implements: 'events options',
/**
* @param {function()} onMotion
* @param {(Array.<function()>)|function()} equations
* @param {Object} options
*/
initialize: function(onMotion, equations, options){
if(!NR.isFunction(onMotion)) return;
var self = this,
bind = NR.bind;
self.set(options);
self.fn = onMotion;
self.eq = NR.makeArray(equations);
self.num = self.eq.length;
self._setPeriodical(self.get('fps'));
bind('_step', self);
self.timer = NR.delay(self._step, self.periodical, true);
},
_setPeriodical: function(fps){
self.periodical = Math.round(1000 / fps);
return fps;
},
_step: function(){
var self = this,
end = self.get('end');
self.time += + new Date - self.ts;
if( !end || self.time < end ){
self._motion();
/**
* set the timing timestamp when a single frame completed
* so that timer would not be affected by the executing of user functions and efficiency
*/
self.ts = + new Date;
}else{
self.time = end;
self._motion();
self.complete();
}
},
_motion: function(){
var self = this,
args = self._getArgs();
self.fn.apply(self, args);
},
_getArgs: function(){
var self = this,
ret = [],
time = self.time * self.get('prop'),
i = 0, len = self.num;
for(; i < len; i ++ ){
ret.push( self.eq[i]( time ) );
}
//return the computed arguments
return ret;
},
start: function(){
var self = this;
if(!self.timer.id){
self.time = self.get('begin');
self._startTimer();
self.fireEvent('start');
}
return self;
},
cancel: function(){
this._stopTimer().fireEvent('cancel');
return this;
},
//what's coming...
pause: function(){
this._stopTimer().fireEvent('pause');
return this;
},
resume: function(){
this._startTimer().fireEvent('resume');
return this;
},
complete: function(){
this._stopTimer().fireEvent('complete');
return this;
},
_stopTimer: function(){
this.timer.cancel();
return this;
},
_startTimer: function(){
var self = this;
// timestamp
self.ts = + new Date;
self.timer.start();
return self;
}
}, {
// frame per second, i.e. how many this.options.props per second
fps: {
value: 50,
setter: '_setPeriodical'
},
// one frame equal to <prop> miniseconds, proportion of time-axis in offset formula
prop: {
value: 1
},
begin: {
value: 0,
validator: NR.isNumber
},
end: {
value: 500,
validator: NR.isNumber,
setter: function(v){
return v < 0 ? 0 : v;
}
}
});