@@ -251,16 +251,43 @@ const VisualScripting = () => {
251251 const nodeType = nodeTypes [ node . type ] ;
252252 const dimensions = renderer . getNodeDimensions ( node , canvasRef . current . getContext ( '2d' ) ) ;
253253
254+ // Port icon dimensions (from Renderer.drawPortIcon)
255+ const portIconWidth = 6 * 1.5 ; // Base width of triangle * scale
256+ const portIconHeight = 10 * 1.5 ; // Base height of triangle * scale
257+ const portOffset = 5 ; // Distance from node border
258+
259+ // Calculate port Y position using the same logic as in drawEdges
260+ const getPortY = ( index ) => {
261+ const titleHeight = 25 ;
262+ const portSpacing = 14 ;
263+ const portVerticalGap = 5 ;
264+ return node . y + titleHeight + portVerticalGap + ( index * portSpacing ) + 4 ;
265+ } ;
266+
254267 // Check input ports
255268 for ( let i = 0 ; i < nodeType . inputs . length ; i ++ ) {
256- if ( node . isPortClicked ( x , y , i , true , dimensions ) ) {
269+ const portY = getPortY ( i ) ;
270+ const portX = node . x - portOffset ;
271+
272+ // Create a square hitbox around the port
273+ if ( x >= portX - portIconWidth &&
274+ x <= portX + portIconWidth &&
275+ y >= portY - portIconHeight / 2 &&
276+ y <= portY + portIconHeight / 2 ) {
257277 return node . getPortPosition ( i , true , dimensions ) ;
258278 }
259279 }
260280
261281 // Check output ports
262282 for ( let i = 0 ; i < nodeType . outputs . length ; i ++ ) {
263- if ( node . isPortClicked ( x , y , i , false , dimensions ) ) {
283+ const portY = getPortY ( i ) ;
284+ const portX = node . x + dimensions . width + portOffset ;
285+
286+ // Create a square hitbox around the port
287+ if ( x >= portX - portIconWidth &&
288+ x <= portX + portIconWidth &&
289+ y >= portY - portIconHeight / 2 &&
290+ y <= portY + portIconHeight / 2 ) {
264291 return node . getPortPosition ( i , false , dimensions ) ;
265292 }
266293 }
@@ -357,8 +384,34 @@ const VisualScripting = () => {
357384 const exampleNodes = examples [ param ] . nodes . map ( node =>
358385 Node . createFromExample ( node , nodeTypes )
359386 ) ;
387+
388+ // Reconstruct edges with proper port positions
389+ const reconstructedEdges = examples [ param ] . edges . map ( edge => {
390+ const startNode = exampleNodes . find ( n => n . id === edge . start . nodeId ) ;
391+ const endNode = exampleNodes . find ( n => n . id === edge . end . nodeId ) ;
392+
393+ if ( startNode && endNode ) {
394+ const startDimensions = renderer . getNodeDimensions ( startNode , canvasRef . current . getContext ( '2d' ) ) ;
395+ const endDimensions = renderer . getNodeDimensions ( endNode , canvasRef . current . getContext ( '2d' ) ) ;
396+
397+ return {
398+ start : {
399+ ...edge . start ,
400+ x : edge . start . isInput ? startNode . x : startNode . x + startDimensions . width ,
401+ y : startNode . y + startDimensions . portStartY + ( edge . start . index * 20 )
402+ } ,
403+ end : {
404+ ...edge . end ,
405+ x : edge . end . isInput ? endNode . x : endNode . x + endDimensions . width ,
406+ y : endNode . y + endDimensions . portStartY + ( edge . end . index * 20 )
407+ }
408+ } ;
409+ }
410+ return null ;
411+ } ) . filter ( edge => edge !== null ) ;
412+
360413 setNodes ( exampleNodes ) ;
361- setEdges ( examples [ param ] . edges ) ;
414+ setEdges ( reconstructedEdges ) ;
362415 setUndoStack ( [ ] ) ;
363416 setRedoStack ( [ ] ) ;
364417 }
0 commit comments