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(Object) removed code for options we dont want to use in _getTransformedDimensions #9620

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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]

- chore(Object) removed code for options we dont want to use in \_getTransformedDimensions [#9620](https://github.com/fabricjs/fabric.js/pull/9620)
- fix(StyledText): add ability to unset style (issue #9578) [#9597](https://github.com/fabricjs/fabric.js/pull/9597)
- CD(): expose vue deployed app [#9615](https://github.com/fabricjs/fabric.js/pull/9615)
- chore(): Upgrade Rollup to 4.9.5 [#9613](https://github.com/fabricjs/fabric.js/pull/9613)
Expand Down
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
43 changes: 23 additions & 20 deletions 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,40 +54,36 @@ export class ObjectOrigin<EventSpec>
* @private
* @returns {Point} dimensions
*/
_getTransformedDimensions(options: any = {}): Point {
const dimOptions = {
scaleX: this.scaleX,
scaleY: this.scaleY,
skewX: this.skewX,
skewY: this.skewY,
width: this.width,
height: this.height,
strokeWidth: this.strokeWidth,
...options,
};
_getTransformedDimensions(
options: _getTransformedDimensionsParams = {}
): Point {
const {
scaleX = this.scaleX,
scaleY = this.scaleY,
skewX = this.skewX,
skewY = this.skewY,
} = options;

// stroke is applied before/after transformations are applied according to `strokeUniform`
const strokeWidth = dimOptions.strokeWidth;
const strokeWidth = this.strokeWidth;
let preScalingStrokeValue = strokeWidth,
postScalingStrokeValue = 0;

if (this.strokeUniform) {
preScalingStrokeValue = 0;
postScalingStrokeValue = strokeWidth;
}
const dimX = dimOptions.width + preScalingStrokeValue,
dimY = dimOptions.height + preScalingStrokeValue,
noSkew = dimOptions.skewX === 0 && dimOptions.skewY === 0;
const dimX = this.width + preScalingStrokeValue,
dimY = this.height + preScalingStrokeValue,
noSkew = skewX === 0 && skewY === 0;
let finalDimensions;
if (noSkew) {
finalDimensions = new Point(
dimX * dimOptions.scaleX,
dimY * dimOptions.scaleY
);
finalDimensions = new Point(dimX * scaleX, dimY * scaleY);
} else {
finalDimensions = sizeAfterTransform(
dimX,
dimY,
calcDimensionsMatrix(dimOptions)
calcDimensionsMatrix({ scaleX, scaleY, skewX, skewY })
);
}

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) {
Copy link
Member Author

Choose a reason for hiding this comment

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

there are 4 options and only skewX and skewY are in layoutProperties, so we can check specifically.

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
Loading