How to draw a line between two points with js and CSS

OJNSim

Here is a simple HTML page containing 2 points and a line connecting the points, being added with jQuery.

The result is that the line is not connecting the points, but for some reason places the line with some offset.

function drawLine(){

    const line_ = $("<div>", { class: "line" });
    const p1 = $("#point1");
    var p1T = p1.position().top;
    var p1L = p1.css("left");

    // set line top equal to point1 top
    var lineT = p1T + "px";

    line_.css ({
        width: length + "px",
        transform: `rotate(${angle}rad)`,
        top: lineT,
        left: p1L
    });

    p1.parent().append(line_);
 }

// Get the elements representing the two points you want to draw a line between
 const point1 = document.getElementById('point1');
 const point2 = document.getElementById('point2');
 
 // Calculate the coordinates of the two points
 const point1Rect = point1.getBoundingClientRect();
 const point2Rect = point2.getBoundingClientRect();
 
 // Calculate the length and angle of the line
 const length = Math.sqrt((point2Rect.left - point1Rect.left) ** 2 + (point2Rect.top - point1Rect.top) ** 2);
 const angle = Math.atan2(point2Rect.top - point1Rect.top, point2Rect.left - point1Rect.left);

 drawLine();

 
#line-container {
  position: relative;
  border: 1px solid blue;
}

.line {
  position: absolute;
  height: 2px;
  background-color: aqua;
  margin: 0px;
}

.point{
  position: absolute; 
  margin: 0px;
  
}

#point1{
  background-color: red;
  top: 100px; 
  left: 100px; 
  width: 10px; 
  height: 10px;
}
#point2{
  background-color: blue;
  top: 200px; 
  left: 300px; 
  width: 10px; 
  height: 10px;
}
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <div id="line-container">
    <div id="point1" class="point"></div>
    <div id="point2" class="point"></div>
  </div>

The line and the 2 points have position: absolute;, so that the top & left are relative to the container.

margin also set to zero for all 3

The line top is set to the point1 top, but the line is above point1.

Why is that?

IT goldman

The line rotates around the center, whereas you want it to rotate according to the origin point, which is top left in this instance, and probably all others as well. so need to add transform-origin: top left

function drawLine() {

  const line_ = $("<div>", {
    class: "line"
  });
  const p1 = $("#point1");
  var p1T = p1.position().top;
  var p1L = p1.position().left;

  // set line top equal to point1 top
  var lineT = p1T + "px";

  line_.css({
    width: length + "px",
    transform: `rotate(${angle}rad)`,
    "transform-origin": "top left",
    top: lineT,
    left: p1L
  });

  p1.parent().append(line_);
}

// Get the elements representing the two points you want to draw a line between
const point1 = document.getElementById('point1');
const point2 = document.getElementById('point2');

// Calculate the coordinates of the two points
const point1Rect = point1.getBoundingClientRect();
const point2Rect = point2.getBoundingClientRect();

// Calculate the length and angle of the line
const length = Math.sqrt((point2Rect.left - point1Rect.left) ** 2 + (point2Rect.top - point1Rect.top) ** 2);
const angle = Math.atan2(point2Rect.top - point1Rect.top, point2Rect.left - point1Rect.left);

drawLine();
#line-container {
  position: relative;
  border: 1px solid blue;
}

.line {
  position: absolute;
  height: 2px;
  background-color: aqua;
  margin: 0px;
}

.point {
  position: absolute;
  margin: 0px;
}

#point1 {
  background-color: red;
  top: 100px;
  left: 100px;
  width: 10px;
  height: 10px;
}

#point2 {
  background-color: blue;
  top: 200px;
  left: 300px;
  width: 10px;
  height: 10px;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="line-container">
  <div id="point1" class="point"></div>
  <div id="point2" class="point"></div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Draw arrow between on line between two points

Matplotlib how to draw vertical line between two Y points

How to draw a line between two points over an image in swift 3?

How to draw a line between two given points in R with plotly?

How to draw a line between two points objective-C

Draw line between two distinct points

R: draw a line between two points in ggplot

Draw line between two points in JavaScript?

Draw curved line between two points

How do I draw a horizontal line between two circles with CSS?

Draw a line between points

How to draw a line between two points when holding one axis fixed (time series)

How to draw a line / link between two points on a D3 map based on latitude / longitude?

How to draw line between two points in JavaScript - Google Maps Api Route

Javascript + svg, draw sinus (wave) line between two given points

Draw line between two given points (OpenCV, Python)

ChartJS: Draw line between two data points on hover

Is there a way to draw a line between two points on a HTML page without Canvas?

How to draw an overlapping curve between two bezier points on existing curve p5.js

Draw an ellipse arc between two points in Three.js

Draw an arc between two points

How can draw a line using the x and y coordinates of two points?

How to draw a line beet wen two points in loop?

How to drag and draw line between 2 points in Java Swing

how to draw line between to points using mapKit in xcode 6

How to draw vertical separation line between two text line android?

Draw a line between two points from different groups while simultaneously dogging the points

How to draw an arc between two known points in Qt?

How to draw a tiled wall between two points on a grid on canvas?