在HTML页面上在两个点之间绘制一条线,不使用Canvas,是否有方法?

huangapple go评论70阅读模式
英文:

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

问题

I'm designing a web interface for a robotic arm, and I need a way to draw lines between points in an HTML div element, given a start (x, y) and an 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.

英文:

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.

&lt;div id=&quot;somediv&quot;&gt;
    &lt;!--This div is 100px * 100px, I need to draw a line 4px thick from (23, 24) to (87, 96)--&gt;
&lt;/div&gt;

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.

答案1

得分: 3

以下是翻译好的内容:

基本思想是将SVG放在DIV元素上方,将DIV的位置设为相对,SVG的位置设为绝对。然后,您可以将SVG绘制到任何您喜欢的x/y点。

而且如预期的那样,像点击这样的事件可以与SVG一起工作,尝试点击线条,双击黄色框。

示例:

<div id="somediv">
    <!-- 这个div是100x100像素,我需要从(23, 24)到(87, 96)绘制一条4像素宽的线条 -->
    <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>
英文:

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 ->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&#39;#theline&#39;).
  addEventListener(&#39;click&#39;, 
    () =&gt; alert(&quot;line clicked&quot;));

document.querySelector(&#39;#somediv&#39;).
  addEventListener(&#39;dblclick&#39;, 
    () =&gt; alert(&#39;box double clicked&#39;));

<!-- language: lang-css -->

#somediv {
  width: 100px;
  height: 100px;
  background-color: yellow;
  position: relative;
}

#svg {
  position: absolute;
  top: 0px;
  left: 0px;
}

<!-- language: lang-html -->

&lt;div id=&quot;somediv&quot;&gt;
    &lt;!--This div is 100px * 100px, I need to draw a line 4px thick from (23, 24) to (87, 96)--&gt;
   &lt;svg id=&quot;svg&quot; width=&quot;100&quot; height=&quot;100&quot;&gt; 
      &lt;line 
        style=&quot;cursor:pointer&quot;
        id=&quot;theline&quot; 
        x1=&quot;23&quot; y1=&quot;24&quot; x2=&quot;87&quot; y2=&quot;96&quot; stroke=&quot;black&quot; stroke-width=&quot;4&quot;/&gt;
   &lt;/svg&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

I modified @Keith's answer, making it into a function that draws a line between given points in an element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
function drawLine(x1, y1, x2, y2, line_class, element=document.documentElement) {
    element.innerHTML += `<svg><line class=${line_class} x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}"/></svg>`
}

drawLine(20, 25, 89, 78, "line", document.getElementById("adiv"))

<!-- language: lang-css -->
.line {
    stroke-width: 5px;
    stroke: black;
}
#adiv {
    height: 100px;
    width: 100px;
    background-color: blue;
}

<!-- language: lang-html -->
<div id="adiv">
</div>

<!-- end snippet -->
英文:

I modified @Keith's answer, making it into a function that draws a line between given points in a element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function drawLine(x1, y1, x2, y2, line_class,     element=document.documentElement) {
            element.innerHTML += `&lt;svg&gt;&lt;line class=${line_class} x1=&quot;${x1}&quot; y1=&quot;${y1}&quot; x2=&quot;${x2}&quot; y2=&quot;${y2}&quot;/&gt;&lt;/svg&gt;`
}

drawLine(20, 25, 89, 78, &quot;line&quot;, document.getElementById(&quot;adiv&quot;)) 

<!-- language: lang-css -->

.line {
    stroke-width: 5px;
    stroke: black;
}
#adiv {
    height: 100px;
    width: 100px;
    background-color: blue;
}

<!-- language: lang-html -->

&lt;div id=&quot;adiv&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月6日 04:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定