Creates a new Bounds object.
The minimum X coordinate of the bounds.
The minimum Y coordinate of the bounds.
The maximum X coordinate of the bounds.
The maximum Y coordinate of the bounds.
Advanced
matrixThe transformation matrix applied to this bounds object. Used when calculating bounds with transforms.
const bounds = new Bounds();
// Apply translation matrix
bounds.matrix = new Matrix()
.translate(100, 100);
// Combine transformations
bounds.matrix = new Matrix()
.translate(50, 50)
.rotate(Math.PI / 4)
.scale(2, 2);
// Use in bounds calculations
bounds.addFrame(0, 0, 100, 100); // Uses current matrix
bounds.addFrame(0, 0, 100, 100, customMatrix); // Override matrix
The maximum X coordinate of the bounds. Represents the rightmost edge of the bounding box.
The maximum Y coordinate of the bounds. Represents the bottommost edge of the bounding box.
The minimum X coordinate of the bounds. Represents the leftmost edge of the bounding box.
The minimum Y coordinate of the bounds. Represents the topmost edge of the bounding box.
Whether the bounds has positive width and height. Checks if both dimensions are greater than zero.
const bounds = new Bounds(0, 0, 100, 100);
// Check if bounds are positive
console.log(bounds.isPositive); // true
// Negative bounds
bounds.maxX = bounds.minX;
console.log(bounds.isPositive); // false, width is 0
Whether the bounds has valid coordinates. Checks if the bounds has been initialized with real values.
const bounds = new Bounds();
console.log(bounds.isValid); // false, default state
// Set valid bounds
bounds.addFrame(0, 0, 100, 100);
console.log(bounds.isValid); // true
The bounding rectangle representation of these bounds. Lazily creates and updates a Rectangle instance based on the current bounds.
const bounds = new Bounds(0, 0, 100, 100);
// Get rectangle representation
const rect = bounds.rectangle;
console.log(rect.x, rect.y, rect.width, rect.height);
// Use for hit testing
if (bounds.rectangle.contains(mouseX, mouseY)) {
console.log('Mouse is inside bounds!');
}
The x position of the bounds in local space. Setting this value will move the bounds while maintaining its width.
The y position of the bounds in local space. Setting this value will move the bounds while maintaining its height.
Adds another bounds object to this one, optionally transformed by a matrix. Expands the bounds to include the given bounds' area.
The bounds to be added
Optional
matrix: MatrixOptional transformation matrix
const bounds = new Bounds();
// Add child bounds
const childBounds = sprite.getBounds();
bounds.addBounds(childBounds);
// Add transformed bounds
const matrix = new Matrix()
.scale(2, 2);
bounds.addBounds(childBounds, matrix);
Adds other Bounds as a mask, creating an intersection of the two bounds. Only keeps the overlapping region between current bounds and mask bounds.
The Bounds to use as a mask
const bounds = new Bounds(0, 0, 100, 100);
// Create mask bounds
const mask = new Bounds();
mask.addFrame(50, 50, 150, 150);
// Apply mask - results in bounds of (50,50,100,100)
bounds.addBoundsMask(mask);
Adds a rectangular frame to the bounds, optionally transformed by a matrix. Updates the bounds to encompass the new frame coordinates.
Left X coordinate of frame
Top Y coordinate of frame
Right X coordinate of frame
Bottom Y coordinate of frame
Optional
matrix: MatrixOptional transformation matrix
const bounds = new Bounds();
bounds.addFrame(0, 0, 100, 100);
// Add transformed frame
const matrix = new Matrix()
.translate(50, 50)
.rotate(Math.PI / 4);
bounds.addFrame(0, 0, 100, 100, matrix);
Adds a rectangle to the bounds, optionally transformed by a matrix. Updates the bounds to encompass the given rectangle.
const bounds = new Bounds();
// Add simple rectangle
const rect = new Rectangle(0, 0, 100, 100);
bounds.addRect(rect);
// Add transformed rectangle
const matrix = new Matrix()
.translate(50, 50)
.rotate(Math.PI / 4);
bounds.addRect(rect, matrix);
Adds vertices from a Float32Array to the bounds, optionally transformed by a matrix. Used for efficiently updating bounds from raw vertex data.
The array of vertices to add
Starting index in the vertex array
Ending index in the vertex array (excluded)
Optional
matrix: MatrixOptional transformation matrix
const bounds = new Bounds();
// Add vertices from geometry
const vertices = new Float32Array([
0, 0, // Vertex 1
100, 0, // Vertex 2
100, 100 // Vertex 3
]);
bounds.addVertexData(vertices, 0, 6);
// Add transformed vertices
const matrix = new Matrix()
.translate(50, 50)
.rotate(Math.PI / 4);
bounds.addVertexData(vertices, 0, 6, matrix);
// Add subset of vertices
bounds.addVertexData(vertices, 2, 4); // Only second vertex
Applies a transformation matrix to the bounds, updating its coordinates. Transforms all corners of the bounds using the given matrix.
The matrix to apply to the bounds
const bounds = new Bounds(0, 0, 100, 100);
// Apply translation
const translateMatrix = new Matrix()
.translate(50, 50);
bounds.applyMatrix(translateMatrix);
Ceils the bounds by rounding up max values and rounding down min values. Useful for pixel-perfect calculations and avoiding fractional pixels.
This bounds object for chaining
const bounds = new Bounds();
bounds.set(10.2, 10.9, 50.1, 50.8);
// Round to whole pixels
bounds.ceil();
// bounds are now (10, 10, 51, 51)
Clears the bounds and resets all coordinates to their default values. Resets the transformation matrix back to identity.
This bounds object for chaining
Creates a new Bounds instance with the same values.
A new Bounds instance with the same values
const bounds = new Bounds(0, 0, 100, 100);
// Create a copy
const copy = bounds.clone();
// Original and copy are independent
bounds.pad(10);
console.log(copy.width === bounds.width); // false
Bounds#copyFrom For reusing existing bounds
Checks if a point is contained within the bounds. Returns true if the point's coordinates fall within the bounds' area.
x coordinate to check
y coordinate to check
True if the point is inside the bounds
const bounds = new Bounds(0, 0, 100, 100);
// Basic point check
console.log(bounds.containsPoint(50, 50)); // true
console.log(bounds.containsPoint(150, 150)); // false
// Check edges
console.log(bounds.containsPoint(0, 0)); // true, includes edges
console.log(bounds.containsPoint(100, 100)); // true, includes edges
Copies the bounds from another bounds object. Useful for reusing bounds objects and avoiding allocations.
The bounds to copy from
This bounds object for chaining
const sourceBounds = new Bounds(0, 0, 100, 100);
// Copy bounds
const targetBounds = new Bounds();
targetBounds.copyFrom(sourceBounds);
Bounds#clone For creating new instances
Resizes the bounds object to fit within the given rectangle. Clips the bounds if they extend beyond the rectangle's edges.
The rectangle to fit within
This bounds object for chaining
const bounds = new Bounds(0, 0, 200, 200);
// Fit within viewport
const viewport = new Rectangle(50, 50, 100, 100);
bounds.fit(viewport);
// bounds are now (50, 50, 150, 150)
Resizes the bounds object to include the given bounds. Similar to fit() but works with raw coordinate values instead of a Rectangle.
The left value of the bounds
The right value of the bounds
The top value of the bounds
The bottom value of the bounds
This bounds object for chaining
const bounds = new Bounds(0, 0, 200, 200);
// Fit to specific coordinates
bounds.fitBounds(50, 150, 50, 150);
// bounds are now (50, 50, 150, 150)
Checks if bounds are empty, meaning either width or height is zero or negative. Empty bounds occur when min values exceed max values on either axis.
True if bounds are empty (have no area)
const bounds = new Bounds();
// Check if newly created bounds are empty
console.log(bounds.isEmpty()); // true, default bounds are empty
// Add frame and check again
bounds.addFrame(0, 0, 100, 100);
console.log(bounds.isEmpty()); // false, bounds now have area
// Clear bounds
bounds.clear();
console.log(bounds.isEmpty()); // true, bounds are empty again
Pads bounds object, making it grow in all directions. If paddingY is omitted, both paddingX and paddingY will be set to paddingX.
The horizontal padding amount
The vertical padding amount
This bounds object for chaining
const bounds = new Bounds(0, 0, 100, 100);
// Add equal padding
bounds.pad(10);
// bounds are now (-10, -10, 110, 110)
// Add different padding for x and y
bounds.pad(20, 10);
// bounds are now (-30, -20, 130, 120)
Scales the bounds by the given values, adjusting all edges proportionally.
The X value to scale by
The Y value to scale by (defaults to x)
This bounds object for chaining
const bounds = new Bounds(0, 0, 100, 100);
// Scale uniformly
bounds.scale(2);
// bounds are now (0, 0, 200, 200)
// Scale non-uniformly
bounds.scale(0.5, 2);
// bounds are now (0, 0, 100, 400)
Sets the bounds directly using coordinate values. Provides a way to set all bounds values at once.
Left X coordinate of frame
Top Y coordinate of frame
Right X coordinate of frame
Bottom Y coordinate of frame
Returns a string representation of the bounds. Useful for debugging and logging bounds information.
A string describing the bounds
const bounds = new Bounds(0, 0, 100, 100);
console.log(bounds.toString()); // "[pixi.js:Bounds minX=0 minY=0 maxX=100 maxY=100 width=100 height=100]"
A representation of an axis-aligned bounding box (AABB) used for efficient collision detection and culling. Stores minimum and maximum coordinates to define a rectangular boundary.
Example