【问题标题】:Stroke-dasharray tween on multiple paths多条路径上的 Stroke-dasharray 补间
【发布时间】:2015-03-17 15:41:11
【问题描述】:

我正在地图上投影多条线路路径,并希望在它们上使用stroke-dash interpolation 来显示路径移动。

我想在所有路径上使用相同的 Tween 函数,但 Tween 似乎为每条路径返回相同的值。所有的线条似乎都很好。只是 Tween 引起了一些问题。

它会导致这个问题,其中路径显示笔划破折号:

它应该如下所示:

我的线条是使用以下标准流程投影的,用于传单 x/y 转换:

var svg = d3.select(map.getPanes().overlayPane).append("svg");

var g = svg.append("g").attr("class", "leaflet-zoom-hide");
var transform = d3.geo.transform({
            point: projectPoint
        });
    var d3path = d3.geo.path().projection(transform);

    var toLine = d3.svg.line()
      .interpolate("linear")
        .x(function(d,i) {
            return applyLatLngToLayer(d).x
        })
        .y(function(d,i) {
            return applyLatLngToLayer(d).y
        });

    g.selectAll(".lineConnect").remove();

    var linePath = g.selectAll(".lineConnect")
        .data(series)
        .enter()
        .append("path")
        .attr("class", "lineConnect")

        linePath.attr("d", toLine)

在这里您可以看到调用 Tween 的函数:

 function transition() {
            linePath.transition()
                .duration(700).attrTween("stroke-dasharray", tweenDash);     
 } 
 function tweenDash() {
            return function(t) {
                var l = linePath.node().getTotalLength(); 
                interpolate = d3.interpolateString("0," + l, l + "," + l);
                return interpolate(t);
            }
 } 

【问题讨论】:

    标签: javascript d3.js interpolation tween


    【解决方案1】:

    您的代码假定您在选择中只有一个元素 (linePath.node()) - 您只获得第一个元素的长度。例如,您可以使用 .each() 使其适用于每一行:

    function transition() {
            d3.select(this).transition()
                .duration(700).attrTween("stroke-dasharray", tweenDash);     
     } 
     function tweenDash() {
            var that = this;
            return function(t) {
                var l = that.getTotalLength(); 
                interpolate = d3.interpolateString("0," + l, l + "," + l);
                return interpolate(t);
            }
     }
    
     linePath.each(transition);
    

    【讨论】:

    • 我尝试使用 .each(d3.select(this).attrTween("stroke-dasharray",tweenDash); 但它没有用.. 将尝试您的解决方案。
    • tweenDash函数中的this指的是窗口。
    • 对,嵌套函数...我已经编辑了答案。
    • 成功了。你是个坏蛋,先生。一个附带问题:您认为在两点之间进行补间和在它们之间使用 x、y 线过渡之间的性能比较如何?
    • 嗯,我不知道 - 我会针对个别情况进行基准测试。我的直觉是,只有当人类发生太多转变而无法理解整个事情时,这才会成为一个问题。
    猜你喜欢
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 2021-08-08
    • 2018-05-12
    • 2019-08-18
    • 2014-04-02
    相关资源
    最近更新 更多