Comment tracer une ligne entre deux points en utilisant js et CSS
P粉714844743
P粉714844743 2023-09-12 11:41:32
0
1
714

Il s'agit d'une simple page HTML avec 2 points et une ligne reliant les points, ajoutée à l'aide de jQuery.

Le résultat est que la ligne n'a aucun point de connexion, mais la ligne est décalée pour une raison quelconque.

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>

Ligne et 2 points ont position:absolute;,因此 topleft 相对于容器。 p>

margin Réglez également les 3 à zéro

Le haut de la ligne est placé au sommet du point1, mais la ligne est au-dessus du point1.

Pourquoi est-ce ?

P粉714844743
P粉714844743

répondre à tous(1)
P粉158473780

La ligne tourne autour du centre et vous souhaitez qu'elle tourne en fonction de l'origine, dans ce cas l'origine est 左上角,也可能是所有其他点。所以需要添加 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>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!