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
Source Map (JSON)
{"version":3,"file":"Circle.mjs","sources":["../../../src/maths/shapes/Circle.ts"],"sourcesContent":["import { Rectangle } from './Rectangle';\n\nimport type { SHAPE_PRIMITIVE } from '../misc/const';\nimport type { ShapePrimitive } from './ShapePrimitive';\n\n/**\n * The Circle object represents a circle shape in a two-dimensional coordinate system.\n * Used for drawing graphics and specifying hit areas for containers.\n * @example\n * ```ts\n * // Basic circle creation\n * const circle = new Circle(100, 100, 50);\n *\n * // Use as hit area\n * container.hitArea = new Circle(0, 0, 100);\n *\n * // Check point containment\n * const isInside = circle.contains(mouseX, mouseY);\n *\n * // Get bounding box\n * const bounds = circle.getBounds();\n * ```\n * @remarks\n * - Defined by center (x,y) and radius\n * - Supports point containment tests\n * - Can check stroke intersections\n * @see {@link Rectangle} For rectangular shapes\n * @category maths\n * @standard\n */\nexport class Circle implements ShapePrimitive\n{\n /**\n * The X coordinate of the center of this circle\n * @example\n * ```ts\n * // Basic x position\n * const circle = new Circle();\n * circle.x = 100;\n *\n * // Center circle on point\n * circle.x = point.x;\n * ```\n * @default 0\n */\n public x: number;\n\n /**\n * The Y coordinate of the center of this circle\n * @example\n * ```ts\n * // Basic y position\n * const circle = new Circle();\n * circle.y = 200;\n *\n * // Center circle on point\n * circle.y = point.y;\n * ```\n * @default 0\n */\n public y: number;\n\n /**\n * The radius of the circle\n * @example\n * ```ts\n * // Basic radius setting\n * const circle = new Circle(100, 100);\n * circle.radius = 50;\n *\n * // Calculate area\n * const area = Math.PI * circle.radius * circle.radius;\n * ```\n * @default 0\n */\n public radius: number;\n\n /**\n * The type of the object, mainly used to avoid `instanceof` checks.\n * @example\n * ```ts\n * // Check shape type\n * const shape = new Circle(0, 0, 50);\n * console.log(shape.type); // 'circle'\n *\n * // Use in type guards\n * if (shape.type === 'circle') {\n * console.log(shape.radius);\n * }\n * ```\n * @remarks\n * - Used for shape type checking\n * - More efficient than instanceof\n * - Read-only property\n * @readonly\n * @default 'circle'\n * @see {@link SHAPE_PRIMITIVE} For all shape types\n * @see {@link ShapePrimitive} For shape interface\n */\n public readonly type: SHAPE_PRIMITIVE = 'circle';\n\n /**\n * @param x - The X coordinate of the center of this circle\n * @param y - The Y coordinate of the center of this circle\n * @param radius - The radius of the circle\n */\n constructor(x = 0, y = 0, radius = 0)\n {\n this.x = x;\n this.y = y;\n this.radius = radius;\n }\n\n /**\n * Creates a clone of this Circle instance.\n * @example\n * ```ts\n * // Basic circle cloning\n * const original = new Circle(100, 100, 50);\n * const copy = original.clone();\n *\n * // Clone and modify\n * const modified = original.clone();\n * modified.radius = 75;\n *\n * // Verify independence\n * console.log(original.radius); // 50\n * console.log(modified.radius); // 75\n * ```\n * @returns A copy of the Circle\n * @see {@link Circle.copyFrom} For copying into existing circle\n * @see {@link Circle.copyTo} For copying to another circle\n */\n public clone(): Circle\n {\n return new Circle(this.x, this.y, this.radius);\n }\n\n /**\n * Checks whether the x and y coordinates given are contained within this circle.\n *\n * Uses the distance formula to determine if a point is inside the circle's radius.\n *\n * Commonly used for hit testing in PixiJS events and graphics.\n * @example\n * ```ts\n * // Basic containment check\n * const circle = new Circle(100, 100, 50);\n * const isInside = circle.contains(120, 120);\n *\n * // Check mouse position\n * const circle = new Circle(0, 0, 100);\n * container.hitArea = circle;\n * container.on('pointermove', (e) => {\n * // only called if pointer is within circle\n * });\n * ```\n * @param x - The X coordinate of the point to test\n * @param y - The Y coordinate of the point to test\n * @returns Whether the x/y coordinates are within this Circle\n * @see {@link Circle.strokeContains} For checking stroke intersection\n * @see {@link Circle.getBounds} For getting bounding box\n */\n public contains(x: number, y: number): boolean\n {\n if (this.radius <= 0) return false;\n\n const r2 = this.radius * this.radius;\n let dx = (this.x - x);\n let dy = (this.y - y);\n\n dx *= dx;\n dy *= dy;\n\n return (dx + dy <= r2);\n }\n\n /**\n * Checks whether the x and y coordinates given are contained within this circle including the stroke.\n * @example\n * ```ts\n * // Basic stroke check\n * const circle = new Circle(100, 100, 50);\n * const isOnStroke = circle.strokeContains(150, 100, 4); // 4px line width\n *\n * // Check with different alignments\n * const innerStroke = circle.strokeContains(150, 100, 4, 0); // Inside\n * const centerStroke = circle.strokeContains(150, 100, 4, 0.5); // Centered\n * const outerStroke = circle.strokeContains(150, 100, 4, 1); // Outside\n * ```\n * @param x - The X coordinate of the point to test\n * @param y - The Y coordinate of the point to test\n * @param width - The width of the line to check\n * @param alignment - The alignment of the stroke, 0.5 by default\n * @returns Whether the x/y coordinates are within this Circle's stroke\n * @see {@link Circle.contains} For checking fill containment\n * @see {@link Circle.getBounds} For getting stroke bounds\n */\n public strokeContains(x: number, y: number, width: number, alignment: number = 0.5): boolean\n {\n if (this.radius === 0) return false;\n\n const dx = (this.x - x);\n const dy = (this.y - y);\n const radius = this.radius;\n const outerWidth = (1 - alignment) * width;\n const distance = Math.sqrt((dx * dx) + (dy * dy));\n\n return (distance <= radius + outerWidth && distance > radius - (width - outerWidth));\n }\n\n /**\n * Returns the framing rectangle of the circle as a Rectangle object.\n * @example\n * ```ts\n * // Basic bounds calculation\n * const circle = new Circle(100, 100, 50);\n * const bounds = circle.getBounds();\n * // bounds: x=50, y=50, width=100, height=100\n *\n * // Reuse existing rectangle\n * const rect = new Rectangle();\n * circle.getBounds(rect);\n * ```\n * @param out - Optional Rectangle object to store the result\n * @returns The framing rectangle\n * @see {@link Rectangle} For rectangle properties\n * @see {@link Circle.contains} For point containment\n */\n public getBounds(out?: Rectangle): Rectangle\n {\n out ||= new Rectangle();\n\n out.x = this.x - this.radius;\n out.y = this.y - this.radius;\n out.width = this.radius * 2;\n out.height = this.radius * 2;\n\n return out;\n }\n\n /**\n * Copies another circle to this one.\n * @example\n * ```ts\n * // Basic copying\n * const source = new Circle(100, 100, 50);\n * const target = new Circle();\n * target.copyFrom(source);\n * ```\n * @param circle - The circle to copy from\n * @returns Returns itself\n * @see {@link Circle.copyTo} For copying to another circle\n * @see {@link Circle.clone} For creating new circle copy\n */\n public copyFrom(circle: Circle): this\n {\n this.x = circle.x;\n this.y = circle.y;\n this.radius = circle.radius;\n\n return this;\n }\n\n /**\n * Copies this circle to another one.\n * @example\n * ```ts\n * // Basic copying\n * const source = new Circle(100, 100, 50);\n * const target = new Circle();\n * source.copyTo(target);\n * ```\n * @param circle - The circle to copy to\n * @returns Returns given parameter\n * @see {@link Circle.copyFrom} For copying from another circle\n * @see {@link Circle.clone} For creating new circle copy\n */\n public copyTo(circle: Circle): Circle\n {\n circle.copyFrom(this);\n\n return circle;\n }\n\n // #if _DEBUG\n public toString(): string\n {\n return `[pixi.js/math:Circle x=${this.x} y=${this.y} radius=${this.radius}]`;\n }\n // #endif\n}\n"],"names":[],"mappings":";;;AA8BO,MAAM,MACb,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA2EI,YAAY,CAAI,GAAA,CAAA,EAAG,CAAI,GAAA,CAAA,EAAG,SAAS,CACnC,EAAA;AARA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAAA,IAAA,CAAgB,IAAwB,GAAA,QAAA,CAAA;AASpC,IAAA,IAAA,CAAK,CAAI,GAAA,CAAA,CAAA;AACT,IAAA,IAAA,CAAK,CAAI,GAAA,CAAA,CAAA;AACT,IAAA,IAAA,CAAK,MAAS,GAAA,MAAA,CAAA;AAAA,GAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBO,KACP,GAAA;AACI,IAAA,OAAO,IAAI,MAAO,CAAA,IAAA,CAAK,GAAG,IAAK,CAAA,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