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 11.8 kB
{"version":3,"file":"ObservablePoint.mjs","sources":["../../../src/maths/point/ObservablePoint.ts"],"sourcesContent":["import 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 ObservablePoint extends PixiMixins.ObservablePoint { }\n\n/**\n * Observer used to listen for observable point changes.\n * Provides callback mechanism for point value updates.\n * @example\n * ```ts\n * // Basic observer implementation\n * const observer: Observer<ObservablePoint> = {\n * _onUpdate: (point) => {\n * console.log(`Point updated to (${point.x}, ${point.y})`);\n * }\n * };\n *\n * // Create observable point with observer\n * const point = new ObservablePoint(observer, 100, 100);\n *\n * // Observer will be notified on changes\n * point.x = 200; // Logs: Point updated to (200, 100)\n * ```\n * @remarks\n * - Used internally by ObservablePoint\n * - Triggered on x/y changes\n * - Can track multiple points\n * - Useful for change detection\n * @typeParam T - The type of point being observed\n * @see {@link ObservablePoint} The observable point class\n * @see {@link PointLike} For point interface\n * @category maths\n * @standard\n */\nexport interface Observer<T>\n{\n /**\n * Callback to call when the point has updated.\n * Triggered whenever x or y coordinates change.\n * @param point - The point that was updated\n */\n _onUpdate: (point?: T) => void;\n}\n\n/**\n * The ObservablePoint object represents a location in a two-dimensional coordinate system.\n * Triggers a callback when its position changes.\n *\n * The x and y properties represent the position on the horizontal and vertical axes, respectively.\n * @example\n * ```ts\n * // Basic observable point usage\n * const point = new ObservablePoint(\n * { _onUpdate: (p) => console.log(`Updated to (${p.x}, ${p.y})`) },\n * 100, 100\n * );\n *\n * // Update triggers callback\n * point.x = 200; // Logs: Updated to (200, 100)\n * point.y = 300; // Logs: Updated to (200, 300)\n *\n * // Set both coordinates\n * point.set(50, 50); // Logs: Updated to (50, 50)\n * ```\n * @see {@link Point} For non-observable version\n * @see {@link Observer} For observer interface\n * @see {@link PointLike} For point interface\n * @category maths\n * @standard\n */\nexport class ObservablePoint implements PointLike\n{\n /** @ignore */\n public _x: number;\n /** @ignore */\n public _y: number;\n\n /** This object used to call the `onUpdate` callback when the point changes. */\n private readonly _observer: Observer<ObservablePoint>;\n\n /**\n * Creates a new `ObservablePoint`\n * @param observer - Observer to pass to listen for change events.\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(observer: Observer<ObservablePoint>, x?: number, y?: number)\n {\n this._x = x || 0;\n this._y = y || 0;\n\n this._observer = observer;\n }\n\n /**\n * Creates a clone of this point.\n * @example\n * ```ts\n * // Basic cloning\n * const point = new ObservablePoint(observer, 100, 200);\n * const copy = point.clone();\n *\n * // Clone with new observer\n * const newObserver = {\n * _onUpdate: (p) => console.log(`Clone updated: (${p.x}, ${p.y})`)\n * };\n * const watched = point.clone(newObserver);\n *\n * // Verify independence\n * watched.set(300, 400); // Only triggers new observer\n * ```\n * @param observer - Optional observer to pass to the new observable point\n * @returns A copy of this observable point\n * @see {@link ObservablePoint.copyFrom} For copying into existing point\n * @see {@link Observer} For observer interface details\n */\n public clone(observer?: Observer<ObservablePoint>): ObservablePoint\n {\n return new ObservablePoint(observer ?? this._observer, this._x, 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 ObservablePoint(observer);\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 * @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 ObservablePoint.copyFrom} For copying from another point\n * @see {@link ObservablePoint.equals} For comparing positions\n */\n public set(x = 0, y = x): this\n {\n if (this._x !== x || this._y !== y)\n {\n this._x = x;\n this._y = y;\n this._observer._onUpdate(this);\n }\n\n return this;\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 ObservablePoint(observer, 100, 200);\n * const target = new ObservablePoint();\n * target.copyFrom(source);\n *\n * // Copy and chain operations\n * const point = new ObservablePoint()\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 ObservablePoint.copyTo} For copying to another point\n * @see {@link ObservablePoint.clone} For creating new point copy\n */\n public copyFrom(p: PointData): this\n {\n if (this._x !== p.x || this._y !== p.y)\n {\n this._x = p.x;\n this._y = p.y;\n this._observer._onUpdate(this);\n }\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 ObservablePoint(100, 200);\n * const target = new ObservablePoint();\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 ObservablePoint.copyFrom} For copying from another point\n * @see {@link ObservablePoint.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 ObservablePoint(100, 200);\n * const p2 = new ObservablePoint(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 ObservablePoint(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 ObservablePoint.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 // #if _DEBUG\n public toString(): string\n {\n return `[pixi.js/math:ObservablePoint x=${this._x} y=${this._y} scope=${this._observer}]`;\n }\n // #endif\n\n /**\n * Position of the observable point on the x axis.\n * Triggers observer callback when value changes.\n * @example\n * ```ts\n * // Basic x position\n * const point = new ObservablePoint(observer);\n * point.x = 100; // Triggers observer\n *\n * // Use in calculations\n * const width = rightPoint.x - leftPoint.x;\n * ```\n * @default 0\n */\n get x(): number\n {\n return this._x;\n }\n\n set x(value: number)\n {\n if (this._x !== value)\n {\n this._x = value;\n this._observer._onUpdate(this);\n }\n }\n\n /**\n * Position of the observable point on the y axis.\n * Triggers observer callback when value changes.\n * @example\n * ```ts\n * // Basic y position\n * const point = new ObservablePoint(observer);\n * point.y = 200; // Triggers observer\n *\n * // Use in calculations\n * const height = bottomPoint.y - topPoint.y;\n * ```\n * @default 0\n */\n get y(): number\n {\n return this._y;\n }\n\n set y(value: number)\n {\n if (this._y !== value)\n {\n this._y = value;\n this._observer._onUpdate(this);\n }\n }\n}\n"],"names":[],"mappings":";AAwEO,MAAM,eACb,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAeI,WAAA,CAAY,QAAqC,EAAA,CAAA,EAAY,CAC7D,EAAA;AACI,IAAA,IAAA,CAAK,KAAK,CAAK,IAAA,CAAA,CAAA;AACf,IAAA,IAAA,CAAK,KAAK,CAAK,IAAA,CAAA,CAAA;AAEf,IAAA,IAAA,CAAK,SAAY,GAAA,QAAA,CAAA;AAAA,GACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAwBO,MAAM,QACb,EAAA;AACI,IAAO,OAAA,IAAI,gBAAgB,QAAY,IAAA,IAAA,CAAK,WAAW,IAAK,CAAA,EAAA,EAAI,KAAK,EAAE,CAAA,CAAA;AAAA,GAC3E;AAAA;AAAA;AAAA;AA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