HSRect
TypeThis 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
origin
The "origin" of the rectangle, ie the coordinates of its top left corner, as an HSPoint object
size
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 |
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 | HSPoint | HSRect | An HSPoint, or an HSRect (whose center will be used) |
other is not an HSPoint or HSRectnew 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 | HSPoint | HSRect | An HSPoint, or an HSRect (whose center will be used) |
other is not an HSPoint or HSRectnew 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 |
true if both rects have the same origin and size, otherwise falsenew 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 |
boundsnew 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
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 |
framenew 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 |
true if this rect lies fully within the bounds of rect, otherwise falsenew 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 |
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 | HSPoint | HSSize | An HSPoint (using its x/y), or an HSSize (using its w/h) |
offset is not an HSPoint or HSSizenew 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 | number | HSSize | HSPoint | A number to scale both dimensions uniformly, or an HSSize/HSPoint to scale the width and height independently |
factor is not a positive number, HSSize or HSPointnew 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 |
framenew 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 |
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 | HSPoint | HSRect | An HSPoint, or an HSRect (whose center will be used) |
new HSPoint(0, 0) if other is not an HSPoint or HSRectnew HSRect(0, 0, 2, 2).vector(new HSPoint(4, 5)) // new HSPoint(3, 4)
toJSON() -> object
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() -> {x: number, y: number, w: number, h: number}
x, y, w and h properties| Field | Type | Description |
|---|---|---|
| x | number | |
| y | number | |
| w | number | |
| h | number |
JSON.stringify(hs.screen.main().frame) // '{"x":0,"y":25,"w":1440,"h":875}'