简体   繁体   English

D3圆半径的弹性缓和过渡导致设置负半径

[英]D3 Elastic easing transition of circle radius causes negative radius to be set

I have a very weird problem whilst using the ease("elastic", a, p) function in d3js. 在d3js中使用easy ease("elastic", a, p)函数时,我遇到一个非常奇怪的问题。 I am transitioning the radius of circles and I'm getting a flood of errors in the console, which are significantly slowing down my webpage. 我正在过渡圆的半径,并且控制台中出现大量错误,这大大降低了我的网页的速度。 The errors state that I'm setting a negative radius — after some debugging (from console logging everything to diffs with previous code) I found out that's caused by the elastic easing. 错误指出我将半径设置为负-在进行一些调试(从控制台记录所有内容到使用先前代码的差异)后,我发现这是由elastic松弛引起的。 Here's my code: 这是我的代码:

function redraw() {
    var day = d3.select('input[name="day"]:checked').node().value;
    var time = d3.select('input[type="range"]').node().value.toString();
    var direction = d3.select('input[name="direction"]:checked').node().value;

    // fix bug in which data for single digit hours is not displayed
    if (time.length == 1) {time = "0" + time;}

    if (circles !== undefined) {
        circles.transition().duration(900).ease('elastic', 1, 0.75).attr({
            fill: function(d) {
                return shadeColor("#ff0000", (-1 * getCircleSize(d.data, day, time, direction)));
            },
            r: function(d) {
                var size = getCircleSize(d.data, day, time, direction);

                if (size <= 0) {
                    return 0;
                } else if (size > 0 && size < 2) {
                    return size + 2;
                } else if (size > 50) {
                    return size*0.1 + 50;
                } else {
                    return size;
                }
            }
        });
    }
}

And here is an example of the error I'm getting: 这是我遇到的错误的示例:

Error: Invalid negative value for <circle> attribute r="-0.04404809159933931"
(anonymous function)
@ d3.v3.min.js:5l
@ d3.v3.min.js:3Lt
@ d3.v3.min.js:1qt
@ d3.v3.min.js:1

If I change the line: 如果我更改行:

circles.transition().duration(900).ease('elastic', 1, 0.75).attr({...});

to: 至:

circles.transition().duration(900).ease('quad').attr({...});

...it doesn't spit out any errors in the console. ...它不会在控制台中吐出任何错误。

Here's a screenshot as well: 这也是屏幕截图: d3-弹性虫

It would be great if you can give me some guidance on how to solve this problem. 如果您能给我一些有关如何解决此问题的指导,那将是很棒的。 Thanks in advance! 提前致谢!

Check the spec on d3.ease() : 检查d3.ease()的规格:

  • elastic(a, p) - simulates an elastic band; elastic(a,p)-模拟松紧带; may extend slightly beyond 0 and 1. 可能会略微超出0和1。

Because the elastic easing overshoots the range [0,1] a little, it is not useful for lengths which must not be negative like radius, width etc. As you already noticed there are other easing functions which are guaranteed to yield positive values only. 由于弹性缓动会稍微超出[0,1]范围,因此对于长度不能为负的长度(例如半径,宽度等)无效。您已经注意到,还有其他缓动函数只能保证产生正值。 Have a look at this overview on easing function to find a function which will stay within the range of [0,1]. 看一下此缓动函数概述,以找到将保持在[0,1]范围内的函数。

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

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