UNPKG

fabric

Version:

Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.

1 lines 11.8 kB
{"version":3,"file":"Point.mjs","names":[],"sources":["../../src/Point.ts"],"sourcesContent":["import type { TMat2D, TRadian } from './typedefs';\nimport { cos } from './util/misc/cos';\nimport { sin } from './util/misc/sin';\n\nexport interface XY {\n x: number;\n y: number;\n}\n\n/**\n * Adaptation of work of Kevin Lindsey(kevin@kevlindev.com)\n */\nexport class Point implements XY {\n declare x: number;\n\n declare y: number;\n\n constructor();\n constructor(x: number, y: number);\n constructor(point?: XY);\n constructor(arg0: number | XY = 0, y = 0) {\n if (typeof arg0 === 'object') {\n this.x = arg0.x;\n this.y = arg0.y;\n } else {\n this.x = arg0;\n this.y = y;\n }\n }\n\n /**\n * Adds another point to this one and returns a new one with the sum\n * @param {XY} that\n * @return {Point} new Point instance with added values\n */\n add(that: XY): Point {\n return new Point(this.x + that.x, this.y + that.y);\n }\n\n /**\n * Adds another point to this one\n * @param {XY} that\n * @return {Point} thisArg\n * @deprecated\n */\n addEquals(that: XY): Point {\n this.x += that.x;\n this.y += that.y;\n return this;\n }\n\n /**\n * Adds value to this point and returns a new one\n * @param {Number} scalar\n * @return {Point} new Point with added value\n */\n scalarAdd(scalar: number): Point {\n return new Point(this.x + scalar, this.y + scalar);\n }\n\n /**\n * Adds value to this point\n * @param {Number} scalar\n * @return {Point} thisArg\n * @deprecated\n */\n scalarAddEquals(scalar: number): Point {\n this.x += scalar;\n this.y += scalar;\n return this;\n }\n\n /**\n * Subtracts another point from this point and returns a new one\n * @param {XY} that\n * @return {Point} new Point object with subtracted values\n */\n subtract(that: XY): Point {\n return new Point(this.x - that.x, this.y - that.y);\n }\n\n /**\n * Subtracts another point from this point\n * @param {XY} that\n * @return {Point} thisArg\n * @deprecated\n */\n subtractEquals(that: XY): Point {\n this.x -= that.x;\n this.y -= that.y;\n return this;\n }\n\n /**\n * Subtracts value from this point and returns a new one\n * @param {Number} scalar\n * @return {Point}\n */\n scalarSubtract(scalar: number): Point {\n return new Point(this.x - scalar, this.y - scalar);\n }\n\n /**\n * Subtracts value from this point\n * @param {Number} scalar\n * @return {Point} thisArg\n * @deprecated\n */\n scalarSubtractEquals(scalar: number): Point {\n this.x -= scalar;\n this.y -= scalar;\n return this;\n }\n\n /**\n * Multiplies this point by another value and returns a new one\n * @param {XY} that\n * @return {Point}\n */\n multiply(that: XY): Point {\n return new Point(this.x * that.x, this.y * that.y);\n }\n\n /**\n * Multiplies this point by a value and returns a new one\n * @param {Number} scalar\n * @return {Point}\n */\n scalarMultiply(scalar: number): Point {\n return new Point(this.x * scalar, this.y * scalar);\n }\n\n /**\n * Multiplies this point by a value\n * @param {Number} scalar\n * @return {Point} thisArg\n * @deprecated\n */\n scalarMultiplyEquals(scalar: number): Point {\n this.x *= scalar;\n this.y *= scalar;\n return this;\n }\n\n /**\n * Divides this point by another and returns a new one\n * @param {XY} that\n * @return {Point}\n */\n divide(that: XY): Point {\n return new Point(this.x / that.x, this.y / that.y);\n }\n\n /**\n * Divides this point by a value and returns a new one\n * @param {Number} scalar\n * @return {Point}\n */\n scalarDivide(scalar: number): Point {\n return new Point(this.x / scalar, this.y / scalar);\n }\n\n /**\n * Divides this point by a value\n * @param {Number} scalar\n * @return {Point} thisArg\n * @deprecated\n */\n scalarDivideEquals(scalar: number): Point {\n this.x /= scalar;\n this.y /= scalar;\n return this;\n }\n\n /**\n * Returns true if this point is equal to another one\n * @param {XY} that\n * @return {Boolean}\n */\n eq(that: XY): boolean {\n return this.x === that.x && this.y === that.y;\n }\n\n /**\n * Returns true if this point is less than another one\n * @param {XY} that\n * @return {Boolean}\n */\n lt(that: XY): boolean {\n return this.x < that.x && this.y < that.y;\n }\n\n /**\n * Returns true if this point is less than or equal to another one\n * @param {XY} that\n * @return {Boolean}\n */\n lte(that: XY): boolean {\n return this.x <= that.x && this.y <= that.y;\n }\n\n /**\n\n * Returns true if this point is greater another one\n * @param {XY} that\n * @return {Boolean}\n */\n gt(that: XY): boolean {\n return this.x > that.x && this.y > that.y;\n }\n\n /**\n * Returns true if this point is greater than or equal to another one\n * @param {XY} that\n * @return {Boolean}\n */\n gte(that: XY): boolean {\n return this.x >= that.x && this.y >= that.y;\n }\n\n /**\n * Returns new point which is the result of linear interpolation with this one and another one\n * @param {XY} that\n * @param {Number} t , position of interpolation, between 0 and 1 default 0.5\n * @return {Point}\n */\n lerp(that: XY, t = 0.5): Point {\n t = Math.max(Math.min(1, t), 0);\n return new Point(\n this.x + (that.x - this.x) * t,\n this.y + (that.y - this.y) * t,\n );\n }\n\n /**\n * Returns distance from this point and another one\n * @param {XY} that\n * @return {Number}\n */\n distanceFrom(that: XY): number {\n const dx = this.x - that.x,\n dy = this.y - that.y;\n return Math.sqrt(dx * dx + dy * dy);\n }\n\n /**\n * Returns the point between this point and another one\n * @param {XY} that\n * @return {Point}\n */\n midPointFrom(that: XY): Point {\n return this.lerp(that);\n }\n\n /**\n * Returns a new point which is the min of this and another one\n * @param {XY} that\n * @return {Point}\n */\n min(that: XY): Point {\n return new Point(Math.min(this.x, that.x), Math.min(this.y, that.y));\n }\n\n /**\n * Returns a new point which is the max of this and another one\n * @param {XY} that\n * @return {Point}\n */\n max(that: XY): Point {\n return new Point(Math.max(this.x, that.x), Math.max(this.y, that.y));\n }\n\n /**\n * Returns string representation of this point\n * @return {String}\n */\n toString(): string {\n return `${this.x},${this.y}`;\n }\n\n /**\n * Sets x/y of this point\n * @param {Number} x\n * @param {Number} y\n */\n setXY(x: number, y: number) {\n this.x = x;\n this.y = y;\n return this;\n }\n\n /**\n * Sets x of this point\n * @param {Number} x\n */\n setX(x: number) {\n this.x = x;\n return this;\n }\n\n /**\n * Sets y of this point\n * @param {Number} y\n */\n setY(y: number) {\n this.y = y;\n return this;\n }\n\n /**\n * Sets x/y of this point from another point\n * @param {XY} that\n */\n setFromPoint(that: XY) {\n this.x = that.x;\n this.y = that.y;\n return this;\n }\n\n /**\n * Swaps x/y of this point and another point\n * @param {XY} that\n */\n swap(that: XY) {\n const x = this.x,\n y = this.y;\n this.x = that.x;\n this.y = that.y;\n that.x = x;\n that.y = y;\n }\n\n /**\n * return a cloned instance of the point\n * @return {Point}\n */\n clone(): Point {\n return new Point(this.x, this.y);\n }\n\n /**\n * Rotates `point` around `origin` with `radians`\n * @param {XY} origin The origin of the rotation\n * @param {TRadian} radians The radians of the angle for the rotation\n * @return {Point} The new rotated point\n */\n rotate(radians: TRadian, origin: XY = ZERO): Point {\n // TODO benchmark and verify the add and subtract how much cost\n // and then in case early return if no origin is passed\n const sinus = sin(radians),\n cosinus = cos(radians);\n const p = this.subtract(origin);\n const rotated = new Point(\n p.x * cosinus - p.y * sinus,\n p.x * sinus + p.y * cosinus,\n );\n return rotated.add(origin);\n }\n\n /**\n * Apply transform t to point p\n * @param {TMat2D} t The transform\n * @param {Boolean} [ignoreOffset] Indicates that the offset should not be applied\n * @return {Point} The transformed point\n */\n transform(t: TMat2D, ignoreOffset = false): Point {\n return new Point(\n t[0] * this.x + t[2] * this.y + (ignoreOffset ? 0 : t[4]),\n t[1] * this.x + t[3] * this.y + (ignoreOffset ? 0 : t[5]),\n );\n }\n}\n\nexport const ZERO = new Point(0, 0);\n"],"mappings":";;;;;;AAYA,IAAa,QAAb,MAAa,MAAoB;CAQ/B,YAAY,OAAoB,GAAG,IAAI,GAAG;AACxC,MAAI,OAAO,SAAS,UAAU;AAC5B,QAAK,IAAI,KAAK;AACd,QAAK,IAAI,KAAK;SACT;AACL,QAAK,IAAI;AACT,QAAK,IAAI;;;;;;;;CASb,IAAI,MAAiB;AACnB,SAAO,IAAI,MAAM,KAAK,IAAI,KAAK,GAAG,KAAK,IAAI,KAAK,EAAE;;;;;;;;CASpD,UAAU,MAAiB;AACzB,OAAK,KAAK,KAAK;AACf,OAAK,KAAK,KAAK;AACf,SAAO;;;;;;;CAQT,UAAU,QAAuB;AAC/B,SAAO,IAAI,MAAM,KAAK,IAAI,QAAQ,KAAK,IAAI,OAAO;;;;;;;;CASpD,gBA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