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

rename(): IPoint => XY #8806

Merged
merged 3 commits into from
Mar 27, 2023
Merged
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]

- rename(): `IPoint` => `XY` [#8806](https://github.com/fabricjs/fabric.js/pull/8806)
- ci(): use sandbox apps in issue template, use the current branch when deploying an app, minors [#8803](https://github.com/fabricjs/fabric.js/pull/8803)
- 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)
Expand Down
84 changes: 42 additions & 42 deletions src/Point.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ 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;
}

/**
* 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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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,
Expand All @@ -240,39 +240,39 @@ 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);
}

/**
* 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));
}

Expand Down Expand Up @@ -318,20 +318,20 @@ 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;
}

/**
* 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;
Expand All @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions src/shapes/IText/ITextClickBehavior.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<string, never>;
private declare __lastPointer: XY | Record<string, never>;
private declare __newClickTime: number;

protected draggableTextDelegate: DraggableTextDelegate;
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -293,7 +293,7 @@ export abstract class ITextClickBehavior<
* @private
*/
_getNewSelectionStartFromOffset(
mouseOffset: IPoint,
mouseOffset: XY,
prevWidth: number,
width: number,
index: number,
Expand Down
6 changes: 3 additions & 3 deletions src/shapes/Polyline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -21,7 +21,7 @@ export class Polyline extends FabricObject {
* @type Array
* @default
*/
declare points: IPoint[];
declare points: XY[];

/**
* WARNING: Feature in progress
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/util/misc/boundingBoxFromPoints.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/util/misc/matrix.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/util/misc/projectStroke/StrokeLineCapProjections.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
Expand Down
Loading