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
constructor(x, y) -> None
| 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 |
angle() -> number
Returns the angle between the positive x axis and this point, treated as a vector
angle() -> number
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
angleTo(other) -> number
| Name | Type | Description |
|---|---|---|
| other | JSValue | An HSPoint, or an HSRect (whose center will be used) |
other is not an HSPoint or HSRectnew 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
distance(other) -> number
| Name | Type | Description |
|---|---|---|
| other | JSValue | An HSPoint, or an HSRect (whose center will be used) |
other is not an HSPoint or HSRectnew HSPoint(0, 0).distance(new HSPoint(3, 4)) // 5.0
equals(other) -> boolean
Checks if this point is equal to another point
equals(other) -> boolean
| Name | Type | Description |
|---|---|---|
| other | HSPoint | An HSPoint to compare against |
true if both points have the same x and y coordinates, otherwise falsenew HSPoint(1, 2).equals(new HSPoint(1, 2)) // true
floor() -> HSPoint
Truncates the coordinates of this point towards negative infinity
floor() -> HSPoint
new HSPoint(1.7, -1.2).floor() // new HSPoint(1, -2)
inside(rect) -> boolean
Checks if this point lies inside a given rect
inside(rect) -> boolean
| Name | Type | Description |
|---|---|---|
| rect | HSRect | An HSRect to check against |
true if this point lies within the bounds of rect, otherwise falsenew HSPoint(5, 5).inside(new HSRect(0, 0, 10, 10)) // true
move(offset) -> HSPoint
Moves this point by an offset
move(offset) -> HSPoint
| Name | Type | Description |
|---|---|---|
| offset | JSValue | An HSPoint (using its x/y), or an HSSize (using its w/h) |
offset is not an HSPoint or HSSizenew 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
normalize() -> HSPoint
new HSPoint(0, 0) if this point has zero lengthnew HSPoint(3, 4).normalize() // new HSPoint(0.6, 0.8)
rotateCCW(aroundPoint, times) -> HSPoint
Rotates this point counter-clockwise around another point
rotateCCW(aroundPoint, times) -> HSPoint
| Name | Type | Description |
|---|---|---|
| aroundPoint | HSPoint | The HSPoint to rotate around |
| times | number | The number of 90 degree counter-clockwise rotations to perform |
new HSPoint(1, 0).rotateCCW(new HSPoint(0, 0), 1) // new HSPoint(0, 1)
scale(factor) -> HSPoint
Scales this point, treated as a vector
scale(factor) -> HSPoint
| Name | Type | Description |
|---|---|---|
| factor | JSValue | A number to scale both coordinates uniformly, or an HSSize/HSPoint to scale the x and y coordinates independently |
factor is not a number, HSSize or HSPointnew 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
vector(other) -> HSPoint
| Name | Type | Description |
|---|---|---|
| other | JSValue | An HSPoint, or an HSRect (whose center will be used) |
new HSPoint(0, 0) if other is not an HSPoint or HSRectnew HSPoint(1, 1).vector(new HSPoint(4, 5)) // new HSPoint(3, 4)
toJSON() -> {[key: string]: any}
Returns a plain object containing this point's coordinates, for JSON serialisation
JSON.stringify() calls this automatically, so you rarely need to call it yourself.
toJSON() -> {[key: string]: any}
x and y propertiesJSON.stringify(new HSPoint(10, 20)) // '{"x":10,"y":20}'