2D graphics rendering library for drawing primitive shapes, paths, and complex vector graphics with fills, strokes, and advanced geometric operations
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Low-level geometry management for advanced use cases including custom vertex manipulation, batching optimization, and geometry reuse across multiple graphics objects.
Core class for managing geometry data including vertices, indices, colors, and batching optimization for high-performance rendering.
class GraphicsGeometry extends BatchGeometry {
/** Creates new GraphicsGeometry with empty data */
constructor();
/** Current geometry bounds (readonly) */
readonly bounds: Bounds;
/** Array of points to draw (2 numbers per point: x, y) */
points: number[];
/** Collection of colors corresponding to vertices */
colors: number[];
/** UV coordinates collection for texture mapping */
uvs: number[];
/** Vertex indices for triangle construction */
indices: number[];
/** Reference to texture IDs for multi-texture rendering */
textureIds: number[];
/** Collection of drawn shapes as GraphicsData objects */
graphicsData: GraphicsData[];
/** List of draw calls generated from batches */
drawCalls: BatchDrawCall[];
/** Intermediate batch format for rendering optimization */
batches: BatchPart[];
/** Flag indicating if batches need regeneration (-1 = dirty) */
batchDirty: number;
/** Whether geometry can be batched with other geometries */
batchable: boolean;
/** Float32 UV coordinates array for GPU */
uvsFloat32: Float32Array;
/** Indices array (Uint16 or Uint32 based on vertex count) */
indicesUint16: Uint16Array | Uint32Array;
}Configuration constants for geometry optimization and batching behavior.
class GraphicsGeometry {
/** Maximum points for batchable objects (default: 100) */
static BATCHABLE_SIZE: number;
}Properties controlling geometry behavior and precision settings.
interface GraphicsGeometry {
/** Minimal distance between different points (default: 1e-4) */
closePointEps: number;
/** Padding to add to geometry bounds (default: 0) */
boundsPadding: number;
}Methods for managing geometry state and drawing operations.
/**
* Clears all graphics data and resets styles to defaults
*/
clear(): GraphicsGeometry;
/**
* Draws any shape with optional styling and transformation
* @param shape - Shape object to draw (Circle, Ellipse, Polygon, Rectangle, RoundedRectangle)
* @param fillStyle - Optional fill style configuration
* @param lineStyle - Optional line style configuration
* @param matrix - Optional transformation matrix
*/
drawShape(shape: IShape, fillStyle?: FillStyle, lineStyle?: LineStyle, matrix?: Matrix): GraphicsGeometry;
/**
* Draws a hole in the last drawn shape
* @param shape - Shape object defining the hole
* @param matrix - Optional transformation matrix for the hole
*/
drawHole(shape: IShape, matrix?: Matrix): GraphicsGeometry;
/**
* Generates intermediate batch data for rendering optimization
* Call this after modifying geometry data directly
*/
updateBatches(): void;
/**
* Tests if a point is within the geometry bounds
* @param point - Point to test for containment
*/
containsPoint(point: IPointData): boolean;
/**
* Destroys the geometry and cleans up resources
*/
destroy(): void;Class representing individual drawn shapes with their styling and transformation information.
class GraphicsData {
/** The shape object (Circle|Ellipse|Polygon|Rectangle|RoundedRectangle) */
shape: IShape;
/** Line style configuration for this shape */
lineStyle: LineStyle;
/** Fill style configuration for this shape */
fillStyle: FillStyle;
/** Transform matrix applied to this shape */
matrix: Matrix;
/** Shape type from SHAPES enum */
type: SHAPES;
/** Collection of calculated points for the shape */
points: number[];
/** Collection of holes within this shape */
holes: GraphicsData[];
/**
* Creates GraphicsData for a shape
* @param shape - The shape object to store
* @param fillStyle - Fill style configuration (optional)
* @param lineStyle - Line style configuration (optional)
* @param matrix - Transformation matrix (optional)
*/
constructor(shape: IShape, fillStyle?: FillStyle, lineStyle?: LineStyle, matrix?: Matrix);
/** Creates a copy of the GraphicsData with same values */
clone(): GraphicsData;
/** Destroys the graphics data and cleans up resources */
destroy(): void;
}Classes and interfaces for managing rendering batches and draw calls.
/**
* Structure for interim batch objects during geometry processing
*/
class BatchPart {
/** Creates new BatchPart for batching operations */
constructor();
/**
* Begin a new batch part
* @param style - Style configuration for the batch
* @param startIndex - Starting vertex index
* @param attribStart - Starting attribute index
*/
begin(style: any, startIndex: number, attribStart: number): void;
/**
* End the current batch part
* @param endIndex - Ending vertex index
* @param endAttrib - Ending attribute index
*/
end(endIndex: number, endAttrib: number): void;
/** Reset the batch part to initial state */
reset(): void;
}
/**
* Batch element computed from Graphics geometry for rendering
*/
interface IGraphicsBatchElement {
/** Vertex position data as Float32Array */
vertexData: Float32Array;
/** Blend mode for rendering this batch */
blendMode: BLEND_MODES;
/** Face indices (Uint16Array or Uint32Array) */
indices: Uint16Array | Uint32Array;
/** UV texture coordinates */
uvs: Float32Array;
/** Local alpha value */
alpha: number;
/** Combined world alpha value */
worldAlpha: number;
/** Batch RGB values array */
_batchRGB: number[];
/** Tint RGB value */
_tintRGB: number;
/** Associated texture */
_texture: Texture;
}Creating and Reusing Geometry:
import { Graphics, GraphicsGeometry, FillStyle, LineStyle } from "@pixi/graphics";
// Create reusable geometry
const sharedGeometry = new GraphicsGeometry();
// Draw shapes directly to geometry
const fillStyle = new FillStyle();
fillStyle.color = 0xff0000;
fillStyle.visible = true;
const lineStyle = new LineStyle();
lineStyle.width = 2;
lineStyle.color = 0x000000;
sharedGeometry.drawShape(
new Rectangle(0, 0, 100, 100),
fillStyle,
lineStyle
);
// Use geometry in multiple Graphics objects
const graphics1 = new Graphics(sharedGeometry);
const graphics2 = new Graphics(sharedGeometry);
// Position them differently
graphics1.position.set(100, 100);
graphics2.position.set(300, 100);Direct Geometry Manipulation:
import { GraphicsGeometry, Circle, FillStyle } from "@pixi/graphics";
const geometry = new GraphicsGeometry();
// Manually add geometry data
geometry.points = [0, 0, 100, 0, 100, 100, 0, 100]; // Rectangle points
geometry.colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00]; // Vertex colors
geometry.indices = [0, 1, 2, 0, 2, 3]; // Triangle indices
geometry.uvs = [0, 0, 1, 0, 1, 1, 0, 1]; // UV coordinates
// Update batches after manual changes
geometry.updateBatches();
const graphics = new Graphics(geometry);Advanced Batch Management:
import { GraphicsGeometry } from "@pixi/graphics";
const geometry = new GraphicsGeometry();
// Check batching status
console.log(`Geometry is batchable: ${geometry.batchable}`);
console.log(`Current batch count: ${geometry.batches.length}`);
console.log(`Current draw calls: ${geometry.drawCalls.length}`);
// Force batch regeneration
geometry.batchDirty = -1;
geometry.updateBatches();
// Check geometry bounds
console.log(`Bounds: ${geometry.bounds.x}, ${geometry.bounds.y}, ${geometry.bounds.width}, ${geometry.bounds.height}`);Working with GraphicsData:
import { GraphicsData, Circle, Rectangle, FillStyle, LineStyle, Matrix } from "@pixi/graphics";
// Create styled shapes
const fillStyle = new FillStyle();
fillStyle.color = 0x00ff00;
fillStyle.alpha = 0.7;
const lineStyle = new LineStyle();
lineStyle.width = 3;
lineStyle.color = 0xff0000;
const transform = new Matrix().translate(50, 50).scale(1.2, 1.2);
// Create GraphicsData objects
const circleData = new GraphicsData(
new Circle(0, 0, 30),
fillStyle,
lineStyle,
transform
);
const rectData = new GraphicsData(
new Rectangle(100, 0, 80, 60),
fillStyle.clone(),
lineStyle.clone()
);
// Add to geometry
const geometry = new GraphicsGeometry();
geometry.graphicsData.push(circleData, rectData);
geometry.updateBatches();Performance Optimization:
import { GraphicsGeometry } from "@pixi/graphics";
// Configure for performance
const geometry = new GraphicsGeometry();
// Adjust precision for better performance (larger epsilon = fewer points)
geometry.closePointEps = 1e-3;
// Add padding to bounds to avoid frequent recalculation
geometry.boundsPadding = 5;
// Check if geometry can be batched efficiently
if (geometry.points.length / 2 <= GraphicsGeometry.BATCHABLE_SIZE) {
console.log("Geometry is suitable for batching");
} else {
console.log("Geometry may need splitting for optimal performance");
}Geometry Analysis:
import { GraphicsGeometry } from "@pixi/graphics";
const geometry = new GraphicsGeometry();
// ... add shapes to geometry ...
// Analyze geometry properties
console.log(`Total points: ${geometry.points.length / 2}`);
console.log(`Total triangles: ${geometry.indices.length / 3}`);
console.log(`Total shapes: ${geometry.graphicsData.length}`);
console.log(`Bounds: ${JSON.stringify(geometry.bounds)}`);
// Check containment
const testPoint = { x: 50, y: 50 };
if (geometry.containsPoint(testPoint)) {
console.log("Point is inside geometry");
}
// Access UV and color data
console.log(`UV coordinates: ${geometry.uvs.length / 2} pairs`);
console.log(`Colors: ${geometry.colors.length} values`);Install with Tessl CLI
npx tessl i tessl/npm-pixi--graphics