Rect
A helper class representing a ClientRect object, e.g. value returned by the native object.getBoundingClientRect() method. Provides a set of methods to manipulate the rect and compare it against other rect instances.
Properties
bottom : numberreadonlymodule:utils/dom/rect~Rect#bottomThe "bottom" value of the rect.
height : numberreadonlymodule:utils/dom/rect~Rect#heightThe "height" value of the rect.
left : numberreadonlymodule:utils/dom/rect~Rect#leftThe "left" value of the rect.
right : numberreadonlymodule:utils/dom/rect~Rect#rightThe "right" value of the rect.
top : numberreadonlymodule:utils/dom/rect~Rect#topThe "top" value of the rect.
width : numberreadonlymodule:utils/dom/rect~Rect#widthThe "width" value of the rect.
_source : RectSourceprivatereadonlymodule:utils/dom/rect~Rect#_sourceThe object this rect is for.
Methods
constructor( source )module:utils/dom/rect~Rect#constructorCreates an instance of rect.
// Rect of an HTMLElement. const rectA = new Rect( document.body ); // Rect of a DOM Range. const rectB = new Rect( document.getSelection().getRangeAt( 0 ) ); // Rect of a window (web browser viewport). const rectC = new Rect( window ); // Rect out of an object. const rectD = new Rect( { top: 0, right: 10, bottom: 10, left: 0, width: 10, height: 10 } ); // Rect out of another Rect instance. const rectE = new Rect( rectD ); // Rect out of a ClientRect. const rectF = new Rect( document.body.getClientRects().item( 0 ) );Copy codeNote: By default a rect of an HTML element includes its CSS borders and scrollbars (if any) ant the rect of a
windowincludes scrollbars too. UseexcludeScrollbarsAndBordersto get the inner part of the rect.Parameters
source : RectSourceA source object to create the rect.
module:utils/dom/rect~Rect#clonecontains( anotherRect ) → booleanmodule:utils/dom/rect~Rect#containsChecks whether a rect fully contains another rect instance.
Parameters
anotherRect : Rect
Returns
booleantrueif contains,falseotherwise.
excludeScrollbarsAndBorders() → thismodule:utils/dom/rect~Rect#excludeScrollbarsAndBordersExcludes scrollbars and CSS borders from the rect.
- Borders are removed when
_sourceis an HTML element. - Scrollbars are excluded from HTML elements and the
window.
Returns
thisA rect which has been updated.
- Borders are removed when
getArea() → numbermodule:utils/dom/rect~Rect#getAreagetIntersection( anotherRect ) → null | Rectmodule:utils/dom/rect~Rect#getIntersectiongetIntersectionArea( anotherRect ) → numbermodule:utils/dom/rect~Rect#getIntersectionAreaReturns the area of intersection with another rect.
Parameters
anotherRect : Rect
Returns
numberArea of intersection.
getVisible() → null | Rectmodule:utils/dom/rect~Rect#getVisibleReturns a new rect, a part of the original rect, which is actually visible to the user and is relative to the,
body, e.g. an original rect cropped by parent element rects which haveoverflowset in CSS other than"visible".If there's no such visible rect, which is when the rect is limited by one or many of the ancestors,
nullis returned.Note: This method does not consider the boundaries of the viewport (window). To get a rect cropped by all ancestors and the viewport, use an intersection such as:
const visibleInViewportRect = new Rect( window ).getIntersection( new Rect( source ).getVisible() );Copy codeReturns
null | RectA visible rect instance or
null, if there's none.
isEqual( anotherRect ) → booleanmodule:utils/dom/rect~Rect#isEqualmoveBy( x, y ) → thismodule:utils/dom/rect~Rect#moveByMoves the rect in–place by a dedicated offset.
Parameters
x : numberA horizontal offset.
y : numberA vertical offset
Returns
thisA rect which has been moved.
moveTo( x, y ) → thismodule:utils/dom/rect~Rect#moveToMoves the rect so that its upper–left corner lands in desired
[ x, y ]location.Parameters
x : numberDesired horizontal location.
y : numberDesired vertical location.
Returns
thisA rect which has been moved.
toAbsoluteRect() → Rectmodule:utils/dom/rect~Rect#toAbsoluteRectRecalculates screen coordinates to coordinates relative to the positioned ancestor offset.
Returns
Static methods
getBoundingRect( rects ) → null | Rectstaticmodule:utils/dom/rect~Rect.getBoundingRectgetDomRangeRects( range ) → Array<Rect>staticmodule:utils/dom/rect~Rect.getDomRangeRectsReturns an array of rects of the given native DOM Range.
Parameters
range : RangeA native DOM range.
Returns
Array<Rect>DOM Range rects.