HSPoint
TypeThis is a JavaScript object used to represent coordinates, or "points", as used in various places throughout Hammerspoon's API, particularly where dealing with positions on a screen. Behind the scenes it is a wrapper for the CGPoint type in Swift/ObjectiveC.
Properties
Methods
constructor(x, y) -> None
Create a new HSPoint object
Declaration
constructor(x, y) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| x | number | A coordinate for this point on the x-axis |
| y | number | A coordinate for this point on the y-axis |
Returns
None
angle() -> number
Returns the angle between the positive x axis and this point, treated as a vector
Declaration
angle() -> number
Returns
number
A number containing the angle in radians
Example
new HSPoint(1, 1).angle() // 0.7853981633974483 (pi/4)
angleTo(other) -> number
Returns the angle between the positive x axis and the vector from this point to another point or rect's center
Declaration
angleTo(other) -> number
Parameters
| Name | Type | Description |
|---|---|---|
| other | JSValue | 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
Example
new HSPoint(0, 0).angleTo(new HSPoint(1, 1)) // 0.7853981633974483
distance(other) -> number
Finds the distance between this point and another point or rect's center
Declaration
distance(other) -> number
Parameters
| Name | Type | Description |
|---|---|---|
| other | JSValue | 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
Example
new HSPoint(0, 0).distance(new HSPoint(3, 4)) // 5.0
equals(other) -> boolean
Checks if this point is equal to another point
Declaration
equals(other) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| other | HSPoint | An HSPoint to compare against |
Returns
boolean
`true` if both points have the same x and y coordinates, otherwise `false`
Example
new HSPoint(1, 2).equals(new HSPoint(1, 2)) // true
floor() -> HSPoint
Truncates the coordinates of this point towards negative infinity
Declaration
floor() -> HSPoint
Returns
HSPoint
A new HSPoint with the x and y coordinates floored to the nearest integer
Example
new HSPoint(1.7, -1.2).floor() // new HSPoint(1, -2)
inside(rect) -> boolean
Checks if this point lies inside a given rect
Declaration
inside(rect) -> boolean
Parameters
| Name | Type | Description |
|---|---|---|
| rect | HSRect | An HSRect to check against |
Returns
boolean
`true` if this point lies within the bounds of `rect`, otherwise `false`
Example
new HSPoint(5, 5).inside(new HSRect(0, 0, 10, 10)) // true
move(offset) -> HSPoint
Moves this point by an offset
Declaration
move(offset) -> HSPoint
Parameters
| Name | Type | Description |
|---|---|---|
| offset | JSValue | An HSPoint (using its x/y), or an HSSize (using its w/h) |
Returns
HSPoint
A new HSPoint moved by the given offset, or an unchanged copy of this point if `offset` is not an HSPoint or HSSize
Example
new HSPoint(1, 1).move(new HSPoint(2, 3)) // new HSPoint(3, 4)
normalize() -> HSPoint
Normalizes this point, treated as a vector, to a length of 1
Declaration
normalize() -> HSPoint
Returns
HSPoint
A new HSPoint with the same direction as this point but a length of 1, or `new HSPoint(0, 0)` if this point has zero length
Example
new HSPoint(3, 4).normalize() // new HSPoint(0.6, 0.8)
rotateCCW(aroundPoint, times) -> HSPoint
Rotates this point counter-clockwise around another point
Declaration
rotateCCW(aroundPoint, times) -> HSPoint
Parameters
| Name | Type | Description |
|---|---|---|
| aroundPoint | HSPoint | The HSPoint to rotate around |
| times | number | The number of 90 degree counter-clockwise rotations to perform |
Returns
HSPoint
A new HSPoint containing the rotated coordinates
Example
new HSPoint(1, 0).rotateCCW(new HSPoint(0, 0), 1) // new HSPoint(0, 1)
scale(factor) -> HSPoint
Scales this point, treated as a vector
Declaration
scale(factor) -> HSPoint
Parameters
| Name | Type | Description |
|---|---|---|
| factor | JSValue | A number to scale both coordinates uniformly, or an HSSize/HSPoint to scale the x and y coordinates independently |
Returns
HSPoint
A new HSPoint scaled by the given factor, or an unchanged copy of this point if `factor` is not a number, HSSize or HSPoint
Example
new HSPoint(2, 3).scale(2) // new HSPoint(4, 6)
vector(other) -> HSPoint
Returns the vector from this point to another point or rect's center
Declaration
vector(other) -> HSPoint
Parameters
| Name | Type | Description |
|---|---|---|
| other | JSValue | 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
Example
new HSPoint(1, 1).vector(new HSPoint(4, 5)) // new HSPoint(3, 4)