动态更改 SVG 动画速度而无需位置跳跃

Change SVG animation speed dynamically without position jumping

我有一个简单的 SVG,其中有一个元素围绕圆形路径旋转。当用户悬停时,通过更改 dur 属性,旋转会变得更快。问题是当它发生时元素会跳转到不同的位置。如何解决?

$('g').hover(
  function() {
    $(this).find('animateMotion').attr('dur', '3s');
  },
  function() {
    $(this).find('animateMotion').attr('dur', '7s');
  }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width='500' height='500'>
  <g>
    <path id='circle' d='M 250, 250 m -200, 0 a 200,200 0 1,0  400,0 a 200,200 0 1,0 -400,0' fill='none' stroke='red' stroke-width='10' />
    <circle cx="50" cy="50" r="40" fill="red" transform='translate(-50, -50)'>
      <animateMotion dur="7s" repeatCount="indefinite">
        <mpath xlink:href="#circle" />
      </animateMotion>
    </circle>
  </g>
</svg>

调整动画时间轴,使动画在同一点。

var firstTime = true;
$('g').hover(
  function() {
    $(this).find('animateMotion').attr('dur', '3s');
    if (firstTime) {
      firstTime = false;
      return;
    }
    document.getElementById("root").setCurrentTime(
      document.getElementById("root").getCurrentTime() * 3 / 7);
  },
  function() {
    $(this).find('animateMotion').attr('dur', '7s');
    document.getElementById("root").setCurrentTime(
      document.getElementById("root").getCurrentTime() * 7 / 3);      }
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg id="root" width='500' height='500'>
  <g>
    <path id='circle' d='M 250, 250 m -200, 0 a 200,200 0 1,0  400,0 a 200,200 0 1,0 -400,0' fill='none' stroke='red' stroke-width='10' />
    <circle cx="50" cy="50" r="40" fill="red" transform='translate(-50, -50)'>
      <animateMotion dur="3s" repeatCount="indefinite">
        <mpath xlink:href="#circle" />
      </animateMotion>
    </circle>
  </g>
</svg>