conversion.mjs 1.2 KB

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