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

TheTridentGuy

I'm designing a web interface for a robotic arm, and I need a way to draw lines between points in a HTML div element, given a start (x, y) and a end (x, y). I'd rather not use a canvas, because I need the lines to be elements themselves (They should have onclick events and CSS styles). Maybe something with SVG? Note that this is different from questions like this, as that question was trying to connect elements, whereas I'm trying to connect points inside an element.

<div id="somediv">
    <!--This div is 100px * 100px, I need to draw a line 4px thick from (23, 24) to (87, 96)-->
</div>

It would actually be preferable if I could create the line based on a single point and angle, but the math to convert that to two points is easy.

Keith

The basic idea here is to place the SVG on top of the DIV element, make the DIV position relative, and the SVG absolute to do this. And then you can draw the SVG to any x/y point you like.

And as expected, events like click work with SVG, try clicking the line, and double clicking the yellow box.

example ->

document.querySelector('#theline').
  addEventListener('click', 
    () => alert("line clicked"));

document.querySelector('#somediv').
  addEventListener('dblclick', 
    () => alert('box double clicked'));
#somediv {
  width: 100px;
  height: 100px;
  background-color: yellow;
  position: relative;
}

#svg {
  position: absolute;
  top: 0px;
  left: 0px;
}
<div id="somediv">
    <!--This div is 100px * 100px, I need to draw a line 4px thick from (23, 24) to (87, 96)-->
   <svg id="svg" width="100" height="100"> 
      <line 
        style="cursor:pointer"
        id="theline" 
        x1="23" y1="24" x2="87" y2="96" stroke="black" stroke-width="4"/>
   </svg>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to draw line between two circle in html canvas element

Draw arrow between on line between two points

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 to draw a tiled wall between two points on a grid on canvas?

Draw a line between points

How to draw a triangle given two points using an html canvas and typescript

How to draw image dynamically on a canvas line between two cells

Matplotlib how to draw vertical line between two Y points

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

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

Draw line between two given points (OpenCV, Python)

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

How to draw a line between two points objective-C

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

ChartJS: Draw line between two data points on hover

Line between two draggable points on a matplotlib pyqt5 canvas

Draw an arc between two points

Add canvas between line in itext 7 Without Overlapping Page

draw rectangle on canvas between 2 points

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

Draw Line Arrowhead Without Rotating in Canvas

Draw line between points with groups in ggplot

draw line between select points in ggplot

Draw a line to connect points between subfigures

Draw a line with specific angle in a html canvas

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