var points = [
[480, 200],
[580, 400],
[680, 100],
[780, 300],
[180, 300],
[280, 100],
[380, 400]
];
var svg = d3.select("body").append("svg")
.attr("width", 960)
.attr("height", 500);
svg.append("defs")
.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", 960/2)
.attr("height", 500/2);
var g = svg.append("g")
.attr("clip-path", "url(#clip)")
var path = g.append("path")
.data([points])
.attr("d", d3.svg.line()
.tension(0) // Catmull–Rom
.interpolate("cardinal-closed"));
g.selectAll(".point")
.data(points)
.enter()
.append("circle")
.attr("r", 4)
.attr("transform", function(d) { return "translate(" + d + ")"; });
circle = g.append("circle")
.attr("r", 13)
.attr("transform", "translate(" + points[0] + ")");
line = g.append("line")
.attr("y1", -50)
.attr("y2", 50)
.attr("class", "x-hover-line hover-line")
.attr("transform", "translate(" + points[0] + ")");
transition();
function transition() {
circle.transition()
.duration(10000)
.attrTween("transform", translateAlong(path.node()))
.each("end", transition);
line.transition()
.duration(10000)
.attrTween("transform", translateAlong(path.node()))
.each("end", transition);
}
// Returns an attrTween for translating along the specified path element.
function translateAlong(path) {
var l = path.getTotalLength();
return function(d, i, a) {
return function(t) {
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
path {
fill: none;
stroke: #000;
stroke-width: 3px;
}
circle {
fill: steelblue;
stroke: #fff;
stroke-width: 3px;
}
.hover-line {
/*stroke: #6F257F;*/
stroke: #140917;
stroke-width: 2.5px;
/*stroke-dasharray: 3,3;*/
}
.hover-text {
font-size: 22px;
font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.8/d3.min.js"></script>