Rect
constructorβ
g.Rect(x, y, width, height)
Return a new rectangle object with top left corner at point with coordinates x
, y
and dimensions width
and height
.
Also accepts a single argument in the form of an object { x: [number], y: [number], width: [number], height: [number] }
.
Methodsβ
bbox()β
rect.bbox([angle])
Return a rectangle that is the bounding box of the rectangle.
If angle
is specified, the bounding box calculation will take into account the rotation of the rectangle by angle
degrees around its center.
bottomLeft()β
rect.bottomLeft()
Return the point that is the bottom left corner of the rectangle.
bottomLine()β
rect.bottomLine()
Return the bottom line of the rectangle.
bottomMiddle()β
rect.bottomMiddle()
Return the point that is at the bottom middle of the rectangle.
bottomRight()β
rect.bottomRight()
Return the point that is the bottom right corner of the rectangle.
center()β
rect.center()
Return the point that is the center of the rectangle.
clone()β
rect.clone()
Return another rectangle which is a clone of the rectangle.
containsPoint()β
rect.containsPoint(p [, opt])
Return true
if the point p
is inside the rectangle (inclusive). Return false
otherwise.
If opt.strict
is set to true
, the point must be strictly inside the rectangle (i.e. not on the boundary).
containsRect()β
rect.containsRect(r)
Return true
if the rectangle r
is (completely) inside the rectangle (inclusive). Return false
otherwise.
corner()β
rect.corner()
Return the point that is the bottom right corner of the rectangle.
equals()β
rect.equals(r)
Return true
if the rectangle equals another rectangle r
Return false
otherwise.
inflate()β
rect.inflate(dx [, dy])
Return a rectangle inflated in axis x by 2 * dx and in axis y by 2 * dy.
When the function is called with a single parameter, the resulting rectangle is inflated by 2 * dx in both x and y axis.
intersect()β
rect.intersect(r)
Return a rectangle object that is a subtraction of the two rectangles if such an object exists (the two rectangles intersect). Return null
otherwise.
intersectionWithLine()β
rect.intersectionWithLine(line)
Return an array of the intersection points of the rectangle and the line. Return null
if no intersection exists.
intersectionWithLineFromCenterToPoint()β
rect.intersectionWithLineFromCenterToPoint(p [, angle])
Return the point on the boundary of the rectangle that is the intersection of the rectangle with a line starting in the center of the rectangle ending in the point p
.
If angle
is specified, the intersection will take into account the rotation of the rectangle by angle
degrees around its center.
leftLine()β
rect.leftLine()
Return the left line of the rectangle.
leftMiddle()β
rect.leftMiddle()
Return the point that is at the left middle of the rectangle.
maxRectScaleToFit()β
rect.maxRectScaleToFit(limitRectangle [, origin])
Returns an object where sx
and sy
give the maximum scaling that can be applied to the rectangle so that it would still fit into limitRectangle
.
If origin
is specified, the rectangle is scaled around it; otherwise, it is scaled around its center.
maxRectUniformScaleToFit()β
rect.maxRectUniformScaleToFit(limitRectangle [, origin])
Returns a number that specifies the maximum scaling that can be applied to the rectangle along both axes so that it would still fit into limitRectangle
.
If origin
is specified, the rectangle is scaled around it; otherwise, it is scaled around its center.
moveAndExpand()β
rect.moveAndExpand(r)
Offset the rectangle by r.x
and r.y
and expand it by r.width
and r.height
.
normalize()β
rect.normalize()
Normalize the rectangle, i.e. make it so that it has non-negative width and height.
If width is less than 0
, the function swaps left and right corners and if height is less than 0
, the top and bottom corners are swapped.
offset()β
rect.offset(dx [, dy])
Offset the rectangle (change its x
and y
coordinates) by dx
in x-axis and dy
in y-axis.
If only dx
is specified and is a number, dy
is considered to be zero. If only dx
is specified and is an object, it is considered to be another point or an object in the form { x: [number], y: [number] }
origin()β
rect.origin()
Return the point that is the top left corner of the rectangle.
pointNearestToPoint()β
rect.pointNearestToPoint(p)
Return the point on the boundary of the rectangle nearest to the point p
.
rightLine()β
rect.rightLine()
Return the right line of the rectangle.
rightMiddle()β
rect.rightMiddle()
Return the point that is at the right middle of the rectangle.
rotateAroundCenter()β
rect.rotateAroundCenter(angle)
Rotates the rectangle around its center. The method updates the size and the position to match the bounding box of the rotated rectangle.
round()β
rect.round([precision])
Round all coordinates of the rectangle to the given precision.
Default precision is 0
.
Modifies this rectangle in-place, and returns it.
scale()β
rect.scale(sx, sy [, origin])
Scale the rectangle by sx
,sy
around the given origin
.
If origin is not specified, the rectangle is scaled around the point 0,0.
sideNearestToPoint()β
rect.sideNearestToPoint(p)
Return a string ("top"
, "left"
, "right"
or "bottom"
) denoting the side of the rectangle which is nearest to the point p
.
snapToGrid()β
rect.snapToGrid(gx, gy)
Adjust the position and dimensions of the rectangle such that its edges are on the nearest increment of gx
on the x-axis and gy
on the y-axis.
toJSON()β
rect.toJSON()
Return the rectangle as a simple JSON object. For example: { "x": 0, "y": 0, "width": 40, "height": 40 }
.
toString()β
rect.toString()
Returns the rectangle as a string.
new g.Rect(10, 20, 30, 40).toString() = "10@20 40@60"
topLeft()β
rect.topLeft()
Return the point that is the top left corner of the rectangle.
topLine()β
rect.topLine()
Return the top line of the rectangle.
topMiddle()β
rect.topMiddle()
Return the point that is at the top middle of the rectangle.
topRight()β
rect.topRight()
Return the point that is the top right corner of the rectangle.
translate()β
rect.translate(tx [, ty])
Translate the rectangle by tx
on the x-axis and ty
on the y-axis.
If only tx
is specified and is a number, ty
is considered to be zero. If only tx
is specified and is an object, it is considered to be a point or an object in the form { x: [number], y: [number] }
union()β
rect.union(r)
Return a rectangle that is a union of this rectangle and rectangle r
.
update()β
rect.update(x, y, width, height)
rect.update(r)
Update the rect's x
, y
, width
and height
properties with new values and return the rect itself. Useful for chaining.
Also accepts a single argument in the form of an object { x: [number], y: [number], width: [number], height: [number] }
.
Static methodsβ
fromEllipse()β
g.Rect.fromEllipse(ellipse)
Returns a new rectangle object from the given ellipse.
fromPointUnion()β
g.Rect.fromPointUnion(...points)
Returns a new rectangle object that is large enough to contain given points
, where each point is an object with x
and y
properties. Examples:
const rect = g.Rect.fromPointUnion(new g.Point(0, 0), new g.Point(20, 20));
const rect2 = g.Rect.fromPointUnion({ x: 0, y: 0 }, { x: 20, y: 20 });
// { x: 0, y: 0, width: 20, height: 20 }
fromRectUnion()β
g.Rect.fromRectUnion(...rects)
Returns a new rectangle object that is large enough to contain given rects
, where each rect is an object with x
, y
, width
and height
properties. Examples:
const rect = g.Rect.fromRectUnion(new g.Rect(0, 0, 10, 10), new g.Rect(10, 10, 10, 10));
const rect2 = g.Rect.fromRectUnion({ x: 0, y: 0, height: 10, width: 10 }, { x: 10, y: 10, height: 10, width: 10 });
// { x: 0, y: 0, width: 20, height: 20 }