Rect

  • Revision slug: JavaScript_code_modules/Geometry.jsm/Rect
  • Revision title: Rect
  • Revision id: 94495
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 5 words added

Revision Content

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

Revision Source

<p>The <code>Rect</code> class offers methods for performing common geometry operations on rectangles in two dimensional space.</p>
<div class="note"><strong>Note:</strong> Rectangles can be empty; in that case, they will report non-positive width and height.</div>
<h2 id="Method_overview">Method overview</h2>
<table class="standard-table"> <tbody> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#blend()" title="en/JavaScript code modules/Geometry.jsm/Rect#blend()">blend</a>(Rect otherRect, Number scalar);<br> </code></td> </tr> <tr> <td><code>Point <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#center()" title="en/JavaScript code modules/Geometry.jsm/Rect#center()">center</a>();<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#clone()" title="en/JavaScript code modules/Geometry.jsm/Rect#clone()">clone</a>();<br> </code></td> </tr> <tr> <td><code>boolean <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#contains()" title="en/JavaScript code modules/Geometry.jsm/Rect#contains()">contains</a>(Rect otherRect);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#copyFrom()" title="en/JavaScript code modules/Geometry.jsm/Rect#copyFrom()">copyFrom</a>(Rect otherRect);<br> </code></td> </tr> <tr> <td><code>boolean <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#equals()" title="en/JavaScript code modules/Geometry.jsm/Rect#equals()">equals</a>(Rect otherRect);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#expandToContain()" title="en/JavaScript code modules/Geometry.jsm/Rect#expandToContain()">expandToContain</a>(Rect otherRect);</code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#expandToIntegers()" title="en/JavaScript code modules/Geometry.jsm/Rect#expandToIntegers()">expandToIntegers</a>();<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#inflate()" title="en/JavaScript code modules/Geometry.jsm/Rect#inflate()">inflate</a>(</code><code>Number </code><code>xScale[, </code><code>Number </code><code>yScale]);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#intersect()" title="en/JavaScript code modules/Geometry.jsm/Rect#intersect()">intersect</a>(Rect otherRect);</code></td> </tr> <tr> <td><code>boolean <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#intersects()" title="en/JavaScript code modules/Geometry.jsm/Rect#intersects()">intersects</a>(Rect otherRect);</code></td> </tr> <tr> <td><code>boolean <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#isEmpty()" title="en/JavaScript code modules/Geometry.jsm/Rect#isEmpty()">isEmpty</a>();<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#map()" title="en/JavaScript code modules/Geometry.jsm/Rect#map()">map</a>(mapFunction);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#restrictTo()" title="en/JavaScript code modules/Geometry.jsm/Rect#restrictTo()">restrictTo</a>(Rect otherRect);</code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#scale()" title="en/JavaScript code modules/Geometry.jsm/Rect#scale()">scale</a>(</code><code>Number </code><code>xScale, </code><code>Number </code><code>yScale);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#setBounds()" title="en/JavaScript code modules/Geometry.jsm/Rect#setBounds()">setBounds</a>(</code><code>Number </code><code>left, </code><code>Number </code><code>top, </code><code>Number </code><code>right, </code><code>Number </code><code>bottom);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#setRect()" title="en/JavaScript code modules/Geometry.jsm/Rect#setRect()">setRect</a>(</code><code>Number </code><code>x, </code><code>Number </code><code>y, </code><code>Number </code><code>width, </code><code>Number </code><code>height);<br> </code></td> </tr> <tr> <td><code>Rect </code><code><a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#setRect()" title="en/JavaScript code modules/Geometry.jsm/Rect#setRect()">setRect</a></code><code>(Rect otherRect);<br> </code></td> </tr> <tr> <td><code>Rect[] <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#subtract()" title="en/JavaScript code modules/Geometry.jsm/Rect#subtract()">subtract</a>(Rect otherRect);<br> </code></td> </tr> <tr> <td><code>String <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#toString()" title="en/JavaScript code modules/Geometry.jsm/Rect#toString()">toString</a>();<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#translate()" title="en/JavaScript code modules/Geometry.jsm/Rect#translate()">translate</a>(</code><code>Number deltaX</code><code>, </code><code>Number </code><code>deltaY);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#translateInside()" title="en/JavaScript code modules/Geometry.jsm/Rect#translateInside()">translateInside</a>(Rect otherRect);<br> </code></td> </tr> <tr> <td><code>Rect <a href="/en/JavaScript_code_modules/Geometry.jsm/Rect#union()" title="en/JavaScript code modules/Geometry.jsm/Rect#union()">union</a>(Rect otherRect);<br> </code></td> </tr> </tbody>
</table>
<h2 id="Attributes">Attributes</h2>
<table class="standard-table" style="width: auto;"> <tbody> <tr> <td class="header">Attribute</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>height<br> </code></td> <td><code>Number<br> </code></td> <td>The rectangle's width.</td> </tr> <tr> <td><code>width<br> </code></td> <td><code>Number<br> </code></td> <td>The rectangle's height.</td> </tr> <tr> <td><code>x</code></td> <td><code>Number</code></td> <td>The X coordinate of the top-left corner of the rectangle.</td> </tr> <tr> <td><code>y</code></td> <td><code>Number</code></td> <td>The Y coordinate of the top-left corner of the rectangle.</td> </tr> </tbody>
</table>
<h2 id="Methods">Methods</h2>
<h3 id="blend()">blend()</h3>
<p>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.</p>
<p>You can use this when you want to animate the resizing and repositioning of one rectangle to another, for example.</p>
<pre>Rect blend(
  Rect otherRect,
  Number scalar
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>The rectangle to blend with.</dd> <dt><code>scalar</code></dt> <dd>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 <code>otherRect</code> the result will be.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>A new <code>Rect</code> object representing the requested blending of the two rectangles.</p>
<h3 id="center()">center()</h3>
<p>Returns a <a href="/en/JavaScript_code_modules/Geometry.jsm/Point" title="en/JavaScript code modules/Geometry.jsm/Point"><code>Point</code></a> whose coordinates represent the center of the rectangle.</p>
<pre>Point center();
</pre>
<h6 id="Parameters">Parameters</h6>
<p>None</p>
<h6 id="Return_value">Return value</h6>
<p>A new <a href="/en/JavaScript_code_modules/Geometry.jsm/Point" title="en/JavaScript code modules/Geometry.jsm/Point"><code>Point</code></a> object whose coordinates place it at the center of the rectangle.</p>
<h6 id="Exceptions_thrown">Exceptions thrown</h6>
<p>Throws an exception if the rectangle is empty.</p>
<h3 id="clone()">clone()</h3>
<p>Returns a new <code>Rect</code> object which is a duplicate of this one.</p>
<pre>Rect clone();
</pre>
<h6 id="Parameters">Parameters</h6>
<p>None.</p>
<h6 id="Return_value">Return value</h6>
<p>A new <code>Rect</code> object whose position, width, and height match the original object's.</p>
<h3 id="contains()">contains()</h3>
<p>Determines whether or not this rectangle wholly encloses another rectangle.</p>
<pre>boolean contains(
  Rect otherRect
);</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>The rectangle to check.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p><code>true</code> if the rectangle specified by <code>otherRect</code> is wholly enclosed by this one; otherwise <code>false</code>.</p>
<h3 id="copyFrom()">copyFrom()</h3>
<p>Copies another rectangle's position, width, and height to this rectangle, replacing the existing values.</p>
<pre>Rect copyFrom(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>Another rectangle to copy into this one.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object with its values changed.</p>
<h3 id="equals()">equals()</h3>
<p>Determines whether or not another rectangle equals this one.</p>
<pre>boolean equals(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>Another rectangle to compare to this one.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p><code>true</code> 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 <code>false</code>.</p>
<div class="note"><strong>Note</strong>: This will also return <code>false</code> if <code>otherRect</code> is <code>null</code>.</div>
<h3 id="expandToContain()">expandToContain()</h3>
<p>Expands the rectangle to the union of this rectangle with the specified one.</p>
<pre>Rect expandToContain(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>Another rectangle to expand to contain.</dd>
</dl>
<h3 id="expandToIntegers()">expandToIntegers()</h3>
<p>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.</p>
<pre>Rect expandToIntegers();
</pre>
<h6 id="Parameters">Parameters</h6>
<p>None.</p>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object with its boundaries adjusted to be integer values; the resulting rectangle wholly contains the original rectangle.</p>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object, but expanded so that it is the union of the original rectangle and <code>otherRect</code>.</p>
<h3 id="inflate()">inflate()</h3>
<p>Resizes the rectangle while keeping its center the same. You may specify a single scaling factor for both axes, or one for each axis.</p>
<pre>Rect inflate(
  Number xScale[,
  Number yScale]
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>xScale</code></dt> <dd>The scaling factor for the X direction (or both directions if you don't specify <code>yScale</code>).</dd> <dt><code>yScale</code></dt> <dd>The scaling factor for the Y direction.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object adjusted by the specified scaling factor(s).</p>
<h3 id="intersect()">intersect()</h3>
<p>Returns a new <code>Rect</code> object containing only the area that both this object and the specified one occupy.</p>
<pre>Rect intersect(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>Another rectangle with which to intersect.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>A new <code>Rect</code> object containing only the area both rectangles share. This will be an empty rectangle if the two rectangles don't overlap.</p>
<h3 id="intersects()">intersects()</h3>
<p>Determines whether or not the rectangle intersects with another.</p>
<pre>boolean intersects(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>The rectangle to check for intersection.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p><code>true</code> if the two rectangles intersect; otherwise <code>false</code>.</p>
<h3 id="isEmpty()">isEmpty()</h3>
<p>Determines whether or not the rectangle is empty.</p>
<pre>boolean isEmpty();
</pre>
<h6 id="Parameters">Parameters</h6>
<p>None.</p>
<h6 id="Return_value">Return value</h6>
<p><code>true</code> if the rectangle is empty; otherwise <code>false</code>.</p>
<h3 id="map()">map()</h3>
<p>Calls a specified function on each of the <code>left</code>, <code>right</code>, <code>top</code>, and <code>bottom</code> values of the rectangle.</p>
<pre>Rect map(
  mapFunction
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>mapFunction</code></dt> <dd>The function to call for each of the values; it receives a single parameter, the value to manipulate. When the function is called, <code>this</code> refers to the <code>Rect</code> object being manipulated.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object with its values adjusted by the specified <code>mapFunction</code>.</p>
<h3 id="restrictTo()">restrictTo()</h3>
<p>Reduces the size of the rectangle as necessary to prevent it from extending outside the bounds of the specified rectangle.</p>
<pre>Rect restrictTo(
  Rect otherRect
);
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>The rectangle to restrict this one to fit inside.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The same <code>Rect</code> object you started with, but with its position and bounds adjusted as necessary to prevent the rectangle from extending outside <code>otherRect</code> in any direction. The result may be an empty rectangle if the two rectangles don't intersect at all.</p>
<h3 id="scale()">scale()</h3>
<p>Scales the size and position of the rectangle by the given horizontal and vertical scaling factors. This is done by multiplying the <code>left</code> and <code>right</code> values by <code>xScale</code> and the <code>top</code> and <code>bottom</code> values by <code>yScale</code>.</p>
<pre>Rect scale(
  Number xScale,
  Number yScale
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>xScale</code></dt> <dd>The amount by which to scale along the horizontal axis.</dd> <dt><code>yScale</code></dt> <dd>The amount by which to scale along the vertical axis.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object with its position and size adjusted by the scaling values specified.</p>
<h3 id="setBounds()">setBounds()</h3>
<p>Sets the rectangle's bounds.</p>
<pre>Rect setBounds(
  Number left,
  Number top,
  Number right,
  Number bottom
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>left</code></dt> <dd>The X coordinate of the left edge of the rectangle.</dd> <dt><code>top</code></dt> <dd>The Y coordinate of the top edge of the rectangle.</dd> <dt><code>right</code></dt> <dd>The X coordinate of the right edge of the rectangle.</dd> <dt><code>bottom</code></dt> <dd>The Y coordinate of the bottom edge of the rectangle.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The same <code>Rect</code> object, with its bounds changed.</p>
<h3 id="setRect()">setRect()</h3>
<p>Sets the rectangle's position and size.</p>
<pre>Rect setRect(
  Number x,
  Number y,
  Number width,
  Number height
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>x</code></dt> <dd>The X coordinate of the left edge of the rectangle.</dd> <dt><code>y</code></dt> <dd>The Y coordinate of the top edge of the rectangle.</dd> <dt><code>width</code></dt> <dd>The rectangle's width.</dd> <dt><code>height</code></dt> <dd>The rectangle's height.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The same <code>Rect</code> object, with its position and size changed.</p>
<h3 id="subtract()">subtract()</h3>
<p>Subtracts the specified rectangle from this one. The result is an array of <code>Rect</code> objects representing the area from this rectangle that doesn't exist inside the specified rectangle.</p>
<pre>Rect[] subtract(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>The rectangle to subtract from this one.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>An array of rectangles representing the area contained by the original rectangle but not contained by <code>otherRect</code>.</p>
<h3 id="toString()">toString()</h3>
<p>Returns a <a href="/en/JavaScript/Reference/Global_Objects/String" title="en/JavaScript/Reference/Global Objects/String"><code>String</code></a> object describing the rectangle.</p>
<pre>String toString();
</pre>
<h6 id="Parameters">Parameters</h6>
<p>None.</p>
<h6 id="Return_value">Return value</h6>
<p>A string describing the object, in the format "[<em>x</em>,<em>y</em>,<em>width</em>,<em>height</em>]".</p>
<h3 id="translate()">translate()</h3>
<p>Translates the rectangle by moving it by given distances horizontally and vertically.</p>
<pre>Rect translate(
  Number deltaX,
  Number deltaX
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>deltaX</code></dt> <dd>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.</dd> <dt><code>deltaY</code></dt> <dd>The distance by which to translate along the Y axis; specify a negative number to move up or a positive number to move down.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The same <code>Rect</code> object, with its position changed.</p>
<h3 id="translateInside()">translateInside()</h3>
<p>Translates the rectangle to ensure that it is entirely within the specified rectangle, if possible. The rectangle's width and height are not changed.</p>
<div class="note"><strong>Note:</strong> 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 <code>otherRect.contains(thisRect)</code> after translating.</div>
<pre>Rect translateInside(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>The rectangle to translate this one to try to fit inside.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>The original <code>Rect</code> object with its position changed to attempt to be inside the specified rectangle.</p>
<h3 id="union()">union()</h3>
<p>Returns a new <code>Rect</code> which is the <strong>union</strong> 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).</p>
<pre>Rect union(
  Rect otherRect
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>otherRect</code></dt> <dd>Another rectangle with which to compose the union.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>A new <code>Rect</code> object representing the union of the two rectangles.</p>
<h2 id="See_also">See also</h2>
<ul> <li><a href="/en/JavaScript_code_modules/Geometry.jsm" title="en/JavaScript code modules/Geometry.jsm"><code>Geometry.jsm</code></a></li>
</ul>
Revert to this revision