UNPKG

@pdfme/pdf-lib

Version:

Create and modify PDF files with JavaScript

77 lines 2.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const GraphElement_1 = __importDefault(require("./GraphElement")); const Point_1 = __importDefault(require("./Point")); const Segment_1 = __importDefault(require("./Segment")); class Rectangle extends GraphElement_1.default { constructor(start = new Point_1.default(), end = new Point_1.default()) { super(); Object.defineProperty(this, "start", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "end", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.start = start; this.end = end; } getSize() { const start = this.start.toCoords(); const end = this.end.toCoords(); return { width: Math.abs(start.x - end.x), height: Math.abs(start.y - end.y), }; } getCoords() { const start = this.start.toCoords(); const end = this.end.toCoords(); return { x: Math.min(start.x, end.x), y: Math.max(start.y, end.y), }; } getStart() { const start = new Point_1.default(this.getCoords()); return start; } getEnd() { const { width, height } = this.getSize(); const end = new Point_1.default(this.getStart()).plus({ x: width, y: -height }); return end; } center() { const center = new Segment_1.default(this.getStart(), this.getEnd()).middle(); return center; } isEqual(element) { return (element instanceof Rectangle && this.getStart().isEqual(element.getStart()) && this.getEnd().isEqual(element.getEnd())); } orthoProjection(P) { const { x, y } = this.getCoords(); const end = this.getEnd().toCoords(); const { x: Px, y: Py } = P.toCoords(); const Hx = Px < x ? x : Px > end.x ? end.x : Px; const Hy = Py > y ? y : Py < end.y ? end.y : Py; return new Point_1.default({ x: Hx, y: Hy }); } } Object.defineProperty(Rectangle, "type", { enumerable: true, configurable: true, writable: true, value: 'Rectangle' }); exports.default = Rectangle; //# sourceMappingURL=Rectangle.js.map