-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtweenmixin.js
92 lines (77 loc) · 1.77 KB
/
tweenmixin.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
'use strict';
var React = require('react-native');
var Timers = React.Timers;
var tweenFunctions = require('tween-functions');
var TweenMixin = {
_startTime : "",
_config: {},
__proto__: Object.prototype,
componentWillMount() {
this._startTime = Date.now();
},
setConfig(config) {
this._config = config;
},
getConfig() {
return this._config;
},
start() {
this._startTime = Date.now();
this.frameLoop();
},
setDestinations(dests) {
this._config.end[0].x = dests[0];
this._config.end[1].x = dests[1];
},
tweenCompleted() {
return ((this.raf == null)? true : false);
},
frameLoop() {
if(this._break) {
if (this.raf) {
window.cancelAnimationFrame(this.raf);
this.raf = null;
}
return;
}
var tweenVals = [];
var elapsed = (Date.now() - this._startTime);
var {
duration,
start,
end,
tween
} = this._config;
var maxDuration = Math.max.apply(Math, duration);
if(elapsed >= maxDuration){
this._config.frame(end);
if(this.raf) {
window.cancelAnimationFrame(this.raf);
this.raf = null;
}
if(typeof this._config.done === 'function') {
this._config.done();
}
return;
}
for (var i=0; i< start.length; i++) {
var tpVals = {};
for (var prop in start[i]) {
if (elapsed >= duration[i]) {
tpVals[prop] = end[i][prop];
} else {
tpVals[prop] = tweenFunctions[tween[i]](
elapsed, start[i][prop], end[i][prop], duration[i]
);
}
}
tweenVals.push(tpVals);
}
this._config.frame(tweenVals);
this.raf = window.requestAnimationFrame(this.frameLoop);
},
terminate() {
this._break = true;
}
};
module.exports = TweenMixin;