Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaMan123 committed May 6, 2024
1 parent 210c18c commit 076402a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
19 changes: 19 additions & 0 deletions src/util/misc/matrix.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { hasMatrixDimensionProps } from './matrix';

describe('matrix', () => {
it('hasMatrixDimensionProps', () => {
expect(hasMatrixDimensionProps({})).toBeFalsy();
expect(hasMatrixDimensionProps({ scaleX: 0 })).toBeTruthy();
expect(hasMatrixDimensionProps({ scaleY: 0 })).toBeTruthy();
expect(hasMatrixDimensionProps({ scaleX: 1 })).toBeFalsy();
expect(hasMatrixDimensionProps({ scaleY: 1 })).toBeFalsy();
expect(hasMatrixDimensionProps({ skewX: 0 })).toBeFalsy();
expect(hasMatrixDimensionProps({ skewY: 0 })).toBeFalsy();
expect(hasMatrixDimensionProps({ flipX: true })).toBeTruthy();
expect(hasMatrixDimensionProps({ flipY: true })).toBeTruthy();
expect(hasMatrixDimensionProps({ flipX: false })).toBeFalsy();
expect(hasMatrixDimensionProps({ flipY: false })).toBeFalsy();
expect(hasMatrixDimensionProps({ flipY: false, scaleX: 0 })).toBeTruthy();
expect(hasMatrixDimensionProps({ scaleY: 1, skewX: 1 })).toBeTruthy();
});
});
57 changes: 28 additions & 29 deletions src/util/misc/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ export const createSkewYMatrix = (skewValue: TDegree): TMat2D => [
0,
];

export const hasMatrixDimensionProps = ({
scaleX,
scaleY,
skewX,
skewY,
flipX,
flipY,
}: TComposeMatrixArgs) =>
(typeof scaleX === 'number' && scaleX !== 1) ||
(typeof scaleY === 'number' && scaleY !== 1) ||
!!(skewX || skewY || flipX || flipY);

/**
* Returns a transform matrix starting from an object of the same kind of
* the one returned from qrDecompose, useful also if you want to calculate some
Expand All @@ -286,22 +298,20 @@ export const createSkewYMatrix = (skewValue: TDegree): TMat2D => [
* @param {Number} [options.skewY]
* @return {Number[]} transform matrix
*/
export const calcDimensionsMatrix = ({
scaleX = 1,
scaleY = 1,
flipX = false,
flipY = false,
skewX = 0 as TDegree,
skewY = 0 as TDegree,
}: TScaleMatrixArgs) => {
return multiplyTransformMatrixArray(
[
createScaleMatrix(flipX ? -scaleX : scaleX, flipY ? -scaleY : scaleY),
skewX && createSkewXMatrix(skewX),
skewY && createSkewYMatrix(skewY),
],
true
);
export const calcDimensionsMatrix = (decomposedValues: TScaleMatrixArgs) => {
return hasMatrixDimensionProps(decomposedValues)
? multiplyTransformMatrixArray(
[
createScaleMatrix(
(decomposedValues.flipX ? -1 : 1) * (decomposedValues.scaleX ?? 1),
(decomposedValues.flipY ? -1 : 1) * (decomposedValues.scaleY ?? 1)
),
decomposedValues.skewX && createSkewXMatrix(decomposedValues.skewX),
decomposedValues.skewY && createSkewYMatrix(decomposedValues.skewY),
],
true
)
: (iMatrix.concat() as TMat2D);
};

/**
Expand All @@ -324,23 +334,12 @@ export const composeMatrix = ({
translateX,
translateY,
angle,
scaleX,
scaleY,
skewX,
skewY,
flipX,
flipY,
...dimensionProps
}: TComposeMatrixArgs): TMat2D => {
return multiplyTransformMatrixArray([
!!(translateX || translateY) &&
createTranslateMatrix(translateX, translateY),
angle && createRotateMatrix({ angle }),
((scaleX && scaleX !== 1) ||
(scaleY && scaleY !== 1) ||
skewX ||
skewY ||
flipX ||
flipY) &&
calcDimensionsMatrix({ scaleX, scaleY, skewX, skewY, flipX, flipY }),
calcDimensionsMatrix(dimensionProps),
]);
};

0 comments on commit 076402a

Please sign in to comment.