-
Notifications
You must be signed in to change notification settings - Fork 24.5k
/
Copy pathbuildStyleInterpolator.js
211 lines (198 loc) · 5.39 KB
/
buildStyleInterpolator.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';
const keyOf = require('fbjs/lib/keyOf');
const X_DIM = keyOf({x: null});
const Y_DIM = keyOf({y: null});
const Z_DIM = keyOf({z: null});
const InitialOperationField = {
transformTranslate: [0, 0, 0],
transformScale: [1, 1, 1],
};
const InterpolateMatrix = {
transformScale: function(mat, x, y, z) {
mat[0] = mat[0] * x;
mat[1] = mat[1] * x;
mat[2] = mat[2] * x;
mat[3] = mat[3] * x;
mat[4] = mat[4] * y;
mat[5] = mat[5] * y;
mat[6] = mat[6] * y;
mat[7] = mat[7] * y;
mat[8] = mat[8] * z;
mat[9] = mat[9] * z;
mat[10] = mat[10] * z;
mat[11] = mat[11] * z;
},
transformTranslate: function(mat, x, y, z) {
mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
},
};
const computeNextValLinear = function(anim, from, to, value) {
const hasRoundRatio = 'round' in anim;
const roundRatio = anim.round;
let ratio = (value - anim.min) / (anim.max - anim.min);
if (!anim.extrapolate) {
ratio = ratio > 1 ? 1 : ratio < 0 ? 0 : ratio;
}
let nextVal = from * (1 - ratio) + to * ratio;
if (hasRoundRatio) {
nextVal = Math.round(roundRatio * nextVal) / roundRatio;
}
if (!isFinite(nextVal)) {
nextVal = null;
}
return nextVal;
};
const computeNextValLinearScalar = function(anim, value) {
return computeNextValLinear(anim, anim.from, anim.to, value);
};
const setNextValAndDetectChange = function(result, name, nextVal, didChange) {
if (!didChange) {
const prevVal = result[name];
result[name] = nextVal;
didChange = didChange || nextVal !== prevVal;
} else {
result[name] = nextVal;
}
return didChange;
};
const initIdentity = function(mat) {
mat[0] = 1;
mat[1] = 0;
mat[2] = 0;
mat[3] = 0;
mat[4] = 0;
mat[5] = 1;
mat[6] = 0;
mat[7] = 0;
mat[8] = 0;
mat[9] = 0;
mat[10] = 1;
mat[11] = 0;
mat[12] = 0;
mat[13] = 0;
mat[14] = 0;
mat[15] = 1;
};
const computeNextMatrixOperationField = function(
anim,
name,
dim,
index,
value,
) {
if (anim.from[dim] !== undefined && anim.to[dim] !== undefined) {
return computeNextValLinear(anim, anim.from[dim], anim.to[dim], value);
} else {
return InitialOperationField[name][index];
}
};
const computeTransform = function(
anim,
name,
value,
result,
didChange,
didMatrix,
) {
const transform =
result.transform !== undefined
? result.transform
: (result.transform = [{matrix: []}]);
const mat = transform[0].matrix;
const m0 = mat[0];
const m1 = mat[1];
const m2 = mat[2];
const m3 = mat[3];
const m4 = mat[4];
const m5 = mat[5];
const m6 = mat[6];
const m7 = mat[7];
const m8 = mat[8];
const m9 = mat[9];
const m10 = mat[10];
const m11 = mat[11];
const m12 = mat[12];
const m13 = mat[13];
const m14 = mat[14];
const m15 = mat[15];
if (!didMatrix) {
initIdentity(mat); // This will be the first transform.
}
const x = computeNextMatrixOperationField(anim, name, X_DIM, 0, value);
const y = computeNextMatrixOperationField(anim, name, Y_DIM, 1, value);
const z = computeNextMatrixOperationField(anim, name, Z_DIM, 2, value);
InterpolateMatrix[name](mat, x, y, z);
if (!didChange) {
didChange =
m0 !== mat[0] ||
m1 !== mat[1] ||
m2 !== mat[2] ||
m3 !== mat[3] ||
m4 !== mat[4] ||
m5 !== mat[5] ||
m6 !== mat[6] ||
m7 !== mat[7] ||
m8 !== mat[8] ||
m9 !== mat[9] ||
m10 !== mat[10] ||
m11 !== mat[11] ||
m12 !== mat[12] ||
m13 !== mat[13] ||
m14 !== mat[14] ||
m15 !== mat[15];
}
return didChange;
};
/**
* @param {object} anims Animation configuration by style property name.
* @return {function} Function accepting style object, that mutates that style
* object and returns a boolean describing if any update was actually applied.
*/
const buildStyleInterpolator = function(anims) {
function styleInterpolator(result, value) {
let didChange = false;
let didMatrix = false;
for (const name in anims) {
const anim = anims[name];
if (anim.type === 'linear') {
if (name in InterpolateMatrix) {
didChange = computeTransform(
anim,
name,
value,
result,
didChange,
didMatrix,
);
didMatrix = true;
} else {
const next = computeNextValLinearScalar(anim, value);
didChange = setNextValAndDetectChange(result, name, next, didChange);
}
} else if (anim.type === 'constant') {
const next = anim.value;
didChange = setNextValAndDetectChange(result, name, next, didChange);
} else if (anim.type === 'step') {
const next = value >= anim.threshold ? anim.to : anim.from;
didChange = setNextValAndDetectChange(result, name, next, didChange);
} else if (anim.type === 'identity') {
const next = value;
didChange = setNextValAndDetectChange(result, name, next, didChange);
}
}
return didChange;
}
return styleInterpolator;
};
module.exports = buildStyleInterpolator;