animation - 如何在SVG中添加自定义动画?

标签 animation svg

我想使用动画旋转已在 SVG 中内置的图像,无需任何在线网站、编辑器且无需 jQuery。我想定制。

请帮忙。

最佳答案

CSS 动画

可以使用 <image> 在 SVG 中添加光栅图像标签

并使用常用的 CSS 动画规则

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="499.968" height="281.232" viewBox="0 0 1920 1080">
<defs>
    <style>
      .cls-1 {
        opacity: 0.96;
      }
	 #disk{
	transform-origin: center center;
	transform-box: fill-box;
	animation: rotate_disk 10s linear forwards infinite;
  }

  @keyframes rotate_disk {
  	100% {
  		transform: rotateZ(360deg);
  	}
  }
	  
    </style>
  </defs>
<image x="293" y="75" width="1255" height="959" xlink:href="/image/34irD.png"/>

<image id="disk" class="cls-1" x="515" y="165" width="815" height="815" xlink:href=" /image/Qas6q.png"/> 

<image id="scris" class="cls-1" x="363" y="740" width="1105" height="233" xlink:href=" /image/TCbha.png"/>
</svg>

SVG 动画

旋转动画时没有圆盘跳动,因此有必要精确计算旋转中心

计算时,JS方法getBBox ()已使用

<script>
let BB = gr1.getBBox();

 console.log(BB.x + BB.width / 2);
 console.log(BB.y + BB.height / 2);
</script>

获取的旋转中心坐标:x="925.5" y="572.5"

旋转动画公式:

<animateTransform attributeName="transform" type="rotate" dur="10s"
 values="0 922.5 572.5;360 922.5 572.5;"  />

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="499.968" height="281.232" viewBox="0 0 1920 1080">

<image x="293" y="75" width="1255" height="959" xlink:href="/image/34irD.png"/>
<g id="gr1">
<image id="disk" class="cls-1" x="515" y="165" width="815" height="815" xlink:href=" /image/Qas6q.png"/> 
  <animateTransform attributeName="transform" type="rotate" dur="10s" values="0 922.5 572.5;360 922.5 572.5;" repeatCount="indefinite"  />
</g>
<image id="scris" class="cls-1" x="363" y="740" width="1105" height="233" xlink:href=" /image/TCbha.png"/>
</svg> 

   

关于animation - 如何在SVG中添加自定义动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61897490/

相关文章:

javascript - 如何设置 .animate 函数 vanilla JS 参数中的动画填充模式?

javascript - 在 svg 文档中嵌入 svg 文件,通过 JavaScript 访问它的对象

pdf - 使用具有可靠字体选择的 ImageMagick 将 SVG 转换为 PDF?

jquery - 查找可点击的世界地图

animation - 如何在 StackNavigator 中更改动画的方向?

javascript - 根据ids选择多个元素,得到相邻的最大组

html - 如何垂直和水平定位 SVG 图像中心?

javascript - 将 SVG 组件作为背景图像 react 到 div

animation - 如何用 Flutter 在同屏路线上制作英雄风格的动画?

jQuery 自动改变 div