4
I have two objects circle
, that I can move with the mouse. I wanted a line between the two circles, that whenever one moves, the line is always glued.
The code I have for moving objects:
var dragged = null; //L'élément en cours de drag
function start_drag(objet,event)
{
dragged = objet;
if( event.preventDefault ) event.preventDefault();
}
function drag_onmousemove(event) //Lorsque la souris se déplace
{
if( dragged )
{
x = event.clientX;
y = event.clientY;
elementHeight = dragged.clientHeight; //hauteur du obj
elementWidth = dragged.clientWidth;
dragged.style.position = 'absolute';
dragged.style.left = x - elementWidth/2 + 'px'; //divise longueur par 2 pour le milieu
dragged.style.top = y - elementHeight/2 + 'px';
}
}
function drag_onmouseup(event)
{
dragged = null;
}
function addEvent(obj,event,fct)
{
if( obj.attachEvent)
obj.attachEvent('on' + event,fct);
else
obj.addEventListener(event,fct,true);
}
addEvent(document,'mousemove',drag_onmousemove);
addEvent(document,'mouseup',drag_onmouseup);
<div id="divcircle" onmousedown="start_drag(document.getElementById('divcircle'), event);" style="position:absolute; height:50px; width:50px;">
<svg id="svg">
<circle cx="50" cy="50" r="5" stroke="black" stroke-width="3" fill="green" />
</svg>
</div>
<div id="divcircle2" onmousedown="start_drag(document.getElementById('divcircle2'), event);" style="position:absolute; height:50px; width:50px;">
<svg id="svg">
<circle cx="40" cy="40" r="5" stroke="black" stroke-width="3" fill="green" />
</svg>
</div>
Thank you, and how can I keep the position of the two circles?
– akm
@akm can take the attribute ELEMENT.cx.baseVal.value and Cy from the desired circle.
– Bacco
I used for example circle1.cx.baseVal.value, and from the initial position, what I want is the position whenever the object changes.
– akm
@akm take from the tip of the line instead of catch from Circle
selectedLine.y2.baseVal.value
– Bacco
And if you want to add more elements, just add the svg circles, and selectedLine.X3 so successively?
– akm
Every line has one X1 and one x2, are the separate ends. In case, if you want more circles, you have to put more lines as well. I put an example with another set of lines and circles. Remember that this is an example, the idea is not you copy and paste as is, but rather learn how to use, and improve for your own use.
– Bacco
For example, if you have too many circles, it pays to do the lines automatically, not in the SVG source. If you have interconnected chains, then you have to increase the function to move two lines or more in each circle. Remember that depending on the use, you can have more efficient ways. Use this as a starting point only.
– Bacco
@akm I think here you can see working legal. I did so to be independent of the function of drag 'n' drop http://jsfiddle.net/t26ou3u6/
– Bacco
Thanks, it works well. I don’t understand the code Circ.transform.animVal[0].matrix.e
– akm
@akm as the drag'n'drop is using a transformation matrix (a kind of "space distortion" instead of dragging objects originally), I am adding the original position of the object with the distortion factor applied to the drag, to obtain the final destination of the circle. To tell you the truth, you can change all the code so you don’t use transformation matrices, but it would hinder using Svgs at different scales from the original. Kind of tricky to explain around here.
– Bacco
I added the circles and the line, but I wanted to have the 3 in line for example. I put them in the corectas positions, but the line does not move. In other words, the middle circles would have 2 tips of 2 lines.
– akm
It would only increase the function to receive two lines instead of one, but look at all the possibilities, because depending on the combinations it pays to rethink the function or automate the generation of lines.
– Bacco