This repository was archived by the owner on Dec 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathprocessTransform.vr.js
executable file
·233 lines (220 loc) · 7.36 KB
/
processTransform.vr.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule processTransform
* @flow
*/
/* eslint-disable */
'use strict';
const MatrixMath = require('MatrixMath');
const Platform = require('Platform');
const invariant = require('invariant');
const stringifySafe = require('stringifySafe');
/**
* Generate a transform matrix based on the provided transforms, and use that
* within the style object instead.
*
* This allows us to provide an API that is similar to CSS, where transforms may
* be applied in an arbitrary order, and yet have a universal, singular
* interface to native code.
*/
function processTransform(transform: Object): Object {
const result = MatrixMath.createIdentityMatrix();
transform.forEach(transformation => {
const key = Object.keys(transformation)[0];
const value = transformation[key];
if (__DEV__) {
_validateTransform(key, value, transformation);
}
switch (key) {
case 'matrix':
MatrixMath.multiplyInto(result, result, value);
break;
case 'perspective':
_multiplyTransform(result, MatrixMath.reusePerspectiveCommand, [value]);
break;
case 'rotateX':
_multiplyTransform(result, MatrixMath.reuseRotateXCommand, [_convertToRadians(value)]);
break;
case 'rotateY':
_multiplyTransform(result, MatrixMath.reuseRotateYCommand, [_convertToRadians(value)]);
break;
case 'rotate':
case 'rotateZ':
_multiplyTransform(result, MatrixMath.reuseRotateZCommand, [_convertToRadians(value)]);
break;
case 'scale':
if (typeof value === 'number') {
_multiplyTransform(result, MatrixMath.reuseScale3dCommand, [value, value, value]);
} else {
_multiplyTransform(result, MatrixMath.reuseScale3dCommand, [
value[0],
value[1],
value[2],
]);
}
break;
case 'scaleX':
_multiplyTransform(result, MatrixMath.reuseScaleXCommand, [value]);
break;
case 'scaleY':
_multiplyTransform(result, MatrixMath.reuseScaleYCommand, [value]);
break;
case 'scaleZ':
_multiplyTransform(result, MatrixMath.reuseScaleZCommand, [value]);
break;
case 'scale3d':
_multiplyTransform(result, MatrixMath.reuseScale3dCommand, [value[0], value[1], value[2]]);
break;
case 'translate':
_multiplyTransform(result, MatrixMath.reuseTranslate3dCommand, [
value[0],
value[1],
value[2] || 0,
]);
break;
case 'translateX':
_multiplyTransform(result, MatrixMath.reuseTranslate2dCommand, [value, 0]);
break;
case 'translateY':
_multiplyTransform(result, MatrixMath.reuseTranslate2dCommand, [0, value]);
break;
case 'translateZ':
_multiplyTransform(result, MatrixMath.reuseTranslate3dCommand, [0, 0, value]);
break;
case 'skewX':
_multiplyTransform(result, MatrixMath.reuseSkewXCommand, [_convertToRadians(value)]);
break;
case 'skewY':
_multiplyTransform(result, MatrixMath.reuseSkewYCommand, [_convertToRadians(value)]);
break;
default:
throw new Error('Invalid transform name: ' + key);
}
});
// Android does not support the direct application of a transform matrix to
// a view, so we need to decompose the result matrix into transforms that can
// get applied in the specific order of (1) translate (2) scale (3) rotate.
// Once we can directly apply a matrix, we can remove this decomposition.
if (Platform.OS === 'android') {
return MatrixMath.decomposeMatrix(result);
}
return result;
}
/**
* Performs a destructive operation on a transform matrix.
*/
function _multiplyTransform(
result: Array<number>,
matrixMathFunction: Function,
args: Array<number>
): void {
const matrixToApply = MatrixMath.createIdentityMatrix();
const argsWithIdentity = [matrixToApply].concat(args);
matrixMathFunction.apply(this, argsWithIdentity);
MatrixMath.multiplyInto(result, result, matrixToApply);
}
/**
* Parses a string like '0.5rad' or '60deg' into radians expressed in a float.
* Note that validation on the string is done in `_validateTransform()`.
*/
function _convertToRadians(value: string): number {
const isRad = typeof value === 'string' ? value.indexOf('rad') > -1 : false;
const floatValue = typeof value === 'string' ? parseFloat(value, 10) : value;
return isRad ? floatValue : floatValue * Math.PI / 180;
}
function _validateTransform(key, value, transformation) {
invariant(
!value.getValue,
'You passed an Animated.Value to a normal component. ' +
'You need to wrap that component in an Animated. For example, ' +
'replace <View /> by <Animated.View />.'
);
const multivalueTransforms = ['matrix', 'translate'];
if (multivalueTransforms.indexOf(key) !== -1) {
invariant(
Array.isArray(value),
'Transform with key of %s must have an array as the value: %s',
key,
stringifySafe(transformation)
);
}
switch (key) {
case 'matrix':
invariant(
value.length === 9 || value.length === 16,
'Matrix transform must have a length of 9 (2d) or 16 (3d). ' +
'Provided matrix has a length of %s: %s',
value.length,
stringifySafe(transformation)
);
break;
case 'translate':
break;
case 'scale':
invariant(
typeof value === 'number' || value.length === 3,
'scale with key of "%s" must be a number or array of length 3: %s',
key,
stringifySafe(transformation)
);
break;
case 'rotateX':
case 'rotateY':
case 'rotateZ':
case 'rotate':
invariant(
typeof value === 'string' || typeof value === 'number',
'Transform with key of "%s" must be a string or number: %s',
key,
stringifySafe(transformation)
);
invariant(
typeof value === 'number' || value.indexOf('deg') > -1 || value.indexOf('rad') > -1,
'Rotate transform must be expressed in degrees (deg) or radians (rad): %s',
stringifySafe(transformation)
);
break;
case 'skewX':
case 'skewY':
invariant(
typeof value === 'string',
'Transform with key of "%s" must be a string: %s',
key,
stringifySafe(transformation)
);
invariant(
value.indexOf('deg') > -1 || value.indexOf('rad') > -1,
'Rotate transform must be expressed in degrees (deg) or radians (rad): %s',
stringifySafe(transformation)
);
break;
case 'perspective':
invariant(
typeof value === 'number',
'Transform with key of "%s" must be a number: %s',
key,
stringifySafe(transformation)
);
invariant(
value !== 0,
'Transform with key of "%s" cannot be zero: %s',
key,
stringifySafe(transformation)
);
break;
default:
invariant(
typeof value === 'number',
'Transform with key of "%s" must be a number: %s',
key,
stringifySafe(transformation)
);
}
}
module.exports = processTransform;