| 9 | import { getPointerPositionRelativeToStage } from "./getPointerPosition"; |
| 10 | |
| 11 | export function useEllipseTool() { |
| 12 | const tool = useStateStore((state) => state.selectedTool); |
| 13 | const properties = useToolPropertiesStore( |
| 14 | (s) => s.properties.ellipse ?? defaultEllipseProperties |
| 15 | ); |
| 16 | |
| 17 | const ellipses = useHistoryStore((state) => state.current.ellipses); |
| 18 | const addEllipse = useHistoryStore((state) => state.addEllipse); |
| 19 | const updateEllipseInStore = useHistoryStore((state) => state.updateEllipse); |
| 20 | const saveToHistory = useHistoryStore((state) => state.saveToHistory); |
| 21 | |
| 22 | const isDrawing = useRef(false); |
| 23 | const currentDraw = useRef<{ |
| 24 | id: string; |
| 25 | startX: number; |
| 26 | startY: number; |
| 27 | } | null>(null); |
| 28 | |
| 29 | const handlePointerDown = (e: KonvaEventObject<MouseEvent | TouchEvent>) => { |
| 30 | if (tool !== "ellipse") return; |
| 31 | |
| 32 | // Save current state before starting new drawing |
| 33 | saveToHistory(); |
| 34 | |
| 35 | isDrawing.current = true; |
| 36 | |
| 37 | const stage = e.target.getStage(); |
| 38 | const pos = stage ? getPointerPositionRelativeToStage(stage) : null; |
| 39 | if (!pos) return; |
| 40 | |
| 41 | const id = createShapeId(); |
| 42 | currentDraw.current = { id, startX: pos.x, startY: pos.y }; |
| 43 | |
| 44 | addEllipse({ |
| 45 | id, |
| 46 | x: pos.x, |
| 47 | y: pos.y, |
| 48 | radiusX: 0, |
| 49 | radiusY: 0, |
| 50 | stroke: properties.stroke, |
| 51 | fill: properties.fill, |
| 52 | strokeWidth: properties.strokeWidth, |
| 53 | opacity: properties.opacity, |
| 54 | }); |
| 55 | }; |
| 56 | |
| 57 | const handlePointerMove = (e: KonvaEventObject<MouseEvent | TouchEvent>) => { |
| 58 | if (!isDrawing.current || tool !== "ellipse") return; |
| 59 | |
| 60 | const stage = e.target.getStage(); |
| 61 | const pos = stage ? getPointerPositionRelativeToStage(stage) : null; |
| 62 | if (!pos) return; |
| 63 | |
| 64 | const current = currentDraw.current; |
| 65 | if (!current) return; |
| 66 | |
| 67 | const { startX, startY, id } = current; |
| 68 | const radiusX = Math.max(Math.abs(pos.x - startX) / 2, 1); |