| 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 | /* |
| 10 | * HTML element utility functions |
| 11 | */ |
| 12 | |
| 13 | export function clientToElement(x, y, elem) { |
| 14 | const bounds = elem.getBoundingClientRect(); |
| 15 | let pos = { x: 0, y: 0 }; |
| 16 | // Clip to target bounds |
| 17 | if (x < bounds.left) { |
| 18 | pos.x = 0; |
| 19 | } else if (x >= bounds.right) { |
| 20 | pos.x = bounds.width - 1; |
| 21 | } else { |
| 22 | pos.x = x - bounds.left; |
| 23 | } |
| 24 | if (y < bounds.top) { |
| 25 | pos.y = 0; |
| 26 | } else if (y >= bounds.bottom) { |
| 27 | pos.y = bounds.height - 1; |
| 28 | } else { |
| 29 | pos.y = y - bounds.top; |
| 30 | } |
| 31 | return pos; |
| 32 | } |