substance
Version:
Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing system. It is developed to power our online editing platform [Substance](http://substance.io).
20 lines (18 loc) • 681 B
JavaScript
/**
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) {
const containerElRect = containerEl.getBoundingClientRect()
const left = mouseEvent.clientX - containerElRect.left
const top = mouseEvent.clientY - containerElRect.top
const res = {
left: left,
right: containerElRect.width - left,
top: top,
bottom: containerElRect.height - top
}
return res
}