MCPcopy Create free account
hub / github.com/Shitanshukumar607/Drawspace / useDrawRectangleTool

Function useDrawRectangleTool

hooks/DrawRectangleTool.ts:13–101  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers 1

CanvasComponentFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected