2015-04-23 17:23:07 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-04-23 17:23:07 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-04-23 17:23:07 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
|
2015-04-23 17:23:07 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const MatrixMath = require('MatrixMath');
|
|
|
|
const Platform = require('Platform');
|
2015-05-15 17:47:19 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
|
|
|
const stringifySafe = require('stringifySafe');
|
2015-04-23 17:23:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a transform matrix based on the provided transforms, and use that
|
|
|
|
* within the style object instead.
|
|
|
|
*
|
2015-05-12 22:40:32 +00:00
|
|
|
* 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.
|
2015-04-23 17:23:07 +00:00
|
|
|
*/
|
2018-05-11 02:06:46 +00:00
|
|
|
function processTransform(
|
|
|
|
transform: Array<Object>,
|
|
|
|
): Array<Object> | Array<number> {
|
2016-08-05 17:48:34 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
_validateTransforms(transform);
|
|
|
|
}
|
|
|
|
|
2017-01-27 02:14:40 +00:00
|
|
|
// Android & iOS implementations of transform property accept the list of
|
|
|
|
// transform properties as opposed to a transform Matrix. This is necessary
|
|
|
|
// to control transform property updates completely on the native thread.
|
|
|
|
if (Platform.OS === 'android' || Platform.OS === 'ios') {
|
2016-08-05 17:48:34 +00:00
|
|
|
return transform;
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const result = MatrixMath.createIdentityMatrix();
|
2015-04-23 17:23:07 +00:00
|
|
|
|
|
|
|
transform.forEach(transformation => {
|
2018-05-10 22:44:52 +00:00
|
|
|
const key = Object.keys(transformation)[0];
|
|
|
|
const value = transformation[key];
|
2015-04-23 17:23:07 +00:00
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'matrix':
|
|
|
|
MatrixMath.multiplyInto(result, result, value);
|
|
|
|
break;
|
2015-07-15 14:57:25 +00:00
|
|
|
case 'perspective':
|
|
|
|
_multiplyTransform(result, MatrixMath.reusePerspectiveCommand, [value]);
|
|
|
|
break;
|
2015-07-15 02:41:07 +00:00
|
|
|
case 'rotateX':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseRotateXCommand, [
|
|
|
|
_convertToRadians(value),
|
|
|
|
]);
|
2015-07-15 02:41:07 +00:00
|
|
|
break;
|
|
|
|
case 'rotateY':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseRotateYCommand, [
|
|
|
|
_convertToRadians(value),
|
|
|
|
]);
|
2015-07-15 02:41:07 +00:00
|
|
|
break;
|
2015-04-23 17:23:07 +00:00
|
|
|
case 'rotate':
|
2015-07-15 02:41:07 +00:00
|
|
|
case 'rotateZ':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseRotateZCommand, [
|
|
|
|
_convertToRadians(value),
|
|
|
|
]);
|
2015-04-23 17:23:07 +00:00
|
|
|
break;
|
|
|
|
case 'scale':
|
|
|
|
_multiplyTransform(result, MatrixMath.reuseScaleCommand, [value]);
|
|
|
|
break;
|
|
|
|
case 'scaleX':
|
|
|
|
_multiplyTransform(result, MatrixMath.reuseScaleXCommand, [value]);
|
|
|
|
break;
|
|
|
|
case 'scaleY':
|
|
|
|
_multiplyTransform(result, MatrixMath.reuseScaleYCommand, [value]);
|
|
|
|
break;
|
|
|
|
case 'translate':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseTranslate3dCommand, [
|
|
|
|
value[0],
|
|
|
|
value[1],
|
|
|
|
value[2] || 0,
|
|
|
|
]);
|
2015-04-23 17:23:07 +00:00
|
|
|
break;
|
|
|
|
case 'translateX':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseTranslate2dCommand, [
|
|
|
|
value,
|
|
|
|
0,
|
|
|
|
]);
|
2015-04-23 17:23:07 +00:00
|
|
|
break;
|
|
|
|
case 'translateY':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseTranslate2dCommand, [
|
|
|
|
0,
|
|
|
|
value,
|
|
|
|
]);
|
2015-04-23 17:23:07 +00:00
|
|
|
break;
|
2015-09-24 03:03:16 +00:00
|
|
|
case 'skewX':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseSkewXCommand, [
|
|
|
|
_convertToRadians(value),
|
|
|
|
]);
|
2015-09-24 03:03:16 +00:00
|
|
|
break;
|
|
|
|
case 'skewY':
|
2018-05-11 02:06:46 +00:00
|
|
|
_multiplyTransform(result, MatrixMath.reuseSkewYCommand, [
|
|
|
|
_convertToRadians(value),
|
|
|
|
]);
|
2015-09-24 03:03:16 +00:00
|
|
|
break;
|
2015-04-23 17:23:07 +00:00
|
|
|
default:
|
|
|
|
throw new Error('Invalid transform name: ' + key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-06 22:19:59 +00:00
|
|
|
return result;
|
2015-04-23 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a destructive operation on a transform matrix.
|
|
|
|
*/
|
|
|
|
function _multiplyTransform(
|
|
|
|
result: Array<number>,
|
|
|
|
matrixMathFunction: Function,
|
2018-05-11 02:06:46 +00:00
|
|
|
args: Array<number>,
|
2015-04-23 17:23:07 +00:00
|
|
|
): void {
|
2018-05-10 22:44:52 +00:00
|
|
|
const matrixToApply = MatrixMath.createIdentityMatrix();
|
|
|
|
const argsWithIdentity = [matrixToApply].concat(args);
|
2015-04-23 17:23:07 +00:00
|
|
|
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 {
|
2018-05-10 22:44:52 +00:00
|
|
|
const floatValue = parseFloat(value);
|
2018-06-06 12:20:40 +00:00
|
|
|
return value.indexOf('rad') > -1 ? floatValue : (floatValue * Math.PI) / 180;
|
2015-04-23 17:23:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 14:23:30 +00:00
|
|
|
function _validateTransforms(transform: Array<Object>): void {
|
2016-08-05 17:48:34 +00:00
|
|
|
transform.forEach(transformation => {
|
2018-05-10 22:44:52 +00:00
|
|
|
const keys = Object.keys(transformation);
|
2017-02-02 14:23:30 +00:00
|
|
|
invariant(
|
|
|
|
keys.length === 1,
|
|
|
|
'You must specify exactly one property per transform object. Passed properties: %s',
|
|
|
|
stringifySafe(transformation),
|
|
|
|
);
|
2018-05-10 22:44:52 +00:00
|
|
|
const key = keys[0];
|
|
|
|
const value = transformation[key];
|
2016-08-05 17:48:34 +00:00
|
|
|
_validateTransform(key, value, transformation);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:23:07 +00:00
|
|
|
function _validateTransform(key, value, transformation) {
|
2015-05-15 17:47:19 +00:00
|
|
|
invariant(
|
|
|
|
!value.getValue,
|
2015-06-08 20:44:32 +00:00
|
|
|
'You passed an Animated.Value to a normal component. ' +
|
2018-05-11 02:06:46 +00:00
|
|
|
'You need to wrap that component in an Animated. For example, ' +
|
|
|
|
'replace <View /> by <Animated.View />.',
|
2015-05-15 17:47:19 +00:00
|
|
|
);
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const multivalueTransforms = ['matrix', 'translate'];
|
2015-04-23 17:23:07 +00:00
|
|
|
if (multivalueTransforms.indexOf(key) !== -1) {
|
|
|
|
invariant(
|
|
|
|
Array.isArray(value),
|
|
|
|
'Transform with key of %s must have an array as the value: %s',
|
|
|
|
key,
|
2015-05-15 17:47:19 +00:00
|
|
|
stringifySafe(transformation),
|
2015-04-23 17:23:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
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,
|
2015-05-15 17:47:19 +00:00
|
|
|
stringifySafe(transformation),
|
2015-04-23 17:23:07 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'translate':
|
2017-02-02 14:23:30 +00:00
|
|
|
invariant(
|
|
|
|
value.length === 2 || value.length === 3,
|
|
|
|
'Transform with key translate must be an array of length 2 or 3, found %s: %s',
|
|
|
|
value.length,
|
|
|
|
stringifySafe(transformation),
|
|
|
|
);
|
2015-04-23 17:23:07 +00:00
|
|
|
break;
|
2015-07-15 02:41:07 +00:00
|
|
|
case 'rotateX':
|
|
|
|
case 'rotateY':
|
|
|
|
case 'rotateZ':
|
2015-04-23 17:23:07 +00:00
|
|
|
case 'rotate':
|
2015-09-24 03:03:16 +00:00
|
|
|
case 'skewX':
|
|
|
|
case 'skewY':
|
2015-04-23 17:23:07 +00:00
|
|
|
invariant(
|
|
|
|
typeof value === 'string',
|
|
|
|
'Transform with key of "%s" must be a string: %s',
|
|
|
|
key,
|
2015-05-15 17:47:19 +00:00
|
|
|
stringifySafe(transformation),
|
2015-04-23 17:23:07 +00:00
|
|
|
);
|
|
|
|
invariant(
|
|
|
|
value.indexOf('deg') > -1 || value.indexOf('rad') > -1,
|
|
|
|
'Rotate transform must be expressed in degrees (deg) or radians ' +
|
|
|
|
'(rad): %s',
|
2015-05-15 17:47:19 +00:00
|
|
|
stringifySafe(transformation),
|
2015-04-23 17:23:07 +00:00
|
|
|
);
|
|
|
|
break;
|
2016-01-25 14:00:26 +00:00
|
|
|
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;
|
2017-02-02 14:23:30 +00:00
|
|
|
case 'translateX':
|
|
|
|
case 'translateY':
|
|
|
|
case 'scale':
|
|
|
|
case 'scaleX':
|
|
|
|
case 'scaleY':
|
2015-04-23 17:23:07 +00:00
|
|
|
invariant(
|
|
|
|
typeof value === 'number',
|
|
|
|
'Transform with key of "%s" must be a number: %s',
|
|
|
|
key,
|
2015-05-15 17:47:19 +00:00
|
|
|
stringifySafe(transformation),
|
2015-04-23 17:23:07 +00:00
|
|
|
);
|
2017-02-02 14:23:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-05-11 02:06:46 +00:00
|
|
|
invariant(
|
|
|
|
false,
|
|
|
|
'Invalid transform %s: %s',
|
|
|
|
key,
|
|
|
|
stringifySafe(transformation),
|
|
|
|
);
|
2015-04-23 17:23:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 22:19:59 +00:00
|
|
|
module.exports = processTransform;
|