Implement PolygonTool.ts
This commit is contained in:
@@ -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 };
|
||||||
Reference in New Issue
Block a user