css - 在 animateMotion 的路径上设置原点和旋转

标签 css animation svg

我想在使用 SVG 的 animateMotion 标签设置动画的路径上应用旋转。 似乎应用于 animateMotion 标记遵循的路径的规则 transform-origin:50%;transform: rotate(240deg); 不会改变动画。

<path id="theMotionPathIdLikeToRotate" d="m 100,100 -3e-6,-52.916668 45.82718,26.458333 0,52.916665" stroke-width="5px" stroke="aqua" fill="none" style="transform-origin:50%;transform: rotate(120deg);" stroke-linecap="round" stroke-linejoin="round" />

我的目标是创建动画并重复变换。 In this example 我想创建其他移动的圆圈,围绕六边形中心旋转 120 度和 240 度。

最佳答案

仅由 d 引用的路径的路径定义 ( <mpath> )元素被使用。任何transform它可能会被忽略。

您需要将变换应​​用于圆和 <mpath>在一起。

    <g style="transform-origin:50%;transform: rotate(240deg);">
      <circle cx="0" cy="0" r="5" fill="#333333">
        <animateMotion dur="4.45s" repeatCount="once">
          <mpath xlink:href="#theMotionPath3"/>
        </animateMotion>
      </circle>
    </g>"

<!DOCTYPE HTML>
  <html>
    <body>
      <?xml version="1.0"?>
    <svg width="400" height="400" viewBox="0 0 200 200"
        xmlns="http://www.w3.org/2000/svg" version="1.1"
        xmlns:xlink="http://www.w3.org/1999/xlink" style="background:aquamarine">
        <style>
        path {
          animation-name:animateDash;
          animation-duration:5s;
          animation-iteration-count:once;
        }
        @keyframes animateDash {
          from{stroke-dasharray:0,2305}
          to  {stroke-dasharray:2305,0}
        }
        </style>


        <circle cx="50%" cy="50%" r="1" fill="firebrick"  />

        <path id="theMotionPath" d="m 100,100 -3e-6,-52.916668 45.82718,26.458333 0,52.916665" stroke-width="5px" stroke="antiquewhite" fill="none" stroke-linecap="round" stroke-linejoin="round" />

        <path id="theMotionPath2" d="m 100,100 -3e-6,-52.916668 45.82718,26.458333 0,52.916665" stroke-width="5px" stroke="aqua" fill="none" style="transform-origin:50%;transform: rotate(120deg);" stroke-linecap="round" stroke-linejoin="round" />
        
        <path id="theMotionPath3" d="m 100,100 -3e-6,-52.916668 45.82718,26.458333 0,52.916665" stroke-width="5px" stroke="azure" fill="none" style="transform-origin:50%;transform: rotate(240deg);" stroke-linecap="round" stroke-linejoin="round" />

        <circle cx="0" cy="0" r="5" fill="#333333">
          <animateMotion dur="4.45s" repeatCount="once">
            <mpath xlink:href="#theMotionPath3"/>
          </animateMotion>
        </circle>

        <g style="transform-origin:50%;transform: rotate(120deg);">
          <circle cx="0" cy="0" r="5" fill="#333333">
            <animateMotion dur="4.45s" repeatCount="once">
              <mpath xlink:href="#theMotionPath3"/>
            </animateMotion>
          </circle>
        </g>"

        <g style="transform-origin:50%;transform: rotate(240deg);">
          <circle cx="0" cy="0" r="5" fill="#333333">
            <animateMotion dur="4.45s" repeatCount="once">
              <mpath xlink:href="#theMotionPath3"/>
            </animateMotion>
          </circle>
        </g>"

    <!--- HIDES animateMotion's reset-->
            <circle cx="" cy="" r="20" fill="aquamarine"  />
      <script type="text/javascript">
        console.log(theMotionPath.getTotalLength());
      </script>
    </svg>
  </body>
</html>

关于css - 在 animateMotion 的路径上设置原点和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632058/

相关文章:

css - 附加脚本不起作用

javascript - 如何计算贝塞尔曲线上的控制点?

html - HTML 中包含的带有 "img"标签的 SVG 是否可以链接到带有 "image"标签的外部图像?

jquery - 当我们在 bootstrap 中向下滚动时如何在顶部附加 div

css - 为什么它们在 html 和 css 上不同? id inside vs outside 视频标签

jquery - Bootstrap 中的巨型封面照片(全宽、全高)

css - 从一个动画过渡到另一个动画

jquery - 使用 css dvision 正确设置动画

Java动画内存问题

SVG 滤镜对于直线不能正常工作