Geometry

Generated by website/scripts/generate-api.ts from packages/gridla/src. Do not edit by hand; run bun run generate in website/.

Rectangle math, normalization, bounds and clamping, content extent, validation, and gap enforcement. These are the building blocks the solvers are made of, exported so you can build your own.

import { boundsFromCanvas, canPlaceItem, canvasesEqual, canvasInnerHeight, canvasInnerRect, canvasInnerWidth, clampItem, cloneItems, contentBottom, contentRight, createItem, enforceMinimumGaps, findLayoutViolations, fitCanvasToContent, inferGap, itemArea, itemBottom, itemRight, layoutIsValid, normalizeCanvas, normalizeItem, normalizeItems, normalizeLayout, normalizePadding, overlapArea, pointInRect, rectsOverlap, rectsViolateGap, rectToEdges, rectToViewportEdges, resizeRect, roundItem, toCanvasItem, toInnerItem } from 'gridla'
import type { LayoutViolation } from 'gridla'
ExportKindSummary
boundsFromCanvasfunctionDerive solver bounds from a canvas.
canPlaceItemfunctionTrue when item fits inside bounds (within one pixel) and does not violate gap against any solid sibling.
canvasesEqualfunctionTrue when two canvases have the same width, height, and padding on every side.
canvasInnerHeightfunctionHeight of the canvas inside its top and bottom padding, in pixels.
canvasInnerRectfunctionThe area inside the canvas padding as a rectangle in canvas coordinates: origin at the padding offset, size from canvasInnerWidth and canvasInnerHeight.
canvasInnerWidthfunctionWidth of the canvas inside its left and right padding, in pixels.
clampItemfunctionFit an item inside bounds without moving it more than necessary.
cloneItemsfunctionShallow-copy every item into a new array.
contentBottomfunctionLowest bottom edge among the items, in canvas coordinates.
contentRightfunctionRightmost right edge among the items, in canvas coordinates.
createItemfunctionBuild a GridItem from an id, a size (with optional constraints), and a top-left position (default 0, 0).
enforceMinimumGapsfunctionPush and trim items so that no two neighbors sit closer than gap.
findLayoutViolationsfunctionReport bounds and overlap violations.
fitCanvasToContentfunctionFor scrollable canvases, grow height so every item fits.
inferGapfunctionSmallest positive distance between any two neighboring items, or 0.
itemAreafunctionArea of a rectangle in square pixels (w * h).
itemBottomfunctionBottom edge of a rectangle (y + h).
itemRightfunctionRight edge of a rectangle (x + w).
layoutIsValidfunctionTrue when every item in the layout can be placed.
LayoutViolationtypeOne problem reported by findLayoutViolations: an item outside the canvas bounds, or two solid items that overlap.
normalizeCanvasfunctionFill in defaults and clamp a canvas so it is at least one pixel larger than its padding on both axes.
normalizeItemfunctionRound an item and fit it inside a canvas, honoring min/max constraints and the canvas height mode.
normalizeItemsfunctionApply normalizeItem to every item: round its geometry and clamp it into the canvas.
normalizeLayoutfunctionNormalize a whole layout: canvas defaults plus every item clamped to it.
normalizePaddingfunctionFill in missing sides with 0, round each side to a whole pixel, and clamp negative values to 0.
overlapAreafunctionArea shared by two rectangles in square pixels.
pointInRectfunctionTrue when the point lies inside the rectangle.
rectsOverlapfunctionTrue when two rectangles share area.
rectsViolateGapfunctionTrue when two rectangles are closer than gap on both axes.
rectToEdgesfunctionConvert an x/y/w/h rectangle into top/right/bottom/left edges.
rectToViewportEdgesfunctionOffset a rect by a container origin, producing viewport edges.
resizeRectfunctionCompute the rectangle produced by dragging one edge or corner of an item by a pixel delta.
roundItemfunctionRound an item's geometry and constraints to whole pixels.
toCanvasItemfunctionTranslate an item from inner coordinates back to canvas coordinates.
toInnerItemfunctionTranslate an item from canvas coordinates to padding-relative inner coordinates.

Functions

boundsFromCanvas

function · core/geometry.ts:301

Derive solver bounds from a canvas. height becomes null for scrollable canvases so items may extend below the visible height.

export function boundsFromCanvas(canvas: GridCanvas): GridBounds

canPlaceItem

function · core/geometry.ts:482

True when item fits inside bounds (within one pixel) and does not violate gap against any solid sibling.

export function canPlaceItem(
  items: readonly GridItem[],
  item: GridItem,
  bounds: GridBounds,
  gap: number,
): boolean

canvasesEqual

function · core/geometry.ts:189

True when two canvases have the same width, height, and padding on every side. heightMode is not compared.

export function canvasesEqual(a: GridCanvas, b: GridCanvas): boolean

canvasInnerHeight

function · core/geometry.ts:168

Height of the canvas inside its top and bottom padding, in pixels. Never less than 1.

export function canvasInnerHeight(canvas: GridCanvas): number

canvasInnerRect

function · core/geometry.ts:176

The area inside the canvas padding as a rectangle in canvas coordinates: origin at the padding offset, size from canvasInnerWidth and canvasInnerHeight.

export function canvasInnerRect(canvas: GridCanvas): GridRect

canvasInnerWidth

function · core/geometry.ts:163

Width of the canvas inside its left and right padding, in pixels. Never less than 1.

export function canvasInnerWidth(canvas: GridCanvas): number

clampItem

function · core/geometry.ts:322

Fit an item inside bounds without moving it more than necessary. Sizes are clamped from the current position first; the position only shifts when the minimum size would not fit otherwise.

export function clampItem<T>(item: GridItem<T>, bounds: GridBounds): GridItem<T>

cloneItems

function · core/geometry.ts:116

Shallow-copy every item into a new array. data is shared, not cloned.

export function cloneItems<T>(items: readonly GridItem<T>[]): GridItem<T>[]

contentBottom

function · core/geometry.ts:419

Lowest bottom edge among the items, in canvas coordinates. Returns the top padding when there are no items.

export function contentBottom(items: readonly GridRect[], canvas: GridCanvas): number

contentRight

function · core/geometry.ts:427

Rightmost right edge among the items, in canvas coordinates. Returns the left padding when there are no items.

export function contentRight(items: readonly GridRect[], canvas: GridCanvas): number

createItem

function · core/geometry.ts:453

Build a GridItem from an id, a size (with optional constraints), and a top-left position (default 0, 0). w and h are clamped to MIN_ITEM_SIZE; constraint fields and data are copied only when defined.

export function createItem<T = unknown>(
  id: string,
  size: GridItemSize,
  x = 0,
  y = 0,
  data?: T,
): GridItem<T>

enforceMinimumGaps

function · core/geometry.ts:598

Push and trim items so that no two neighbors sit closer than gap. Items are processed left-to-right, then top-to-bottom, and clamped to bounds.

export function enforceMinimumGaps<T>(
  items: readonly GridItem<T>[],
  bounds: GridBounds,
  gap: number,
): GridItem<T>[]

findLayoutViolations

function · core/geometry.ts:524

Report bounds and overlap violations. Ghost items and pairs of locked items are exempt from the overlap check.

export function findLayoutViolations(layout: GridLayout): LayoutViolation[]

fitCanvasToContent

function · core/geometry.ts:435

For scrollable canvases, grow height so every item fits. Bounded canvases are returned unchanged.

export function fitCanvasToContent(canvas: GridCanvas, items: readonly GridRect[]): GridCanvas

inferGap

function · core/geometry.ts:623

Smallest positive distance between any two neighboring items, or 0.

export function inferGap(items: readonly GridRect[]): number

itemArea

function · core/geometry.ts:35

Area of a rectangle in square pixels (w * h).

export function itemArea(item: Pick<GridRect, 'w' | 'h'>): number

itemBottom

function · core/geometry.ts:30

Bottom edge of a rectangle (y + h).

export function itemBottom(item: Pick<GridRect, 'y' | 'h'>): number

itemRight

function · core/geometry.ts:25

Right edge of a rectangle (x + w).

export function itemRight(item: Pick<GridRect, 'x' | 'w'>): number

layoutIsValid

function · core/geometry.ts:504

True when every item in the layout can be placed.

export function layoutIsValid(
  items: readonly GridItem[],
  bounds: GridBounds,
  gap: number,
): boolean

normalizeCanvas

function · core/geometry.ts:146

Fill in defaults and clamp a canvas so it is at least one pixel larger than its padding on both axes.

export function normalizeCanvas(
  value: Partial<GridCanvas> | undefined,
  fallback: GridCanvas = DEFAULT_CANVAS,
): GridCanvas

normalizeItem

function · core/geometry.ts:231

Round an item and fit it inside a canvas, honoring min/max constraints and the canvas height mode.

export function normalizeItem<T>(item: GridItem<T>, canvas: GridCanvas): GridItem<T>

normalizeItems

function · core/geometry.ts:280

Apply normalizeItem to every item: round its geometry and clamp it into the canvas.

export function normalizeItems<T>(
  items: readonly GridItem<T>[],
  canvas: GridCanvas,
): GridItem<T>[]

normalizeLayout

function · core/geometry.ts:288

Normalize a whole layout: canvas defaults plus every item clamped to it.

export function normalizeLayout<T>(layout: GridLayout<T>): GridLayout<T>

normalizePadding

function · core/geometry.ts:133

Fill in missing sides with 0, round each side to a whole pixel, and clamp negative values to 0.

export function normalizePadding(value: Partial<GridPadding> = {}): GridPadding

overlapArea

function · core/geometry.ts:61

Area shared by two rectangles in square pixels. 0 when they do not overlap.

export function overlapArea(left: GridRect, right: GridRect): number

pointInRect

function · core/geometry.ts:94

True when the point lies inside the rectangle. Edges are inclusive.

export function pointInRect(point: GridPoint, rect: GridRect): boolean

rectsOverlap

function · core/geometry.ts:40

True when two rectangles share area. Touching edges do not count.

export function rectsOverlap(left: GridRect, right: GridRect): boolean

rectsViolateGap

function · core/geometry.ts:50

True when two rectangles are closer than gap on both axes.

export function rectsViolateGap(left: GridRect, right: GridRect, gap: number): boolean

rectToEdges

function · core/geometry.ts:104

Convert an x/y/w/h rectangle into top/right/bottom/left edges.

export function rectToEdges(rect: GridRect): GridEdges

rectToViewportEdges

function · core/geometry.ts:109

Offset a rect by a container origin, producing viewport edges.

export function rectToViewportEdges(origin: GridPoint, rect: GridRect): GridEdges

resizeRect

function · core/geometry.ts:361

Compute the rectangle produced by dragging one edge or corner of an item by a pixel delta. The opposite edge stays anchored. Constraints and bounds are respected; siblings are not considered.

export function resizeRect<T>(
  item: GridItem<T>,
  edge: GridResizeEdge,
  delta: GridPoint,
  bounds: GridBounds,
): GridItem<T>

roundItem

function · core/geometry.ts:211

Round an item's geometry and constraints to whole pixels.

export function roundItem<T>(item: GridItem<T>): GridItem<T>

toCanvasItem

function · core/geometry.ts:206

Translate an item from inner coordinates back to canvas coordinates.

export function toCanvasItem<T>(item: GridItem<T>, canvas: GridCanvas): GridItem<T>

toInnerItem

function · core/geometry.ts:201

Translate an item from canvas coordinates to padding-relative inner coordinates.

export function toInnerItem<T>(item: GridItem<T>, canvas: GridCanvas): GridItem<T>

Types

LayoutViolation

type · core/geometry.ts:516

One problem reported by findLayoutViolations: an item outside the canvas bounds, or two solid items that overlap.

export type LayoutViolation =
  | { kind: 'out-of-bounds'; itemId: string }
  | { kind: 'overlap'; itemId: string; otherId: string }
MemberTypeDescription
kind`"out-of-bounds" \"overlap"`
itemIdstring