简体   繁体   English

d3沿SVG路径的稳定水平过渡

[英]d3 steady horizontal transition along an SVG path

I'm using a d3 attrTween to translate a circle over a path smoothly, similar to this example and as shown in the picture below: 我正在使用d3 attrTween在路径上平滑地平移一个圆,类似于此示例 ,如下图所示:

图形

The circle's transition is defined here: 圆的过渡在这里定义:

function transition() {
    circle.transition()
    .duration(2051)
    .ease("linear")
    .attrTween("transform", translateAlong(path.node()))
}

And the attribute tween is shown here: 补间属性如下所示:

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 + ")";
        };
    };
}

This works well thanks to the SVG method getPointAtLength , which allows us to retrieve coordinates at different lengths of the path. 这要归功于SVG方法getPointAtLength ,它使我们能够检索路径不同长度的坐标。 However, I need a different kind of behavior and I've been unable to come up with a solution so far. 但是,我需要一种不同的行为,到目前为止,我一直无法提出解决方案。

I need the circle to animate along the path, but at a steady horizontal speed. 我需要圆沿路径进行动画处理,但水平速度要稳定。 Meaning that the circle ought to take as much time to navigate this slice: 这意味着该圆应该花很多时间来导航此切片:

slice1

As it does with this slice: 与该切片一样:

slice2

Because both slices encompass the same width. 因为两个切片都包含相同的宽度。 On a low level, what I need is to be able to translate any X coordinate with its corresponding Y coordinate along the path . 在较低的层次上,我需要的是能够沿着path转换任何X坐标及其对应的Y坐标 I've looked at all the SVG path methods and I haven't found anything particularly useful here. 我已经研究了所有SVG路径方法 ,但在这里没有发现任何特别有用的方法。 I'm hoping there's some way in D3 to feed an X coordinate to a d3 line and retrieve its corresponding Y coordinate. 我希望D3中有某种方法可以将X坐标输入到d3线并检索其相应的Y坐标。

Here's a JSFiddle working as described above. 这是如上所述的JSFiddle工作。 I'd really appreciate any help I can get on this. 我真的很感谢我能为此提供的任何帮助。 Thanks! 谢谢!

I ended up creating a lookup array for all my points along the line using getPointAtLength : 我最终使用getPointAtLength为沿线的所有点创建了一个查找数组:

var lookup = [];
var granularity = 1000;
var l = path.node().getTotalLength();
for(var i = 1; i <= granularity; i++) {
    var p = path.node().getPointAtLength(l * (i/granularity))
    lookup.push({
        x: p.x,
        y: p.y
    })
}

Once I had all those points in my lookup table, I used a bisector in my translate tween: 在查找表中拥有所有这些点后,我在翻译补间中使用了平分线:

var xBisect = d3.bisector(function(d) { return d.x; }).left;
function translateAlong(path) {
    var l = path.getTotalLength();
    return function (d, i, a) {
        return function (t) {
            var index = xBisect(lookup, l * t);
            var p = lookup[index];
            return "translate(" + p.x + "," + p.y + ")";
        };
    };
}

And it works as expected! 它按预期工作! Yahoo! 雅虎

Fiddle 小提琴

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

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