hammerspoon2-docs
    Preparing search index...

    Class HSRect

    This is a JavaScript object used to represent a rectangle, as used in various places throughout Hammerspoon's API, particularly where dealing with portions of a display. Behind the scenes it is a wrapper for the CGRect type in Swift/ObjectiveC.

    Index

    Constructors

    • Create a new HSRect object

      Parameters

      • x: number

        The x-axis coordinate of the top-left corner

      • y: number

        The y-axis coordinate of the top-left corner

      • w: number

        The width of the rectangle

      • h: number

        The height of the rectangle

      Returns HSRect

    Properties

    h: number

    The height of the rectangle

    origin: HSPoint

    The "origin" of the rectangle, ie the coordinates of its top left corner, as an HSPoint object

    size: HSSize

    The size of the rectangle, ie its width and height, as an HSSize object

    w: number

    The width of the rectangle

    x: number

    An x-axis coordinate for the top-left point of the rectangle

    y: number

    A y-axis coordinate for the top-left point of the rectangle

    Methods

    • Returns the angle between the positive x axis and the vector from this rect's center to another point or rect's center

      Parameters

      • other: HSRect | HSPoint

        An HSPoint, or an HSRect (whose center will be used)

      Returns number

      A number containing the angle in radians, or 0 if other is not an HSPoint or HSRect

    • Finds the distance between this rect's center and another point or rect's center

      Parameters

      • other: HSRect | HSPoint

        An HSPoint, or an HSRect (whose center will be used)

      Returns number

      A number containing the distance, or 0 if other is not an HSPoint or HSRect

    • Checks if this rect is equal to another rect

      Parameters

      • other: HSRect

        An HSRect to compare against

      Returns boolean

      true if both rects have the same origin and size, otherwise false

    • Ensures this rect is fully inside bounds, scaling it down (preserving aspect ratio) if it's larger, and moving it if necessary

      Parameters

      • bounds: HSRect

        An HSRect describing the bounds to fit within

      Returns HSRect

      A new HSRect that fits fully inside bounds

    • Truncates the origin and size of this rect towards negative infinity

      Returns HSRect

      A new HSRect with the x, y, w and h values floored to the nearest integer

    • Converts a unit rect (coordinates and dimensions between 0 and 1) within a given frame into absolute coordinates

      Parameters

      • frame: HSRect

        An HSRect describing the frame this unit rect is relative to

      Returns HSRect

      A new HSRect with coordinates and dimensions converted from the 0-1 range into absolute values within frame

    • Checks if this rect lies fully inside another rect

      Parameters

      • rect: HSRect

        An HSRect to check against

      Returns boolean

      true if this rect lies fully within the bounds of rect, otherwise false

    • Returns the intersection of this rect and another rect

      Parameters

      • rect: HSRect

        An HSRect to intersect with

      Returns HSRect

      A new HSRect describing the overlapping area, or a zero-sized HSRect if they don't overlap

    • Moves this rect by an offset

      Parameters

      • offset: HSSize | HSPoint

        An HSPoint (using its x/y), or an HSSize (using its w/h)

      Returns HSRect

      A new HSRect moved by the given offset, or an unchanged copy of this rect if offset is not an HSPoint or HSSize

    • Scales the size of this rect, keeping its center constant

      Parameters

      • factor: number | HSSize | HSPoint

        A number to scale both dimensions uniformly, or an HSSize/HSPoint to scale the width and height independently

      Returns HSRect

      A new HSRect scaled by the given factor, or an unchanged copy of this rect if factor is not a positive number, HSSize or HSPoint

    • Converts this rect into a unit rect (coordinates and dimensions between 0 and 1) within a given frame

      Parameters

      • frame: HSRect

        An HSRect describing the frame this rect is relative to

      Returns HSRect

      A new HSRect with coordinates and dimensions normalized to the 0-1 range within frame

    • Returns the smallest rect that encloses both this rect and another rect

      Parameters

      • rect: HSRect

        An HSRect to union with

      Returns HSRect

      A new HSRect that fully encloses both rects

    • Returns the vector from this rect's center to another point or rect's center

      Parameters

      • other: HSRect | HSPoint

        An HSPoint, or an HSRect (whose center will be used)

      Returns HSPoint

      A new HSPoint representing the vector, or new HSPoint(0, 0) if other is not an HSPoint or HSRect