Rectangle

Encapsulates rectangular areas for comparison, intersection etc.

Note that the right and bottom properties are exclusive.

Properties

9
bottom: Number

Get/sets the bottom edge of the Rectangle. Note that the requested height will change.

The bottom edge value is exclusive of the calculated rectangle height. So y=0 and bottom=10 means a height of 10.

The center point of this rectangle.

height: Number

Get/sets the height of the Rectangle. Note that the requested bottom will change.

left: Number

Alias for x. To match DOMRect.

right: Number

Get/sets the right edge of the Rectangle. Note that the requested width will change.

The right edge value is exclusive of the calculated rectangle width. So x=0 and right=10 means a width of 10.

top: Number

Alias for y. To match DOMRect.

width: Number

Get/sets the width of the Rectangle. Note that the requested right will change.

x: Number

Get/sets the X coordinate of the Rectangle. Note that this does not translate the Rectangle. The requested width will change.

y: Number

Get/sets the Y coordinate of the Rectangle. Note that this does not translate the Rectangle. The requested height will change.

Functions

17

Modifies the bounds of this Rectangle by the specified deltas.

ParameterTypeDescription
xNumber

How much to add to the x position.

yNumber

How much to add to the y position.

widthNumber

How much to add to the width.

heightNumber

How much to add to the height.

Returns: Rectangle -

This Rectangle

Returns a cloned version of this Rectangle aligned to a target Rectangle, or element or Widget.

ParameterTypeDescription
specObject

Alignment specification.

spec.targetHTMLElement | Widget | Rectangle

The Widget or element or Rectangle to align to.

spec.inflateNumber | Number[]

Arguments to inflate. Specify as a number to inflate the target rectangle by that many pixels, or as an array of inflate values for each edge.

spec.anchorSizeNumber[]

The [width, height] of the anchor pointer when in top position. The width is the baseline length, and the height is the height of the arrow. If passed, the anchor position will be calculated to be at the centre of the overlap of the two aligned edges and returned in the anchor property of the resulting Rectangle:

{
    edge: 'top',         // or 'right' or 'bottom' or 'left'
    x/y: offset          // dimension to translate and value to translate by.
}
spec.anchorPositionObject

an { x: n, y: n } anchor translation to be used if the requested alignment succeeds without violating constraints. If a fallback alignment is used, the anchor will be centered in the overlap of the aligned edges as usual.

spec.overlapBoolean

True to allow this to overlap the target.

spec.alignString

The edge alignment specification string, specifying two points to bring together.

Each point is described by an edge initial (t for top edge, b for bottom edge etc) followed by a percentage along that edge.

So the form would be [trblc][n]-[trblc][n]. The n is the percentage offset along that edge which defines the alignment point. This is not valid for alignment point c which means the center point.

For example t0-b0' would align this Rectangle's top left corner with the bottom left corner of the target`.

spec.constrainToHTMLElement | Widget | Rectangle

The Widget or Element or Rectangle to constrain to. If the requested alignment cannot be constrained (it will first shrink the resulting Rectangle according to the minWidth and minHeight properties of this rectangle), then it will try aligning at other edges (honouring the axisLock option), and pick the fallback alignment which results in the shortest translation.

spec.axisLockBoolean

Specify as a truthy value to fall back to aligning against the opposite edge first if the requested alignment cannot be constrained into the constrainTo option. If specified as 'flexible', then fallback will continue searching for solutions on the remaining two sides.

spec.matchSizeBoolean | min

When aligning edge-to-edge, match the length of the aligned-to edge of the target.

Setting this value to 'min' means that the resulting aligned Rectangle will grow to the size of the aligned-to edge if it is smaller, but will not shrink to match the aligned-to edge.

This is only honored when axisLock is enabled and alignment succeeds on the requested axis. If not aligning edge-to-edge, matchSize matches both dimensions of the target.

spec.offsetNumber | Number[]

The 'x' and 'y' offset values to create an extra margin round the target to offset the aligned widget further from the target. May be configured as -ve to move the aligned widget towards the target - for example producing the effect of the anchor pointer piercing the target.

spec.constrainPaddingNumber | Number[]

The amount of pixels to pad from the constrainTo target, either a single value, or an array of values in CSS edge order.

spec.rtlBoolean

Pass as true if this is being used in an RTL environment, and aligning 0% to 100% along a horizontal edge must proceed from right to left.

Returns: AlignResult -

A new Rectangle aligned as requested if possible, but if the requested position violates the constrainTo Rectangle, the shortest translation from the requested position which obeys constraints will be used.

clientstatic

Returns the client Rectangle (within border and padding and scrollbars) in document based coordinates of the passed element.

ParameterTypeDescription
elementHTMLElement

The element to calculate the Rectangle for.

relativeToHTMLElement

Optionally, a parent element in whose space to calculate the Rectangle.

ignorePageScrollBoolean

Use browser viewport based coordinates.

clipStickiesBoolean

Return the Rectangle within any sticky elements docked at the element edges.

Returns: Rectangle -

The Rectangle in document based (or, optionally viewport based) coordinates. Relative to the relativeTo parameter if passed.

Creates a copy of this Rectangle.

Attempts to constrain this Rectangle into the passed Rectangle. If the strict parameter is true then this method will return false if constraint could not be achieved.

If this Rectangle has a minHeight or minWidth property, size will be adjusted while attempting to constrain.

Right and bottom are adjusted first leaving the top and bottom sides to "win" in the case that this Rectangle overflows the constrainTo Rectangle.

ParameterTypeDescription
constrainToRectangle

The Rectangle to constrain this Rectangle into if possible.

strictBoolean

Pass true to return false, and leave this Rectangle unchanged if constraint could not be achieved.

Returns: Rectangle | Boolean -

This Rectangle. If strict is true, and constraining was not successful, false.

Returns true if this Rectangle wholly contains the passed rectangle.

Note that a Point may be passed.

ParameterTypeDescription
otherRectangle

The Rectangle to test for containment within this Rectangle

Returns: Boolean -

true if the other Rectangle is wholly contained within this Rectangle

contentstatic

Returns the content Rectangle (within border and padding) in document based coordinates of the passed element.

ParameterTypeDescription
elementHTMLElement

The element to calculate the Rectangle for.

relativeToHTMLElement

Optionally, a parent element in whose space to calculate the Rectangle.

ignorePageScrollBoolean

Use browser viewport based coordinates.

Returns: Rectangle -

The Rectangle in document based (or, optionally viewport based) coordinates. Relative to the relativeTo parameter if passed.

Clips off the area of the "other" Rectangle(s) from this Rectangle if any of the the other rectangles occlude a whole edge of this Rectangle.

In the example below, the other Rectangle obscures the whole of the left edge of this Rectangle.

The result from this call would be a Rectangle with the left edge moved to the right edge of the other Rectangle.

This is useful for clipping off sticky elements which are docked to the edges of an element.

  other                   this
┌───────┐─────────────────────────────────────────┐
│   │   │                                         │
│   │   │                                         │
│   │   │                                         │
│   │   │                                         │
│   │   │                                         │
│   │   │                                         │
└───────┘─────────────────────────────────────────┘
ParameterTypeDescription
otherRectangle

The potentially occluding Rectangle(s).

Returns: Rectangle -

A new rectangle with the area of the other Rectangle clipped off.

fromstatic

Returns the Rectangle in document based coordinates of the passed element.

Note: If the element passed is the document or window the window's rectangle is returned which is always at [0, 0] and encompasses the browser's entire document viewport.

ParameterTypeDescription
elementHTMLElement | Widget | Mask | String

The element (or selector) to calculate the Rectangle for.

relativeToHTMLElement | String

Optionally, a parent element (or selector) in whose space to calculate the Rectangle.

ignorePageScrollBoolean

Use browser viewport based coordinates.

Returns: Rectangle -

The Rectangle in document based (or, optionally viewport based) coordinates. Relative to the relativeTo parameter if passed.

Returns the Rectangle in viewport coordinates of the passed element.

Note: If the element passed is the document or window the window's rectangle is returned which is always at [0, 0] and encompasses the browser's entire document viewport.

ParameterTypeDescription
elementHTMLElement

The element to calculate the Rectangle for.

relativeToHTMLElement

Optionally, a parent element in whose space to calculate the Rectangle.

Returns: Rectangle -

The Rectangle in viewport based coordinates. Relative to the relativeTo parameter if provided.

Highlights this Rectangle using the highlighting effect of DomHelper on a transient element which encapsulates the region's area.

innerstatic

Returns the inner Rectangle (within border) in document based coordinates of the passed element.

ParameterTypeDescription
elementHTMLElement

The element to calculate the Rectangle for.

relativeToHTMLElement

Optionally, a parent element in whose space to calculate the Rectangle.

ignorePageScrollBoolean

Use browser viewport based coordinates.

Returns: Rectangle -

The Rectangle in document based (or, optionally viewport based) coordinates. Relative to the relativeTo parameter if passed.

Checks if this Rectangle intersects the passed Rectangle

ParameterTypeDescription
otherRectangle

The Rectangle to intersect with this.

useBooleanBoolean

Specify true to return a boolean value instead of constructing a new Rectangle

allowZeroDimensionsBoolean

true to consider zero-width or zero-hight rectangles as intersecting if coordinates indicate the intersection

Returns: Rectangle | Boolean -

Returns the intersection Rectangle or false if there is no intersection.

Moves this Rectangle to the passed x, y position. Size is maintained.

ParameterTypeDescription
xNumber

The new X position.

yNumber

The new Y position.

Returns: Rectangle -

This Rectangle;

Rounds this Rectangle to the pixel resolution of the current display or to the nearest passed unit which defaults to the current display's devicePixelRatio.

ParameterTypeDescription
devicePixelRatioNumber

device pixel ratio which defaults to window.devicePixelRatio

Translates this Rectangle by the passed vector. Size is maintained.

ParameterTypeDescription
xNumber

The X translation vector.

yNumber

The Y translation vector.

Returns: Rectangle -

This Rectangle;

unionstatic

Returns a new rectangle created as the union of all supplied rectangles.

ParameterTypeDescription
rectanglesRectangle
Returns: Rectangle

Typedefs

1
ParameterTypeDescription
anchorObject

If an anchor point was specified, this is the point on the aligned Rectangle at the alignment position.

anchor.xNumber

The X coordinate of the anchor point.

anchor.yNumber

The Y coordinate of the anchor point.

anchor.edgetop | right | bottom | left

The edge on which the anchor point lies.

alignString

The final alignment string used after constraint was applied.

zoneNumber

The zone number the result was aligned into (0, 1, 2, 3 for top right bottom, left).

constraintRectangle

The Rectangle the result was constrained into.