UNPKG

@snap/camera-kit

Version:
62 lines 1.84 kB
import { isRecord } from "../common/typeguards"; const REGION_TYPE_MAPPING = { roundButton: "round_button", topBar: "top_bar", keyboard: "keyboard", safeRender: "safe_render", captureExitButton: "capture_exit_button", }; export function isScreenRegions(value) { if (!isRecord(value)) return false; for (const [key, rect] of Object.entries(value)) { if (!(key in REGION_TYPE_MAPPING)) return false; if (!isNormalizedRectLike(rect)) return false; } return true; } const EPS = 1e-6; const isFiniteNumber = (v) => typeof v === "number" && Number.isFinite(v); function isNormalizedRectLike(obj) { if (!isRecord(obj)) return false; const x = obj.x; const y = obj.y; const width = obj.width; const height = obj.height; if (![x, y, width, height].every(isFiniteNumber)) return false; if (width < 0 || height < 0) return false; const x2 = x + width; const y2 = y + height; if (x < -EPS || y < -EPS) return false; if (x > 1 + EPS || y > 1 + EPS) return false; if (x2 > 1 + EPS || y2 > 1 + EPS) return false; return true; } export function prepareScreenRegionsUpdate(newRegions) { const mappedRegions = {}; for (const [regionType, region] of Object.entries(newRegions)) { const lensCoreRegionType = REGION_TYPE_MAPPING[regionType]; mappedRegions[lensCoreRegionType] = { normalizedRect: { origin: { x: region.x, y: region.y, }, size: { width: region.width, height: region.height, }, }, }; } return mappedRegions; } //# sourceMappingURL=screenRegions.js.map