point.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Types
  2. /** Convert a point in local space to viewport space */
  3. export function elementToViewport(point, offset) {
  4. return {
  5. x: point.x + offset.x,
  6. y: point.y + offset.y
  7. };
  8. }
  9. /** Convert a point in viewport space to local space */
  10. export function viewportToElement(point, offset) {
  11. return {
  12. x: point.x - offset.x,
  13. y: point.y - offset.y
  14. };
  15. }
  16. /** Get the difference between two points */
  17. export function getOffset(a, b) {
  18. return {
  19. x: a.x - b.x,
  20. y: a.y - b.y
  21. };
  22. }
  23. /** Convert an anchor object to a point in local space */
  24. export function anchorToPoint(anchor, box) {
  25. if (anchor.side === 'top' || anchor.side === 'bottom') {
  26. const {
  27. side,
  28. align
  29. } = anchor;
  30. const x = align === 'left' ? 0 : align === 'center' ? box.width / 2 : align === 'right' ? box.width : align;
  31. const y = side === 'top' ? 0 : side === 'bottom' ? box.height : side;
  32. return elementToViewport({
  33. x,
  34. y
  35. }, box);
  36. } else if (anchor.side === 'left' || anchor.side === 'right') {
  37. const {
  38. side,
  39. align
  40. } = anchor;
  41. const x = side === 'left' ? 0 : side === 'right' ? box.width : side;
  42. const y = align === 'top' ? 0 : align === 'center' ? box.height / 2 : align === 'bottom' ? box.height : align;
  43. return elementToViewport({
  44. x,
  45. y
  46. }, box);
  47. }
  48. return elementToViewport({
  49. x: box.width / 2,
  50. y: box.height / 2
  51. }, box);
  52. }
  53. //# sourceMappingURL=point.mjs.map