Skip to content

pencil.js / position / Position

Class: Position ​

Defined in: position.ts:24

Pair of value in 2d space

Constructors ​

Constructor ​

ts
new Position(x?, y?): Position;

Defined in: position.ts:36

Position constructor

Parameters ​

ParameterTypeDefault valueDescription
x?number0Horizontal component
y?number0Vertical component

Returns ​

Position

Properties ​

PropertyTypeDescriptionDefined in
xnumberHorizontal componentposition.ts:26
ynumberVertical componentposition.ts:29

Accessors ​

angle ​

Get Signature ​

ts
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 ​

ts
get length(): number;

Defined in: position.ts:236

Get the length of the vector from (0, 0) to this

Returns ​

number

Methods ​

add() ​

ts
add(definition?, y?): this;

Defined in: position.ts:97

Add another position or number

Parameters ​

ParameterTypeDescription
definition?number | PositionDefinitionAnother position or a number
y?numberValue for "y" if "definition" is a number

Returns ​

this

Itself


calc() ​

ts
calc(
   operation, 
   definition, 
   diffY?
): this;

Defined in: position.ts:83

Apply an operation to this position values

Parameters ​

ParameterTypeDescription
operationOperationFunction to apply on value
definitionnumber | PositionDefinitionAnother position or a number
diffY?numberValue to apply on "y" if "definition" is a number

Returns ​

this

Itself


clone() ​

ts
clone(): Position;

Defined in: position.ts:63

Create a copy of this position

Returns ​

Position


constrain() ​

ts
constrain(startDefinition, endDefinition): this;

Defined in: position.ts:174

Constrain the position to a rectangle define by two positions

Parameters ​

ParameterTypeDescription
startDefinitionPositionDefinitionStarting position of the constrain (upper-left corner)
endDefinitionPositionDefinitionEnding position of the constrain (lower-right corner)

Returns ​

this

Itself


crossProduct() ​

ts
crossProduct(positionDefinition): number;

Defined in: position.ts:215

Cross product

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionAnother position

Returns ​

number


distance() ​

ts
distance(positionDefinition?): number;

Defined in: position.ts:197

Compute distance with another position

Parameters ​

ParameterTypeDescription
positionDefinition?PositionDefinitionAny position (by default (0, 0))

Returns ​

number


divide() ​

ts
divide(definition, y?): this;

Defined in: position.ts:127

Divide by another position or number

Parameters ​

ParameterTypeDescription
definitionnumber | PositionDefinitionAnother position or a number
y?numberValue for "y" if "definition" is a number

Returns ​

this

Itself


dotProduct() ​

ts
dotProduct(positionDefinition): number;

Defined in: position.ts:206

Dot product

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionAnother position

Returns ​

number


equals() ​

ts
equals(positionDefinition): boolean;

Defined in: position.ts:71

Determine if is equal to another position

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionAny other position

Returns ​

boolean


isOnSameSide() ​

ts
isOnSameSide(positionDefinition, vector): boolean;

Defined in: position.ts:225

Define if this is on the same side of a vector as another position

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionAnother position
vectorVectorAny vector

Returns ​

boolean


lerp() ​

ts
lerp(positionDefinition, ratio): this;

Defined in: position.ts:188

Move the position towards another by a ratio

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionAny other position
rationumberRatio of distance to move, 0 mean no change, 1 mean arrive at position

Returns ​

this

Itself


modulo() ​

ts
modulo(definition, y?): this;

Defined in: position.ts:137

Gives the modulo by another position or number

Parameters ​

ParameterTypeDescription
definitionnumber | PositionDefinitionAnother position or a number
y?numberValue for "y" if "definition" is a number

Returns ​

this

Itself


multiply() ​

ts
multiply(definition, y?): this;

Defined in: position.ts:117

Multiply by another position or number

Parameters ​

ParameterTypeDescription
definitionnumber | PositionDefinitionAnother position or a number
y?numberValue for "y" if "definition" is a number

Returns ​

this

Itself


power() ​

ts
power(definition, y?): this;

Defined in: position.ts:147

Raise to a power

Parameters ​

ParameterTypeDescription
definitionnumber | PositionDefinitionAnother position or a number
y?numberValue for "y" if "definition" is a number

Returns ​

this

Itself


rotate() ​

ts
rotate(angle?, originDefinition?): this;

Defined in: position.ts:158

Rotate the position around the origin clockwise

Parameters ​

ParameterTypeDefault valueDescription
angle?number0Angle of rotation in ratio of full circle (0 means no rotation, 1 means go full circle back to same position)
originDefinition?PositionDefinitionundefinedPoint of origin to rotate around (by default (0, 0))

Returns ​

this

Itself


set() ​

ts
set(definition, diffY?): this;

Defined in: position.ts:47

Define this position value

Parameters ​

ParameterTypeDescription
definitionnumber | PositionDefinitionHorizontal position or another position
diffY?numberVertical position if "definition" is a number

Returns ​

this

Itself


subtract() ​

ts
subtract(definition?, y?): this;

Defined in: position.ts:107

Subtract another position or number

Parameters ​

ParameterTypeDescription
definition?number | PositionDefinitionAnother position or a number
y?numberValue for "y" if "definition" is a number

Returns ​

this

Itself


toJSON() ​

ts
toJSON(): [number, number];

Defined in: position.ts:254

Return a JSON ready Position definition

Returns ​

[number, number]


average() ​

ts
static average(...positionDefinitions): Position;

Defined in: position.ts:283

Compute the average for a set of positions

Parameters ​

ParameterTypeDescription
...positionDefinitionsPositionDefinition[]List of positions to average

Returns ​

Position


from() ​

ts
static from(positionDefinition?): Position;

Defined in: position.ts:262

Create a Position from a generic definition or do nothing if already a Position

Parameters ​

ParameterTypeDescription
positionDefinition?PositionDefinitionPosition definition

Returns ​

Position

Released under the MIT License.