Implement PolygonTool.ts

This commit is contained in:
2026-06-22 07:23:22 +00:00
parent 5d8dc15d86
commit d1591e3b9d
+51
View File
@@ -0,0 +1,51 @@
import { Tool } from '../Tool';
import { YjsDocument } from '../../crdt/YjsDocument';
class PolygonTool extends Tool {
private points: Array<{ x: number; y: number }> = [];
private isDrawing: boolean = false;
constructor(yjsDoc: YjsDocument) {
super(yjsDoc);
this.name = 'PolygonTool';
}
onMouseDown(event: MouseEvent): void {
if (!this.isDrawing) {
// Start drawing
this.isDrawing = true;
this.points = [{ x: event.clientX, y: event.clientY }];
} else {
// Add point to polygon
this.points.push({ x: event.clientX, y: event.clientY });
// If shift key is not pressed, finish the polygon (close it)
if (!event.shiftKey) {
this.yjsDoc.createPolygon([...this.points]);
this.reset();
}
}
}
onMouseMove(event: MouseEvent): void {
// Preview polygon while drawing
}
onMouseUp(event: MouseEvent): void {
// Handle mouse up if needed
}
onKeyUp(event: KeyboardEvent): void {
// Handle key up events
if (event.key === 'Escape') {
this.reset();
}
}
reset(): void {
this.points = [];
this.isDrawing = false;
}
}
export { PolygonTool };