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
23 changes: 13 additions & 10 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/cleanupSvAttribute';
import { skewXMatrix, skewYMatrix } from './skewMatrix';
import {
calcRotateMatrix,
calcScaleMatrix,
calcSkewXMatrix,
calcSkewYMatrix,
calcTranslateMatrix,
multiplyTransformMatrices,
} 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 = calcTranslateMatrix(arg0, arg1);
break;
case 'rotate':
matrix = rotateMatrix(arg0, arg1, arg2);
matrix = calcRotateMatrix({ angle: arg0 }, arg1, arg2);
break;
case 'scale':
matrix = scaleMatrix(arg0, arg1);
matrix = calcScaleMatrix(arg0, arg1);
break;
case 'skewX':
matrix = skewXMatrix(arg0);
matrix = calcSkewXMatrix(arg0);
break;
case 'skewY':
matrix = skewYMatrix(arg0);
matrix = calcSkewYMatrix(arg0);
break;
case 'matrix':
matrix = [arg0, arg1, arg2, arg3, arg4, arg5];
Expand Down
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.

5 changes: 3 additions & 2 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,
calcTranslateMatrix,
multiplyTransformMatrices,
qrDecompose,
TQrDecomposeOut,
Expand Down Expand Up @@ -235,7 +236,7 @@ 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,
tMatrix = calcTranslateMatrix(center.x, center.y),
rMatrix = calcRotateMatrix({
angle: this.getTotalAngle() - (!!this.group && this.flipX ? 180 : 0),
}),
Expand Down
4 changes: 2 additions & 2 deletions src/shapes/Object/ObjectGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { makeBoundingBoxFromPoints } from '../../util/misc/boundingBoxFromPoints
import { cos } from '../../util/misc/cos';
import {
calcRotateMatrix,
calcTranslateMatrix,
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 @@ -664,7 +664,7 @@ export class ObjectGeometry<EventSpec extends ObjectEvents = ObjectEvents>
calcACoords(): TCornerPoint {
const rotateMatrix = calcRotateMatrix({ angle: this.angle }),
{ x, y } = this.getRelativeCenterPoint(),
tMatrix = translateMatrix(x, y),
tMatrix = calcTranslateMatrix(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,
calcDimensionsMatrix,
calcTranslateMatrix,
calcRotateMatrix,
calcScaleMatrix,
calcSkewXMatrix,
calcSkewYMatrix,
calcDimensionsMatrix,
multiplyTransformMatrices,
isIdentityMatrix,
} from './misc/matrix';
Expand Down
8 changes: 8 additions & 0 deletions src/util/misc/angleSkewConversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TRadian, TDegree } from '../../typedefs';
import { degreesToRadians, radiansToDegrees } from './radiansDegreesConversion';

export const angleToSkew = (angle: TDegree) =>
ShaMan123 marked this conversation as resolved.
Show resolved Hide resolved
Math.tan(degreesToRadians(angle));

export const skewToAngle = (value: TRadian) =>
radiansToDegrees(Math.atan(value));
Loading