在D3 V4中,如何使用.ease(“bounce”)属性?

时间:2016-11-23 12:50:44

标签: javascript d3.js

这是我的代码:

<html>
<head>
<meta charset="UTF-8">
<title>circle</title>
</head>
 
<body>
    <script src="http://d3js.org/d3.v4.min.js"></script>
    <script> 
        var width = 400;    
        var height = 400;    
        var svg = d3.select("body")    
                    .append("svg")     
                    .attr("width",width)     
                    .attr("height",height);     

        var circle1 = svg.append("circle")  
                         .attr("cx", 100)  
                         .attr("cy", 100)   
                         .attr("r", 45) 
                         .style("fill","green");
        circle1.transition()
            .duration(1000)  //延迟1000ms
            .attr("cx", 300);

        var circle2 = svg.append("circle")
                         .attr("cx", 100)
                         .attr("cy", 100)
                         .attr("r", 45)
                         .style("fill","green");

        circle2.transition()
            .duration(1500)
            .attr("cx", 300)
            .style("fill", "red");

        var circle3 = svg.append("circle")
                         .attr("cx", 100)
                         .attr("cy", 100)
                         .attr("r", 45)
                         .style("fill","green");
        circle3.transition()
            .duration(2000)
            .transition()
            .ease("bounce")
            .attr("cx", 300)
            .style("fill", "red")
            .attr("r", 25);

    </script>     
</body>
</html>

当我学习如何在d3 v4.x中使用.ease("bounce")时,错误总是在html:45中跳出。在官方介绍中:.ease("bounce")现在应该像这样使用:

d3.easeBounce(t)

但它也不起作用,所以我不知道如何修改它。你能给我一个很好的介绍吗?谢谢!

2 个答案:

答案 0 :(得分:7)

transition.ease([value])上的文档告诉我们,

  

必须将指定为函数。

因此,您只需要传入缓动函数d3.easeBounce而不实际调用它(注意d3.easeBounce后面没有括号):

circle3.transition()
  .duration(2000)
  .transition()
  .ease(d3.easeBounce)   // Pass in the easing function

答案 1 :(得分:1)

I agree with Altocumulus's answer, but I try to get rid of one of the middle.transition(), and it will run well.

circle3.transition()
  .duration(2000)
  .ease(d3.easeBounce)