Skip to content

Commit

Permalink
added typings
Browse files Browse the repository at this point in the history
  • Loading branch information
asturur committed Jan 21, 2024
1 parent e99fec2 commit aa18cfe
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
9 changes: 7 additions & 2 deletions src/shapes/Object/ObjectGeometry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -550,7 +553,9 @@ export class ObjectGeometry<EventSpec extends ObjectEvents = ObjectEvents>
* @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);
Expand Down
11 changes: 10 additions & 1 deletion src/shapes/Object/ObjectOrigin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventSpec>
extends CommonMethods<EventSpec>
implements BaseProps, Pick<FillStrokeProps, 'strokeWidth' | 'strokeUniform'>
Expand Down Expand Up @@ -47,7 +54,9 @@ export class ObjectOrigin<EventSpec>
* @private
* @returns {Point} dimensions
*/
_getTransformedDimensions(options: any = {}): Point {
_getTransformedDimensions(
options: _getTransformedDimensionsParams = {}
): Point {
const {
scaleX = this.scaleX,
scaleY = this.scaleY,
Expand Down
25 changes: 9 additions & 16 deletions src/shapes/Polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TClassProperties<Polyline>> = {
/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aa18cfe

Please sign in to comment.