Sign up (with export icon)

Rect

Api-class iconclass

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

Methods

  • Chevron-right icon

    constructor( source )

    Creates 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 code

    Note: By default a rect of an HTML element includes its CSS borders and scrollbars (if any) ant the rect of a window includes scrollbars too. Use excludeScrollbarsAndBorders to get the inner part of the rect.

    Parameters

    source : RectSource

    A source object to create the rect.

  • Chevron-right icon

    clone() → Rect

    Returns a clone of the rect.

    Returns

    Rect

    A cloned rect.

  • Chevron-right icon

    contains( anotherRect ) → boolean

    Checks whether a rect fully contains another rect instance.

    Parameters

    anotherRect : Rect

    Returns

    boolean

    true if contains, false otherwise.

  • Chevron-right icon

    Excludes scrollbars and CSS borders from the rect.

    • Borders are removed when _source is an HTML element.
    • Scrollbars are excluded from HTML elements and the window.

    Returns

    this

    A rect which has been updated.

  • Chevron-right icon

    getArea() → number

    Returns the area of the rect.

    Returns

    number
  • Chevron-right icon

    getIntersection( anotherRect ) → null | Rect

    Returns a new rect a a result of intersection with another rect.

    Parameters

    anotherRect : Rect

    Returns

    null | Rect
  • Chevron-right icon

    getIntersectionArea( anotherRect ) → number

    Returns the area of intersection with another rect.

    Parameters

    anotherRect : Rect

    Returns

    number

    Area of intersection.

  • Chevron-right icon

    getVisible() → null | Rect

    Returns 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 have overflow set 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, null is 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 code

    Returns

    null | Rect

    A visible rect instance or null, if there's none.

  • Chevron-right icon

    isEqual( anotherRect ) → boolean

    Checks if all property values (top, left, right, bottom, width and height) are the equal in both rect instances.

    Parameters

    anotherRect : Rect

    A rect instance to compare with.

    Returns

    boolean

    true when Rects are equal. false otherwise.

  • Chevron-right icon

    moveBy( x, y ) → this

    Moves the rect in–place by a dedicated offset.

    Parameters

    x : number

    A horizontal offset.

    y : number

    A vertical offset

    Returns

    this

    A rect which has been moved.

  • Chevron-right icon

    moveTo( x, y ) → this

    Moves the rect so that its upper–left corner lands in desired [ x, y ] location.

    Parameters

    x : number

    Desired horizontal location.

    y : number

    Desired vertical location.

    Returns

    this

    A rect which has been moved.

  • Chevron-right icon

    Recalculates screen coordinates to coordinates relative to the positioned ancestor offset.

    Returns

    Rect

Static methods

  • Chevron-right icon

    getBoundingRect( rects ) → null | Rect
    static

    Returns a bounding rectangle that contains all the given rects.

    Parameters

    rects : Iterable<Rect>

    A list of rectangles that should be contained in the result rectangle.

    Returns

    null | Rect

    Bounding rectangle or null if no rects were given.

  • Chevron-right icon

    getDomRangeRects( range ) → Array<Rect>
    static

    Returns an array of rects of the given native DOM Range.

    Parameters

    range : Range

    A native DOM range.

    Returns

    Array<Rect>

    DOM Range rects.