pencil.js / position / Position
Class: Position
Defined in: position.ts:24
Pair of value in 2d space
Constructors
Constructor
new Position(x?, y?): Position;Defined in: position.ts:36
Position constructor
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
x? | number | 0 | Horizontal component |
y? | number | 0 | Vertical component |
Returns
Position
Properties
| Property | Type | Description | Defined in |
|---|---|---|---|
x | number | Horizontal component | position.ts:26 |
y | number | Vertical component | position.ts:29 |
Accessors
angle
Get Signature
get angle(): number;Defined in: position.ts:243
Get the angle of a position relative to the vertical axis (0 is up, 0.25 is right ...)
Returns
number
length
Get Signature
get length(): number;Defined in: position.ts:236
Get the length of the vector from (0, 0) to this
Returns
number
Methods
add()
add(definition?, y?): this;Defined in: position.ts:97
Add another position or number
Parameters
| Parameter | Type | Description |
|---|---|---|
definition? | number | PositionDefinition | Another position or a number |
y? | number | Value for "y" if "definition" is a number |
Returns
this
Itself
calc()
calc(
operation,
definition,
diffY?
): this;Defined in: position.ts:83
Apply an operation to this position values
Parameters
| Parameter | Type | Description |
|---|---|---|
operation | Operation | Function to apply on value |
definition | number | PositionDefinition | Another position or a number |
diffY? | number | Value to apply on "y" if "definition" is a number |
Returns
this
Itself
clone()
clone(): Position;Defined in: position.ts:63
Create a copy of this position
Returns
Position
constrain()
constrain(startDefinition, endDefinition): this;Defined in: position.ts:174
Constrain the position to a rectangle define by two positions
Parameters
| Parameter | Type | Description |
|---|---|---|
startDefinition | PositionDefinition | Starting position of the constrain (upper-left corner) |
endDefinition | PositionDefinition | Ending position of the constrain (lower-right corner) |
Returns
this
Itself
crossProduct()
crossProduct(positionDefinition): number;Defined in: position.ts:215
Cross product
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition | PositionDefinition | Another position |
Returns
number
distance()
distance(positionDefinition?): number;Defined in: position.ts:197
Compute distance with another position
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition? | PositionDefinition | Any position (by default (0, 0)) |
Returns
number
divide()
divide(definition, y?): this;Defined in: position.ts:127
Divide by another position or number
Parameters
| Parameter | Type | Description |
|---|---|---|
definition | number | PositionDefinition | Another position or a number |
y? | number | Value for "y" if "definition" is a number |
Returns
this
Itself
dotProduct()
dotProduct(positionDefinition): number;Defined in: position.ts:206
Dot product
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition | PositionDefinition | Another position |
Returns
number
equals()
equals(positionDefinition): boolean;Defined in: position.ts:71
Determine if is equal to another position
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition | PositionDefinition | Any other position |
Returns
boolean
isOnSameSide()
isOnSameSide(positionDefinition, vector): boolean;Defined in: position.ts:225
Define if this is on the same side of a vector as another position
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition | PositionDefinition | Another position |
vector | Vector | Any vector |
Returns
boolean
lerp()
lerp(positionDefinition, ratio): this;Defined in: position.ts:188
Move the position towards another by a ratio
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition | PositionDefinition | Any other position |
ratio | number | Ratio of distance to move, 0 mean no change, 1 mean arrive at position |
Returns
this
Itself
modulo()
modulo(definition, y?): this;Defined in: position.ts:137
Gives the modulo by another position or number
Parameters
| Parameter | Type | Description |
|---|---|---|
definition | number | PositionDefinition | Another position or a number |
y? | number | Value for "y" if "definition" is a number |
Returns
this
Itself
multiply()
multiply(definition, y?): this;Defined in: position.ts:117
Multiply by another position or number
Parameters
| Parameter | Type | Description |
|---|---|---|
definition | number | PositionDefinition | Another position or a number |
y? | number | Value for "y" if "definition" is a number |
Returns
this
Itself
power()
power(definition, y?): this;Defined in: position.ts:147
Raise to a power
Parameters
| Parameter | Type | Description |
|---|---|---|
definition | number | PositionDefinition | Another position or a number |
y? | number | Value for "y" if "definition" is a number |
Returns
this
Itself
rotate()
rotate(angle?, originDefinition?): this;Defined in: position.ts:158
Rotate the position around the origin clockwise
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
angle? | number | 0 | Angle of rotation in ratio of full circle (0 means no rotation, 1 means go full circle back to same position) |
originDefinition? | PositionDefinition | undefined | Point of origin to rotate around (by default (0, 0)) |
Returns
this
Itself
set()
set(definition, diffY?): this;Defined in: position.ts:47
Define this position value
Parameters
| Parameter | Type | Description |
|---|---|---|
definition | number | PositionDefinition | Horizontal position or another position |
diffY? | number | Vertical position if "definition" is a number |
Returns
this
Itself
subtract()
subtract(definition?, y?): this;Defined in: position.ts:107
Subtract another position or number
Parameters
| Parameter | Type | Description |
|---|---|---|
definition? | number | PositionDefinition | Another position or a number |
y? | number | Value for "y" if "definition" is a number |
Returns
this
Itself
toJSON()
toJSON(): [number, number];Defined in: position.ts:254
Return a JSON ready Position definition
Returns
[number, number]
average()
static average(...positionDefinitions): Position;Defined in: position.ts:283
Compute the average for a set of positions
Parameters
| Parameter | Type | Description |
|---|---|---|
...positionDefinitions | PositionDefinition[] | List of positions to average |
Returns
Position
from()
static from(positionDefinition?): Position;Defined in: position.ts:262
Create a Position from a generic definition or do nothing if already a Position
Parameters
| Parameter | Type | Description |
|---|---|---|
positionDefinition? | PositionDefinition | Position definition |
Returns
Position
