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 67.3 kB
{"version":3,"file":"GraphicsContext.mjs","sources":["../../../../src/scene/graphics/shared/GraphicsContext.ts"],"sourcesContent":["/* eslint-disable max-len */\nimport EventEmitter from 'eventemitter3';\nimport { Color, type ColorSource } from '../../../color/Color';\nimport { Matrix } from '../../../maths/matrix/Matrix';\nimport { Point } from '../../../maths/point/Point';\nimport { Texture } from '../../../rendering/renderers/shared/texture/Texture';\nimport { uid } from '../../../utils/data/uid';\nimport { deprecation, v8_0_0 } from '../../../utils/logging/deprecation';\nimport { Bounds } from '../../container/bounds/Bounds';\nimport { GraphicsPath } from './path/GraphicsPath';\nimport { SVGParser } from './svg/SVGParser';\nimport { toFillStyle, toStrokeStyle } from './utils/convertFillInputToFillStyle';\n\nimport type { PointData } from '../../../maths/point/PointData';\nimport type { Shader } from '../../../rendering/renderers/shared/shader/Shader';\nimport type { TextureDestroyOptions, TypeOrBool } from '../../container/destroyTypes';\nimport type { ConvertedFillStyle, ConvertedStrokeStyle, FillInput, StrokeInput } from './FillTypes';\nimport type { RoundedPoint } from './path/roundShape';\n\nconst tmpPoint = new Point();\n\n/**\n * The mode for batching graphics instructions.\n *\n * It can be:\n * - 'auto': Automatically determines whether to batch based on the number of instructions.\n * - 'batch': Forces batching of all instructions.\n * - 'no-batch': Disables batching, processing each instruction individually.\n * @category scene\n * @advanced\n */\nexport type BatchMode = 'auto' | 'batch' | 'no-batch';\n\n/** @internal */\nexport interface FillInstruction\n{\n action: 'fill' | 'cut'\n data: { style: ConvertedFillStyle, path: GraphicsPath, hole?: GraphicsPath }\n}\n\n/** @internal */\nexport interface StrokeInstruction\n{\n action: 'stroke'\n data: { style: ConvertedStrokeStyle, path: GraphicsPath, hole?: GraphicsPath }\n}\n\n/** @internal */\nexport interface TextureInstruction\n{\n action: 'texture'\n data: {\n image: Texture,\n\n dx: number\n dy: number\n\n dw: number\n dh: number\n\n transform: Matrix\n alpha: number\n style: number,\n }\n}\n\n/** @internal */\nexport type GraphicsInstructions = FillInstruction | StrokeInstruction | TextureInstruction;\n\nconst tempMatrix = new Matrix();\n\n/**\n * The GraphicsContext class allows for the creation of lightweight objects that contain instructions for drawing shapes and paths.\n * It is used internally by the Graphics class to draw shapes and paths, and can be used directly and shared between Graphics objects,\n *\n * This sharing of a `GraphicsContext` means that the intensive task of converting graphics instructions into GPU-ready geometry is done once, and the results are reused,\n * much like sprites reusing textures.\n * @category scene\n * @standard\n */\nexport class GraphicsContext extends EventEmitter<{\n update: GraphicsContext\n destroy: GraphicsContext\n}>\n{\n /** The default fill style to use when none is provided. */\n public static defaultFillStyle: ConvertedFillStyle = {\n /** The color to use for the fill. */\n color: 0xffffff,\n /** The alpha value to use for the fill. */\n alpha: 1,\n /** The texture to use for the fill. */\n texture: Texture.WHITE,\n /** The matrix to apply. */\n matrix: null,\n /** The fill pattern to use. */\n fill: null,\n /** Whether coordinates are 'global' or 'local' */\n textureSpace: 'local',\n };\n\n /** The default stroke style to use when none is provided. */\n public static defaultStrokeStyle: ConvertedStrokeStyle = {\n /** The width of the stroke. */\n width: 1,\n /** The color to use for the stroke. */\n color: 0xffffff,\n /** The alpha value to use for the stroke. */\n alpha: 1,\n /** The alignment of the stroke. */\n alignment: 0.5,\n /** The miter limit to use. */\n miterLimit: 10,\n /** The line cap style to use. */\n cap: 'butt',\n /** The line join style to use. */\n join: 'miter',\n /** The texture to use for the fill. */\n texture: Texture.WHITE,\n /** The matrix to apply. */\n matrix: null,\n /** The fill pattern to use. */\n fill: null,\n /** Whether coordinates are 'global' or 'local' */\n textureSpace: 'local',\n /** If the stroke is a pixel line. */\n pixelLine: false,\n };\n\n /**\n * unique id for this graphics context\n * @internal\n */\n public readonly uid: number = uid('graphicsContext');\n /** @internal */\n public dirty = true;\n /** The batch mode for this graphics context. It can be 'auto', 'batch', or 'no-batch'. */\n public batchMode: BatchMode = 'auto';\n /** @internal */\n public instructions: GraphicsInstructions[] = [];\n /**\n * Custom shader to apply to the graphics when rendering.\n * @advanced\n */\n public customShader?: Shader;\n\n private _activePath: GraphicsPath = new GraphicsPath();\n private _transform: Matrix = new Matrix();\n\n private _fillStyle: ConvertedFillStyle = { ...GraphicsContext.defaultFillStyle };\n private _strokeStyle: ConvertedStrokeStyle = { ...GraphicsContext.defaultStrokeStyle };\n private _stateStack: { fillStyle: ConvertedFillStyle; strokeStyle: ConvertedStrokeStyle, transform: Matrix }[] = [];\n\n private _tick = 0;\n\n private _bounds = new Bounds();\n private _boundsDirty = true;\n\n /**\n * Creates a new GraphicsContext object that is a clone of this instance, copying all properties,\n * including the current drawing state, transformations, styles, and instructions.\n * @returns A new GraphicsContext instance with the same properties and state as this one.\n */\n public clone(): GraphicsContext\n {\n const clone = new GraphicsContext();\n\n clone.batchMode = this.batchMode;\n clone.instructions = this.instructions.slice();\n clone._activePath = this._activePath.clone();\n clone._transform = this._transform.clone();\n clone._fillStyle = { ...this._fillStyle };\n clone._strokeStyle = { ...this._strokeStyle };\n clone._stateStack = this._stateStack.slice();\n clone._bounds = this._bounds.clone();\n clone._boundsDirty = true;\n\n return clone;\n }\n\n /**\n * The current fill style of the graphics context. This can be a color, gradient, pattern, or a more complex style defined by a FillStyle object.\n */\n get fillStyle(): ConvertedFillStyle\n {\n return this._fillStyle;\n }\n\n set fillStyle(value: FillInput)\n {\n this._fillStyle = toFillStyle(value, GraphicsContext.defaultFillStyle);\n }\n\n /**\n * The current stroke style of the graphics context. Similar to fill styles, stroke styles can encompass colors, gradients, patterns, or more detailed configurations via a StrokeStyle object.\n */\n get strokeStyle(): ConvertedStrokeStyle\n {\n return this._strokeStyle;\n }\n\n set strokeStyle(value: FillInput)\n {\n this._strokeStyle = toStrokeStyle(value, GraphicsContext.defaultStrokeStyle);\n }\n\n /**\n * Sets the current fill style of the graphics context. The fill style can be a color, gradient,\n * pattern, or a more complex style defined by a FillStyle object.\n * @param style - The fill style to apply. This can be a simple color, a gradient or pattern object,\n * or a FillStyle or ConvertedFillStyle object.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public setFillStyle(style: FillInput): this\n {\n this._fillStyle = toFillStyle(style, GraphicsContext.defaultFillStyle);\n\n return this;\n }\n\n /**\n * Sets the current stroke style of the graphics context. Similar to fill styles, stroke styles can\n * encompass colors, gradients, patterns, or more detailed configurations via a StrokeStyle object.\n * @param style - The stroke style to apply. Can be defined as a color, a gradient or pattern,\n * or a StrokeStyle or ConvertedStrokeStyle object.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public setStrokeStyle(style: StrokeInput): this\n {\n this._strokeStyle = toFillStyle(style, GraphicsContext.defaultStrokeStyle) as ConvertedStrokeStyle;\n\n return this;\n }\n\n /**\n * Adds a texture to the graphics context. This method supports multiple overloads for specifying the texture.\n * If only a texture is provided, it uses the texture's width and height for drawing.\n * @param texture - The Texture object to use.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public texture(texture: Texture): this;\n /**\n * Adds a texture to the graphics context. This method supports multiple overloads for specifying the texture,\n * tint, and dimensions. If only a texture is provided, it uses the texture's width and height for drawing.\n * Additional parameters allow for specifying a tint color, and custom dimensions for the texture drawing area.\n * @param texture - The Texture object to use.\n * @param tint - (Optional) A ColorSource to tint the texture. If not provided, defaults to white (0xFFFFFF).\n * @param dx - (Optional) The x-coordinate in the destination canvas at which to place the top-left corner of\n * the source image.\n * @param dy - (Optional) The y-coordinate in the destination canvas at which to place the top-left corner of\n * the source image.\n * @param dw - (Optional) The width of the rectangle within the source image to draw onto the destination canvas.\n * If not provided, uses the texture's frame width.\n * @param dh - (Optional) The height of the rectangle within the source image to draw onto the destination canvas.\n * If not provided, uses the texture's frame height.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public texture(texture: Texture, tint?: ColorSource, dx?: number, dy?: number, dw?: number, dh?: number): this;\n public texture(texture: Texture, tint?: ColorSource, dx?: number, dy?: number, dw?: number, dh?: number): this\n {\n this.instructions.push({\n action: 'texture',\n data: {\n image: texture,\n\n dx: dx || 0,\n dy: dy || 0,\n\n dw: dw || texture.frame.width,\n dh: dh || texture.frame.height,\n\n transform: this._transform.clone(),\n alpha: this._fillStyle.alpha,\n style: tint ? Color.shared.setValue(tint).toNumber() : 0xFFFFFF,\n }\n });\n\n this.onUpdate();\n\n return this;\n }\n\n /**\n * Resets the current path. Any previous path and its commands are discarded and a new path is\n * started. This is typically called before beginning a new shape or series of drawing commands.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public beginPath(): this\n {\n this._activePath = new GraphicsPath();\n\n return this;\n }\n\n /**\n * Fills the current or given path with the current fill style. This method can optionally take\n * a color and alpha for a simple fill, or a more complex FillInput object for advanced fills.\n * @param style - (Optional) The style to fill the path with. Can be a color, gradient, pattern, or a complex style object. If omitted, uses the current fill style.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public fill(style?: FillInput): this;\n /** @deprecated 8.0.0 */\n public fill(color: ColorSource, alpha: number): this;\n public fill(style?: FillInput, alpha?: number): this\n {\n let path: GraphicsPath;\n\n const lastInstruction = this.instructions[this.instructions.length - 1];\n\n if (this._tick === 0 && lastInstruction && lastInstruction.action === 'stroke')\n {\n path = lastInstruction.data.path;\n }\n else\n {\n path = this._activePath.clone();\n }\n\n if (!path) return this;\n\n // eslint-disable-next-line no-eq-null, eqeqeq\n if (style != null)\n {\n if (alpha !== undefined && typeof style === 'number')\n {\n // #if _DEBUG\n deprecation(v8_0_0, 'GraphicsContext.fill(color, alpha) is deprecated, use GraphicsContext.fill({ color, alpha }) instead');\n // #endif\n\n style = { color: style, alpha };\n }\n this._fillStyle = toFillStyle(style, GraphicsContext.defaultFillStyle);\n }\n\n // TODO not a fan of the clone!!\n this.instructions.push({\n action: 'fill',\n // TODO copy fill style!\n data: { style: this.fillStyle, path }\n });\n\n this.onUpdate();\n\n this._initNextPathLocation();\n this._tick = 0;\n\n return this;\n }\n\n private _initNextPathLocation()\n {\n // Reset the _activePath with the last point of the current path\n const { x, y } = this._activePath.getLastPoint(Point.shared);\n\n this._activePath.clear();\n this._activePath.moveTo(x, y);\n }\n\n /**\n * Strokes the current path with the current stroke style. This method can take an optional\n * FillInput parameter to define the stroke's appearance, including its color, width, and other properties.\n * @param style - (Optional) The stroke style to apply. Can be defined as a simple color or a more complex style object. If omitted, uses the current stroke style.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public stroke(style?: StrokeInput): this\n {\n let path: GraphicsPath;\n\n const lastInstruction = this.instructions[this.instructions.length - 1];\n\n if (this._tick === 0 && lastInstruction && lastInstruction.action === 'fill')\n {\n path = lastInstruction.data.path;\n }\n else\n {\n path = this._activePath.clone();\n }\n\n if (!path) return this;\n\n // eslint-disable-next-line no-eq-null, eqeqeq\n if (style != null)\n {\n this._strokeStyle = toStrokeStyle(style, GraphicsContext.defaultStrokeStyle);\n }\n\n // TODO not a fan of the clone!!\n this.instructions.push({\n action: 'stroke',\n // TODO copy fill style!\n data: { style: this.strokeStyle, path }\n });\n\n this.onUpdate();\n\n this._initNextPathLocation();\n this._tick = 0;\n\n return this;\n }\n\n /**\n * Applies a cutout to the last drawn shape. This is used to create holes or complex shapes by\n * subtracting a path from the previously drawn path. If a hole is not completely in a shape, it will\n * fail to cut correctly!\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public cut(): this\n {\n for (let i = 0; i < 2; i++)\n {\n const lastInstruction = this.instructions[this.instructions.length - 1 - i];\n\n const holePath = this._activePath.clone();\n\n if (lastInstruction)\n {\n if (lastInstruction.action === 'stroke' || lastInstruction.action === 'fill')\n {\n if (lastInstruction.data.hole)\n {\n lastInstruction.data.hole.addPath(holePath);\n }\n else\n {\n lastInstruction.data.hole = holePath;\n break;\n }\n }\n }\n }\n\n this._initNextPathLocation();\n\n return this;\n }\n\n /**\n * Adds an arc to the current path, which is centered at (x, y) with the specified radius,\n * starting and ending angles, and direction.\n * @param x - The x-coordinate of the arc's center.\n * @param y - The y-coordinate of the arc's center.\n * @param radius - The arc's radius.\n * @param startAngle - The starting angle, in radians.\n * @param endAngle - The ending angle, in radians.\n * @param counterclockwise - (Optional) Specifies whether the arc is drawn counterclockwise (true) or clockwise (false). Defaults to false.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise?: boolean): this\n {\n this._tick++;\n\n const t = this._transform;\n\n this._activePath.arc(\n (t.a * x) + (t.c * y) + t.tx,\n (t.b * x) + (t.d * y) + t.ty,\n radius,\n startAngle,\n endAngle,\n counterclockwise,\n );\n\n return this;\n }\n\n /**\n * Adds an arc to the current path with the given control points and radius, connected to the previous point\n * by a straight line if necessary.\n * @param x1 - The x-coordinate of the first control point.\n * @param y1 - The y-coordinate of the first control point.\n * @param x2 - The x-coordinate of the second control point.\n * @param y2 - The y-coordinate of the second control point.\n * @param radius - The arc's radius.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public arcTo(x1: number, y1: number, x2: number, y2: number, radius: number): this\n {\n this._tick++;\n\n const t = this._transform;\n\n this._activePath.arcTo(\n (t.a * x1) + (t.c * y1) + t.tx,\n (t.b * x1) + (t.d * y1) + t.ty,\n (t.a * x2) + (t.c * y2) + t.tx,\n (t.b * x2) + (t.d * y2) + t.ty,\n radius,\n );\n\n return this;\n }\n\n /**\n * Adds an SVG-style arc to the path, allowing for elliptical arcs based on the SVG spec.\n * @param rx - The x-radius of the ellipse.\n * @param ry - The y-radius of the ellipse.\n * @param xAxisRotation - The rotation of the ellipse's x-axis relative\n * to the x-axis of the coordinate system, in degrees.\n * @param largeArcFlag - Determines if the arc should be greater than or less than 180 degrees.\n * @param sweepFlag - Determines if the arc should be swept in a positive angle direction.\n * @param x - The x-coordinate of the arc's end point.\n * @param y - The y-coordinate of the arc's end point.\n * @returns The instance of the current object for chaining.\n */\n public arcToSvg(\n rx: number, ry: number,\n xAxisRotation: number,\n largeArcFlag: number,\n sweepFlag: number,\n x: number, y: number\n ): this\n {\n this._tick++;\n\n const t = this._transform;\n\n this._activePath.arcToSvg(\n rx, ry,\n xAxisRotation, // should we rotate this with transform??\n largeArcFlag,\n sweepFlag,\n (t.a * x) + (t.c * y) + t.tx,\n (t.b * x) + (t.d * y) + t.ty,\n );\n\n return this;\n }\n\n /**\n * Adds a cubic Bezier curve to the path.\n * It requires three points: the first two are control points and the third one is the end point.\n * The starting point is the last point in the current path.\n * @param cp1x - The x-coordinate of the first control point.\n * @param cp1y - The y-coordinate of the first control point.\n * @param cp2x - The x-coordinate of the second control point.\n * @param cp2y - The y-coordinate of the second control point.\n * @param x - The x-coordinate of the end point.\n * @param y - The y-coordinate of the end point.\n * @param smoothness - Optional parameter to adjust the smoothness of the curve.\n * @returns The instance of the current object for chaining.\n */\n public bezierCurveTo(cp1x: number, cp1y: number, cp2x: number, cp2y: number, x: number, y: number, smoothness?: number): this\n {\n this._tick++;\n\n // TODO optimize for no transform\n const t = this._transform;\n\n this._activePath.bezierCurveTo(\n (t.a * cp1x) + (t.c * cp1y) + t.tx,\n (t.b * cp1x) + (t.d * cp1y) + t.ty,\n (t.a * cp2x) + (t.c * cp2y) + t.tx,\n (t.b * cp2x) + (t.d * cp2y) + t.ty,\n (t.a * x) + (t.c * y) + t.tx,\n (t.b * x) + (t.d * y) + t.ty,\n smoothness,\n );\n\n return this;\n }\n\n /**\n * Closes the current path by drawing a straight line back to the start.\n * If the shape is already closed or there are no points in the path, this method does nothing.\n * @returns The instance of the current object for chaining.\n */\n public closePath(): this\n {\n this._tick++;\n\n this._activePath?.closePath();\n\n return this;\n }\n\n /**\n * Draws an ellipse at the specified location and with the given x and y radii.\n * An optional transformation can be applied, allowing for rotation, scaling, and translation.\n * @param x - The x-coordinate of the center of the ellipse.\n * @param y - The y-coordinate of the center of the ellipse.\n * @param radiusX - The horizontal radius of the ellipse.\n * @param radiusY - The vertical radius of the ellipse.\n * @returns The instance of the current object for chaining.\n */\n public ellipse(x: number, y: number, radiusX: number, radiusY: number): this\n {\n this._tick++;\n\n this._activePath.ellipse(x, y, radiusX, radiusY, this._transform.clone());\n\n return this;\n }\n\n /**\n * Draws a circle shape. This method adds a new circle path to the current drawing.\n * @param x - The x-coordinate of the center of the circle.\n * @param y - The y-coordinate of the center of the circle.\n * @param radius - The radius of the circle.\n * @returns The instance of the current object for chaining.\n */\n public circle(x: number, y: number, radius: number): this\n {\n this._tick++;\n\n this._activePath.circle(x, y, radius, this._transform.clone());\n\n return this;\n }\n\n /**\n * Adds another `GraphicsPath` to this path, optionally applying a transformation.\n * @param path - The `GraphicsPath` to add.\n * @returns The instance of the current object for chaining.\n */\n public path(path: GraphicsPath): this\n {\n this._tick++;\n\n this._activePath.addPath(path, this._transform.clone());\n\n return this;\n }\n\n /**\n * Connects the current point to a new point with a straight line. This method updates the current path.\n * @param x - The x-coordinate of the new point to connect to.\n * @param y - The y-coordinate of the new point to connect to.\n * @returns The instance of the current object for chaining.\n */\n public lineTo(x: number, y: number): this\n {\n this._tick++;\n\n const t = this._transform;\n\n this._activePath.lineTo(\n (t.a * x) + (t.c * y) + t.tx,\n (t.b * x) + (t.d * y) + t.ty\n );\n\n return this;\n }\n\n /**\n * Sets the starting point for a new sub-path. Any subsequent drawing commands are considered part of this path.\n * @param x - The x-coordinate for the starting point.\n * @param y - The y-coordinate for the starting point.\n * @returns The instance of the current object for chaining.\n */\n public moveTo(x: number, y: number): this\n {\n this._tick++;\n\n const t = this._transform;\n\n const instructions = this._activePath.instructions;\n\n const transformedX = (t.a * x) + (t.c * y) + t.tx;\n const transformedY = (t.b * x) + (t.d * y) + t.ty;\n\n if (instructions.length === 1 && instructions[0].action === 'moveTo')\n {\n instructions[0].data[0] = transformedX;\n instructions[0].data[1] = transformedY;\n\n return this;\n }\n this._activePath.moveTo(\n transformedX,\n transformedY\n );\n\n return this;\n }\n\n /**\n * Adds a quadratic curve to the path. It requires two points: the control point and the end point.\n * The starting point is the last point in the current path.\n * @param cpx - The x-coordinate of the control point.\n * @param cpy - The y-coordinate of the control point.\n * @param x - The x-coordinate of the end point.\n * @param y - The y-coordinate of the end point.\n * @param smoothness - Optional parameter to adjust the smoothness of the curve.\n * @returns The instance of the current object for chaining.\n */\n public quadraticCurveTo(cpx: number, cpy: number, x: number, y: number, smoothness?: number): this\n {\n this._tick++;\n\n const t = this._transform;\n\n this._activePath.quadraticCurveTo(\n (t.a * cpx) + (t.c * cpy) + t.tx,\n (t.b * cpx) + (t.d * cpy) + t.ty,\n (t.a * x) + (t.c * y) + t.tx,\n (t.b * x) + (t.d * y) + t.ty,\n smoothness,\n );\n\n return this;\n }\n\n /**\n * Draws a rectangle shape. This method adds a new rectangle path to the current drawing.\n * @param x - The x-coordinate of the top-left corner of the rectangle.\n * @param y - The y-coordinate of the top-left corner of the rectangle.\n * @param w - The width of the rectangle.\n * @param h - The height of the rectangle.\n * @returns The instance of the current object for chaining.\n */\n public rect(x: number, y: number, w: number, h: number): this\n {\n this._tick++;\n\n this._activePath.rect(x, y, w, h, this._transform.clone());\n\n return this;\n }\n\n /**\n * Draws a rectangle with rounded corners.\n * The corner radius can be specified to determine how rounded the corners should be.\n * An optional transformation can be applied, which allows for rotation, scaling, and translation of the rectangle.\n * @param x - The x-coordinate of the top-left corner of the rectangle.\n * @param y - The y-coordinate of the top-left corner of the rectangle.\n * @param w - The width of the rectangle.\n * @param h - The height of the rectangle.\n * @param radius - The radius of the rectangle's corners. If not specified, corners will be sharp.\n * @returns The instance of the current object for chaining.\n */\n public roundRect(x: number, y: number, w: number, h: number, radius?: number): this\n {\n this._tick++;\n\n this._activePath.roundRect(x, y, w, h, radius, this._transform.clone());\n\n return this;\n }\n\n /**\n * Draws a polygon shape by specifying a sequence of points. This method allows for the creation of complex polygons,\n * which can be both open and closed. An optional transformation can be applied, enabling the polygon to be scaled,\n * rotated, or translated as needed.\n * @param points - An array of numbers, or an array of PointData objects eg [{x,y}, {x,y}, {x,y}]\n * representing the x and y coordinates, of the polygon's vertices, in sequence.\n * @param close - A boolean indicating whether to close the polygon path. True by default.\n */\n public poly(points: number[] | PointData[], close?: boolean): this\n {\n this._tick++;\n\n this._activePath.poly(points, close, this._transform.clone());\n\n return this;\n }\n\n /**\n * Draws a regular polygon with a specified number of sides. All sides and angles are equal.\n * @param x - The x-coordinate of the center of the polygon.\n * @param y - The y-coordinate of the center of the polygon.\n * @param radius - The radius of the circumscribed circle of the polygon.\n * @param sides - The number of sides of the polygon. Must be 3 or more.\n * @param rotation - The rotation angle of the polygon, in radians. Zero by default.\n * @param transform - An optional `Matrix` object to apply a transformation to the polygon.\n * @returns The instance of the current object for chaining.\n */\n public regularPoly(x: number, y: number, radius: number, sides: number, rotation = 0, transform?: Matrix): this\n {\n this._tick++;\n this._activePath.regularPoly(x, y, radius, sides, rotation, transform);\n\n return this;\n }\n\n /**\n * Draws a polygon with rounded corners.\n * Similar to `regularPoly` but with the ability to round the corners of the polygon.\n * @param x - The x-coordinate of the center of the polygon.\n * @param y - The y-coordinate of the center of the polygon.\n * @param radius - The radius of the circumscribed circle of the polygon.\n * @param sides - The number of sides of the polygon. Must be 3 or more.\n * @param corner - The radius of the rounding of the corners.\n * @param rotation - The rotation angle of the polygon, in radians. Zero by default.\n * @returns The instance of the current object for chaining.\n */\n public roundPoly(x: number, y: number, radius: number, sides: number, corner: number, rotation?: number): this\n {\n this._tick++;\n this._activePath.roundPoly(x, y, radius, sides, corner, rotation);\n\n return this;\n }\n\n /**\n * Draws a shape with rounded corners. This function supports custom radius for each corner of the shape.\n * Optionally, corners can be rounded using a quadratic curve instead of an arc, providing a different aesthetic.\n * @param points - An array of `RoundedPoint` representing the corners of the shape to draw.\n * A minimum of 3 points is required.\n * @param radius - The default radius for the corners.\n * This radius is applied to all corners unless overridden in `points`.\n * @param useQuadratic - If set to true, rounded corners are drawn using a quadraticCurve\n * method instead of an arc method. Defaults to false.\n * @param smoothness - Specifies the smoothness of the curve when `useQuadratic` is true.\n * Higher values make the curve smoother.\n * @returns The instance of the current object for chaining.\n */\n public roundShape(points: RoundedPoint[], radius: number, useQuadratic?: boolean, smoothness?: number): this\n {\n this._tick++;\n this._activePath.roundShape(points, radius, useQuadratic, smoothness);\n\n return this;\n }\n\n /**\n * Draw Rectangle with fillet corners. This is much like rounded rectangle\n * however it support negative numbers as well for the corner radius.\n * @param x - Upper left corner of rect\n * @param y - Upper right corner of rect\n * @param width - Width of rect\n * @param height - Height of rect\n * @param fillet - accept negative or positive values\n */\n public filletRect(x: number, y: number, width: number, height: number, fillet: number): this\n {\n this._tick++;\n this._activePath.filletRect(x, y, width, height, fillet);\n\n return this;\n }\n\n /**\n * Draw Rectangle with chamfer corners. These are angled corners.\n * @param x - Upper left corner of rect\n * @param y - Upper right corner of rect\n * @param width - Width of rect\n * @param height - Height of rect\n * @param chamfer - non-zero real number, size of corner cutout\n * @param transform\n */\n public chamferRect(x: number, y: number, width: number, height: number, chamfer: number, transform?: Matrix): this\n {\n this._tick++;\n this._activePath.chamferRect(x, y, width, height, chamfer, transform);\n\n return this;\n }\n\n /**\n * Draws a star shape centered at a specified location. This method allows for the creation\n * of stars with a variable number of points, outer radius, optional inner radius, and rotation.\n * The star is drawn as a closed polygon with alternating outer and inner vertices to create the star's points.\n * An optional transformation can be applied to scale, rotate, or translate the star as needed.\n * @param x - The x-coordinate of the center of the star.\n * @param y - The y-coordinate of the center of the star.\n * @param points - The number of points of the star.\n * @param radius - The outer radius of the star (distance from the center to the outer points).\n * @param innerRadius - Optional. The inner radius of the star\n * (distance from the center to the inner points between the outer points).\n * If not provided, defaults to half of the `radius`.\n * @param rotation - Optional. The rotation of the star in radians, where 0 is aligned with the y-axis.\n * Defaults to 0, meaning one point is directly upward.\n * @returns The instance of the current object for chaining further drawing commands.\n */\n public star(x: number, y: number, points: number, radius: number, innerRadius = 0, rotation = 0): this\n {\n this._tick++;\n\n this._activePath.star(x, y, points, radius, innerRadius, rotation, this._transform.clone());\n\n return this;\n }\n\n /**\n * Parses and renders an SVG string into the graphics context. This allows for complex shapes and paths\n * defined in SVG format to be drawn within the graphics context.\n * @param svg - The SVG string to be parsed and rendered.\n */\n public svg(svg: string): this\n {\n this._tick++;\n\n SVGParser(svg, this);\n\n return this;\n }\n\n /**\n * Restores the most recently saved graphics state by popping the top of the graphics state stack.\n * This includes transformations, fill styles, and stroke styles.\n */\n public restore(): this\n {\n const state = this._stateStack.pop();\n\n if (state)\n {\n this._transform = state.transform;\n this._fillStyle = state.fillStyle;\n this._strokeStyle = state.strokeStyle;\n }\n\n return this;\n }\n\n /** Saves the current graphics state, including transformations, fill styles, and stroke styles, onto a stack. */\n public save(): this\n {\n this._stateStack.push({\n transform: this._transform.clone(),\n fillStyle: { ...this._fillStyle },\n strokeStyle: { ...this._strokeStyle },\n });\n\n return this;\n }\n\n /**\n * Returns the current transformation matrix of the graphics context.\n * @returns The current transformation matrix.\n */\n public getTransform(): Matrix\n {\n return this._transform;\n }\n\n /**\n * Resets the current transformation matrix to the identity matrix, effectively removing any transformations (rotation, scaling, translation) previously applied.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public resetTransform(): this\n {\n this._transform.identity();\n\n return this;\n }\n\n /**\n * Applies a rotation transformation to the graphics context around the current origin.\n * @param angle - The angle of rotation in radians.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public rotate(angle: number): this\n {\n this._transform.rotate(angle);\n\n return this;\n }\n\n /**\n * Applies a scaling transformation to the graphics context, scaling drawings by x horizontally and by y vertically.\n * @param x - The scale factor in the horizontal direction.\n * @param y - (Optional) The scale factor in the vertical direction. If not specified, the x value is used for both directions.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public scale(x: number, y: number = x): this\n {\n this._transform.scale(x, y);\n\n return this;\n }\n\n /**\n * Sets the current transformation matrix of the graphics context to the specified matrix or values.\n * This replaces the current transformation matrix.\n * @param transform - The matrix to set as the current transformation matrix.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public setTransform(transform: Matrix): this;\n /**\n * Sets the current transformation matrix of the graphics context to the specified matrix or values.\n * This replaces the current transformation matrix.\n * @param a - The value for the a property of the matrix, or a Matrix object to use directly.\n * @param b - The value for the b property of the matrix.\n * @param c - The value for the c property of the matrix.\n * @param d - The value for the d property of the matrix.\n * @param dx - The value for the tx (translate x) property of the matrix.\n * @param dy - The value for the ty (translate y) property of the matrix.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public setTransform(a: number, b: number, c: number, d: number, dx: number, dy: number): this;\n public setTransform(a: number | Matrix, b?: number, c?: number, d?: number, dx?: number, dy?: number): this\n {\n if (a instanceof Matrix)\n {\n this._transform.set(a.a, a.b, a.c, a.d, a.tx, a.ty);\n\n return this;\n }\n\n this._transform.set(a, b, c, d, dx, dy);\n\n return this;\n }\n\n /**\n * Applies the specified transformation matrix to the current graphics context by multiplying\n * the current matrix with the specified matrix.\n * @param transform - The matrix to apply to the current transformation.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public transform(transform: Matrix): this;\n /**\n * Applies the specified transformation matrix to the current graphics context by multiplying\n * the current matrix with the specified matrix.\n * @param a - The value for the a property of the matrix, or a Matrix object to use directly.\n * @param b - The value for the b property of the matrix.\n * @param c - The value for the c property of the matrix.\n * @param d - The value for the d property of the matrix.\n * @param dx - The value for the tx (translate x) property of the matrix.\n * @param dy - The value for the ty (translate y) property of the matrix.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public transform(a: number, b: number, c: number, d: number, dx: number, dy: number): this;\n public transform(a: number | Matrix, b?: number, c?: number, d?: number, dx?: number, dy?: number): this\n {\n if (a instanceof Matrix)\n {\n this._transform.append(a);\n\n return this;\n }\n\n tempMatrix.set(a, b, c, d, dx, dy);\n this._transform.append(tempMatrix);\n\n return this;\n }\n\n /**\n * Applies a translation transformation to the graphics context, moving the origin by the specified amounts.\n * @param x - The amount to translate in the horizontal direction.\n * @param y - (Optional) The amount to translate in the vertical direction. If not specified, the x value is used for both directions.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public translate(x: number, y: number = x): this\n {\n this._transform.translate(x, y);\n\n return this;\n }\n\n /**\n * Clears all drawing commands from the graphics context, effectively resetting it. This includes clearing the path,\n * and optionally resetting transformations to the identity matrix.\n * @returns The instance of the current GraphicsContext for method chaining.\n */\n public clear(): this\n {\n this._activePath.clear();\n this.instructions.length = 0;\n this.resetTransform();\n\n this.onUpdate();\n\n return this;\n }\n\n protected onUpdate(): void\n {\n if (this.dirty) return;\n\n this.emit('update', this, 0x10);\n this.dirty = true;\n this._boundsDirty = true;\n }\n\n /** The bounds of the graphic shape. */\n get bounds(): Bounds\n {\n if (!this._boundsDirty) return this._bounds;\n\n // TODO switch to idy dirty with tick..\n const bounds = this._bounds;\n\n bounds.clear();\n\n for (let i = 0; i < this.instructions.length; i++)\n {\n const instruction = this.instructions[i];\n const action = instruction.action;\n\n if (action === 'fill')\n {\n const data = instruction.data as FillInstruction['data'];\n\n bounds.addBounds(data.path.bounds);\n }\n else if (action === 'texture')\n {\n const data = instruction.data as TextureInstruction['data'];\n\n bounds.addFrame(data.dx, data.dy, data.dx + data.dw, data.dy + data.dh, data.transform);\n }\n if (action === 'stroke')\n {\n const data = instruction.data as StrokeInstruction['data'];\n\n const alignment = data.style.alignment;\n\n const outerPadding = (data.style.width * (1 - alignment));\n\n const _bounds = data.path.bounds;\n\n bounds.addFrame(\n _bounds.minX - outerPadding,\n _bounds.minY - outerPadding,\n _bounds.maxX + outerPadding,\n _bounds.maxY + outerPadding\n );\n }\n }\n\n return bounds;\n }\n\n /**\n * Check to see if a point is contained within this geometry.\n * @param point - Point to check if it's contained.\n * @returns {boolean} `true` if the point is contained within geometry.\n */\n public containsPoint(point: PointData): boolean\n {\n // early out if the bounding box is not hit\n if (!this.bounds.containsPoint(point.x, point.y)) return false;\n\n const instructions = this.instructions;\n let hasHit = false;\n\n for (let k = 0; k < instructions.length; k++)\n {\n const instruction = instructions[k];\n\n const data = instruction.data as FillInstruction['data'];\n const path = data.path;\n\n if (!instruction.action || !path) continue;\n\n const style = data.style;\n const shapes = path.shapePath.shapePrimitives;\n\n for (let i = 0; i < shapes.length; i++)\n {\n const shape = shapes[i].shape;\n\n if (!style || !shape) continue;\n\n const transform = shapes[i].transform;\n\n const transformedPoint = transform ? transform.applyInverse(point, tmpPoint) : point;\n\n if (instruction.action === 'fill')\n {\n hasHit = shape.contains(transformedPoint.x, transformedPoint.y);\n }\n else\n {\n const strokeStyle = (style as ConvertedStrokeStyle);\n\n hasHit = shape.strokeContains(transformedPoint.x, transformedPoint.y, strokeStyle.width, strokeStyle.alignment);\n }\n\n const holes = data.hole;\n\n if (holes)\n {\n const holeShapes = holes.shapePath?.shapePrimitives;\n\n if (holeShapes)\n {\n for (let j = 0; j < holeShapes.length; j++)\n {\n if (holeShapes[j].shape.contains(transformedPoint.x, transformedPoint.y))\n {\n hasHit = false;\n }\n }\n }\n }\n\n if (hasHit)\n {\n return true;\n }\n }\n }\n\n return hasHit;\n }\n\n /**\n * Destroys the GraphicsData object.\n * @param options - Options parameter. A boolean will act as if all options\n * have been set to that value\n * @example\n * context.destroy();\n * context.destroy(true);\n * context.destroy({ texture: true, textureSource: true });\n */\n public destroy(options: TypeOrBool<TextureDestroyOptions> = false): void\n {\n this._stateStack.length = 0;\n this._transform = null;\n\n this.emit('destroy', this);\n this.removeAllListeners();\n\n const destroyTexture = typeof options === 'boolean' ? options : options?.texture;\n\n if (destroyTexture)\n {\n const destroyTextureSource = typeof options === 'boolean' ? options : options?.textureSource;\n\n if (this._fillStyle.texture)\n {\n this._fillStyle.texture.destroy(destroyTextureSource);\n }\n\n if (this._strokeStyle.texture)\n {\n this._strokeStyle.texture.destroy(destroyTextureSource);\n }\n }\n\n this._fillStyle = null;\n this._strokeStyle = null;\n\n this.instructions = null;\n this._activePath = null;\n this._bounds = null;\n this._stateStack = null;\n this.customShader = null;\n this._transform = null;\n }\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;AAmBA,MAAM,QAAA,GAAW,IAAI,KAAM,EAAA,CAAA;AAkD3B,MAAM,UAAA,GAAa,IAAI,MAAO,EAAA,CAAA;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