All files / src/projection/geometry conversion.ts

75% Statements 12/16
50% Branches 1/2
66.67% Functions 2/3
92.31% Lines 12/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44              35x 81x 81x 81x 81x   81x           35x                 35x       80x 80x 80x   80x              
import { BoundingBox, Box, TransformPoint } from "./types"
 
/**
 * Bounding boxes tend to be defined as top, left, right, bottom. For various operations
 * it's easier to consider each axis individually. This function returns a bounding box
 * as a map of single-axis min/max values.
 */
export function convertBoundingBoxToBox({
    top,
    left,
    right,
    bottom,
}: BoundingBox): Box {
    return {
        x: { min: left, max: right },
        y: { min: top, max: bottom },
    }
}
 
export function convertBoxToBoundingBox({ x, y }: Box): BoundingBox {
    return { top: y.min, right: x.max, bottom: y.max, left: x.min }
}
 
/**
 * Applies a TransformPoint function to a bounding box. TransformPoint is usually a function
 * provided by Framer to allow measured points to be corrected for device scaling. This is used
 * when measuring DOM elements and DOM event points.
 */
export function transformBoxPoints(
    point: BoundingBox,
    transformPoint?: TransformPoint
) {
    Iif (!transformPoint) return point
    const topLeft = transformPoint({ x: point.left, y: point.top })
    const bottomRight = transformPoint({ x: point.right, y: point.bottom })
 
    return {
        top: topLeft.y,
        left: topLeft.x,
        bottom: bottomRight.y,
        right: bottomRight.x,
    }
}