@pdfme/pdf-lib
Version:
Create and modify PDF files with JavaScript
72 lines • 2.18 kB
JavaScript
import GraphElement from './GraphElement';
import Point from './Point';
import Segment from './Segment';
class Rectangle extends GraphElement {
constructor(start = new Point(), end = new Point()) {
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(this.getCoords());
return start;
}
getEnd() {
const { width, height } = this.getSize();
const end = new Point(this.getStart()).plus({ x: width, y: -height });
return end;
}
center() {
const center = new Segment(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({ x: Hx, y: Hy });
}
}
Object.defineProperty(Rectangle, "type", {
enumerable: true,
configurable: true,
writable: true,
value: 'Rectangle'
});
export default Rectangle;
//# sourceMappingURL=Rectangle.js.map