我的回答包含3个主要步骤:
首先,我们必须创建一条从 A 点到 B 点的路径,模拟弧线。有多种方法可以做到这一点,但你的问题不清楚。在这里,我使用二次曲线:
var arc = svg.append("path")
.style("fill", "none")
.style("stroke", "yellow")
.style("stroke-width", 2)
.attr("d", "M" + centroids.ANTIOQUIA[0] + "," +
centroids.ANTIOQUIA[1] + " Q" + centroids.BOYACA[0] +
"," + centroids.ANTIOQUIA[1] + " " +
centroids.BOYACA[0] + "," + centroids.BOYACA[1]);
这条路径可以有颜色或透明,都没关系。
其次,我们使用Bostock著名的沿路径平移 https://bl.ocks.org/mbostock/1705868沿该弧平移元素的代码。我更改了函数以将元素与路径一起传递,因此我们可以更改其大小:
circle.transition()
.duration(5000)
.attrTween("transform", translateAlong(arc.node(), circle.node()));
//the second argument is the circle -----------------^
function translateAlong(path, circle) {
var l = path.getTotalLength();
return function(d, i, a) {
return function(t) {
d3.select(circle).attr("r", circleSize(t))
//here we can change circle's size
var p = path.getPointAtLength(t * l);
return "translate(" + p.x + "," + p.y + ")";
};
};
}
最后,我使用线性比例再次放大和缩小圆:
var circleSize = d3.scale.linear()
.domain([0, 0.5, 1])
.range([4, 10, 4]);
这里的域从 0 到 1,因为这是参数值的范围t
可以假设。
这是更新的小提琴:http://jsfiddle.net/zkc2wton/ http://jsfiddle.net/zkc2wton/
这是第二个小提琴,改变运动开始和结束时的不透明度:http://jsfiddle.net/4pdusase/ http://jsfiddle.net/4pdusase/