// Collect all elements that need to be connected
let test1 = document.querySelector("#test1");
let test2 = document.querySelector("#test2");
let test3 = document.querySelector("#test3");
let test4 = document.querySelector("#test4");
let test5 = document.querySelector("#test5");
let test6 = document.querySelector("#test6");
// get the element that needs to contain the SVG
const svgParent = document.getElementById("form");
// Define the SVG element
let svg = null;
// Get the grid-gap-column value, from css
const gap = parseInt(window
.getComputedStyle(svgParent)
.getPropertyValue("grid-column-gap")
);
// set a constant value that will be used to adjust the bezier curve
const gapControlOffset = 3.5;
// add a small straight line before the start and end of the bezier curve
let offset = gap / 10;
// Connect elements
connectElements(test1, test6);
connectElements(test2, test6);
connectElements(test3, test6);
connectElements(test4, test6);
connectElements(test5, test6);
function connectElements(startElement, endElement) {
// Create the SCG tag only if it doesn't exist and place all path inside a single SVG element
if (!svg) {
svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svgParent.prepend(svg);
}
// Get the positions of the start and end elements
const startRect = startElement.querySelector(".anchor").getBoundingClientRect();
const endRect = endElement.querySelector(".anchor").getBoundingClientRect();
// Calculate the control points for the Bezier curve
const startX = startRect.right;
const startY = startRect.top + startRect.height / 2;
const endX = endRect.left;
const endY = endRect.top + endRect.height / 2;
const control1X = startX + offset + gap / gapControlOffset;
const control1Y = startY;
const control2X = endX - offset - gap / gapControlOffset;
const control2Y = endY;
// Compose the path
// M is the start point of the bezier curve
// L is a straight...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.