UNPKG

@pencil.js/rectangle

Version:

Rectangle shape for Pencil.js package.

119 lines (110 loc) 3.24 kB
import Component from "@pencil.js/component"; import Position from "@pencil.js/position"; /** * Basic rectangle * @class * @extends Component */ export default class Rectangle extends Component { /** * Rectangle constructor * @param {PositionDefinition} positionDefinition - Position in space * @param {Number} [width=0] - Horizontal size * @param {Number} [height=0] - Vertical size * @param {ComponentOptions} [options] - Drawing options */ constructor (positionDefinition, width = 0, height = 0, options) { super(positionDefinition, options); /** * @type {Number} */ this.width = width; /** * @type {Number} */ this.height = height; } /** * Draw the rectangle * @param {path} path - Drawing context * @return {Rectangle} Itself */ trace (path) { path.rect(0, 0, this.width, this.height); return this; } /** * @inheritDoc */ getOrigin () { const { origin } = this.options; if (typeof origin === "string") { const { origins } = Rectangle; const position = new Position(); // Horizontal if ([origins.topRight, origins.centerRight, origins.bottomRight].includes(origin)) { position.x = -this.width; } else if ([origins.topCenter, origins.center, origins.bottomCenter].includes(origin)) { position.x = -this.width / 2; } // Vertical if ([origins.bottomLeft, origins.bottomCenter, origins.bottomRight].includes(origin)) { position.y = -this.height; } else if ([origins.centerLeft, origins.center, origins.centerRight].includes(origin)) { position.y = -this.height / 2; } return position; } return super.getOrigin(); } /** * @inheritDoc */ toJSON () { const { width, height } = this; return { ...super.toJSON(), width, height, }; } /** * @inheritDoc * @param {Object} definition - Rectangle definition * @return {Rectangle} */ static from (definition) { return new Rectangle(definition.position, definition.width, definition.height, definition.options); } /** * @typedef {Object} RectangleOrigins * @enum {String} * @prop {String} topLeft * @prop {String} topRight * @prop {String} topCenter * @prop {String} center * @prop {String} centerLeft * @prop {String} centerRight * @prop {String} bottomLeft * @prop {String} bottomRight * @prop {String} bottomCenter */ /** * @return {RectangleOrigins} */ static get origins () { return { topLeft: "topLeft", topRight: "topRight", topCenter: "topCenter", center: "center", centerLeft: "centerLeft", centerRight: "centerRight", bottomLeft: "bottomLeft", bottomRight: "bottomRight", bottomCenter: "bottomCenter", }; } }