D3 SVG transform rotation transition behaving weirdly

前端 未结 1 1126
野性不改
野性不改 2020-12-22 12:33

I generated a geometric SVG pattern with D3 and I wanted to animate it. I wanted to have the circles and square spinning around their centre. However when they rotate the ce

相关标签:
1条回答
  • 2020-12-22 13:30

    For D3 to rotate around a given center, you need to provide a custom tween function which can make use of a string interpolator:

    function spinCircles() {
      d3
        .selectAll("#fixedGroup")
        .transition()
        .duration(4000)
        .attrTween("transform", function() {
          var center = "" + m/2 + "," +  m/2;
          return d3.interpolateString("rotate(0," + center + ")", "rotate(90," + center + ")");
        });
    } 
    

    Have a look at the updated codepen for a working example.

    0 讨论(0)
提交回复
热议问题