DEV Community

Discussion on: Drawing interactive graphs with canvas and javascript

Collapse
 
hichemkirigaya profile image
Hichem Liebert • Edited

Hello ,first I want to thank you for this awesome work !
I just wanted to point out a little bug ,actually if u draw an edge by selecting two nodes, and then you try to redraw the same edge (by selecting the previous nodes ,either in the same order or reverse order ) ,this will add a different instance of that edge in the edges array .
One way to solve this, is to actually verify if the edge already exists in the edges array .In my case ,I created a function named edgeExists that accepts two parameters (a string representation of the selection and the target using JSON.stringify) , the function loops through the edges array ,for each edge it verifies the edge's ends are actually our selection and target (by comparing JSON.stringify(edge.from /edge.to) to the arguments) ,if so ,then it returns true.

You can find my implementation in the uploaded image.

dev-to-uploads.s3.amazonaws.com/i/...