简体   繁体   English

D3中的循环过渡

[英]Looping transition in D3

Essential exposition: I'm a bit of a D3 newbie. 基本论述:我有点像D3新手。

My goal is to have a line move from point A to point B, then immediately reappear at point A and repeat that transition. 我的目标是让一条线从A点移动到B点,然后立即重新出现在A点并重复该过渡。 I've tried a lot of different things, but this is the closest I've come. 我尝试了很多不同的东西,但这是我最接近的。

var svg = d3.select("body")
        .append("svg")
        .attr("width", 500)
        .attr("height", 500);

// code, code, code, irrelevant code...

function timeForTimeline(){ // har
    var timeline = svg.append("line")
        .attr("stroke", "steelblue")
        .attr({
            'x1': 0,
            'y1': 130,
            'x2': 168,
            'y2': 130
        });
    (function repeat() {
        timeline = timeline
            .transition()
            .duration(4000)
            .ease("linear")
            .attr({
                'x1': 0,
                'y1': 430,
                'x2': 168,
                'y2': 430   
            })
            .each("end", function(){
                d3.select(this)
                    .transition()
                    .duration(0)
                    .attr({
                        'x1': 0,
                        'y1': 130,
                        'x2': 168,
                        'y2': 130
                    })
                    .each("end", repeat);
            });
    })();
};

The result is an excellent starting transition, followed by the line quickly jumping between point A and point B WITHOUT the duration(4000) bit taking effect. 结果是一个很好的起始转换,然后在A点和B点之间快速跳转,而不会使duration(4000)位生效。 I've also tried removing the line at the bottom ( d3.select(this).remove() ) then appending a new one at the top of each call to repeat(). 我也尝试删除底部的行( d3.select(this).remove() )然后在每次调用的顶部追加一个新的重复()。 I've also tried just resetting x1, x2, y1, and y2 and skipping the transition altogether. 我也尝试过重置x1,x2,y1和y2并完全跳过转换。 I'm not saying I attempted those correctly, but my results were either no lines at all, infinite lines, or a single line that reaches point B and stays there. 我并不是说我正确地尝试了这些,但我的结果要么根本没有线,无限的线,要么是到达B点的一条线并且停留在那里。

Any other suggestions on how to accomplish my (likely very simplistic) goal? 关于如何实现我(可能非常简单)的目标的任何其他建议? Thanks so much for your help! 非常感谢你的帮助!

It seems to me that you don't need to specify the starting coordinates twice. 在我看来,你不需要两次指定起始坐标。 You could just assign the initial coordinates inside the repeat function and call it immediately like so: 你可以在repeat函数中分配初始坐标并立即调用它:

function timeForTimeline() {
    var timeline = svg.append("line")
        .attr("stroke", "steelblue");

    repeat();

    function repeat() {
      timeline.attr({
        'x1': 0,
        'y1': 130,
        'x2': 168,
        'y2': 130
      })
      .transition()
      .duration(4000)
      .ease("linear")
      .attr({
        'x1': 0,
        'y1': 430,
        'x2': 168,
        'y2': 430   
      })
      .each("end", repeat);
    }
}

Here's a fiddle 这是一个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM