2
I want to draw up a graph where I can move the dots (circle
) of a Javascript line. When moving the object circle
, the line is always glued to the circle.
I want to play the genre of this:
That is, it is a line for each circle, being possible to move them and get their positions.
The base graph is the following SVG, which is a kind of Cartesian plan:
<svg id="svg" width=100% height=500px >
//vertical
<line title="Vertical" x1=60 y1=60 x2=60 y2=450 stroke-width=2 stroke=black />
//text vertical
<text x=60 y=60 text-anchor=end fill=black ><?php echo $txtmaxresistance." Ohm";?></text>
<text x=55 y=450 text-anchor=end fill=black ><?php echo $txtminresistance;?></text>
//horizontal
<line title="Horizontal" x1=60 y1=450 x2=450 y2=450 stroke-width=2 stroke=black />
//text horizontal
<text x=470 y=465 text-anchor=end fill=black ><?php echo $txtmaxtemp." Temp";?></text>
<text x=80 y=465 text-anchor=end fill=black ><?php echo $txtmintemp;?></text>
<line title=<?php echo $txtnome;?> x1=400 y1=90 x2=60 y2=450 stroke-width=2 stroke=black />
</svg>
The following code creates the various circles (dots) on top of a straight line:
var svg = document.querySelector('svg');
for (var i = 0; i < step; i++) {
var x = 30*i;
var y = 30*i;
var shape = document.createElementNS("http://www.w3.org/2000/svg", "circle");
shape.setAttributeNS(null, "cx", 100+x);
shape.setAttributeNS(null, "cy", 400-y);
shape.setAttributeNS(null, "r", 5);
shape.setAttributeNS(null, "fill", "green");
svg.appendChild(shape);
}
Thank you, good code. How can I get the position of each of the points now?
– akm
@akm Just retrieve a reference to the object created in some way and use the snippet
parseFloat(ponto.getAttributeNS(null, "cx"))
. Do the same for the positioncy
.– utluiz
I am using var x=parseFloat(ddData.element.getAttributeNS(null, "Cx")); within onmousemove function.
– akm
@akm Inside the function works because the
ddData.element
has reference to the clicked circle. If you want to recover the position of other circles or of all of them, you have to recover these references from somewhere else. I don’t know what you want it for exactly, but a simple idea is to store all the circles in one vector, then you can make a loop and retrieve the position of each one.– utluiz
Yes I want to recover the position of all the circles, then save their position x and y after moving in a file.
– akm
How can I take the lines from the beginning and the end? I changed the for, to step-2. I want 10 circles and 8 lines.
– akm
How can I recover the positions of the other circles?
– akm