【发布时间】:2017-01-20 06:14:07
【问题描述】:
我正在尝试创建一个对Arc Tween 演示进行修改的视觉效果。在其中,我想要一个数据数组来定义每个弧的颜色和一般半径,并且在一个间隔内,开始和结束角度都应该缓慢地动画。但我认为我定义每条弧线的方式会导致事物过度动画化。
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="https://d3js.org/d3.v4.min.js"></script>
<title>Arcs</title>
</head>
<body>
<svg width="960" height="500"></svg>
</body>
</html>
脚本
// Modified from Arc Tween
// https://bl.ocks.org/mbostock/5100636
var tau = 2 * Math.PI; // http://tauday.com/tau-manifesto
var overlap = 50
var jsonArcs = [
{ "base_radius": 370, "color" : "red"},
{ "base_radius": 330, "color" : "orange"},
{ "base_radius": 290, "color" : "yellow"},
{ "base_radius": 250, "color" : "green"},
{ "base_radius": 210, "color" : "blue" },
{ "base_radius": 170, "color" : "purple"},
{ "base_radius": 130, "color" : "black"},
{ "base_radius": 90, "color" : "red"}
];
var arc = d3.arc()
.startAngle(function(d) { return Math.random() * tau; })
.endAngle(function(d) { return Math.random() * tau; })
.innerRadius(function(d) { return d.base_radius - overlap * Math.random(); })
.outerRadius(function(d) { return d.base_radius + overlap * Math.random(); });
var center_def = d3.arc()
.innerRadius(0)
.outerRadius(60)
.startAngle(0);
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
var path = g.selectAll("path")
.data(jsonArcs)
.enter().append("path")
.attr("fill", function(d, i) { return d.color; })
.attr("d", arc);
var center = g.append("path")
.datum({endAngle: tau})
.style("fill", "black")
.attr("d", center_def);
d3.interval(function() {
path.transition()
.duration(750)
.attrTween("d", arcTween(Math.random() * tau, arc));
}, 2500);
function arcTween(newAngle, obj) {
return function(d) {
var interpolate = d3.interpolate(d.endAngle, newAngle);
return function(t) {
d.endAngle = interpolate(t);
return obj(d);
};
};
}
不是每条弧线都从起始角度平滑动画到新角度,而是整个视觉多次跳转到新状态。
如何配置此功能,以使每条圆弧平滑地将其起点和终点角度从旧角度过渡到新角度?
【问题讨论】:
-
Related: stackoverflow.com/questions/22792685/… 但是我不知道如何使用提供的数据调用多个弧