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

Function CanvasComponent

components/CanvasComponent.tsx:28–396  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26};
27
28const CanvasComponent: React.FC = () => {
29 const selectedTool = useStateStore((state) => state.selectedTool);
30 const [width, setWidth] = useState(0);
31 const [height, setHeight] = useState(0);
32 const [stageScale, setStageScale] = useState(1);
33 const [stagePosition, setStagePosition] = useState({ x: 0, y: 0 });
34 const [isPanning, setIsPanning] = useState(false);
35
36 useEffect(() => {
37 const handleResize = () => {
38 setWidth(window.innerWidth);
39 setHeight(window.innerHeight);
40 };
41
42 handleResize();
43 window.addEventListener("resize", handleResize);
44 return () => window.removeEventListener("resize", handleResize);
45 }, []);
46
47 const stageRef = useRef<Konva.Stage | null>(null);
48 const transformerRef = useRef<Konva.Transformer | null>(null);
49 const shapeRefs = useRef<Map<string, Konva.Node | null>>(new Map());
50 const [selectedTransformableShape, setSelectedTransformableShape] =
51 useState<SelectedTransformableShape | null>(null);
52 const [activeNode, setActiveNode] = useState<Konva.Node | null>(null);
53
54 const freeDrawing = useFreeDrawingTool();
55 const drawingLines = useDrawLineTool();
56 const drawingRectangle = useDrawRectangleTool();
57 const drawingEllipse = useEllipseTool();
58 const drawingArrow = useDrawArrowTool();
59
60 const isPointerTool = selectedTool === "pointer";
61
62 const SCALE_BY = 1.05;
63 const MIN_SCALE = 0.25;
64 const MAX_SCALE = 4;
65
66 const getCursorForTool = () => {
67 if (selectedTool === "hand") return isPanning ? "grabbing" : "grab";
68 if (selectedTool === "pointer") return "default";
69 if (selectedTool === "text") return "text";
70 if (selectedTool === "image" || selectedTool === "duplicate") return "copy";
71 if (selectedTool === "lock") return "not-allowed";
72 return "crosshair";
73 };
74
75 useEffect(() => {
76 if (!selectedTransformableShape) {
77 setActiveNode(null);
78 return;
79 }
80
81 const node = shapeRefs.current.get(selectedTransformableShape.id) ?? null;
82 setActiveNode(node);
83 }, [selectedTransformableShape]);
84
85 useEffect(() => {

Callers

nothing calls this directly

Calls 7

useFreeDrawingToolFunction · 0.90
useDrawLineToolFunction · 0.90
useDrawRectangleToolFunction · 0.90
useEllipseToolFunction · 0.90
useDrawArrowToolFunction · 0.90
handleResizeFunction · 0.85
getCursorForToolFunction · 0.85

Tested by

no test coverage detected