all files / util/ getRelativeMouseBounds.js

16.67% Statements 1/6
100% Branches 0/0
0% Functions 0/1
16.67% Lines 1/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19                                   
/**
  Get bounding bounds for a given mouse event, relative parent element.
 
  @param {MouseEvent} mouseEvent the source mouse event
  @param {DOMElement} containerEl used as a reference point to calculate position
  @return {object} bound description with left, top, right, bottom
*/
export default function getRelativeMouseBounds(mouseEvent, containerEl) {
  let containerElRect = containerEl.getBoundingClientRect()
  let left = mouseEvent.clientX - containerElRect.left
  let top = mouseEvent.clientY - containerElRect.top
  let res = {
    left: left,
    right: containerElRect.width - left,
    top: top,
    bottom: containerElRect.height - top
  }
  return res;
}