Skip to content

@ue-too/board / index / clampPoint

函式: clampPoint()

clampPoint(point, boundaries): Point

定義於: packages/board/src/camera/utils/position.ts:248

Clamps a point to stay within specified boundaries.

參數

point

Point

Point to clamp in world coordinates

boundaries

Optional boundary constraints

Boundaries | undefined

回傳

Point

Clamped point, or original if already within bounds or no boundaries

備註

Each axis is clamped independently:

  • If a min constraint exists on an axis, ensures point >= min
  • If a max constraint exists on an axis, ensures point <= max
  • If no constraint exists on an axis, that axis is unchanged

範例

typescript
const bounds: Boundaries = {
  min: { x: -100, y: -50 },
  max: { x: 100, y: 50 }
};

clampPoint({ x: 0, y: 0 }, bounds);       // { x: 0, y: 0 } (inside)
clampPoint({ x: 150, y: 0 }, bounds);     // { x: 100, y: 0 } (clamped x)
clampPoint({ x: 0, y: -100 }, bounds);    // { x: 0, y: -50 } (clamped y)
clampPoint({ x: 200, y: -200 }, bounds);  // { x: 100, y: -50 } (both clamped)
clampPoint({ x: 0, y: 0 }, undefined);    // { x: 0, y: 0 } (no bounds)