animation - 如何为 svg 的 animateTransform 设置原点

标签 animation svg

如果您查看此 SVG,我正在为 transform 属性设置动画,默认情况下它从左上角移动。如何将其更改为从中心设置动画?

<svg width="400" height="400" viewBox="0 0 400 400">
  <rect width="400" height="400" fill="#495ade"/>
  <g>
  <circle cx="50" cy="50" r="50" fill="#49de7d"/>
  <animateTransform attributeName="transform"
                    type="scale"
                    values="1 1;.5 .5;1 1"
                    begin="0s"
                    dur="1.5s"
                    repeatCount="indefinite"
                    calcMode="spline"
                    keySplines="0.4 0 0.5 1; 0.5 0 0.5 1"
                />
  </g>
</svg>

我已经尝试过的:
This answer说只在列出的每个值之后抛出 X 和 Y 坐标。所以我尝试了 values="1 1 50 50;.5 .5 50 50;1 1 50 50"但这会杀死动画。
您也可以使用 fromto而不是 values ,并包括 X 和 Y 原点,但我需要 3 个动画状态,而不是两个。

最佳答案

代替动画animateTransform type =" scale " ,可以使用增加圆半径的动画
在这种情况下,不需要定义 transform-origin因为圆圈位于 Canvas 的中心。

<svg width="400" height="400" viewBox="0 0 400 400">
  <rect width="400" height="400" fill="#495ade"/>
  <g>
  <circle cx="200" cy="200" r="50" fill="#49de7d">
  <animate attributeName="r"
                    values="50;25;50"
                    begin="0s"
                    dur="1.5s"
                    repeatCount="indefinite"
                    calcMode="spline"
                    keySplines="0.4 0 0.5 1; 0.5 0 0.5 1"
                /> 
    </circle>           
  </g>
</svg>

在中间状态暂停动画

<svg width="400" height="400" viewBox="0 0 400 400">
  <rect width="400" height="400" fill="#495ade"/>
  <g>
  <circle cx="200" cy="200" r="50" fill="#49de7d">
  <animate attributeName="r"
                    values="50;25;25;50;50"
                    begin="0s"
                    dur="1.5s"
                    repeatCount="indefinite"               
                /> 
    </circle>           
  </g>
</svg>

更新
考虑实现 CSS 动画

#crc1 {
transform-origin:center;
transform-box:fill-box;
animation: scale 1.5s infinite;
}

@keyframes scale {
0%{transform:scale(1);}
50%{transform:scale(.5);}
100%{transform:scale(1);}
}
<svg width="400" height="400" viewBox="0 0 400 400">
  <rect width="400" height="400" fill="#495ade"/>
  <g>
  <circle id="crc1" cx="200" cy="200" r="50" fill="#49de7d"/>
   </g>
</svg>

Upd2
微笑动画
来自 的优秀建议@Sphinxxx ,将css规则添加到group标签,从而保留animateTransform SVG
<svg width="400" height="400" viewBox="0 0 400 400">
  <rect width="400" height="400" fill="#495ade"/>
  <g style="transform-origin: center; transform-box: fill-box;">
  <circle  cx="200" cy="200" r="50" fill="#49de7d"/>
  <animateTransform attributeName="transform"
                    type="scale"
                    values="1 1;.5 .5;1 1"
                    begin="0s"
                    dur="1.5s"
                    repeatCount="indefinite"
                    calcMode="spline"
                    keySplines="0.4 0 0.5 1; 0.5 0 0.5 1"
                />
  </g>
</svg>

关于animation - 如何为 svg 的 animateTransform 设置原点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66445793/

相关文章:

ios - 移动尾部动画

javascript - 如何在 svg 中构建非常大的矩阵

Microsoft Edge 中的 SVG 渲染

html - 如何使用 HTML5 和 CSS3 制作动画?是否有免费的应用程序可以做到这一点?

ios - Swift:检查 SKActions 的进度

android - 矢量绘图中的微光动画

javascript - 笔画质量/使用 svg 进行抗锯齿

javascript - 无法使用 SVGweb 在 Internet Explorer 中将监听器添加到 SVG

javascript - Reactjs - 无法将 onClick 应用于渲染的组件

html - 悬停后使div保持原位