API Docs

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.

Properties

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

w

number

The width of the rectangle

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

Methods

constructor(x, y, w, h) -> None

Create a new HSRect object

constructor(x, y, w, h) -> None
Name Type Description
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
None

angleTo(other) -> number

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

angleTo(other) -> number
Name Type Description
other JSValue An HSPoint, or an HSRect (whose center will be used)
number
A number containing the angle in radians, or 0 if other is not an HSPoint or HSRect
new HSRect(0, 0, 2, 2).angleTo(new HSPoint(2, 2)) // 0.7853981633974483

distance(other) -> number

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

distance(other) -> number
Name Type Description
other JSValue An HSPoint, or an HSRect (whose center will be used)
number
A number containing the distance, or 0 if other is not an HSPoint or HSRect
new HSRect(0, 0, 2, 2).distance(new HSPoint(4, 5)) // 5.0

equals(other) -> boolean

Checks if this rect is equal to another rect

equals(other) -> boolean
Name Type Description
other HSRect An HSRect to compare against
boolean
true if both rects have the same origin and size, otherwise false
new HSRect(0, 0, 10, 10).equals(new HSRect(0, 0, 10, 10)) // true

fit(bounds) -> HSRect

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

fit(bounds) -> HSRect
Name Type Description
bounds HSRect An HSRect describing the bounds to fit within
HSRect
A new HSRect that fits fully inside bounds
new HSRect(0, 0, 200, 100).fit(new HSRect(0, 0, 100, 100)) // new HSRect(0, 0, 100, 50)

floor() -> HSRect

Truncates the origin and size of this rect towards negative infinity

floor() -> HSRect
HSRect
A new HSRect with the x, y, w and h values floored to the nearest integer
new HSRect(1.7, 1.2, 10.9, 10.1).floor() // new HSRect(1, 1, 10, 10)

fromUnitRect(frame) -> HSRect

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

fromUnitRect(frame) -> HSRect
Name Type Description
frame HSRect An HSRect describing the frame this unit rect is relative to
HSRect
A new HSRect with coordinates and dimensions converted from the 0-1 range into absolute values within frame
new HSRect(0.5, 0.5, 0.5, 0.5).fromUnitRect(new HSRect(0, 0, 100, 100)) // new HSRect(50, 50, 50, 50)

inside(rect) -> boolean

Checks if this rect lies fully inside another rect

inside(rect) -> boolean
Name Type Description
rect HSRect An HSRect to check against
boolean
true if this rect lies fully within the bounds of rect, otherwise false
new HSRect(1, 1, 5, 5).inside(new HSRect(0, 0, 10, 10)) // true

intersect(rect) -> HSRect

Returns the intersection of this rect and another rect

intersect(rect) -> HSRect
Name Type Description
rect HSRect An HSRect to intersect with
HSRect
A new HSRect describing the overlapping area, or a zero-sized HSRect if they don't overlap
new HSRect(0, 0, 10, 10).intersect(new HSRect(5, 5, 10, 10)) // new HSRect(5, 5, 5, 5)

move(offset) -> HSRect

Moves this rect by an offset

move(offset) -> HSRect
Name Type Description
offset JSValue An HSPoint (using its x/y), or an HSSize (using its w/h)
HSRect
A new HSRect moved by the given offset, or an unchanged copy of this rect if offset is not an HSPoint or HSSize
new HSRect(0, 0, 10, 10).move(new HSPoint(5, 5)) // new HSRect(5, 5, 10, 10)

scale(factor) -> HSRect

Scales the size of this rect, keeping its center constant

scale(factor) -> HSRect
Name Type Description
factor JSValue A number to scale both dimensions uniformly, or an HSSize/HSPoint to scale the width and height independently
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
new HSRect(0, 0, 10, 10).scale(2) // new HSRect(-5, -5, 20, 20)

toUnitRect(frame) -> HSRect

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

toUnitRect(frame) -> HSRect
Name Type Description
frame HSRect An HSRect describing the frame this rect is relative to
HSRect
A new HSRect with coordinates and dimensions normalized to the 0-1 range within frame
new HSRect(50, 50, 50, 50).toUnitRect(new HSRect(0, 0, 100, 100)) // new HSRect(0.5, 0.5, 0.5, 0.5)

union(rect) -> HSRect

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

union(rect) -> HSRect
Name Type Description
rect HSRect An HSRect to union with
HSRect
A new HSRect that fully encloses both rects
new HSRect(0, 0, 10, 10).union(new HSRect(5, 5, 10, 10)) // new HSRect(0, 0, 15, 15)

vector(other) -> HSPoint

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

vector(other) -> HSPoint
Name Type Description
other JSValue An HSPoint, or an HSRect (whose center will be used)
HSPoint
A new HSPoint representing the vector, or new HSPoint(0, 0) if other is not an HSPoint or HSRect
new HSRect(0, 0, 2, 2).vector(new HSPoint(4, 5)) // new HSPoint(3, 4)

toJSON() -> {[key: string]: any}

Returns a plain object containing this rect's origin and size, for JSON serialisation

JSON.stringify() calls this automatically, so you rarely need to call it yourself.

toJSON() -> {[key: string]: any}
{[key: string]: any}
An object with x, y, w and h properties
JSON.stringify(hs.screen.main().frame) // '{"x":0,"y":25,"w":1440,"h":875}'