Rectangle
A Rectangle specifies an area that is enclosed by itβs top-left point (x, y), its width, and its height. It should not be confused with a rectangular path, it is not an item.
Constructors
Rectangle(point, size)
Creates a Rectangle object.
Parameters:
point:
Point
β the top-left point of the rectanglesize:
Size
β the size of the rectangleReturns:
Rectangle
Rectangle(object)
Creates a Rectangle object.
Parameters:
object:
Object
β an object containing properties to be set on the rectangleReturns:
Rectangle
Example:Create a rectangle between {x: 20, y: 20} and {x: 80, y:80}
Example:Create a rectangle between {x: 20, y: 20} and {x: 80, y:80}
Rectangle(x, y, width, height)
Creates a rectangle object.
Parameters:
x:
Number
β the left coordinatey:
Number
β the top coordinatewidth:
Number
height:
Number
Returns:
Rectangle
Rectangle(from, to)
Creates a rectangle object from the passed points. These do not necessarily need to be the top left and bottom right corners, the constructor figures out how to fit a rectangle between them.
Parameters:
from:
Point
β the first point defining the rectangleto:
Point
β the second point defining the rectangleReturns:
Rectangle
Rectangle(rectangle)
Creates a new rectangle object from the passed rectangle object.
Parameters:
rectangle:
Rectangle
Returns:
Rectangle
Properties
x
The x position of the rectangle.
Type:
Number
y
The y position of the rectangle.
Type:
Number
width
The width of the rectangle.
Type:
Number
height
The height of the rectangle.
Type:
Number
point
The top-left point of the rectangle
Type:
Point
size
The size of the rectangle
Type:
Size
Side Positions
left
The position of the left hand side of the rectangle. Note that this doesnβt move the whole rectangle; the right hand side stays where it was.
Type:
Number
top
The top coordinate of the rectangle. Note that this doesnβt move the whole rectangle: the bottom wonβt move.
Type:
Number
right
The position of the right hand side of the rectangle. Note that this doesnβt move the whole rectangle; the left hand side stays where it was.
Type:
Number
bottom
The bottom coordinate of the rectangle. Note that this doesnβt move the whole rectangle: the top wonβt move.
Type:
Number
Corner and Center Point Positions
center
The center point of the rectangle.
Type:
Point
topLeft
The top-left point of the rectangle.
Type:
Point
topRight
The top-right point of the rectangle.
Type:
Point
bottomLeft
The bottom-left point of the rectangle.
Type:
Point
bottomRight
The bottom-right point of the rectangle.
Type:
Point
leftCenter
The left-center point of the rectangle.
Type:
Point
topCenter
The top-center point of the rectangle.
Type:
Point
rightCenter
The right-center point of the rectangle.
Type:
Point
bottomCenter
The bottom-center point of the rectangle.
Type:
Point
area
The area of the rectangle.
Read only.
Type:
Number
Item Bounds
selected
Specifies whether an itemβs bounds are to appear as selected.
Paper.js draws the bounds of items with selected bounds on top of your project. This is very useful when debugging.
Default:
false
Type:
Boolean
Example:
Run
Methods
set(...values)
Sets the rectangle to the passed values. Note that any sequence of parameters that is supported by the various
Rectangle
() constructors also work for calls ofset()
.Parameters:
values:
any value
Returns:
Rectangle
clone()
Returns a copy of the rectangle.
Returns:
Rectangle
equals(rect)
Checks whether the coordinates and size of the rectangle are equal to that of the supplied rectangle.
Parameters:
rect:
Rectangle
Returns:
Boolean
βtrue
if the rectangles are equal,false
otherwise
toString()
Returns:
String
β a string representation of this rectangle
isEmpty()
Returns:
Boolean
βtrue
if the rectangle is empty,false
otherwise
Geometric Tests
contains(point)
Tests if the specified point is inside the boundary of the rectangle.
Parameters:
point:
Point
β the specified pointReturns:
Boolean
βtrue
if the point is inside the rectangleβs boundary,false
otherwise
Example:Checking whether the mouse position falls within the bounding rectangle of an item:
Run
contains(rect)
Tests if the interior of the rectangle entirely contains the specified rectangle.
Parameters:
rect:
Rectangle
β the specified rectangleReturns:
Boolean
βtrue
if the rectangle entirely contains the specified rectangle,false
otherwise
Example:Checking whether the bounding box of one item is contained within that of another item:
Run
intersects(rect[, epsilon])
Tests if the interior of this rectangle intersects the interior of another rectangle. Rectangles just touching each other are considered as non-intersecting, except if a
epsilon
value is specified by which this rectangleβs dimensions are increased before comparing.Parameters:
rect:
Rectangle
β the specified rectangleepsilon:
Number
β the epsilon against which to compare the rectangleβs dimensions β optional, default:0
Returns:
Boolean
βtrue
if the rectangle and the specified rectangle intersect each other,false
otherwise
Example:Checking whether the bounding box of one item intersects with that of another item:
Run
Boolean Operations
intersect(rect)
Returns a new rectangle representing the intersection of this rectangle with the specified rectangle.
Parameters:
rect:
Rectangle
β the rectangle to be intersected with this rectangleReturns:
Rectangle
β the largest rectangle contained in both the specified rectangle and in this rectangle
Example:Intersecting two rectangles and visualizing the result using rectangle shaped paths:
Run
unite(rect)
Returns a new rectangle representing the union of this rectangle with the specified rectangle.
Parameters:
rect:
Rectangle
β the rectangle to be combined with this rectangleReturns:
Rectangle
β the smallest rectangle containing both the specified rectangle and this rectangle
include(point)
Adds a point to this rectangle. The resulting rectangle is the smallest rectangle that contains both the original rectangle and the specified point.
After adding a point, a call to
contains(point)
with the added point as an argument does not necessarily returntrue
. Therectangle.contains(point)
method does not returntrue
for points on the right or bottom edges of a rectangle. Therefore, if the added point falls on the left or bottom edge of the enlarged rectangle,rectangle.contains(point)
returnsfalse
for that point.Parameters:
point:
Point
Returns:
Rectangle
β the smallest rectangle that contains both the original rectangle and the specified point
expand(amount)
Returns a new rectangle expanded by the specified amount in horizontal and vertical directions.
Parameters:
amount:
Number
βSize
βPoint
β the amount to expand the rectangle in both directionsReturns:
Rectangle
β the expanded rectangle
expand(hor, ver)
Returns a new rectangle expanded by the specified amounts in horizontal and vertical directions.
Parameters:
hor:
Number
β the amount to expand the rectangle in horizontal directionver:
Number
β the amount to expand the rectangle in vertical directionReturns:
Rectangle
β the expanded rectangle
scale(amount)
Returns a new rectangle scaled by the specified amount from its center.
Parameters:
amount:
Number
Returns:
Rectangle
β the scaled rectangle
scale(hor, ver)
Returns a new rectangle scaled in horizontal direction by the specified
hor
amount and in vertical direction by the specifiedver
amount from its center.Parameters:
hor:
Number
ver:
Number
Returns:
Rectangle
β the scaled rectangle