element.js 729 B

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2020 The noVNC Authors
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. /*
  9. * HTML element utility functions
  10. */
  11. export function clientToElement(x, y, elem) {
  12. const bounds = elem.getBoundingClientRect();
  13. let pos = { x: 0, y: 0 };
  14. // Clip to target bounds
  15. if (x < bounds.left) {
  16. pos.x = 0;
  17. } else if (x >= bounds.right) {
  18. pos.x = bounds.width - 1;
  19. } else {
  20. pos.x = x - bounds.left;
  21. }
  22. if (y < bounds.top) {
  23. pos.y = 0;
  24. } else if (y >= bounds.bottom) {
  25. pos.y = bounds.height - 1;
  26. } else {
  27. pos.y = y - bounds.top;
  28. }
  29. return pos;
  30. }