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.6 kB
{"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, 1); // Inside\n * const centerStroke = circle.strokeContains(150, 100, 4, 0.5); // Centered\n * const outerStroke = circle.strokeContains(150, 100, 4, 0); // 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,MAAA,CACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EA2EI,YAAY,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,CAAA,EAAG,SAAS,CAAA,EACnC;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,IAAA,GAAwB,QAAA;AASpC,IAAA,IAAA,CAAK,CAAA,GAAI,CAAA;AACT,IAAA,IAAA,CAAK,CAAA,GAAI,CAAA;AACT,IAAA,IAAA,CAAK,MAAA,GAAS,MAAA;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAsBO,KAAA,GACP;AACI,IAAA,OAAO,IAAI,MAAA,CAAO,IAAA,CAAK,GAAG,IAAA,CAAK,CAAA,EAAG,KAAK,MAAM,CAAA;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