How to find position of HTML elements in JavaScript?

getBoundingClientRect function will give you the x and y position of the element

function getPosition(element) {
  const rect = element.getBoundingClientRect();
  return { x: rect.left, y: rect.top };
}

In SetNodePosition function, instead of calling this function two times for each html element, call it once for each element as shown below

function SetNodePosition(e) {

  let { x: startNodeX, y:startNodeY } = getPosition(startNode);
  startX = startNodeX;
  startY = startNodeY;

  console.log("StartNode: " + startX + " - " + startY);

  ....
}