Skip to content

contains

Whether a point or a rectangle is inside a rectangle.

For a point (hit-testing), the left and top edges are inside and the right and bottom ones are not, so a point on the edge between two adjacent rectangles hits exactly one of them. A rectangle is inside when it does not stick out: it may touch any edge.

Available since v3.1.5

import { contains } from '@helpers4/geometry';
// or, from the all-in-one package (same code, one install):
import { contains } from 'helpers4/geometry';
contains(r: Rect, target: Point | Rect): boolean
ParameterTypeDescription
rRectThe containing rectangle
targetPoint | RectA point, or a rectangle

boolean — true if target is inside r

Shared edges hit once: the right and bottom edges are outside.

const hit = hotspots.find((zone) => contains(zone, { x: event.offsetX, y: event.offsetY }));

A rectangle is inside when it does not stick out.

contains(viewport, layerBounds)

A point in 2D space.

interface Point {
  x: number;
  y: number;
}

An axis-aligned rectangle: its top-left corner and its size. Width and height are expected to be non-negative.

interface Rect {
  height: number;
  width: number;
  x: number;
  y: number;
}

View source on GitHub