Skip to main content

Overview

The Rectangle API provides utilities for working with axis-aligned rectangles defined by a position and size. All operations are immutable and return new rectangle instances.

Types

Rectangle

Represents an axis-aligned rectangle in 2D space.
interface Rectangle {
  position: Vector2;
  size: Vector2;
}
position
Vector2
required
The top-left corner of the rectangle
size
Vector2
required
The width (x) and height (y) of the rectangle

Constructor functions

of

Creates a rectangle from a position vector and size vector.
rectangle.of(position: Vector2, size: Vector2): Rectangle
position
Vector2
required
The top-left corner position
size
Vector2
required
The width and height as a vector
result
Rectangle
A new rectangle with the specified position and size
Example:
import { rectangle, vector2 } from "@temelj/math";

const rect = rectangle.of(
  vector2.of(10, 20),
  vector2.of(100, 50)
);
console.log(rect);
// { position: { x: 10, y: 20 }, size: { x: 100, y: 50 } }

ofBounds

Creates a rectangle from explicit x, y, width, and height values.
rectangle.ofBounds(x: number, y: number, width: number, height: number): Rectangle
x
number
required
The x coordinate of the top-left corner
y
number
required
The y coordinate of the top-left corner
width
number
required
The width of the rectangle
height
number
required
The height of the rectangle
result
Rectangle
A new rectangle with the specified bounds
Example:
const rect = rectangle.ofBounds(10, 20, 100, 50);
console.log(rect);
// { position: { x: 10, y: 20 }, size: { x: 100, y: 50 } }

fromDOM

Converts a browser DOMRect to a Rectangle.
rectangle.fromDOM(rect: DOMRect): Rectangle
rect
DOMRect
required
The DOMRect to convert (e.g., from element.getBoundingClientRect())
result
Rectangle
A new rectangle with the DOMRect’s bounds
Example:
const element = document.getElementById("myElement");
const domRect = element.getBoundingClientRect();
const rect = rectangle.fromDOM(domRect);

Operations

expand

Expands rectangle a to include rectangle b, creating a bounding box.
rectangle.expand(a: Rectangle, b: Rectangle): Rectangle
a
Rectangle
required
The first rectangle
b
Rectangle
required
The rectangle to include in the bounds
result
Rectangle
A new rectangle that encompasses both input rectangles
Example:
const rect1 = rectangle.ofBounds(0, 0, 50, 50);
const rect2 = rectangle.ofBounds(40, 40, 60, 60);
const expanded = rectangle.expand(rect1, rect2);
console.log(expanded);
// { position: { x: 0, y: 0 }, size: { x: 100, y: 100 } }

center

Calculates the center point of a rectangle.
rectangle.center(r: Rectangle): Vector2
r
Rectangle
required
The rectangle to find the center of
result
Vector2
The center point of the rectangle
Example:
const rect = rectangle.ofBounds(0, 0, 100, 50);
const centerPoint = rectangle.center(rect);
console.log(centerPoint); // { x: 50, y: 25 }

overlaps

Checks if two rectangles overlap or intersect.
rectangle.overlaps(a: Rectangle, b: Rectangle): boolean
a
Rectangle
required
The first rectangle
b
Rectangle
required
The second rectangle
result
boolean
True if the rectangles overlap, false otherwise
Example:
const rect1 = rectangle.ofBounds(0, 0, 50, 50);
const rect2 = rectangle.ofBounds(40, 40, 60, 60);
const rect3 = rectangle.ofBounds(100, 100, 50, 50);

console.log(rectangle.overlaps(rect1, rect2)); // true
console.log(rectangle.overlaps(rect1, rect3)); // false

displayString

Converts a rectangle to a readable string representation.
rectangle.displayString(r: Rectangle): string
r
Rectangle
required
The rectangle to convert
result
string
A formatted string representation
Example:
const rect = rectangle.ofBounds(10, 20, 100, 50);
console.log(rectangle.displayString(rect));
// "Rectangle(10, 20, 100, 50)"

Common use cases

Collision detection

function checkCollision(player: Rectangle, enemy: Rectangle): boolean {
  return rectangle.overlaps(player, enemy);
}

Bounding multiple elements

function getBoundingBox(elements: Rectangle[]): Rectangle {
  return elements.reduce((acc, rect) => 
    rectangle.expand(acc, rect)
  );
}

Converting DOM elements

function getElementRectangle(element: HTMLElement): Rectangle {
  return rectangle.fromDOM(element.getBoundingClientRect());
}

Build docs developers (and LLMs) love