Skip to content
D
Documentation

Utils

reference
1 min readUpdated

Import these from @grafloria/engine.

Functions

boxesIntersect

Check if two bounding boxes intersect

ts
function boxesIntersect(box1: BoundingBox, box2: BoundingBox): boolean

clampToBox

Clamp point to bounding box

ts
function clampToBox(point: Point, box: BoundingBox): Point

createBoundingBox

Create bounding box from position and size

ts
function createBoundingBox(
  position: Point,
  size: Size
): BoundingBox

deepClone

Deep clone an object with circular reference handling Handles dates, arrays, maps, sets, plain objects, and circular references

ts
function deepClone<T>(value: T, visited: WeakMap<any, any> = new WeakMap()): T

deepEqual

Check if two values are deeply equal

ts
function deepEqual(a: any, b: any): boolean

distance

Calculate distance between two points

ts
function distance(p1: Point, p2: Point): number

expandBox

Expand bounding box by margin

ts
function expandBox(box: BoundingBox, margin: number): BoundingBox

generateId

Generate a short unique ID (12 characters) Used for internal references

ts
function generateId(): string

generatePrefixedId

Generate a prefixed ID

ts
function generatePrefixedId(prefix: string): string

generateUUID

Generate a UUID Used for persistent identifiers

ts
function generateUUID(): string

isPointInShape

Determine if a point is inside a shape Returns true if point (px, py) is within the shape defined by bounds and shapeConfig

Shape-aware hit detection for accurate mouse interactions

ts
function isPointInShape(
  px: number,
  py: number,
  bounds: BoundingBox,
  shapeConfig?: ShapeConfig
): boolean

isValidId

Validate if string is a valid ID format

ts
function isValidId(id: string): boolean

isValidUUID

Validate if string is a valid UUID format

ts
function isValidUUID(uuid: string): boolean

midpoint

Calculate midpoint between two points

ts
function midpoint(p1: Point, p2: Point): Point

pointInBox

Check if point is inside bounding box

ts
function pointInBox(point: Point, box: BoundingBox): boolean

rotatePoint

Rotate point around origin

ts
function rotatePoint(
  point: Point,
  origin: Point,
  angle: number
): Point

scalePoint

Scale point from origin

ts
function scalePoint(
  point: Point,
  origin: Point,
  scale: Point
): Point

Was this page helpful?

Utils — Grafloria