The X coordinate of the upper-left corner of the rectangle
The Y coordinate of the upper-left corner of the rectangle
The overall width of the rectangle
The overall height of the rectangle
The overall height of this rectangle
Readonly
typeThe type of the object, mainly used to avoid instanceof
checks
// Check shape type
const shape = new Rectangle(0, 0, 100, 100);
console.log(shape.type); // 'rectangle'
// Use in type guards
if (shape.type === 'rectangle') {
console.log(shape.width, shape.height);
}
SHAPE_PRIMITIVE For all shape types
The overall width of this rectangle
The X coordinate of the upper-left corner of the rectangle
The Y coordinate of the upper-left corner of the rectangle
Returns the bottom edge (y + height) of the rectangle.
The y-coordinate of the bottom edge
// Get bottom edge position
const rect = new Rectangle(100, 100, 200, 150);
console.log(rect.bottom); // 250
// Stack below rectangle
sprite.y = rect.bottom + margin;
// Check vertical bounds
if (point.y < rect.bottom) {
console.log('Point is above bottom edge');
}
Returns the left edge (x-coordinate) of the rectangle.
The x-coordinate of the left edge
// Get left edge position
const rect = new Rectangle(100, 100, 200, 150);
console.log(rect.left); // 100
// Use in alignment calculations
sprite.x = rect.left + padding;
// Compare positions
if (point.x > rect.left) {
console.log('Point is right of rectangle');
}
Returns the right edge (x + width) of the rectangle.
The x-coordinate of the right edge
// Get right edge position
const rect = new Rectangle(100, 100, 200, 150);
console.log(rect.right); // 300
// Align to right edge
sprite.x = rect.right - sprite.width;
// Check boundaries
if (point.x < rect.right) {
console.log('Point is inside right bound');
}
Returns the top edge (y-coordinate) of the rectangle.
The y-coordinate of the top edge
// Get top edge position
const rect = new Rectangle(100, 100, 200, 150);
console.log(rect.top); // 100
// Position above rectangle
sprite.y = rect.top - sprite.height;
// Check vertical position
if (point.y > rect.top) {
console.log('Point is below top edge');
}
Static
EMPTYA constant empty rectangle. This is a new object every time the property is accessed.
A new empty rectangle instance
// Get fresh empty rectangle
const empty = Rectangle.EMPTY;
console.log(empty.isEmpty()); // true
Rectangle.isEmpty For empty state testing
Enlarges rectangle so that its corners lie on a grid defined by resolution.
The grid size to align to (1 = whole pixels)
Small number to prevent floating point errors
Returns itself
// Basic grid alignment
const rect = new Rectangle(10.2, 10.6, 100.8, 100.4);
rect.ceil(); // Aligns to whole pixels
// Custom resolution grid
const uiRect = new Rectangle(5.3, 5.7, 50.2, 50.8);
uiRect.ceil(0.5); // Aligns to half pixels
// Use with precision value
const preciseRect = new Rectangle(20.001, 20.999, 100.001, 100.999);
preciseRect.ceil(1, 0.01); // Handles small decimal variations
Creates a clone of this Rectangle
A copy of the rectangle
// Basic cloning
const original = new Rectangle(100, 100, 200, 150);
const copy = original.clone();
// Clone and modify
const modified = original.clone();
modified.width *= 2;
modified.height += 50;
// Verify independence
console.log(original.width); // 200
console.log(modified.width); // 400
Checks whether the x and y coordinates given are contained within this Rectangle
The X coordinate of the point to test
The Y coordinate of the point to test
Whether the x/y coordinates are within this Rectangle
// Basic containment check
const rect = new Rectangle(100, 100, 200, 150);
const isInside = rect.contains(150, 125); // true
// Check edge cases
console.log(rect.contains(100, 100)); // true (on edge)
console.log(rect.contains(300, 250)); // false (outside)
Determines whether another Rectangle is fully contained within this Rectangle.
Rectangles that occupy the same space are considered to be containing each other.
Rectangles without area (width or height equal to zero) can't contain anything, not even other arealess rectangles.
The Rectangle to check for containment
True if other is fully contained within this Rectangle
// Check if one rectangle contains another
const container = new Rectangle(0, 0, 100, 100);
const inner = new Rectangle(25, 25, 50, 50);
console.log(container.containsRect(inner)); // true
// Check overlapping rectangles
const partial = new Rectangle(75, 75, 50, 50);
console.log(container.containsRect(partial)); // false
// Zero-area rectangles
const empty = new Rectangle(0, 0, 0, 100);
console.log(container.containsRect(empty)); // false
Copies another rectangle to this one.
The rectangle to copy from
Returns itself
// Basic copying
const source = new Rectangle(100, 100, 200, 150);
const target = new Rectangle();
target.copyFrom(source);
// Chain with other operations
const rect = new Rectangle()
.copyFrom(source)
.pad(10);
Converts a Bounds object to a Rectangle object.
The bounds to copy and convert to a rectangle
Returns itself
// Convert bounds to rectangle
const bounds = container.getBounds();
const rect = new Rectangle().copyFromBounds(bounds);
Copies this rectangle to another one.
The rectangle to copy to
Returns given parameter
// Basic copying
const source = new Rectangle(100, 100, 200, 150);
const target = new Rectangle();
source.copyTo(target);
// Chain with other operations
const result = source
.copyTo(new Rectangle())
.getBounds();
Enlarges this rectangle to include the passed rectangle.
The rectangle to include
Returns itself
// Basic enlargement
const rect = new Rectangle(50, 50, 100, 100);
const other = new Rectangle(0, 0, 200, 75);
rect.enlarge(other);
// rect is now: x=0, y=0, width=200, height=150
// Use for bounding box calculation
const bounds = new Rectangle();
objects.forEach((obj) => {
bounds.enlarge(obj.getBounds());
});
Accepts other
Rectangle and returns true if the given Rectangle is equal to this
Rectangle.
Only available with pixi.js/math-extras.
The Rectangle to compare with this
Returns true if all x
, y
, width
, and height
are equal.
// Basic equality check
const rect1 = new Rectangle(0, 0, 100, 100);
const rect2 = new Rectangle(0, 0, 100, 100);
console.log(rect1.equals(rect2)); // true
// Check after modifications
rect2.width = 200;
console.log(rect1.equals(rect2)); // false
// Compare with offset rectangle
const offset = new Rectangle(10, 10, 100, 100);
console.log(rect1.equals(offset)); // false
Fits this rectangle around the passed one.
The rectangle to fit around
Returns itself
// Basic fitting
const container = new Rectangle(0, 0, 100, 100);
const content = new Rectangle(25, 25, 200, 200);
content.fit(container); // Clips to container bounds
Returns the framing rectangle of the rectangle as a Rectangle object
Optional
out: RectangleOptional rectangle to store the result
The framing rectangle
// Basic bounds retrieval
const rect = new Rectangle(100, 100, 200, 150);
const bounds = rect.getBounds();
// Reuse existing rectangle
const out = new Rectangle();
rect.getBounds(out);
If the area of the intersection between the Rectangles other
and this
is not zero,
returns the area of intersection as a Rectangle object. Otherwise, return an empty Rectangle
with its properties set to zero.
Rectangles without area (width or height equal to zero) can't intersect or be intersected and will always return an empty rectangle with its properties set to zero.
Only available with pixi.js/math-extras.
The intersection of this
and other
.
// Basic intersection check
const rect1 = new Rectangle(0, 0, 100, 100);
const rect2 = new Rectangle(50, 50, 100, 100);
const overlap = rect1.intersection(rect2);
console.log(overlap); // Rectangle(50, 50, 50, 50)
// Using output rectangle
const out = new Rectangle();
rect1.intersection(rect2, out);
// Zero-area rectangles
const empty = new Rectangle(0, 0, 0, 100);
const result = rect1.intersection(empty);
console.log(result); // Rectangle(0, 0, 0, 0)
Determines whether the other
Rectangle transformed by transform
intersects with this
Rectangle object.
Returns true only if the area of the intersection is greater than 0. This means that rectangles sharing only a side are not considered intersecting.
True if the transformed other
Rectangle intersects with this
// Basic intersection check
const rect1 = new Rectangle(0, 0, 100, 100);
const rect2 = new Rectangle(50, 50, 100, 100);
console.log(rect1.intersects(rect2)); // true
// With transformation matrix
const matrix = new Matrix();
matrix.rotate(Math.PI / 4); // 45 degrees
console.log(rect1.intersects(rect2, matrix)); // Checks with rotation
// Edge cases
const zeroWidth = new Rectangle(0, 0, 0, 100);
console.log(rect1.intersects(zeroWidth)); // false (no area)
Determines whether the Rectangle is empty (has no area).
True if the rectangle has no area
// Check zero dimensions
const rect = new Rectangle(100, 100, 0, 50);
console.log(rect.isEmpty()); // true
Pads the rectangle 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
Returns itself
// Basic padding
const rect = new Rectangle(100, 100, 200, 150);
rect.pad(10); // Adds 10px padding on all sides
// Different horizontal and vertical padding
const uiRect = new Rectangle(0, 0, 100, 50);
uiRect.pad(20, 10); // 20px horizontal, 10px vertical
Sets the position and dimensions of the rectangle.
The X coordinate of the upper-left corner of the rectangle
The Y coordinate of the upper-left corner of the rectangle
The overall width of the rectangle
The overall height of the rectangle
Returns itself for method chaining
// Basic usage
const rect = new Rectangle();
rect.set(100, 100, 200, 150);
// Chain with other operations
const bounds = new Rectangle()
.set(0, 0, 100, 100)
.pad(10);
Checks whether the x and y coordinates given are contained within this rectangle including the stroke.
The X coordinate of the point to test
The Y coordinate of the point to test
The width of the line to check
The alignment of the stroke (0 = inner, 0.5 = centered, 1 = outer)
Whether the x/y coordinates are within this rectangle's stroke
// Basic stroke check
const rect = new Rectangle(100, 100, 200, 150);
const isOnStroke = rect.strokeContains(150, 100, 4); // 4px line width
// Check with different alignments
const innerStroke = rect.strokeContains(150, 100, 4, 0); // Inside
const centerStroke = rect.strokeContains(150, 100, 4, 0.5); // Centered
const outerStroke = rect.strokeContains(150, 100, 4, 1); // Outside
Returns a string representation of an object.
Adds this
and other
Rectangles together to create a new Rectangle object filling
the horizontal and vertical space between the two rectangles.
Only available with pixi.js/math-extras.
The union of this
and other
// Basic union
const rect1 = new Rectangle(0, 0, 100, 100);
const rect2 = new Rectangle(50, 50, 100, 100);
const combined = rect1.union(rect2);
console.log(combined); // Rectangle(0, 0, 150, 150)
// Using output rectangle
const out = new Rectangle();
rect1.union(rect2, out);
// Chain multiple unions
const rect3 = new Rectangle(200, 200, 50, 50);
const result = rect1.union(rect2).union(rect3);
The
Rectangle
object represents a rectangular area defined by its position and dimensions. Used for hit testing, bounds calculation, and general geometric operations.Example
Remarks
See