Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(): Matrix util cleanup #8894

Merged
merged 16 commits into from
May 9, 2023
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]

- chore(): Matrix util cleanup [#8894](https://github.com/fabricjs/fabric.js/pull/8894)
- refactor(fabric.Line): Line position is calculated from the center between the 2 points now [#8877](https://github.com/fabricjs/fabric.js/pull/8877)
- chore(Path, Polyline): Clean up old SVG import code [#8857](https://github.com/fabricjs/fabric.js/pull/8857)

Expand Down
28 changes: 14 additions & 14 deletions src/parser/parseTransformAttribute.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { iMatrix } from '../constants';
import { reNum } from './constants';
import { multiplyTransformMatrices } from '../util/misc/matrix';
import { rotateMatrix } from './rotateMatrix';
import { scaleMatrix } from './scaleMatrix';
import { translateMatrix } from './translateMatrix';
import { TMat2D } from '../typedefs';
import { cleanupSvgAttribute } from '../util/internals/cleanupSvgAttribute';
import { skewXMatrix, skewYMatrix } from './skewMatrix';
import {
createRotateMatrix,
createScaleMatrix,
createSkewXMatrix,
createSkewYMatrix,
createTranslateMatrix,
multiplyTransformMatrices2,
} from '../util/misc/matrix';

// == begin transform regexp
const p = `(${reNum})`;
Expand Down Expand Up @@ -64,19 +67,19 @@ export function parseTransformAttribute(attributeValue: string): TMat2D {

switch (operation) {
case 'translate':
matrix = translateMatrix(arg0, arg1);
matrix = createTranslateMatrix(arg0, arg1);
break;
case 'rotate':
matrix = rotateMatrix(arg0, arg1, arg2);
matrix = createRotateMatrix({ angle: arg0 }, arg1, arg2);
break;
case 'scale':
matrix = scaleMatrix(arg0, arg1);
matrix = createScaleMatrix(arg0, arg1);
break;
case 'skewX':
matrix = skewXMatrix(arg0);
matrix = createSkewXMatrix(arg0);
break;
case 'skewY':
matrix = skewYMatrix(arg0);
matrix = createSkewYMatrix(arg0);
break;
case 'matrix':
matrix = [arg0, arg1, arg2, arg3, arg4, arg5];
Expand All @@ -87,8 +90,5 @@ export function parseTransformAttribute(attributeValue: string): TMat2D {
matrices.push(matrix);
}

return matrices.reduce(
(acc, matrix) => multiplyTransformMatrices(acc, matrix),
iMatrix
);
Comment on lines -90 to -93
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are changing a reduce to reduce right here nad i don't understand why.
I m incline to say don't touche the current transformation code at all, is not a cleanup anymore at that point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree.
Touched it and became uneasy but the math is correct and I double checked that there is a test to cover it.
This proves it is all right: https://en.wikipedia.org/wiki/Matrix_multiplication#Associativity

Notice the args order passed to multiplyTransformMatrices

return multiplyTransformMatrices2(matrices);
}
33 changes: 0 additions & 33 deletions src/parser/rotateMatrix.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/parser/scaleMatrix.ts

This file was deleted.

47 changes: 0 additions & 47 deletions src/parser/skewMatrix.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/parser/translateMatrix.ts

This file was deleted.

9 changes: 5 additions & 4 deletions src/shapes/Object/InteractiveObject.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Point } from '../../Point';
import type { AssertKeys, TCornerPoint, TDegree, TMat2D } from '../../typedefs';
import type { AssertKeys, TCornerPoint, TDegree } from '../../typedefs';
import { FabricObject } from './Object';
import { degreesToRadians } from '../../util/misc/radiansDegreesConversion';
import {
calcRotateMatrix,
createRotateMatrix,
createTranslateMatrix,
multiplyTransformMatrices,
qrDecompose,
TQrDecomposeOut,
Expand Down Expand Up @@ -235,8 +236,8 @@ export class InteractiveFabricObject<
calcOCoords(): Record<string, TOCoord> {
const vpt = this.getViewportTransform(),
center = this.getCenterPoint(),
tMatrix = [1, 0, 0, 1, center.x, center.y] as TMat2D,
rMatrix = calcRotateMatrix({
tMatrix = createTranslateMatrix(center.x, center.y),
rMatrix = createRotateMatrix({
angle: this.getTotalAngle() - (!!this.group && this.flipX ? 180 : 0),
}),
positionMatrix = multiplyTransformMatrices(tMatrix, rMatrix),
Expand Down
8 changes: 4 additions & 4 deletions src/shapes/Object/ObjectGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { Point } from '../../Point';
import { makeBoundingBoxFromPoints } from '../../util/misc/boundingBoxFromPoints';
import { cos } from '../../util/misc/cos';
import {
calcRotateMatrix,
createRotateMatrix,
createTranslateMatrix,
composeMatrix,
invertTransform,
multiplyTransformMatrices,
Expand All @@ -26,7 +27,6 @@ import type { StaticCanvas } from '../../canvas/StaticCanvas';
import { ObjectOrigin } from './ObjectOrigin';
import { ObjectEvents } from '../../EventTypeDefs';
import { ControlProps } from './types/ControlProps';
import { translateMatrix } from '../../parser/translateMatrix';

type TLineDescriptor = {
o: Point;
Expand Down Expand Up @@ -662,9 +662,9 @@ export class ObjectGeometry<EventSpec extends ObjectEvents = ObjectEvents>
* @return {TCornerPoint}
*/
calcACoords(): TCornerPoint {
const rotateMatrix = calcRotateMatrix({ angle: this.angle }),
const rotateMatrix = createRotateMatrix({ angle: this.angle }),
{ x, y } = this.getRelativeCenterPoint(),
tMatrix = translateMatrix(x, y),
tMatrix = createTranslateMatrix(x, y),
finalMatrix = multiplyTransformMatrices(tMatrix, rotateMatrix),
dim = this._getTransformedDimensions(),
w = dim.x / 2,
Expand Down
6 changes: 5 additions & 1 deletion src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ export {
invertTransform,
composeMatrix,
qrDecompose,
createTranslateMatrix,
createRotateMatrix,
createScaleMatrix,
createSkewXMatrix,
createSkewYMatrix,
calcDimensionsMatrix,
calcRotateMatrix,
multiplyTransformMatrices,
isIdentityMatrix,
} from './misc/matrix';
Expand Down