You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.3 KiB

2 months ago
  1. import { isElement, isHTMLElement } from "./instanceOf.js";
  2. import { round } from "../utils/math.js";
  3. import getWindow from "./getWindow.js";
  4. import isLayoutViewport from "./isLayoutViewport.js";
  5. export default function getBoundingClientRect(element, includeScale, isFixedStrategy) {
  6. if (includeScale === void 0) {
  7. includeScale = false;
  8. }
  9. if (isFixedStrategy === void 0) {
  10. isFixedStrategy = false;
  11. }
  12. var clientRect = element.getBoundingClientRect();
  13. var scaleX = 1;
  14. var scaleY = 1;
  15. if (includeScale && isHTMLElement(element)) {
  16. scaleX = element.offsetWidth > 0 ? round(clientRect.width) / element.offsetWidth || 1 : 1;
  17. scaleY = element.offsetHeight > 0 ? round(clientRect.height) / element.offsetHeight || 1 : 1;
  18. }
  19. var _ref = isElement(element) ? getWindow(element) : window,
  20. visualViewport = _ref.visualViewport;
  21. var addVisualOffsets = !isLayoutViewport() && isFixedStrategy;
  22. var x = (clientRect.left + (addVisualOffsets && visualViewport ? visualViewport.offsetLeft : 0)) / scaleX;
  23. var y = (clientRect.top + (addVisualOffsets && visualViewport ? visualViewport.offsetTop : 0)) / scaleY;
  24. var width = clientRect.width / scaleX;
  25. var height = clientRect.height / scaleY;
  26. return {
  27. width: width,
  28. height: height,
  29. top: y,
  30. right: x + width,
  31. bottom: y + height,
  32. left: x,
  33. x: x,
  34. y: y
  35. };
  36. }