import Konva from "konva"; import { IMarkupRectangle } from "../../IMarkupRectangle"; import { IMarkupColorable } from "../../IMarkupColorable"; export class KonvaRectangle implements IMarkupRectangle, IMarkupColorable { private _ref: Konva.Rect; constructor( params: { position: { x: number; y: number }; color: string; lineWidth: number; width: number; height: number; id?: string; }, ref = null ) { if (ref) { this._ref = ref; return; } if (!params.position) return; this._ref = new Konva.Rect({ stroke: params.color ?? "#ff0000", strokeWidth: params.lineWidth ?? 4, globalCompositeOperation: "source-over", lineCap: "round", lineJoin: "round", x: params.position.x, y: params.position.y, width: params.width, height: params.height, draggable: true, strokeScaleEnabled: false, }); this._ref.id(this._ref._id.toString()); } getPosition(): { x: number; y: number } { return this._ref.position(); } getWidth(): number { return this._ref.width(); } getHeigth(): number { return this._ref.height(); } setWidth(w: number) { this._ref.width(w); } setHeight(h: number) { this._ref.height(h); } setPosition(x: number, y: number) { this._ref.setPosition({ x, y }); } ref() { return this._ref; } id(): string { return this._ref.id(); } enableMouseEditing(value: boolean): void { this._ref.draggable(value); } type(): string { return "rectangle"; } getColor(): string { return this._ref.stroke(); } setColor(hex: string): void { this._ref.stroke(hex); } getRotation(): number { return this._ref.rotation(); } setRotation(degrees: number): void { this._ref.rotation(degrees); } getZIndex(): number { return this._ref.zIndex(); } setZIndex(zIndex: number): void { this._ref.zIndex(zIndex); } delete(): void { this._ref.destroy(); this._ref = null; } setLineWidth(size: number): void { this._ref.strokeWidth(size); } getLineWidth(): number { return this._ref.strokeWidth(); } }