anseki / leader-line

Draw a leader line in your web page.
http://anseki.github.io/leader-line/
MIT License
3.03k stars 424 forks source link

How do I detect if an element has been connected or has a leader line connected to it ? #348

Closed mustaphatg closed 2 years ago

mustaphatg commented 2 years ago

HI @anseki , you built such an awesome tool and I really appreciate you.

Any idea on how I can prevent an element from being connected twice. Am building a matching question with javascript and I will like to detect if an element has a ledger line connected to it ,so I prevent double connection.

Thanks.

anseki commented 2 years ago

Hi @mustaphatg, thank you for the comment. Sorry, my English is poor. What does the "connected twice" mean? And what is the "ledger line"?

mustaphatg commented 2 years ago

Sorry, it was a typo error. What I wanted was a way to prevent two LeaderLine on a single element.

anseki commented 2 years ago

You can access to joined elements via start/end options (properties). For example:

if (lineA.start === lineB.start) {
  console.log('This element was joined to line-A and line-B.');
}
mustaphatg commented 2 years ago

I do not have reference to the object.

I am creating a matching app for kids where they match animals to their habitat together, by using leader line.

am using line.position() to update the position due to the mouse event am using to draw the leader line. So no reference to the object is saved. There are numerous element to match so the elements are not known beforehand. Its dynamic and its according to the user action. Is there a way detect that a leader line is already pointing to an element.

anseki commented 2 years ago

In normal computer programs, instances are saved to variables. And you can do that very easy.

const lineA = new LeaderLine(...);

This is best solution and it is standard program code. Also, dropping the instances is very strange and it may make another problems (e.g. memory leaking, slow action, etc.). Do you have any special reason why you drop the instances?

anseki commented 2 years ago

If you really want to check the connection without the instances, you can use class of DOM.

if (!element.classList.contains('connected')) {
  line.start = element;
  element.classList.add('connected')
}

Of course this is not good way.

mustaphatg commented 2 years ago

Thanks so much. I really appreciate it.

anseki commented 2 years ago

:smile: