Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Rect

The Rect class offers methods for performing common geometry operations on rectangles in two dimensional space.

Note: Rectangles can be empty; in that case, they will report non-positive width and height.

Method overview

Rect blend(Rect otherRect, Number scalar);
Point center();
Rect clone();
boolean contains(Rect otherRect);
Rect copyFrom(Rect otherRect);
boolean equals(Rect otherRect);
Rect expandToContain(Rect otherRect);
Rect expandToIntegers();
Rect inflate(Number xScale[, Number yScale]);
Rect intersect(Rect otherRect);
boolean intersects(Rect otherRect);
boolean isEmpty();
Rect map(mapFunction);
Rect restrictTo(Rect otherRect);
Rect scale(Number xScale, Number yScale);
Rect setBounds(Number left, Number top, Number right, Number bottom);
Rect setRect(Number x, Number y, Number width, Number height);
Rect setRect(Rect otherRect);
Rect[] subtract(Rect otherRect);
String toString();
Rect translate(Number deltaX, Number deltaY);
Rect translateInside(Rect otherRect);
Rect union(Rect otherRect);

Attributes

Attribute Type Description
height
Number
The rectangle's width.
width
Number
The rectangle's height.
x Number The X coordinate of the top-left corner of the rectangle.
y Number The Y coordinate of the top-left corner of the rectangle.

Methods

blend()

Blends two rectangles together given a scaling factor. Specifying 0 returns a clone of the original rectangle, while specifying 1 returns a clone of the specified rectangle. Values in between return rectangles somewhere in between.

You can use this when you want to animate the resizing and repositioning of one rectangle to another, for example.

Rect blend(
  Rect otherRect,
  Number scalar
); 
Parameters
otherRect
The rectangle to blend with.
scalar
A scaling factor; the closer to 0 this is, the more like the original rectangle the result will be; the closer to 1 this is, the more like otherRect the result will be.
Return value

A new Rect object representing the requested blending of the two rectangles.

center()

Returns a Point whose coordinates represent the center of the rectangle.

Point center();
Parameters

None

Return value

A new Point object whose coordinates place it at the center of the rectangle.

Exceptions thrown

Throws an exception if the rectangle is empty.

clone()

Returns a new Rect object which is a duplicate of this one.

Rect clone();
Parameters

None.

Return value

A new Rect object whose position, width, and height match the original object's.

contains()

Determines whether or not this rectangle wholly encloses another rectangle.

boolean contains(
  Rect otherRect
);
Parameters
otherRect
The rectangle to check.
Return value

true if the rectangle specified by otherRect is wholly enclosed by this one; otherwise false.

copyFrom()

Copies another rectangle's position, width, and height to this rectangle, replacing the existing values.

Rect copyFrom(
  Rect otherRect
); 
Parameters
otherRect
Another rectangle to copy into this one.
Return value

The original Rect object with its values changed.

equals()

Determines whether or not another rectangle equals this one.

boolean equals(
  Rect otherRect
); 
Parameters
otherRect
Another rectangle to compare to this one.
Return value

true if the two rectangles are equal; that is, either both are empty or both rectangles share the same top and left edges as well as width and height. Otherwise, returns false.

Note: This will also return false if otherRect is null.

expandToContain()

Expands the rectangle to the union of this rectangle with the specified one.

Rect expandToContain(
  Rect otherRect
); 
Parameters
otherRect
Another rectangle to expand to contain.

expandToIntegers()

Expands the rectangle outward if necessary so that all of its edges lie on integer boundaries. This is done by rounding the left and top edges downward and the right and bottom edges upward.

Rect expandToIntegers();
Parameters

None.

Return value

The original Rect object with its boundaries adjusted to be integer values; the resulting rectangle wholly contains the original rectangle.

Return value

The original Rect object, but expanded so that it is the union of the original rectangle and otherRect.

inflate()

Resizes the rectangle while keeping its center the same. You may specify a single scaling factor for both axes, or one for each axis.

Rect inflate(
  Number xScale[,
  Number yScale]
); 
Parameters
xScale
The scaling factor for the X direction (or both directions if you don't specify yScale).
yScale
The scaling factor for the Y direction.
Return value

The original Rect object adjusted by the specified scaling factor(s).

intersect()

Returns a new Rect object containing only the area that both this object and the specified one occupy.

Rect intersect(
  Rect otherRect
); 
Parameters
otherRect
Another rectangle with which to intersect.
Return value

A new Rect object containing only the area both rectangles share. This will be an empty rectangle if the two rectangles don't overlap.

intersects()

Determines whether or not the rectangle intersects with another.

boolean intersects(
  Rect otherRect
); 
Parameters
otherRect
The rectangle to check for intersection.
Return value

true if the two rectangles intersect; otherwise false.

isEmpty()

Determines whether or not the rectangle is empty.

boolean isEmpty();
Parameters

None.

Return value

true if the rectangle is empty; otherwise false.

map()

Calls a specified function on each of the left, right, top, and bottom values of the rectangle.

Rect map(
  mapFunction
); 
Parameters
mapFunction
The function to call for each of the values; it receives a single parameter, the value to manipulate. When the function is called, this refers to the Rect object being manipulated.
Return value

The original Rect object with its values adjusted by the specified mapFunction.

restrictTo()

Reduces the size of the rectangle as necessary to prevent it from extending outside the bounds of the specified rectangle.

Rect restrictTo(
  Rect otherRect
);
Parameters
otherRect
The rectangle to restrict this one to fit inside.
Return value

The same Rect object you started with, but with its position and bounds adjusted as necessary to prevent the rectangle from extending outside otherRect in any direction. The result may be an empty rectangle if the two rectangles don't intersect at all.

scale()

Scales the size and position of the rectangle by the given horizontal and vertical scaling factors. This is done by multiplying the left and right values by xScale and the top and bottom values by yScale.

Rect scale(
  Number xScale,
  Number yScale
); 
Parameters
xScale
The amount by which to scale along the horizontal axis.
yScale
The amount by which to scale along the vertical axis.
Return value

The original Rect object with its position and size adjusted by the scaling values specified.

setBounds()

Sets the rectangle's bounds.

Rect setBounds(
  Number left,
  Number top,
  Number right,
  Number bottom
); 
Parameters
left
The X coordinate of the left edge of the rectangle.
top
The Y coordinate of the top edge of the rectangle.
right
The X coordinate of the right edge of the rectangle.
bottom
The Y coordinate of the bottom edge of the rectangle.
Return value

The same Rect object, with its bounds changed.

setRect()

Sets the rectangle's position and size.

Rect setRect(
  Number x,
  Number y,
  Number width,
  Number height
); 
Parameters
x
The X coordinate of the left edge of the rectangle.
y
The Y coordinate of the top edge of the rectangle.
width
The rectangle's width.
height
The rectangle's height.
Return value

The same Rect object, with its position and size changed.

subtract()

Subtracts the specified rectangle from this one. The result is an array of Rect objects representing the area from this rectangle that doesn't exist inside the specified rectangle.

Rect[] subtract(
  Rect otherRect
); 
Parameters
otherRect
The rectangle to subtract from this one.
Return value

An array of rectangles representing the area contained by the original rectangle but not contained by otherRect.

toString()

Returns a String object describing the rectangle.

String toString();
Parameters

None.

Return value

A string describing the object, in the format "[x,y,width,height]".

translate()

Translates the rectangle by moving it by given distances horizontally and vertically.

Rect translate(
  Number deltaX,
  Number deltaX
); 
Parameters
deltaX
The distance by which to translate along the X axis; specify a negative number to move to the left or a positive number to move to the right.
deltaY
The distance by which to translate along the Y axis; specify a negative number to move up or a positive number to move down.
Return value

The same Rect object, with its position changed.

translateInside()

Translates the rectangle to ensure that it is entirely within the specified rectangle, if possible. The rectangle's width and height are not changed.

Note: It's not guaranteed that the resulting rectangle will be entirely inside the specified rectangle, since it's possible that this rectangle is larger than it. To be sure that the resulting rectangle is entirely contained by the other rectangle, call otherRect.contains(thisRect) after translating.
Rect translateInside(
  Rect otherRect
); 
Parameters
otherRect
The rectangle to translate this one to try to fit inside.
Return value

The original Rect object with its position changed to attempt to be inside the specified rectangle.

union()

Returns a new Rect which is the union of this rectangle and another one. In this context, a union is a rectangle that wholly includes both rectangles (possibly also containing other additional space not included in either of them).

Rect union(
  Rect otherRect
); 
Parameters
otherRect
Another rectangle with which to compose the union.
Return value

A new Rect object representing the union of the two rectangles.

See also

Document Tags and Contributors

 Contributors to this page: teoli, Sheppy
 Last updated by: teoli,