Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- fix(): multiplyTransformMatrixArray redundant multiplication [#9860](https://github.com/fabricjs/fabric.js/pull/9860)
- fix(Polyline): safeguard points arg from options [#9855](https://github.com/fabricjs/fabric.js/pull/9855)
- feat(IText): Adjust cursor blinking for better feedback [#9823](https://github.com/fabricjs/fabric.js/pull/9823)
- feat(FabricObject): pass `e` to `shouldStartDragging` [#9843](https://github.com/fabricjs/fabric.js/pull/9843)
Expand Down
55 changes: 42 additions & 13 deletions src/util/misc/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ export const multiplyTransformMatrices = (
is2x2 ? 0 : a[1] * b[4] + a[3] * b[5] + a[5],
] as TMat2D;

/**
* @perf declare at top level to avoid recreating on the fly
*/
const reduceMatrices = (
product: TMat2D | undefined | null | false,
curr: TMat2D | undefined | null | false
) =>
curr && product ? multiplyTransformMatrices(curr, product) : curr || product;

/**
* @perf declare at top level to avoid recreating on the fly
*/
const reduce2x2Matrices = (
product: TMat2D | undefined | null | false,
curr: TMat2D | undefined | null | false
) =>
curr && product
? multiplyTransformMatrices(curr, product, true)
: curr || product;

/**
* Multiplies {@link matrices} such that a matrix defines the plane for the rest of the matrices **after** it
*
Expand All @@ -96,12 +116,9 @@ export const multiplyTransformMatrices = (
export const multiplyTransformMatrixArray = (
matrices: (TMat2D | undefined | null | false)[],
is2x2?: boolean
) =>
matrices.reduceRight(
(product: TMat2D, curr) =>
curr ? multiplyTransformMatrices(curr, product, is2x2) : product,
iMatrix
);
): TMat2D =>
matrices.reduceRight(is2x2 ? reduce2x2Matrices : reduceMatrices, undefined) ||
(iMatrix.concat() as TMat2D);

export const calcPlaneRotation = ([a, b]: TMat2D) =>
Math.atan2(b, a) as TRadian;
Expand Down Expand Up @@ -142,7 +159,7 @@ export const qrDecompose = (a: TMat2D): TQrDecomposeOut => {
* @param {number} [y] translation on Y axis
* @returns {TMat2D} matrix
*/
export const createTranslateMatrix = (x: number, y = 0): TMat2D => [
export const createTranslateMatrix = (x = 0, y = 0): TMat2D => [
1,
0,
0,
Expand Down Expand Up @@ -304,14 +321,26 @@ export const calcDimensionsMatrix = ({
* @return {Number[]} transform matrix
*/
export const composeMatrix = ({
translateX = 0,
translateY = 0,
angle = 0 as TDegree,
...otherOptions
translateX,
translateY,
angle,
scaleX,
scaleY,
skewX,
skewY,
flipX,
flipY,
}: TComposeMatrixArgs): TMat2D => {
return multiplyTransformMatrixArray([
createTranslateMatrix(translateX, translateY),
!!(translateX || translateY) &&
createTranslateMatrix(translateX, translateY),
angle && createRotateMatrix({ angle }),
calcDimensionsMatrix(otherOptions),
((scaleX && scaleX !== 1) ||
(scaleY && scaleY !== 1) ||
skewX ||
skewY ||
flipX ||
flipY) &&
calcDimensionsMatrix({ scaleX, scaleY, skewX, skewY, flipX, flipY }),
]);
};