I can tween a D3 arc's endAngle, but not it's startAngle. What am I doing wrong?

我的梦境 提交于 2020-01-14 13:14:36

问题


I'm just playing with this demo and recreating it myself: http://bl.ocks.org/mbostock/5100636

I can define a new endAngle and it'll animate just fine, but now I'm trying to animate the startAngle too. I just can't get it to work.

function animateArc(newStart, newEnd) {

    myArc.transition().duration(750).call(arcTween, newStart, newEnd);

    function arcTween(transition, newStart, newEnd) {
        transition.attrTween("d", function(d) {
            var interpolateStart = d3.interpolate(d.startAngle, startAngle);
            var interpolateEnd = d3.interpolate(d.endAngle, endAngle);
            return function(t) {
                d.startAngle = interpolateStart(t);
                d.endAngle = interpolateEnd(t);
                return arc(d);
            };
        });
    }
}

I get no errors of any kind, and the endAngle still animates, but the startAngle code I added is literally just.. ignored.

What is the correct way to tween an arc's startAngle?


回答1:


There are a few things that need to be modified. In the original script, the start value is fixed to 0 in the arc function. Then, since arc doesn't provide for the start value, it is provided through the datum.

var arc = d3.svg.arc()
        .innerRadius(90)
        .outerRadius(150);

var background = svg.append("path")
        .datum({
        endAngle: τ,
        startAngle: 0
    })

var foreground = svg.append("path")
        .datum({
        endAngle: .127 * τ,
        startAngle: -.127 * τ
    })

Et voila: http://jsfiddle.net/breizo/h74f180b/



来源:https://stackoverflow.com/questions/22792685/i-can-tween-a-d3-arcs-endangle-but-not-its-startangle-what-am-i-doing-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!