如何让svg动画在图形中心周围缩放?

问题描述:

我试图通过使用animateTransformtype="scale"来使一颗恒星产生脉动,但不是围绕恒星的中心缩放,而是向左和向下缩放。如何缩放中心周围的星星?如何让svg动画在图形中心周围缩放?

SVG编码:

<svg version="1.1" id="star" xmlns="http://www.w3.org/2000/svg" x="170px" y="385px" width="100px" height="100px" viewBox="0 0 60 60" xml:space="preserve"> 
    <g> 
     <polygon fill="#DAC978" points="18.832,36.694 5.903,25.156 23.146,23.419 30.125,7.557 37.104,23.419 54.347,25.155 41.417,36.694 45.095,53.629 30.125,44.898 15.155,53.63" /> 
     <path fill="#A58144" d="M30.125,13.765l4.104,9.327l1.175,2.669l2.901,0.292l10.139,1.021l-7.603,6.785L38.665,35.8l0.618,2.851 l2.162,9.957l-8.801-5.134l-2.519-1.469l-2.52,1.469l-8.803,5.134l2.162-9.957l0.619-2.85l-2.175-1.942l-7.603-6.785l10.139-1.021 l2.901-0.292l1.174-2.669L30.125,13.765 M30.125,1.35l-8.681,19.729L0,23.237l16.08,14.352l-4.574,21.063l18.62-10.858 l18.618,10.858L44.17,37.589l16.081-14.352l-21.445-2.159L30.125,1.35L30.125,1.35z" /> 
     <animateTransform 
      attributeType="xml" 
      attributeName="transform" 
      type="scale" 
      from="0" 
      by="1" 
      dur="1s" 
      repeatCount="10" 
     /> 

    </g> 
</svg> 

jsFiddle有工作代码。

+0

这里是工作代码http://jsfiddle.net/ayD2N/43/ – 2014-09-20 10:26:26

animateTransform类型scale似乎总是从原点开始在(0, 0)。一种可能的解决方案是翻译您的多边形,使其中心位于(0, 0)

这里是工作jsFiddle example

你原来的代码所做的更改:

  • 视框从viewBox="0 0 60 60"变更为viewBox="-30 -30 60 60",以便有在(0, 0)viewBox中心(关于viewBoxes你可以看到this tutorial

  • polygonpath被分组,然后翻译,以便他们的中心也在(0, 0)

    <g transform="translate(-30,-30)"> 
        <polygon ... /> 
        <path ... /> 
    </g> 
    
+0

非常感谢! 它为我工作 – 2014-09-20 11:07:56