is there a way (i guess its always) to draw a line between 2 elements? A div and an img tags wwith different ids. Heres is some html
<div id="textHolder">
<div class="text" id="text0"><p>masă</p></div>
</div>
<div id="objectHolder">
<img class="obiecte" id="obj0" src="images/Macara.svg">
</div>
Ok,so i need to draw a line between the div inside of another div with id="textHolder" and an image inside a div with id="objectHolder".First clicking on the div inside of textHolder then drawing the line between them when user click on the image inside of objectHolder.
I know i must add some code but i didnt found anything good to show.
Here's a fiddle: http://jsfiddle.net/4DURB/
Your best bet would be to use the HTML
<canvas>
element. I'm no expert at using canvas elements, but yours would probably look something like this:I hope this helps.
I've done a little bit of work on this one, because it piqued my interest. There is a jsbin (preferred to jsfiddle just because of the console) here http://jsbin.com/guken/3/
The method is to create a floating canvas element (shaded pink), and lay that underneath the rest of the DOM (using z-index). I then calculate the points on the borders of the two boxes that correspond with a line between the box centres. The red and blue squares are actually divs that move with the line ends, which could be used for annotation like source, target etc.
In that jsbin, you can click on one element, and then get a line ready to click on the next. It detects hover for the chosen elements and snaps to a target if you hover over one.
I won't paste all the code here, but the bit where we draw a line from one x,y position to another in client DOM coordinates is this:
As the example is just one line, and we can always store lines that have been 'finished' ready to create more, it uses a global variable
lineElem
. On the first attempt to draw a line, it creates a canvas element, inserts it into the DOM and assigns it to lineElem. After this construction, it subsequently reuses the canvas element, changing the size and redrawing for new coordinate pairs.To prevent the line being cut off by the edge of the canvas, there is a gutter setting which pads the canvas width and height. The rest is just getting the coordinate translation right between client DOM coordinates and the coordinates for drawing on the canvas itself.
The only other unstraightforward bit is calculating the coordinates of a point on the border of a box along a line. It's not perfect, but it's a reasonable start. The crux is to calculate the angle of the target (
to
) point from the perspective of the source (from
) point, and see how that compares to the known angles of the box corners:Theta is the angle to the first box corner, and phi is the actual line angle.
To get the positions of the boxes in client coordinates, you need to use
elem.getBoundingClientRect()
, which yields left, top, width, height among other things, and I use to find the centre of a box:Putting all that together, you can draw a line from one element to another.