Explain Codes LogoExplain Codes Logo

Draw a connecting line between two elements

javascript
responsive-design
promises
callbacks
Alex KataevbyAlex Kataev·Oct 15, 2024
TLDR

To draw a line between two elements, mainly two approaches exist: the first relies on CSS's ::after for static positioning, the second entails SVG and JavaScript for dynamic scenarios.

CSS-only solution using the ::after pseudo-element:

#element1::after { content: ''; /* our magical line is invisible without this */ position: absolute; /* trust me, I am the absolute position */ width: calc(100% - 20px); /* Shrink a bit, it's not a weight-loss program though */ height: 2px; /* a thin line, like my patience */ background: black; /* black, like my coffee */ top: 50%; /* meet me in the middle */ left: 100%; /* all the way to the... right? */ }

Corresponding HTML:

<div id="element1">Element 1</div> <div id="element2" style="margin-left:100px;">Element 2</div>

SVG & JavaScript to draw lines in real-time:

const drawLine = (el1, el2, line) => { const {right, top} = el1.getBoundingClientRect(); /* Element 1, report your coordinates! */ const {left, bottom} = el2.getBoundingClientRect(); /* Element 2, report your coordinates! */ line.setAttribute('x1', right); /* X marks the spot... not really, just kidding, starts from the right */ line.setAttribute('y1', top); /* Lift off to... the top? */ line.setAttribute('x2', left); /* Let's meet at the left corner */ line.setAttribute('y2', bottom); /* Descend to the bottom! */ };

Implement with SVG:

<svg><line id="line" stroke="black"/></svg>

Invoke with:

drawLine(document.getElementById('element1'), document.getElementById('element2'), document.getElementById('line')); /* Magic in one line of code */

Advanced usage: Libraries for interactive draggable connections

Some situations require more than just a static line between two elements. Let's examine some libraries and techniques to draw interactive, draggable and editable connections.

jsPlumb: Interactive line drawing

Using jsPlumb, you can create draggable and editable connections between elements. It comes in two flavors, the Community edition which is free and the Toolkit edition (commercially licensed) for fancier use cases.

SVG & jQuery: Dynamically updating lines

With a blend of SVG and jQuery, you can create lines that dynamically adjust based on the current position of the elements. You can use jQuery's position() and .attr() methods to update the endpoints of an SVG line.

VisJS & Leader Line: Data-rich applications

For applications handling data-rich elements, libraries such as VisJS and Leader Line provide excellent support for draggable elements and editable connections.

Advanced scenarios – The extra mile

Drawing curves with Bezier.js

Want to add some style to your connections? Bezier curves can make the lines between your elements follow elegant, curved paths.

Data-driven applications with jQuery

Knowing when your data changes and being able to reflect it in your UI is critical in many applications. Using jQuery's .data() and .attr() methods can help you accomplish this more effectively.

Accessible SVGs

Making your SVG accessible isn't just about complying with WCAG guidelines (which you should), it's also about creating a better experience for all users.