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
Collection of utility functions and classes for shape building, curve calculations, and rendering optimizations. Includes mathematical utilities for arc, bezier, and quadratic curve operations.
Main namespace containing all utility functions, classes, and constants for graphics operations.
declare const graphicsUtils: {
/** Polygon shape builder implementing IShapeBuildCommand */
buildPoly: IShapeBuildCommand;
/** Circle and ellipse shape builder implementing IShapeBuildCommand */
buildCircle: IShapeBuildCommand;
/** Rectangle shape builder implementing IShapeBuildCommand */
buildRectangle: IShapeBuildCommand;
/** Rounded rectangle shape builder implementing IShapeBuildCommand */
buildRoundedRectangle: IShapeBuildCommand;
/** Line builder function for creating line geometry */
buildLine: (graphicsData: GraphicsData, graphicsGeometry: GraphicsGeometry) => void;
/** Arc curve utilities class */
ArcUtils: typeof ArcUtils;
/** Bezier curve utilities class */
BezierUtils: typeof BezierUtils;
/** Quadratic curve utilities class */
QuadraticUtils: typeof QuadraticUtils;
/** Batch part class for batching operations */
BatchPart: typeof BatchPart;
/** Map of fill commands by shape type */
FILL_COMMANDS: Record<SHAPES, IShapeBuildCommand>;
/** Pool of unused BatchPart objects for reuse */
BATCH_POOL: Array<BatchPart>;
/** Pool of unused BatchDrawCall objects for reuse */
DRAW_CALL_POOL: Array<BatchDrawCall>;
};Interface defining the contract for shape building operations used by all shape builders.
/**
* Interface for shape building commands that construct geometry from GraphicsData
*/
interface IShapeBuildCommand {
/**
* Build the shape geometry from graphics data
* @param graphicsData - GraphicsData containing shape and style information
*/
build(graphicsData: GraphicsData): void;
/**
* Triangulate the shape for rendering
* @param graphicsData - GraphicsData containing shape information
* @param target - Target GraphicsGeometry to receive triangulated data
*/
triangulate(graphicsData: GraphicsData, target: GraphicsGeometry): void;
}Individual builder functions for different geometric primitives, accessed through the graphicsUtils namespace.
/**
* Builds polygon geometry from GraphicsData
* Handles complex polygons with holes and self-intersections
*/
declare const buildPoly: IShapeBuildCommand;
/**
* Builds circle and ellipse geometry from GraphicsData
* Optimized for different circle segment counts based on radius
*/
declare const buildCircle: IShapeBuildCommand;
/**
* Builds rectangle geometry from GraphicsData
* Handles basic rectangles with optional line styling
*/
declare const buildRectangle: IShapeBuildCommand;
/**
* Builds rounded rectangle geometry from GraphicsData
* Supports different corner radius values and rendering modes
*/
declare const buildRoundedRectangle: IShapeBuildCommand;
/**
* Builds line geometry from GraphicsData
* Handles line caps, joins, and thickness
* @param graphicsData - GraphicsData containing line information
* @param graphicsGeometry - Target GraphicsGeometry to receive line geometry
*/
declare function buildLine(graphicsData: GraphicsData, graphicsGeometry: GraphicsGeometry): void;Pre-allocated object pools for performance optimization during rendering operations.
/**
* Map of fill commands indexed by shape type for fast lookup
*/
declare const FILL_COMMANDS: Record<SHAPES, IShapeBuildCommand>;
/**
* Pool of unused BatchPart objects for memory efficiency
* Reused during batching operations to avoid garbage collection
*/
declare const BATCH_POOL: Array<BatchPart>;
/**
* Pool of unused BatchDrawCall objects for memory efficiency
* Reused during draw call generation to avoid garbage collection
*/
declare const DRAW_CALL_POOL: Array<BatchDrawCall>;Utility classes providing mathematical functions for complex curve operations (already detailed in curves documentation, included here for completeness).
/**
* Arc curve utilities with mathematical functions
*/
class ArcUtils {
static curveTo(x1: number, y1: number, x2: number, y2: number, radius: number, points: number[]): IArcLikeShape;
static arc(startX: number, startY: number, cx: number, cy: number, radius: number, startAngle: number, endAngle: number, anticlockwise: boolean, points: number[]): void;
}
/**
* Bezier curve utilities with mathematical functions
*/
class BezierUtils {
static curveLength(fromX: number, fromY: number, cpX: number, cpY: number, cpX2: number, cpY2: number, toX: number, toY: number): number;
static curveTo(cpX: number, cpY: number, cpX2: number, cpY2: number, toX: number, toY: number, points: number[]): void;
}
/**
* Quadratic curve utilities with mathematical functions
*/
class QuadraticUtils {
static curveLength(fromX: number, fromY: number, cpX: number, cpY: number, toX: number, toY: number): number;
static curveTo(cpX: number, cpY: number, toX: number, toY: number, points: number[]): void;
}Class for managing interim batch objects during geometry processing and rendering optimization.
/**
* Represents a batch part for grouping geometry data during rendering
*/
class BatchPart {
/** Creates new BatchPart for batching operations */
constructor();
/**
* Begin a new batch part with specified parameters
* @param style - Style configuration for the batch
* @param startIndex - Starting vertex index in the geometry
* @param attribStart - Starting attribute index
*/
begin(style: any, startIndex: number, attribStart: number): void;
/**
* End the current batch part with specified parameters
* @param endIndex - Ending vertex index in the geometry
* @param endAttrib - Ending attribute index
*/
end(endIndex: number, endAttrib: number): void;
/** Reset the batch part to initial state for reuse */
reset(): void;
}Using Shape Builders Directly:
import { graphicsUtils, GraphicsData, Rectangle, FillStyle } from "@pixi/graphics";
// Create graphics data for a rectangle
const fillStyle = new FillStyle();
fillStyle.color = 0xff0000;
fillStyle.visible = true;
const rectData = new GraphicsData(new Rectangle(0, 0, 100, 50), fillStyle);
// Use shape builder directly
graphicsUtils.buildRectangle.build(rectData);
// The shape builder has now populated rectData.points with geometry
console.log(`Generated points: ${rectData.points.length}`);Accessing Fill Commands by Shape Type:
import { graphicsUtils, SHAPES, GraphicsData, Circle } from "@pixi/graphics";
// Get builder for specific shape type
const circleBuilder = graphicsUtils.FILL_COMMANDS[SHAPES.CIRCLE];
// Use builder for circle geometry
const circleData = new GraphicsData(new Circle(0, 0, 25));
circleBuilder.build(circleData);Working with Object Pools:
import { graphicsUtils } from "@pixi/graphics";
// Get a BatchPart from the pool (if available) or create new one
let batchPart: BatchPart;
if (graphicsUtils.BATCH_POOL.length > 0) {
batchPart = graphicsUtils.BATCH_POOL.pop()!;
batchPart.reset();
} else {
batchPart = new graphicsUtils.BatchPart();
}
// Use the batch part
batchPart.begin(styleObject, 0, 0);
// ... perform batching operations ...
batchPart.end(100, 400);
// Return to pool for reuse
graphicsUtils.BATCH_POOL.push(batchPart);Custom Shape Building:
import { graphicsUtils, GraphicsData, Polygon, GraphicsGeometry } from "@pixi/graphics";
// Create custom polygon data
const points = [0, 0, 100, 0, 100, 100, 50, 150, 0, 100];
const polygon = new Polygon(points);
const polygonData = new GraphicsData(polygon);
// Build the polygon geometry
graphicsUtils.buildPoly.build(polygonData);
// Triangulate for rendering
const geometry = new GraphicsGeometry();
graphicsUtils.buildPoly.triangulate(polygonData, geometry);
console.log(`Triangulated indices: ${geometry.indices.length}`);Using Line Builder:
import { graphicsUtils, GraphicsData, LineStyle } from "@pixi/graphics";
// Create line data with styling
const lineStyle = new LineStyle();
lineStyle.width = 5;
lineStyle.color = 0x00ff00;
lineStyle.cap = LINE_CAP.ROUND;
// Line data requires points array for the line path
const lineData = new GraphicsData(null, null, lineStyle);
lineData.points = [0, 0, 100, 50, 200, 0, 300, 75]; // Line path points
// Build line geometry
const geometry = new GraphicsGeometry();
graphicsUtils.buildLine(lineData, geometry);Curve Utilities Integration:
import { graphicsUtils } from "@pixi/graphics";
// Use curve utilities directly
const points: number[] = [];
// Calculate bezier curve points
graphicsUtils.BezierUtils.curveTo(50, -25, 100, 25, 150, 0, points);
// Calculate curve length for animation timing
const length = graphicsUtils.BezierUtils.curveLength(0, 0, 50, -25, 100, 25, 150, 0);
console.log(`Curve length: ${length} pixels`);
// Use calculated points in custom shape
const customData = new GraphicsData(null);
customData.points = [0, 0, ...points]; // Start point + curve points
const customGeometry = new GraphicsGeometry();
graphicsUtils.buildLine(customData, customGeometry);Advanced Batching with BatchPart:
import { graphicsUtils } from "@pixi/graphics";
// Create multiple batch parts for complex geometry
const batch1 = new graphicsUtils.BatchPart();
const batch2 = new graphicsUtils.BatchPart();
// Configure first batch (filled shapes)
batch1.begin({
fillStyle: { color: 0xff0000, alpha: 1 },
texture: null
}, 0, 0);
batch1.end(100, 300);
// Configure second batch (stroked shapes)
batch2.begin({
lineStyle: { width: 2, color: 0x000000 },
texture: null
}, 100, 300);
batch2.end(200, 600);
// Batches can now be used for optimized rendering
console.log(`Batch 1 vertex range: 0-100`);
console.log(`Batch 2 vertex range: 100-200`);Shape Builder Performance Comparison:
import { graphicsUtils, GraphicsData, Rectangle, Circle } from "@pixi/graphics";
// Performance test different builders
const iterations = 1000;
// Rectangle building performance
const rectData = new GraphicsData(new Rectangle(0, 0, 100, 100));
console.time('Rectangle building');
for (let i = 0; i < iterations; i++) {
rectData.points = []; // Reset
graphicsUtils.buildRectangle.build(rectData);
}
console.timeEnd('Rectangle building');
// Circle building performance
const circleData = new GraphicsData(new Circle(0, 0, 50));
console.time('Circle building');
for (let i = 0; i < iterations; i++) {
circleData.points = []; // Reset
graphicsUtils.buildCircle.build(circleData);
}
console.timeEnd('Circle building');Install with Tessl CLI
npx tessl i tessl/npm-pixi--graphics