Skip to main content

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)

Return true if the point p is inside the rectangle (inclusive). Return false otherwise.

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 }