From 17a845ce54a983b02aae1b0751fe898385217e1f Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sat, 25 Mar 2023 10:15:30 +0300 Subject: [PATCH 1/2] rename(): `IPoint` => `XY` thankfully VSCODE is a reliable tool --- src/Point.ts | 84 +++++++++---------- src/shapes/IText/ITextClickBehavior.ts | 8 +- src/shapes/Polyline.ts | 6 +- src/util/misc/boundingBoxFromPoints.ts | 6 +- src/util/misc/matrix.ts | 6 +- .../projectStroke/StrokeLineCapProjections.ts | 4 +- .../StrokeLineJoinProjections.ts | 9 +- .../projectStroke/StrokeProjectionsBase.ts | 4 +- src/util/misc/projectStroke/index.ts | 6 +- src/util/misc/vectors.ts | 4 +- 10 files changed, 66 insertions(+), 71 deletions(-) diff --git a/src/Point.ts b/src/Point.ts index f444f39cc66..1676778845e 100644 --- a/src/Point.ts +++ b/src/Point.ts @@ -2,7 +2,7 @@ import { TMat2D, TRadian } from './typedefs'; import { cos } from './util/misc/cos'; import { sin } from './util/misc/sin'; -export interface IPoint { +export interface XY { x: number; y: number; } @@ -10,15 +10,15 @@ export interface IPoint { /** * Adaptation of work of Kevin Lindsey(kevin@kevlindev.com) */ -export class Point implements IPoint { +export class Point implements XY { declare x: number; declare y: number; constructor(); constructor(x: number, y: number); - constructor(point: IPoint); - constructor(arg0: number | IPoint = 0, y = 0) { + constructor(point: XY); + constructor(arg0: number | XY = 0, y = 0) { if (typeof arg0 === 'object') { this.x = arg0.x; this.y = arg0.y; @@ -30,21 +30,21 @@ export class Point implements IPoint { /** * Adds another point to this one and returns another one - * @param {IPoint} that + * @param {XY} that * @return {Point} new Point instance with added values */ - add(that: IPoint): Point { + add(that: XY): Point { return new Point(this.x + that.x, this.y + that.y); } /** * Adds another point to this one - * @param {IPoint} that + * @param {XY} that * @return {Point} thisArg * @chainable * @deprecated */ - addEquals(that: IPoint): Point { + addEquals(that: XY): Point { this.x += that.x; this.y += that.y; return this; @@ -74,21 +74,21 @@ export class Point implements IPoint { /** * Subtracts another point from this point and returns a new one - * @param {IPoint} that + * @param {XY} that * @return {Point} new Point object with subtracted values */ - subtract(that: IPoint): Point { + subtract(that: XY): Point { return new Point(this.x - that.x, this.y - that.y); } /** * Subtracts another point from this point - * @param {IPoint} that + * @param {XY} that * @return {Point} thisArg * @chainable * @deprecated */ - subtractEquals(that: IPoint): Point { + subtractEquals(that: XY): Point { this.x -= that.x; this.y -= that.y; return this; @@ -118,10 +118,10 @@ export class Point implements IPoint { /** * Multiplies this point by another value and returns a new one - * @param {IPoint} that + * @param {XY} that * @return {Point} */ - multiply(that: IPoint): Point { + multiply(that: XY): Point { return new Point(this.x * that.x, this.y * that.y); } @@ -149,10 +149,10 @@ export class Point implements IPoint { /** * Divides this point by another and returns a new one - * @param {IPoint} that + * @param {XY} that * @return {Point} */ - divide(that: IPoint): Point { + divide(that: XY): Point { return new Point(this.x / that.x, this.y / that.y); } @@ -180,57 +180,57 @@ export class Point implements IPoint { /** * Returns true if this point is equal to another one - * @param {IPoint} that + * @param {XY} that * @return {Boolean} */ - eq(that: IPoint): boolean { + eq(that: XY): boolean { return this.x === that.x && this.y === that.y; } /** * Returns true if this point is less than another one - * @param {IPoint} that + * @param {XY} that * @return {Boolean} */ - lt(that: IPoint): boolean { + lt(that: XY): boolean { return this.x < that.x && this.y < that.y; } /** * Returns true if this point is less than or equal to another one - * @param {IPoint} that + * @param {XY} that * @return {Boolean} */ - lte(that: IPoint): boolean { + lte(that: XY): boolean { return this.x <= that.x && this.y <= that.y; } /** * Returns true if this point is greater another one - * @param {IPoint} that + * @param {XY} that * @return {Boolean} */ - gt(that: IPoint): boolean { + gt(that: XY): boolean { return this.x > that.x && this.y > that.y; } /** * Returns true if this point is greater than or equal to another one - * @param {IPoint} that + * @param {XY} that * @return {Boolean} */ - gte(that: IPoint): boolean { + gte(that: XY): boolean { return this.x >= that.x && this.y >= that.y; } /** * Returns new point which is the result of linear interpolation with this one and another one - * @param {IPoint} that + * @param {XY} that * @param {Number} t , position of interpolation, between 0 and 1 default 0.5 * @return {Point} */ - lerp(that: IPoint, t = 0.5): Point { + lerp(that: XY, t = 0.5): Point { t = Math.max(Math.min(1, t), 0); return new Point( this.x + (that.x - this.x) * t, @@ -240,10 +240,10 @@ export class Point implements IPoint { /** * Returns distance from this point and another one - * @param {IPoint} that + * @param {XY} that * @return {Number} */ - distanceFrom(that: IPoint): number { + distanceFrom(that: XY): number { const dx = this.x - that.x, dy = this.y - that.y; return Math.sqrt(dx * dx + dy * dy); @@ -251,28 +251,28 @@ export class Point implements IPoint { /** * Returns the point between this point and another one - * @param {IPoint} that + * @param {XY} that * @return {Point} */ - midPointFrom(that: IPoint): Point { + midPointFrom(that: XY): Point { return this.lerp(that); } /** * Returns a new point which is the min of this and another one - * @param {IPoint} that + * @param {XY} that * @return {Point} */ - min(that: IPoint): Point { + min(that: XY): Point { return new Point(Math.min(this.x, that.x), Math.min(this.y, that.y)); } /** * Returns a new point which is the max of this and another one - * @param {IPoint} that + * @param {XY} that * @return {Point} */ - max(that: IPoint): Point { + max(that: XY): Point { return new Point(Math.max(this.x, that.x), Math.max(this.y, that.y)); } @@ -318,10 +318,10 @@ export class Point implements IPoint { /** * Sets x/y of this point from another point - * @param {IPoint} that + * @param {XY} that * @chainable */ - setFromPoint(that: IPoint) { + setFromPoint(that: XY) { this.x = that.x; this.y = that.y; return this; @@ -329,9 +329,9 @@ export class Point implements IPoint { /** * Swaps x/y of this point and another point - * @param {IPoint} that + * @param {XY} that */ - swap(that: IPoint) { + swap(that: XY) { const x = this.x, y = this.y; this.x = that.x; @@ -352,11 +352,11 @@ export class Point implements IPoint { * Rotates `point` around `origin` with `radians` * @static * @memberOf fabric.util - * @param {IPoint} origin The origin of the rotation + * @param {XY} origin The origin of the rotation * @param {TRadian} radians The radians of the angle for the rotation * @return {Point} The new rotated point */ - rotate(radians: TRadian, origin: IPoint = originZero): Point { + rotate(radians: TRadian, origin: XY = originZero): Point { // TODO benchmark and verify the add and subtract how much cost // and then in case early return if no origin is passed const sinus = sin(radians), diff --git a/src/shapes/IText/ITextClickBehavior.ts b/src/shapes/IText/ITextClickBehavior.ts index dbc6b7f2def..e306bce2c3d 100644 --- a/src/shapes/IText/ITextClickBehavior.ts +++ b/src/shapes/IText/ITextClickBehavior.ts @@ -1,5 +1,5 @@ import type { TPointerEvent, TPointerEventInfo } from '../../EventTypeDefs'; -import { IPoint, Point } from '../../Point'; +import { XY, Point } from '../../Point'; import type { DragMethods } from '../Object/InteractiveObject'; import { stopEvent } from '../../util/dom_event'; import { invertTransform, transformPoint } from '../../util/misc/matrix'; @@ -23,7 +23,7 @@ export abstract class ITextClickBehavior< private declare __lastSelected: boolean; private declare __lastClickTime: number; private declare __lastLastClickTime: number; - private declare __lastPointer: IPoint | Record; + private declare __lastPointer: XY | Record; private declare __newClickTime: number; protected draggableTextDelegate: DraggableTextDelegate; @@ -88,7 +88,7 @@ export abstract class ITextClickBehavior< this.__lastSelected = this.selected; } - isTripleClick(newPointer: IPoint) { + isTripleClick(newPointer: XY) { return ( this.__newClickTime - this.__lastClickTime < 500 && this.__lastClickTime - this.__lastLastClickTime < 500 && @@ -293,7 +293,7 @@ export abstract class ITextClickBehavior< * @private */ _getNewSelectionStartFromOffset( - mouseOffset: IPoint, + mouseOffset: XY, prevWidth: number, width: number, index: number, diff --git a/src/shapes/Polyline.ts b/src/shapes/Polyline.ts index d198b57c3aa..f853effd91c 100644 --- a/src/shapes/Polyline.ts +++ b/src/shapes/Polyline.ts @@ -2,7 +2,7 @@ import { config } from '../config'; import { SHARED_ATTRIBUTES } from '../parser/attributes'; import { parseAttributes } from '../parser/parseAttributes'; import { parsePointsAttribute } from '../parser/parsePointsAttribute'; -import { IPoint, Point } from '../Point'; +import { XY, Point } from '../Point'; import { TClassProperties } from '../typedefs'; import { classRegistry } from '../ClassRegistry'; import { makeBoundingBoxFromPoints } from '../util/misc/boundingBoxFromPoints'; @@ -21,7 +21,7 @@ export class Polyline extends FabricObject { * @type Array * @default */ - declare points: IPoint[]; + declare points: XY[]; /** * WARNING: Feature in progress @@ -86,7 +86,7 @@ export class Polyline extends FabricObject { * top: 100 * }); */ - constructor(points: IPoint[] = [], { left, top, ...options }: any = {}) { + constructor(points: XY[] = [], { left, top, ...options }: any = {}) { super({ points, ...options }); this.initialized = true; this.setBoundingBox(true); diff --git a/src/util/misc/boundingBoxFromPoints.ts b/src/util/misc/boundingBoxFromPoints.ts index fc41e00e502..9763add32d2 100644 --- a/src/util/misc/boundingBoxFromPoints.ts +++ b/src/util/misc/boundingBoxFromPoints.ts @@ -1,12 +1,12 @@ -import { IPoint, Point } from '../../Point'; +import { XY, Point } from '../../Point'; import { TBBox } from '../../typedefs'; /** * Calculates bounding box (left, top, width, height) from given `points` - * @param {IPoint[]} points + * @param {XY[]} points * @return {Object} Object with left, top, width, height properties */ -export const makeBoundingBoxFromPoints = (points: IPoint[]): TBBox => { +export const makeBoundingBoxFromPoints = (points: XY[]): TBBox => { if (points.length === 0) { return { left: 0, diff --git a/src/util/misc/matrix.ts b/src/util/misc/matrix.ts index 3d0e91a7178..37b6db31deb 100644 --- a/src/util/misc/matrix.ts +++ b/src/util/misc/matrix.ts @@ -1,5 +1,5 @@ import { iMatrix } from '../../constants'; -import { IPoint, Point } from '../../Point'; +import { XY, Point } from '../../Point'; import { TDegree, TMat2D } from '../../typedefs'; import { cos } from './cos'; import { degreesToRadians, radiansToDegrees } from './radiansDegreesConversion'; @@ -36,13 +36,13 @@ export const isIdentityMatrix = (mat: TMat2D) => /** * Apply transform t to point p - * @param {Point | IPoint} p The point to transform + * @param {Point | XY} p The point to transform * @param {Array} t The transform * @param {Boolean} [ignoreOffset] Indicates that the offset should not be applied * @return {Point} The transformed point */ export const transformPoint = ( - p: IPoint, + p: XY, t: TMat2D, ignoreOffset?: boolean ): Point => new Point(p).transform(t, ignoreOffset); diff --git a/src/util/misc/projectStroke/StrokeLineCapProjections.ts b/src/util/misc/projectStroke/StrokeLineCapProjections.ts index 9bf66d738bc..3f5762f6e28 100644 --- a/src/util/misc/projectStroke/StrokeLineCapProjections.ts +++ b/src/util/misc/projectStroke/StrokeLineCapProjections.ts @@ -1,4 +1,4 @@ -import { IPoint, Point } from '../../../Point'; +import { XY, Point } from '../../../Point'; import { createVector, getOrthonormalVector, getUnitVector } from '../vectors'; import { StrokeLineJoinProjections } from './StrokeLineJoinProjections'; import { StrokeProjectionsBase } from './StrokeProjectionsBase'; @@ -26,7 +26,7 @@ export class StrokeLineCapProjections extends StrokeProjectionsBase { */ declare T: Point; - constructor(A: IPoint, T: IPoint, options: TProjectStrokeOnPointsOptions) { + constructor(A: XY, T: XY, options: TProjectStrokeOnPointsOptions) { super(options); this.A = new Point(A); this.T = new Point(T); diff --git a/src/util/misc/projectStroke/StrokeLineJoinProjections.ts b/src/util/misc/projectStroke/StrokeLineJoinProjections.ts index 1d927c4ecfe..63b3f40f962 100644 --- a/src/util/misc/projectStroke/StrokeLineJoinProjections.ts +++ b/src/util/misc/projectStroke/StrokeLineJoinProjections.ts @@ -1,4 +1,4 @@ -import { IPoint, Point } from '../../../Point'; +import { XY, Point } from '../../../Point'; import { degreesToRadians } from '../radiansDegreesConversion'; import { getBisector, getOrthonormalVector, magnitude } from '../vectors'; import { StrokeProjectionsBase } from './StrokeProjectionsBase'; @@ -34,12 +34,7 @@ export class StrokeLineJoinProjections extends StrokeProjectionsBase { */ declare bisector: ReturnType; - constructor( - A: IPoint, - B: IPoint, - C: IPoint, - options: TProjectStrokeOnPointsOptions - ) { + constructor(A: XY, B: XY, C: XY, options: TProjectStrokeOnPointsOptions) { super(options); this.A = new Point(A); this.B = new Point(B); diff --git a/src/util/misc/projectStroke/StrokeProjectionsBase.ts b/src/util/misc/projectStroke/StrokeProjectionsBase.ts index fdb8aa929ca..5610a232c0f 100644 --- a/src/util/misc/projectStroke/StrokeProjectionsBase.ts +++ b/src/util/misc/projectStroke/StrokeProjectionsBase.ts @@ -1,5 +1,5 @@ import { halfPI } from '../../../constants'; -import { IPoint, Point } from '../../../Point'; +import { XY, Point } from '../../../Point'; import { degreesToRadians } from '../radiansDegreesConversion'; import { calcAngleBetweenVectors, @@ -36,7 +36,7 @@ export abstract class StrokeProjectionsBase { /** * When the stroke is uniform, scaling affects the arrangement of points. So we must take it into account. */ - protected createSideVector(from: IPoint, to: IPoint) { + protected createSideVector(from: XY, to: XY) { const v = createVector(from, to); return this.options.strokeUniform ? v.multiply(this.scale) : v; } diff --git a/src/util/misc/projectStroke/index.ts b/src/util/misc/projectStroke/index.ts index 7722dbdc917..2442975f169 100644 --- a/src/util/misc/projectStroke/index.ts +++ b/src/util/misc/projectStroke/index.ts @@ -1,4 +1,4 @@ -import { IPoint } from '../../../Point'; +import { XY } from '../../../Point'; import { StrokeLineCapProjections } from './StrokeLineCapProjections'; import { StrokeLineJoinProjections } from './StrokeLineJoinProjections'; import { TProjection, TProjectStrokeOnPointsOptions } from './types'; @@ -11,7 +11,7 @@ import { TProjection, TProjectStrokeOnPointsOptions } from './types'; * */ export const projectStrokeOnPoints = ( - points: IPoint[], + points: XY[], options: TProjectStrokeOnPointsOptions, openPath = false ): TProjection[] => { @@ -22,7 +22,7 @@ export const projectStrokeOnPoints = ( } points.forEach((A, index) => { - let B: IPoint, C: IPoint; + let B: XY, C: XY; if (index === 0) { C = points[1]; B = openPath ? A : points[points.length - 1]; diff --git a/src/util/misc/vectors.ts b/src/util/misc/vectors.ts index 1f9da0dcf52..9506e82a783 100644 --- a/src/util/misc/vectors.ts +++ b/src/util/misc/vectors.ts @@ -1,4 +1,4 @@ -import { IPoint, Point } from '../../Point'; +import { XY, Point } from '../../Point'; import { TRadian } from '../../typedefs'; const unitVectorX = new Point(1, 0); @@ -19,7 +19,7 @@ export const rotateVector = (vector: Point, radians: TRadian) => * @param {Point} to * @returns {Point} vector */ -export const createVector = (from: IPoint, to: IPoint): Point => +export const createVector = (from: XY, to: XY): Point => new Point(to).subtract(from); /** From e1e81718b985a3a1b200980137f635d1e2bfdfd3 Mon Sep 17 00:00:00 2001 From: ShaMan123 Date: Sat, 25 Mar 2023 10:19:52 +0300 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d28410c3d..81ec6ec995f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [next] +- rename(): `IPoint` => `XY` [#8806](https://github.com/fabricjs/fabric.js/pull/8806) - perf(): optimize `perPixelTargetFind` [#8770](https://github.com/fabricjs/fabric.js/pull/8770) - BREAKING fix(): reflect NUM_FRACTION_DIGITS to SVG path data [#8782] (https://github.com/fabricjs/fabric.js/pull/8782) - fix(IText): layout change regression caused by #8663 (`text` was changed but layout was skipped) [#8711](https://github.com/fabricjs/fabric.js/pull/8711)