UNPKG

pencil.js

Version:

Nice modular interactive 2D drawing library.

345 lines (344 loc) 9.62 kB
/** * @module Position */ /** * Pair of value in 2d space * @class */ export default class Position { /** * @typedef {Object} AbstractPosition * @prop {Number} [x=0] - Vertical position * @prop {Number} [y=0] - Horizontal position */ /** * @typedef {Array<Number>|AbstractPosition} PositionDefinition */ /** * Create a Position from a generic definition or do nothing if already a Position * @param {PositionDefinition} [positionDefinition] - Position definition * @return {Position} */ static from(positionDefinition?: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }): Position; /** * Compute the average for a set of positions * @param {...PositionDefinition} positionDefinitions - List of positions to average * @return {Position} */ static average(...positionDefinitions: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; })[]): Position; /** * Position constructor * @param {Number} x - Vertical component * @param {Number} y - Horizontal component */ constructor(x?: number, y?: number); x: number; y: number; /** * Define this position value * @param {PositionDefinition|Number} definition - Horizontal position or another position * @param {Number} [diffY] - Vertical position if "definition" is a number * @return {Position} Itself */ set(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, diffY?: number): Position; /** * Create a copy of this position * @return {Position} New instance */ clone(): Position; /** * Determine if is equal to another position * @param {PositionDefinition} positionDefinition - Any other position * @return {Boolean} */ equals(positionDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }): boolean; /** * Apply an operation to this position values * @param {Function} operation - Function to apply on value * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [diffY] - Value to apply on "y" if "definition" is a number * @return {Position} Itself */ calc(operation: Function, definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, diffY?: number): Position; /** * Add another position or number * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [y] - Value for "y" if "position" is a number * @return {Position} Itself */ add(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, y?: number): Position; /** * Subtract another position or number * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [y] - Value for "y" if "position" is a number * @return {Position} Itself */ subtract(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, y?: number): Position; /** * Multiply by another position or number * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [y] - Value for "y" if "position" is a number * @return {Position} Itself */ multiply(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, y?: number): Position; /** * Divide by another position or number * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [y] - Value for "y" if "position" is a number * @return {Position} Itself */ divide(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, y?: number): Position; /** * Gives the modulo by another position or number * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [y] - Value for "y" if "position" is a number * @return {Position} Itself */ modulo(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, y?: number): Position; /** * Raise to a power * @param {PositionDefinition|Number} definition - Another position or a number * @param {Number} [y] - Value for "y" if "position" is a number * @return {Position} Itself */ power(definition: (number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }) | number, y?: number): Position; /** * Rotate the position around the origin clockwise * @param {Number} [angle=0] - Angle of rotation in ratio of full circle * (0 means no rotation, 1 means go full circle back to same position) * @param {PositionDefinition} [originDefinition] - Point of origin to rotate around (by default (0, 0)) * @return {Position} Itself */ rotate(angle?: number, originDefinition?: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }): Position; /** * Constrain the position to a rectangle define by two positions * @param {PositionDefinition} startDefinition - Starting position of the constrain (upper-left corner) * @param {PositionDefinition} endDefinition - Ending position of the constrain (lower-right corner) * @return {Position} Itself */ constrain(startDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }, endDefinition: number[] | { /** * - Vertical position */ /** * - Vertical position */ x?: number; /** * - Horizontal position */ /** * - Horizontal position */ y?: number; }): Position; /** * Move the position towards another by a ratio * @param {PositionDefinition} positionDefinition - Any other position * @param {Number} ratio - Ratio of distance to move, 0 mean no change, 1 mean arrive at position * @return {Position} Itself */ lerp(positionDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }, ratio: number): Position; /** * Compute distance with another position * @param {PositionDefinition} positionDefinition - Any position * @return {Number} */ distance(positionDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }): number; /** * Dot product * @param {PositionDefinition} positionDefinition - Another position * @return {Number} */ dotProduct(positionDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }): number; /** * Cross product * @param {PositionDefinition} positionDefinition - Another position * @return {Number} */ crossProduct(positionDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }): number; /** * Define if this is on the same side of a vector as another position * @param {PositionDefinition} positionDefinition - Another position * @param {Vector} vector - Any vector * @return {Boolean} */ isOnSameSide(positionDefinition: number[] | { /** * - Vertical position */ x?: number; /** * - Horizontal position */ y?: number; }, vector: Vector): boolean; /** * Get vector length * @return {Number} */ get length(): number; /** * Get the angle of a position relative to the horizontal axis * @return {Number} */ get angle(): number; /** * Return a JSON ready Position definition * @return {Array<Number>} */ toJSON(): Array<number>; }