可以暂停svg动画而不会丢失累积信息吗?

bit*_*ask 2 svg svg-animate

您可以无限制地停止和重复动画,但如果重新启动无限动画,它将失去其累积值并从初始值开始.

也许我应该用一个例子来澄清; 拍这个动画:

  <animate id="main"
    attributeName="transform" attributeType="XML"
    begin="startbox.click" dur="1s" end="endbox.click"
    from="rotate(0)" to="rotate(360)"
    additive="sum"
    accumulate="sum"
    fill="freeze"
    repeatCount="indefinite"
  />
Run Code Online (Sandbox Code Playgroud)

如果我停止这个动画,并启动另一个(比如说id="second")影响同一个物体的旋转,那么second它将完全从main左边的点继续.但是如果我通过单击startbox(或通过任何其他事件)来启动这个,它将减去所有差异main并在开始之前重置旋转.

我想要的是一个正确的"暂停",我可以继续以前动画停止的地方.当然,我可以添加固定数量的相同动画和相同数量的相同开始/停止按钮来模拟效果,但这不是一个好的解决方案.特别是因为暂停次数有限.


整个例子(似乎只在Opera中工作)

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg
     xmlns="http://www.w3.org/2000/svg" version="1.1"
     xmlns:xlink="http://www.w3.org/1999/xlink">
  <desc>Click example</desc>
  <g>
    <rect id="startbox"
      x="10" y="10"
      width="20" height="20"
      stroke="none" fill="green"
    />
    <rect id="endbox"
      x="10" y="40"
      width="20" height="20"
      stroke="none" fill="red"
    />
    <g transform="translate(200,200)">
    <rect
      x="0" y="0"
      width="50" height="5"
      stroke="#10e9f3" fill="#30ffd0"
    >
      <animate
        attributeName="transform" attributeType="XML"
        begin="startbox.click" dur="1s" end="endbox.click"
        from="rotate(0)" to="rotate(360)"
        additive="sum"
        accumulate="sum"
        fill="freeze"
        repeatCount="indefinite"
      />
    </rect>
    </g>
  </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

joh*_*jik 11

它内置于SVG中

 SVGRoot.pauseAnimations();
Run Code Online (Sandbox Code Playgroud)

  • 注意播放功能是unpausenntions().我花了一段时间才弄明白. (7认同)
  • SVG DOM界面的MDN链接:https://developer.mozilla.org/en-US/docs/Web/API/SVGSVGElement (2认同)

Rob*_*son 5

使用 animate 元素为变换设置动画是无效的,您必须使用 animateTransform。见http://www.w3.org/TR/SVG/animate.html#AnimationAttributesAndProperties

如果 Opera 使用该 UA 制作动画,您应该向 Opera 报告您的测试用例。

  <animateTransform
    attributeName="transform" attributeType="XML"
    type="rotate"
    begin="startbox.click" dur="1s" end="endbox.click"
    from="0" to="360"
    additive="sum"
    accumulate="sum"
    fill="freeze"
    repeatCount="indefinite"
  />
Run Code Online (Sandbox Code Playgroud)

至于所问的问题,您可以使用 javascript 暂停动画,但使用 SVG 1.1,据我所知,您不能以声明方式进行。