UNPKG

pixi.js

Version:

<p align="center"> <a href="https://pixijs.com" target="_blank" rel="noopener noreferrer"> <img height="150" src="https://files.pixijs.download/branding/pixijs-logo-transparent-dark.svg?v=1" alt="PixiJS logo"> </a> </p> <br/> <p align="center">

1 lines 9.7 kB
{"version":3,"file":"Point.mjs","sources":["../../../src/maths/point/Point.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-use-before-define */\nimport type { PointData } from './PointData';\nimport type { PointLike } from './PointLike';\n\n// eslint-disable-next-line max-len\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type, requireExport/require-export-jsdoc, requireMemberAPI/require-member-api-doc\nexport interface Point extends PixiMixins.Point { }\n\n/**\n * The Point object represents a location in a two-dimensional coordinate system, where `x` represents\n * the position on the horizontal axis and `y` represents the position on the vertical axis.\n *\n * Many Pixi functions accept the `PointData` type as an alternative to `Point`,\n * which only requires `x` and `y` properties.\n * @example\n * ```ts\n * // Basic point creation\n * const point = new Point(100, 200);\n *\n * // Using with transformations\n * const matrix = new Matrix();\n * matrix.translate(50, 50).apply(point);\n *\n * // Point arithmetic\n * const start = new Point(0, 0);\n * const end = new Point(100, 100);\n * const middle = new Point(\n * (start.x + end.x) / 2,\n * (start.y + end.y) / 2\n * );\n * ```\n * @see {@link PointData} For basic x,y interface\n * @see {@link PointLike} For point manipulation interface\n * @see {@link ObservablePoint} For observable version\n * @category maths\n * @standard\n */\nexport class Point implements PointLike\n{\n /**\n * Position of the point on the x axis\n * @example\n * ```ts\n * // Set x position\n * const point = new Point();\n * point.x = 100;\n *\n * // Use in calculations\n * const width = rightPoint.x - leftPoint.x;\n * ```\n */\n public x = 0;\n /**\n * Position of the point on the y axis\n * @example\n * ```ts\n * // Set y position\n * const point = new Point();\n * point.y = 200;\n *\n * // Use in calculations\n * const height = bottomPoint.y - topPoint.y;\n * ```\n */\n public y = 0;\n\n /**\n * Creates a new `Point`\n * @param {number} [x=0] - position of the point on the x axis\n * @param {number} [y=0] - position of the point on the y axis\n */\n constructor(x = 0, y = 0)\n {\n this.x = x;\n this.y = y;\n }\n\n /**\n * Creates a clone of this point, which is a new instance with the same `x` and `y` values.\n * @example\n * ```ts\n * // Basic point cloning\n * const original = new Point(100, 200);\n * const copy = original.clone();\n *\n * // Clone and modify\n * const modified = original.clone();\n * modified.set(300, 400);\n *\n * // Verify independence\n * console.log(original); // Point(100, 200)\n * console.log(modified); // Point(300, 400)\n * ```\n * @remarks\n * - Creates new Point instance\n * - Deep copies x and y values\n * - Independent from original\n * - Useful for preserving values\n * @returns A clone of this point\n * @see {@link Point.copyFrom} For copying into existing point\n * @see {@link Point.copyTo} For copying to existing point\n */\n public clone(): Point\n {\n return new Point(this.x, this.y);\n }\n\n /**\n * Copies x and y from the given point into this point.\n * @example\n * ```ts\n * // Basic copying\n * const source = new Point(100, 200);\n * const target = new Point();\n * target.copyFrom(source);\n *\n * // Copy and chain operations\n * const point = new Point()\n * .copyFrom(source)\n * .set(x + 50, y + 50);\n *\n * // Copy from any PointData\n * const data = { x: 10, y: 20 };\n * point.copyFrom(data);\n * ```\n * @param p - The point to copy from\n * @returns The point instance itself\n * @see {@link Point.copyTo} For copying to another point\n * @see {@link Point.clone} For creating new point copy\n */\n public copyFrom(p: PointData): this\n {\n this.set(p.x, p.y);\n\n return this;\n }\n\n /**\n * Copies this point's x and y into the given point.\n * @example\n * ```ts\n * // Basic copying\n * const source = new Point(100, 200);\n * const target = new Point();\n * source.copyTo(target);\n * ```\n * @param p - The point to copy to. Can be any type that is or extends `PointLike`\n * @returns The point (`p`) with values updated\n * @see {@link Point.copyFrom} For copying from another point\n * @see {@link Point.clone} For creating new point copy\n */\n public copyTo<T extends PointLike>(p: T): T\n {\n p.set(this.x, this.y);\n\n return p;\n }\n\n /**\n * Checks if another point is equal to this point.\n *\n * Compares x and y values using strict equality.\n * @example\n * ```ts\n * // Basic equality check\n * const p1 = new Point(100, 200);\n * const p2 = new Point(100, 200);\n * console.log(p1.equals(p2)); // true\n *\n * // Compare with PointData\n * const data = { x: 100, y: 200 };\n * console.log(p1.equals(data)); // true\n *\n * // Check different points\n * const p3 = new Point(200, 300);\n * console.log(p1.equals(p3)); // false\n * ```\n * @param p - The point to check\n * @returns `true` if both `x` and `y` are equal\n * @see {@link Point.copyFrom} For making points equal\n * @see {@link PointData} For point data interface\n */\n public equals(p: PointData): boolean\n {\n return (p.x === this.x) && (p.y === this.y);\n }\n\n /**\n * Sets the point to a new x and y position.\n *\n * If y is omitted, both x and y will be set to x.\n * @example\n * ```ts\n * // Basic position setting\n * const point = new Point();\n * point.set(100, 200);\n *\n * // Set both x and y to same value\n * point.set(50); // x=50, y=50\n *\n * // Chain with other operations\n * point\n * .set(10, 20)\n * .copyTo(otherPoint);\n * ```\n * @param x - Position on the x axis\n * @param y - Position on the y axis, defaults to x\n * @returns The point instance itself\n * @see {@link Point.copyFrom} For copying from another point\n * @see {@link Point.equals} For comparing positions\n */\n public set(x = 0, y: number = x): this\n {\n this.x = x;\n this.y = y;\n\n return this;\n }\n\n // #if _DEBUG\n public toString(): string\n {\n return `[pixi.js/math:Point x=${this.x} y=${this.y}]`;\n }\n // #endif\n\n /**\n * A static Point object with `x` and `y` values of `0`.\n *\n * This shared instance is reset to zero values when accessed.\n *\n * > [!IMPORTANT] This point is shared and temporary. Do not store references to it.\n * @example\n * ```ts\n * // Use for temporary calculations\n * const tempPoint = Point.shared;\n * tempPoint.set(100, 200);\n * matrix.apply(tempPoint);\n *\n * // Will be reset to (0,0) on next access\n * const fresh = Point.shared; // x=0, y=0\n * ```\n * @readonly\n * @returns A fresh zeroed point for temporary use\n * @see {@link Point.constructor} For creating new points\n * @see {@link PointData} For basic point interface\n */\n static get shared(): Point\n {\n tempPoint.x = 0;\n tempPoint.y = 0;\n\n return tempPoint;\n }\n}\n\nconst tempPoint = new Point();\n"],"names":[],"mappings":";AAqCO,MAAM,KACb,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiCI,WAAY,CAAA,CAAA,GAAI,CAAG,EAAA,CAAA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