SVG缩放动画从中心点而不是左上角

Mat*_*dor 10 animation svg svg-animate

如何animateTransform在SVG中使用从中心点而不是左上角缩放对象?

例:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100px" height="100px">
    <circle style="fill:blue;" cx="50" cy="50" r="45">
        <animateTransform attributeName="transform"
             type="scale"
             from="0 0"
             to="1 1"
             begin="0s"
             dur="1s"
             repeatCount="indefinite"
        />
    </circle>
</svg>
Run Code Online (Sandbox Code Playgroud)

(Codepen:http://codepen.io/anon/pen/wKwrPg?editors = 100 )

在此输入图像描述

tro*_*per 11

更改缩放变换以使用additive="sum"并应用将圆转换为图像中心的其他变换.因此,不要在图像的中心定义形状,而是定义其中心0 0,然后使用transform属性将其转换为50, 50(特定图像的确切中心).

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100px" height="100px">
    <circle style="fill:blue;" cx="0" cy="0" r="45" transform="translate(50 50)">
        <animateTransform attributeName="transform"
             type="scale"
             additive="sum" 
             from="0 0"
             to="1 1"
             begin="0s"
             dur="1s"
             repeatCount="indefinite"
        />
    </circle>
</svg>
Run Code Online (Sandbox Code Playgroud)

这是使用defsuse标签重用圆定义的另一个例子:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100px" height="100px">
    <defs>
        <circle id="def-circle" style="fill:blue;" cx="0" cy="0" r="45" />
    </defs>

    <use xlink:href="#def-circle" transform="translate(50 50)">
        <animateTransform attributeName="transform" 
        type="scale"
        additive="sum"    
        from="0 0"
        to="1 1"      
        beg="0s"
        dur="1s"
        repeatCount="indefinite" />
    </use>   
</svg>
Run Code Online (Sandbox Code Playgroud)


Mah*_*zad 3

您也可以借助CSS 样式transform-origin属性来完成此操作。
好处是您不必计算坐标并平移对象。

<svg version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
  <style>
    #logo { transform-origin: center; }
  </style>

  <g id="logo">
    <animateTransform attributeName="transform" begin="0s" type="scale" dur="2s" from="1" to=".5" repeatCount="indefinite"/>
    <circle r="8" cx="12" cy="12" />
  </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

<svg version="1.1" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
  <style>
    #logo { transform-origin: center; }
  </style>

  <g id="logo">
    <animateTransform attributeName="transform" begin="0s" type="scale" dur="2s" from="1" to=".5" repeatCount="indefinite"/>
    <circle r="8" cx="12" cy="12" />
  </g>
</svg>
Run Code Online (Sandbox Code Playgroud)
#logo {
  transform-origin: center;
}
Run Code Online (Sandbox Code Playgroud)

在您的具体情况下,另一种方法是对圆本身的半径进行动画处理:

<circle r="0" cx="50" cy="50">
  <animate attributeName="r" from="0" to ="10" begin="1s" dur="300ms" fill="freeze" />
</circle>
Run Code Online (Sandbox Code Playgroud)

感谢罗伯特·朗森的回答