Issue
I want to find the simplest barebones (that is, no libraries if possible; this is a learning exercise) way to draw a simple line between components. The elements are divs representing cards always stacked vertically potentially forever. Cards can be different heights. The line will exit the left hand side of any given element (card a), turn 90 degrees and go up, turning 90 degrees back into another (card b).
I've tried a few things. I haven't got any fully working yet and they're looking like they all need some serious time dedicated to figuring them out. What I want to know is what's the right/preferred way to do this so that I spend time on the right thing and it's future proof with the view:
- I can add as many connecting lines as I need between any two boxes, not just consecutive ones
- These lines obey resizing and scrolling down and up the cards
- Some cards may not have an end point and will instead terminate top left of page, waiting for their card to scroll into view or be created.
Attempts
My first thought was a <canvas> in a full column component on the left but aligning canvas' and the drawings in them to my divs was a pain, as well as having an infinite scrolling canvas. Couldn't make it work.
Next I tried <div>s. Like McBrackets has done here. Colouring the top, bottom and outer edge of the div and aligning it with the two cards in question but while I can position it relative to card a, I can't figure out how to then stop it at card b.
Lastly I tried <SVG>s. Just .getElementById()
then add an SVG path that follows the instructions above. i.e.
const connectingPath =
"M " + aRect.left + " " + aRect.top + " " +
"H " + (aRect.left - 50) +
"V " + (bRect.top) +
"H " + (bRect.left);
Nothing seems to line up, it's proving pretty difficult to debug and it's looking like a much more complex solution as I need to take into account resizing and whatnot.
Solution
You might be able to apply something like this by taking a few measurements from the boxes you want to connect; offsetTop
and clientHeight
.
Update Added some logic for undrawn cards requirement.
While this doesn't fully simulate dynamic populating of cards, I made an update to show how to handle a scenario where only one card is drawn.
- Click connect using the default values (1 and 5). This will show an open connector starting from box 1.
- Click "Add box 5". This will add the missing box and update the connector.
The remaining work here is to create an event listener on scroll
to check the list of connectors. From there you can check if both boxes appear or not in the DOM (see checkConnectors
function). If they appear, then pass values to addConnector
which will connect them fully.
const connectButton = document.getElementById("connect");
const container = document.getElementById("container");
const error = document.getElementById("error");
const addBoxButton = document.getElementById("addBox");
const connectors = new Map();
const getMidpoint = element => element.offsetTop + element.clientHeight / 2;
const getElementAtBoxId = id => document.getElementById(`box${id}`);
connectButton.addEventListener("click", () => {
const firstBoxId = document.getElementById("selectFirstBox").value;
const secondBoxId = document.getElementById("selectSecondBox").value;
if (firstBoxId === null && secondBoxId === null) return;
error.style.display = firstBoxId === secondBoxId ? "block" : "none";
if (firstBoxId === secondBoxId) return;
const firstInput = getElementAtBoxId(firstBoxId);
const secondInput = getElementAtBoxId(secondBoxId);
// Check for undrawn cards
if (firstInput && !secondInput || !firstInput && secondInput) {
addConnector(firstBoxId, firstInput, secondBoxId, secondInput, true);
return;
}
const firstBox = firstInput.offsetTop < secondInput.offsetTop ? firstInput : secondInput;
const secondBox = firstInput.offsetTop < secondInput.offsetTop ? secondInput : firstInput;
addConnector(firstBoxId, firstBox, secondBoxId, secondBox);
});
function addConnector(firstBoxId, firstBox, secondBoxId, secondBox, half = false) {
const args = { firstBoxId, firstBox, secondBoxId, secondBox, half };
if (!firstBox && !secondBox) throw new Error(`Invalid params: ${JSON.stringify(args)}`);
const connectorId = `${firstBoxId}:${secondBoxId}`;
let connector, color;
if (connectors.has(connectorId)) color = connectors.get(connectorId).color;
else color = '#' + Math.floor(Math.random() * 16777215).toString(16);
if (half) {
const box = firstBox ? firstBox : secondBox;
// if firstBox draw up else draw down
if (firstBox) {
connector = buildConnector(getMidpoint(box), box.parentElement.clientHeight);
connector.style.borderBottom = "unset";
} else {
// similar logic for draw up
}
} else {
connector = buildConnector(getMidpoint(firstBox), getMidpoint(secondBox));
}
connector.style.borderColor = color;
container.appendChild(connector);
connectors.set(connectorId, { ...args, color });
}
function buildConnector(height1, height2) {
const connector = document.createElement("div");
connector.classList.add("connector");
connector.style.top = `${height1}px`;
connector.style.height = `${Math.abs(height2 - height1)}px`;
return connector;
}
window.addEventListener("resize", () => {
for (const connector of connectors.values()) {
const { firstBoxId, firstBox, secondBoxId, secondBox, half } = connector;
if(firstBox || secondBox) {
addConnector(firstBoxId, firstBox, secondBoxId, secondBox, half);
console.log(`resize detected, adjusting connector between ${firstBoxId} and ${secondBoxId}`);
}
}
});
addBoxButton.addEventListener("click", () => {
const box = document.createElement("div");
box.innerText = 5;
box.id = "box5";
box.classList.add("box");
container.appendChild(box);
addBoxButton.style.display = 'none';
// check if connectors need to be updated
checkConnectors();
});
function checkConnectors() {
for (const connector of connectors.values()) {
if (connector.half) {
let { firstBox, firstBoxId, secondBox, secondBoxId } = connector;
firstBox = firstBox ? firstBox : getElementAtBoxId(firstBoxId);
secondBox = secondBox ? secondBox : getElementAtBoxId(secondBoxId);
// both boxes in DOM -> update connector
if (firstBox && secondBox) {
addConnector(firstBoxId, firstBox, secondBoxId, secondBox);
}
}
}
}
.box {
border: solid 1px;
width: 60px;
margin-left: 30px;
margin-bottom: 5px;
text-align: center;
}
#inputs {
margin-top: 20px;
}
#inputs input {
width: 150px;
}
.connector {
position: absolute;
border-top: solid 1px;
border-left: solid 1px;
border-bottom: solid 1px;
width: 29px;
}
#error {
display: none;
color: red;
}
<div id="container">
<div id="box1" class="box">1</div>
<div id="box2" class="box">2</div>
<div id="box3" class="box">3</div>
<div id="box4" class="box">4</div>
</div>
<div id="inputs">
<input id="selectFirstBox" type="number" placeholder="Provide first box id" min="1" value="1" max="5" />
<input id="selectSecondBox" type="number" placeholder="Provide second box id" min="1" value="5" max="5" />
<div id="error">Please select different boxes to connect.</div>
</div>
<button id="connect">Connect</button>
<button id="addBox">Add box 5</button>
Answered By - GenericUser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.