forked from Wizcorp/tina.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransition.js
135 lines (119 loc) · 3.38 KB
/
Transition.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
123
124
125
126
127
128
129
130
131
132
133
134
135
// The file is a good representation of the constant fight between maintainability and performance
// For performance reasons several update methods are created
// The appropriate method should be used for tweening. The selection depends on:
// - The number of props to tween
// - Whether or not an easing is being used
// - Whether or not an interpolation is being used
// One property
function update(object, t) {
var p = this.prop;
object[p] = this.from[p] * (1 - t) + this.to[p] * t;
}
// Several Properties
function updateP(object, t) {
var q = this.props;
for (var i = 0; i < this.props.length; i += 1) {
var p = q[i];
object[p] = this.from[p] * (1 - t) + this.to[p] * t;
}
}
// Interpolation
function updateI(object, t) {
var p = this.prop;
object[p] = this.interps[p](t, this.from[p], this.to[p], this.interpParams[p]);
}
// Interpolation
// Several Properties
function updatePI(object, t) {
var q = this.props;
for (var i = 0; i < q.length; i += 1) {
var p = q[i];
object[p] = this.interps[p](t, this.from[p], this.to[p], this.interpParams[p]);
}
}
// Easing
function updateE(object, t) {
t = this.easing(t, this.easingParam);
var p = this.prop;
object[p] = this.from[p] * (1 - t) + this.to[p] * t;
}
// Easing
// Several Properties
function updatePE(object, t) {
var q = this.props;
t = this.easing(t, this.easingParam);
for (var i = 0; i < q.length; i += 1) {
var p = q[i];
object[p] = this.from[p] * (1 - t) + this.to[p] * t;
}
}
// Easing
// Interpolation
function updateIE(object, t) {
var p = this.prop;
object[p] = this.interps[p](this.easing(t, this.easingParam), this.from[p], this.to[p], this.interpParams[p]);
}
// Easing
// Interpolation
// Several Properties
function updatePIE(object, t) {
var q = this.props;
t = this.easing(t, this.easingParam);
for (var i = 0; i < q.length; i += 1) {
var p = q[i];
object[p] = this.interps[p](t, this.from[p], this.to[p], this.interpParams[p]);
}
}
var updateMethods = [
[
[update, updateP],
[updateI, updatePI]
], [
[updateE, updatePE],
[updateIE, updatePIE]
]
];
function Transition(properties, from, to, start, duration, easing, easingParam, interpolations, interpolationParams) {
this.start = start;
this.end = start + duration;
this.duration = duration;
this.from = from;
this.to = to;
// Easing flag - Whether an easing function is used
// 0 => Using linear easing
// 1 => Using custom easing
var easingFlag;
if (easing) {
easingFlag = 1;
this.easing = easing;
this.easingParam = easingParam;
} else {
easingFlag = 0;
}
// Interpolation flag - Whether an interpolation function is used
// 0 => No Interpolation
// 1 => At least one interpolation
var interpFlag;
if (interpolations === null) {
interpFlag = 0;
} else {
interpFlag = 1;
this.interps = interpolations;
this.interpParams = interpolationParams || {};
}
// Property flag - Whether the transition has several properties
// 0 => Only one property
// 1 => Several properties
var propsFlag;
if (properties.length === 1) {
propsFlag = 0;
this.prop = properties[0]; // string
this.props = null;
} else {
propsFlag = 1;
this.prop = null;
this.props = properties; // array
}
this.update = updateMethods[easingFlag][interpFlag][propsFlag];
}
module.exports = Transition;