Edit: the Answer (credits to @musically_ut for getPointAtLength, but it seems like I have to do the real work myself. Note: the "draw.circle"-syntax is from svg.js)
.attr({id: "mypath"});
find it: var a = document.getElementById("IdOfSvgCurve");
Draw a few circles on the curve. Coordinates must be relative to starting point.
var pt_start = a.getPointAtLength(0);
for (var i = 0; i < len; i++) {
var pt = a.getPointAtLength(i*10);
var c = draw.circle(7)
.fill({ color: "green" })
.move(pt.x - pt_start.x, pt.y - pt_start.y);
}
Original Question:
Say I have an SVG curve, could be a bezier or an ellipse
<path id="SvgjsPath1044" d="M 125,75 a100,50 0 0,0 100,50"/>
http://jsfiddle.net/wVC7j/
How can I place dots right on that curve? Like this:
https://upload.wikimedia.org/wikipedia/commons/thumb/4/41/Interpolation_example_polynomial.svg/220px-Interpolation_example_polynomial.svg.png
It could be using something like pathLength, use a "coordinate system", or it could be by actually calculting points that are on the curve and adding them using their coordinates.
This is mostly for illustration, so I don't necessarily need a general solution.
I am using the svg.js library but could do this part "natively".
Thanks!
Arcs. The other type of curved line that can be created using SVG is the arc, called with the A command. Arcs are sections of circles or ellipses. For a given x-radius and y-radius, there are two ellipses that can connect any two points (as long as they're within the radius of the circle).
The d attribute defines a path to be drawn. A path definition is a list of path commands where each command is composed of a command letter and numbers that represent the command parameters.
You can change the svg viewBox attribute to center the path. This is especially useful if you have several paths in the svg, so you do not have to add a transform to each one. In your example that would be viewBox="0 15.674 144 144" , getting the y-offset the same way as in Pauls answer.
SVGPathElement
has a function getPointAtLength().
You can use this function to find the points on the path and then put circle
elements at those positions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With