Skip to content

paintvec


Class: Rect

Defined in: index.ts:459

Rect represents an axis-aligned rectangle in 2D space.

Remarks

All operations are immutable and return new Rect instances. The rectangle is defined by its top-left corner and size.

Example

js
// 100x200 rectangle at (0, 0)
const r1 = new Rect(new Vec2(0, 0), new Vec2(100, 200))
// 200x300 rectangle at (50, 50)
const r2 = new Rect(new Vec2(50, 50), new Vec2(200, 300))

Constructors

Constructor

new Rect(topLeft, size): Rect

Defined in: index.ts:465

Creates a new Rect instance.

Parameters

topLeft

Vec2 = ...

The top-left position (in top-left origin coordinates)

size

Vec2 = ...

The size of the rectangle (width, height)

Returns

Rect

Properties

topLeft

topLeft: Vec2

Defined in: index.ts:466

The top-left position (in top-left origin coordinates)


size

size: Vec2

Defined in: index.ts:467

The size of the rectangle (width, height)


empty

readonly static empty: Rect

Defined in: index.ts:947

Empty rectangle at origin.

Accessors

tl

Get Signature

get tl(): Vec2

Defined in: index.ts:480

Alias for topLeft.

Returns

Vec2


bottomRight

Get Signature

get bottomRight(): Vec2

Defined in: index.ts:488

Gets the bottom-right corner position.

Returns

Vec2

The bottom-right point


br

Get Signature

get br(): Vec2

Defined in: index.ts:493

Alias for bottomRight.

Returns

Vec2


topRight

Get Signature

get topRight(): Vec2

Defined in: index.ts:501

Gets the top-right corner position.

Returns

Vec2

The top-right point


tr

Get Signature

get tr(): Vec2

Defined in: index.ts:506

Alias for topRight.

Returns

Vec2


bottomLeft

Get Signature

get bottomLeft(): Vec2

Defined in: index.ts:514

Gets the bottom-left corner position.

Returns

Vec2

The bottom-left point


bl

Get Signature

get bl(): Vec2

Defined in: index.ts:519

Alias for bottomLeft.

Returns

Vec2


left

Get Signature

get left(): number

Defined in: index.ts:527

Gets the left edge coordinate.

Returns

number

The x-coordinate of the left edge


l

Get Signature

get l(): number

Defined in: index.ts:532

Alias for left.

Returns

number


top

Get Signature

get top(): number

Defined in: index.ts:540

Gets the top edge coordinate.

Returns

number

The y-coordinate of the top edge


t

Get Signature

get t(): number

Defined in: index.ts:545

Alias for top.

Returns

number


Get Signature

get right(): number

Defined in: index.ts:553

Gets the right edge coordinate.

Returns

number

The x-coordinate of the right edge


r

Get Signature

get r(): number

Defined in: index.ts:558

Alias for right.

Returns

number


bottom

Get Signature

get bottom(): number

Defined in: index.ts:566

Gets the bottom edge coordinate.

Returns

number

The y-coordinate of the bottom edge


b

Get Signature

get b(): number

Defined in: index.ts:571

Alias for bottom.

Returns

number


width

Get Signature

get width(): number

Defined in: index.ts:579

Gets the width of this rectangle.

Returns

number

The width


w

Get Signature

get w(): number

Defined in: index.ts:584

Alias for width.

Returns

number


height

Get Signature

get height(): number

Defined in: index.ts:592

Gets the height of this rectangle.

Returns

number

The height


h

Get Signature

get h(): number

Defined in: index.ts:597

Alias for height.

Returns

number


center

Get Signature

get center(): Vec2

Defined in: index.ts:605

Gets the center point of this rectangle.

Returns

Vec2

The center point


topLine

Get Signature

get topLine(): Segment

Defined in: index.ts:613

Gets the top edge as a line segment.

Returns

Segment

A segment from top-left to top-right


bottomLine

Get Signature

get bottomLine(): Segment

Defined in: index.ts:621

Gets the bottom edge as a line segment.

Returns

Segment

A segment from bottom-left to bottom-right


leftLine

Get Signature

get leftLine(): Segment

Defined in: index.ts:629

Gets the left edge as a line segment.

Returns

Segment

A segment from top-left to bottom-left


rightLine

Get Signature

get rightLine(): Segment

Defined in: index.ts:637

Gets the right edge as a line segment.

Returns

Segment

A segment from top-right to bottom-right


startLines

Get Signature

get startLines(): object

Defined in: index.ts:645

Gets the start lines for x and y axes.

Returns

object

An object with x (left) and y (top) line segments

x

x: Segment

y

y: Segment


endLines

Get Signature

get endLines(): object

Defined in: index.ts:659

Gets the end lines for x and y axes.

Returns

object

An object with x (right) and y (bottom) line segments

x

x: Segment

y

y: Segment


area

Get Signature

get area(): number

Defined in: index.ts:739

Gets the area of this rectangle.

Returns

number

The area (width × height)


vertices

Get Signature

get vertices(): [Vec2, Vec2, Vec2, Vec2]

Defined in: index.ts:821

Gets the four corner points of this rectangle.

Returns

[Vec2, Vec2, Vec2, Vec2]

A tuple of [topLeft, topRight, bottomRight, bottomLeft]

Methods

equals()

equals(other): boolean

Defined in: index.ts:475

Checks if this rectangle equals another rectangle.

Parameters

other

Rect

The rectangle to compare with

Returns

boolean

True if position and size are equal


toIntBounding()

toIntBounding(): Rect

Defined in: index.ts:673

Calculates the smallest integer rectangle that contains this rectangle.

Returns

Rect

A new Rect with floored top-left and ceiled bottom-right


translate()

translate(offset): Rect

Defined in: index.ts:684

Translates this rectangle by an offset.

Parameters

offset

Vec2

The translation vector

Returns

Rect

A new translated Rect


inflate()

inflate(offset): Rect

Defined in: index.ts:693

Inflates this rectangle by a uniform amount on all sides.

Parameters

offset

number

The amount to expand (positive) or shrink (negative)

Returns

Rect

A new inflated Rect


inset()

inset(offsets): Rect

Defined in: index.ts:703

Insets this rectangle by the given edge offsets.

Parameters

offsets

EdgeOffsets

The edge offsets to apply

Returns

Rect

A new inset Rect


insetsTo()

insetsTo(other): EdgeOffsets

Defined in: index.ts:714

Calculates the edge offsets from this rectangle to another.

Parameters

other

Rect

The target rectangle

Returns

EdgeOffsets

The EdgeOffsets from this to other


includes()

includes(pos): boolean

Defined in: index.ts:726

Checks if this rectangle contains a point.

Parameters

pos

Vec2

The point to test

Returns

boolean

True if the point is inside or on the rectangle boundary


transform()

transform(transform): Rect

Defined in: index.ts:748

Transforms this rectangle by a transformation matrix.

Parameters

transform

Transform

The transformation to apply

Returns

Rect

The axis-aligned bounding box of the transformed rectangle


union()

union(other): Rect

Defined in: index.ts:761

Returns the union of this rectangle with another.

Parameters

other

Rect

The other rectangle

Returns

Rect

The smallest rectangle containing both rectangles


intersection()

intersection(other): undefined | Rect

Defined in: index.ts:774

Returns the intersection of this rectangle with another.

Parameters

other

Rect

The other rectangle

Returns

undefined | Rect

The overlapping rectangle, or undefined if no overlap


toString()

toString(): string

Defined in: index.ts:791

Returns a string representation of this rectangle.

Returns

string

A string in the format "Rect(topLeft,bottomRight)"


toDOMRect()

toDOMRect(): DOMRect

Defined in: index.ts:800

Converts this rectangle to a DOMRect.

Returns

DOMRect

A DOMRect with the same position and size


toSVGRectProps()

toSVGRectProps(): object

Defined in: index.ts:808

Converts this rectangle to SVG rect element properties.

Returns

object

An object with x, y, width, height properties

x

x: number

y

y: number

width

width: number

height

height: number


union()

static union(...rects): undefined | Rect

Defined in: index.ts:830

Calculates the union of multiple rectangles.

Parameters

rects

...Rect[]

Rectangles to unite

Returns

undefined | Rect

The bounding rectangle, or undefined if no rectangles provided


intersection()

static intersection(...rects): undefined | Rect

Defined in: index.ts:846

Calculates the intersection of multiple rectangles.

Parameters

rects

...Rect[]

Rectangles to intersect

Returns

undefined | Rect

The shared region, or undefined if no overlap or no rectangles


from()

static from(options): Rect

Defined in: index.ts:873

Creates a Rect from various input formats.

Parameters

options

An object with rectangle properties

{ x: number; y: number; width: number; height: number; } | { left: number; top: number; width: number; height: number; } | { left: number; top: number; right: number; bottom: number; } | { topLeft: Vec2; bottomRight: Vec2; } | { topLeft: Vec2; size: Vec2; }

Returns

Rect

A new Rect instance

Example

js
Rect.from({ x: 0, y: 0, width: 100, height: 50 })
Rect.from({ left: 0, top: 0, right: 100, bottom: 50 })
Rect.from({ topLeft: new Vec2(0, 0), size: new Vec2(100, 50) })

boundingRect()

static boundingRect(points): Rect

Defined in: index.ts:934

Creates a bounding rectangle from a set of points.

Parameters

points

Vec2[]

The points to bound

Returns

Rect

A rectangle containing all points


fromSize()

static fromSize(pos, size): Rect

Defined in: index.ts:955

Creates a rectangle from position and size.

Parameters

pos

Vec2

The top-left position

size

Vec2

The size

Returns

Rect

A new Rect


fromTLBR()

static fromTLBR(top, left, bottom, right): Rect

Defined in: index.ts:967

Creates a rectangle from top-left-bottom-right coordinates.

Parameters

top

number

Top edge

left

number

Left edge

bottom

number

Bottom edge

right

number

Right edge

Returns

Rect

A new Rect


fromLTRB()

static fromLTRB(left, top, right, bottom): Rect

Defined in: index.ts:979

Creates a rectangle from left-top-right-bottom coordinates.

Parameters

left

number

Left edge

top

number

Top edge

right

number

Right edge

bottom

number

Bottom edge

Returns

Rect

A new Rect


intersects()

static intersects(a, b): boolean

Defined in: index.ts:989

Checks if two rectangles intersect.

Parameters

a

Rect

First rectangle

b

Rect

Second rectangle

Returns

boolean

True if rectangles overlap

Released under the MIT License.