refactor transform processing
This commit is contained in:
Horcrux
2016-07-27 10:43:24 +08:00
parent 1051bc078f
commit a636211917
5 changed files with 370 additions and 174 deletions
+75 -35
View File
@@ -1,45 +1,85 @@
import Transform from '../Transform';
import Matrix2D from '../Matrix2D';
import _ from 'lodash';
let pooledTransform = new Transform();
let pooledMatrix = new Matrix2D();
function transformToMatrix(props) {
let scaleX = !_.isNil(props.scaleX) ? props.scaleX :
!_.isNil(props.scale) ? props.scale : 1;
let scaleY = !_.isNil(props.scaleY) ? props.scaleY :
!_.isNil(props.scale) ? props.scale : 1;
function transformToMatrix(props, transform) {
pooledMatrix.reset();
appendTransform(props);
pooledTransform
.transformTo(1, 0, 0, 1, 0, 0)
.move(props.x || 0, props.y || 0)
.rotate(props.rotation || 0, props.originX, props.originY)
.scale(scaleX, scaleY, props.originX, props.originY);
if (!_.isNil(props.transform)) {
pooledTransform.transform(props.transform);
if (transform) {
appendTransform(transform);
}
return [
pooledTransform.xx, pooledTransform.yx,
pooledTransform.xy, pooledTransform.yy,
pooledTransform.x, pooledTransform.y
];
return pooledMatrix.toArray();
}
function appendTransform(transform) {
pooledMatrix
.appendTransform(
transform.x + transform.originX,
transform.y + transform.originY,
transform.scaleX, transform.scaleY,
transform.rotation,
transform.skewX,
transform.skewY,
transform.originX,
transform.originY
);
}
function universal2axis(universal, axisX, axisY, defaultValue) {
let coords = [];
let x;
let y;
if (_.isString(universal)) {
coords = universal.split(/\s*,\s*/);
if (coords.length === 2) {
x = +coords[0];
y = +coords[1];
} else if (coords.length === 1) {
x = y = +coords[0];
}
} else if (_.isNumber(universal)) {
x = y = universal;
}
axisX = +axisX;
if (!isNaN(axisX)) {
x = axisX;
}
axisY = +axisY;
if (!isNaN(axisY)) {
y = axisY;
}
return [x || defaultValue || 0, y || defaultValue || 0];
}
function props2transform(props) {
let [originX, originY] = universal2axis(props.origin, props.originX, props.originY);
let [scaleX, scaleY] = universal2axis(props.scale, props.scaleX, props.scaleY, 1);
let [skewX, skewY] = universal2axis(props.skew, props.skewX, props.skewY);
let [translateX, translateY] = universal2axis(props.translate, props.translateX, props.translateY);
return {
rotation: +props.rotation || +props.rotate || 0,
scaleX: scaleX,
scaleY: scaleY,
originX: originX,
originY: originY,
skewX: skewX,
skewY: skewY,
x: translateX,
y: translateY
};
}
/**
*
* @param props
*/
export default function (props) {
let coords = props.origin ? props.origin.split(/\s*,\s*/) : [];
// TODO: support Percentage for originX,originY
let originX = coords.length === 2 ? coords[0] : props.originX;
let originY = coords.length === 2 ? coords[1] : props.originY;
return transformToMatrix({
rotation: +props.rotation / 2 || +props.rotate / 2 || 0,
scale: props.scale,
scaleX: props.scaleX,
scaleY: props.scaleY,
originX: +originX || 0,
originY: +originY || 0,
x: +props.x || 0,
y: +props.y || 0
});
// TODO: support Percentage for transform
return transformToMatrix(props2transform(props), props.transform ? props2transform(props.transform) : null);
}