@@ -12,6 +12,7 @@ const ShapeBuilder = () => {
1212 const polyRef = useRef ( null ) ;
1313 const keyHandlersRef = useRef ( { } ) ;
1414 const [ result , setResult ] = useState ( "" ) ;
15+ const [ error , setError ] = useState ( null ) ;
1516
1617 const showCytoArray = ( ) => {
1718 const poly = polyRef . current ;
@@ -82,19 +83,37 @@ const ShapeBuilder = () => {
8283 } ;
8384
8485 const initializeDrawing = ( ) => {
85- if ( ! boardRef . current || ! window . SVG ) return ;
86+ if ( ! boardRef . current ) {
87+ setError ( "Canvas reference not found" ) ;
88+ return ;
89+ }
8690
87- const draw = window . SVG ( boardRef . current )
88- . size ( "100%" , "100%" )
89- . polygon ( )
90- . draw ( )
91- . attr ( { stroke : "#00B39F" , "stroke-width" : 1 , fill : "none" } ) ;
91+ if ( ! window . SVG ) {
92+ setError ( "SVG.js not loaded" ) ;
93+ return ;
94+ }
9295
93- draw . draw ( "param" , "snapToGrid" , 0.001 ) ;
94- draw . on ( "drawstart" , attachKeyListeners ) ;
95- draw . on ( "drawdone" , detachKeyListeners ) ;
96+ if ( ! window . SVG . Element . prototype . draw ) {
97+ setError ( "svg.draw.js plugin not loaded" ) ;
98+ return ;
99+ }
96100
97- polyRef . current = draw ;
101+ try {
102+ const draw = window . SVG ( boardRef . current )
103+ . size ( "100%" , "100%" )
104+ . polygon ( )
105+ . draw ( )
106+ . attr ( { stroke : "#00B39F" , "stroke-width" : 1 , fill : "none" } ) ;
107+
108+ draw . draw ( "param" , "snapToGrid" , 0.001 ) ;
109+ draw . on ( "drawstart" , attachKeyListeners ) ;
110+ draw . on ( "drawdone" , detachKeyListeners ) ;
111+
112+ polyRef . current = draw ;
113+ setError ( null ) ;
114+ } catch ( err ) {
115+ setError ( `Failed to initialize drawing: ${ err . message } ` ) ;
116+ }
98117 } ;
99118
100119 const clearShape = ( ) => {
@@ -119,12 +138,27 @@ const ShapeBuilder = () => {
119138 } ;
120139
121140 useEffect ( ( ) => {
122- if ( ! window . SVG || ! window . SVG . Element . prototype . draw ) {
123- console . error ( "SVG.js or svg.draw.js plugin not loaded." ) ;
124- return ;
125- }
141+ const checkSVG = ( ) => {
142+ if ( ! window . SVG || ! window . SVG . Element . prototype . draw ) {
143+ setError ( "SVG.js or svg.draw.js plugin not loaded" ) ;
144+ return false ;
145+ }
146+ return true ;
147+ } ;
126148
127- initializeDrawing ( ) ;
149+ // Initial check
150+ if ( checkSVG ( ) ) {
151+ initializeDrawing ( ) ;
152+ } else {
153+ // If not loaded, try again after a short delay
154+ const timer = setTimeout ( ( ) => {
155+ if ( checkSVG ( ) ) {
156+ initializeDrawing ( ) ;
157+ }
158+ } , 1000 ) ;
159+
160+ return ( ) => clearTimeout ( timer ) ;
161+ }
128162
129163 return ( ) => {
130164 detachKeyListeners ( ) ;
@@ -147,6 +181,20 @@ const ShapeBuilder = () => {
147181 </ defs >
148182 < rect className = "grid" width = "100%" height = "100%" fill = "url(#grid)" />
149183 </ StyledSVG >
184+ { error && (
185+ < div style = { {
186+ position : 'absolute' ,
187+ top : '50%' ,
188+ left : '50%' ,
189+ transform : 'translate(-50%, -50%)' ,
190+ color : 'red' ,
191+ backgroundColor : 'white' ,
192+ padding : '10px' ,
193+ borderRadius : '5px'
194+ } } >
195+ { error }
196+ </ div >
197+ ) }
150198 </ CanvasContainer >
151199
152200 < Controls >
0 commit comments