From aa18cfee35805df98de7bb5e64d6cd0953f50f44 Mon Sep 17 00:00:00 2001 From: Andrea Bogazzi Date: Sun, 21 Jan 2024 20:15:08 +0100 Subject: [PATCH] added typings --- src/shapes/Object/ObjectGeometry.ts | 9 +++++++-- src/shapes/Object/ObjectOrigin.ts | 11 ++++++++++- src/shapes/Polyline.ts | 25 +++++++++---------------- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/shapes/Object/ObjectGeometry.ts b/src/shapes/Object/ObjectGeometry.ts index fa7e6c0c320..3c9c3712223 100644 --- a/src/shapes/Object/ObjectGeometry.ts +++ b/src/shapes/Object/ObjectGeometry.ts @@ -22,7 +22,10 @@ import { import { radiansToDegrees } from '../../util/misc/radiansDegreesConversion'; import type { Canvas } from '../../canvas/Canvas'; import type { StaticCanvas } from '../../canvas/StaticCanvas'; -import { ObjectOrigin } from './ObjectOrigin'; +import { + ObjectOrigin, + type _getTransformedDimensionsParams, +} from './ObjectOrigin'; import type { ObjectEvents } from '../../EventTypeDefs'; import type { ControlProps } from './types/ControlProps'; @@ -550,7 +553,9 @@ export class ObjectGeometry * @param {object} [options] transform options * @returns {Point} dimensions */ - _calculateCurrentDimensions(options?: any): Point { + _calculateCurrentDimensions( + options?: _getTransformedDimensionsParams + ): Point { return this._getTransformedDimensions(options) .transform(this.getViewportTransform(), true) .scalarAdd(2 * this.padding); diff --git a/src/shapes/Object/ObjectOrigin.ts b/src/shapes/Object/ObjectOrigin.ts index a0f9866b3b1..9d1d3f70332 100644 --- a/src/shapes/Object/ObjectOrigin.ts +++ b/src/shapes/Object/ObjectOrigin.ts @@ -10,6 +10,13 @@ import type { BaseProps } from './types/BaseProps'; import type { FillStrokeProps } from './types/FillStrokeProps'; import { CENTER, LEFT, TOP } from '../../constants'; +export type _getTransformedDimensionsParams = { + scaleX?: number; + scaleY?: number; + skewX?: number; + skewY?: number; +}; + export class ObjectOrigin extends CommonMethods implements BaseProps, Pick @@ -47,7 +54,9 @@ export class ObjectOrigin * @private * @returns {Point} dimensions */ - _getTransformedDimensions(options: any = {}): Point { + _getTransformedDimensions( + options: _getTransformedDimensionsParams = {} + ): Point { const { scaleX = this.scaleX, scaleY = this.scaleY, diff --git a/src/shapes/Polyline.ts b/src/shapes/Polyline.ts index 6909fe6499f..9f67981b315 100644 --- a/src/shapes/Polyline.ts +++ b/src/shapes/Polyline.ts @@ -18,6 +18,7 @@ import type { ObjectEvents } from '../EventTypeDefs'; import { cloneDeep } from '../util/internals/cloneDeep'; import { CENTER, LEFT, TOP } from '../constants'; import type { CSSRules } from '../parser/typedefs'; +import type { _getTransformedDimensionsParams } from './Object/ObjectOrigin'; export const polylineDefaultValues: Partial> = { /** @@ -163,10 +164,14 @@ export class Polyline< bboxNoStroke = makeBoundingBoxFromPoints( this.points.map((p) => transformPoint(p, matrix, true)) ), + // @TODO it is very strange that we allow for scaleX and scaleY in the options + // but then here we don't use them scale = new Point(this.scaleX, this.scaleY); let offsetX = bbox.left + bbox.width / 2, offsetY = bbox.top + bbox.height / 2; if (this.exactBoundingBox) { + // @TODO it is very strange that we allow for skewX and skewY in the options + // but then here we don't use them offsetX = offsetX - offsetY * Math.tan(degreesToRadians(this.skewX)); // Order of those assignments is important. // offsetY relies on offsetX being already changed by the line above @@ -232,33 +237,21 @@ export class Polyline< /** * @override stroke and skewing are taken into account when projecting stroke on points, * therefore we don't want the default calculation to account for skewing as well. - * Though it is possible to pass `width` and `height` in `options`, doing so is very strange, use with discretion. * * @private */ - _getTransformedDimensions(options: any = {}) { + _getTransformedDimensions(options: _getTransformedDimensionsParams = {}) { if (this.exactBoundingBox) { let size: Point; /* When `strokeUniform = true`, any changes to the properties require recalculating the `width` and `height` because the stroke projections are affected. When `strokeUniform = false`, we don't need to recalculate for scale transformations, as the effect of scale on projections follows a linear function (e.g. scaleX of 2 just multiply width by 2)*/ - if ( - Object.keys(options).some( - (key) => - this.strokeUniform || - (this.constructor as typeof Polyline).layoutProperties.includes( - key as keyof TProjectStrokeOnPointsOptions - ) - ) - ) { + if (this.strokeUniform || 'skewX' in options || 'skewY' in options) { const { width, height } = this._calcDimensions(options); - size = new Point(options.width ?? width, options.height ?? height); + size = new Point(width, height); } else { - size = new Point( - options.width ?? this.width, - options.height ?? this.height - ); + size = new Point(this.width, this.height); } return size.multiply( new Point(options.scaleX || this.scaleX, options.scaleY || this.scaleY)