【问题标题】:Cannot animate SVG along path produced in Inkscape无法沿 Inkscape 中生成的路径为 SVG 设置动画
【发布时间】:2019-05-15 19:53:12
【问题描述】:

我在 Inkscape 中生成了以下 SVG,它有一个 Pacman 图形 (id=pacman) 和一个路径 (id=trajectory)。目标是让人物沿着路径移动。

将图像保存到文件后,我对其进行编辑,在 Pacman-figure 中添加以下部分:

      <animateMotion
         path="m 39.645298,114.12279 c 24.797657,0.79422 33.02086,-32.601373 61.073142,-32.542731 27.55775,0.05761 37.28264,-38.721653 63.91959,-39.287919"
         begin="2s"
         dur="10s"
         repeatCount="indefinite"
         rotate="auto" />

路径坐标从trajectory 对象复制而来。结果,我得到了这个文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   width="165.06496mm"
   height="104.70296mm"
   viewBox="0 0 165.06496 104.70296"
   version="1.1"
   id="svg8"
   inkscape:version="0.92.3 (2405546, 2018-03-11)"
   sodipodi:docname="clean-animation.svg">
  <defs
     id="defs2" />
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="1.4"
     inkscape:cx="343.91864"
     inkscape:cy="205.09936"
     inkscape:document-units="mm"
     inkscape:current-layer="layer1"
     showgrid="false"
     fit-margin-top="0"
     fit-margin-left="0"
     fit-margin-right="0"
     fit-margin-bottom="0"
     inkscape:window-width="1920"
     inkscape:window-height="1005"
     inkscape:window-x="-9"
     inkscape:window-y="1071"
     inkscape:window-maximized="1" />
  <metadata
     id="metadata5">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1"
     transform="translate(-19.575883,-23.159755)">
    <path
       style="opacity:0.597;fill:#ffff00;fill-opacity:1;stroke:#008000;stroke-width:0.26458332;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:0.26458331, 0.52916662999999997;stroke-dashoffset:0;stroke-opacity:0.8"
       id="pacman"
       sodipodi:type="arc"
       sodipodi:cx="33.408691"
       sodipodi:cy="115.25672"
       sodipodi:rx="13.698855"
       sodipodi:ry="12.473214"
       sodipodi:start="0"
       sodipodi:end="5.9207142"
       d="m 47.107547,115.25672 a 13.698855,12.473214 0 0 1 -12.459194,12.42204 13.698855,12.473214 0 0 1 -14.714154,-10.1738 13.698855,12.473214 0 0 1 9.796115,-14.26337 13.698855,12.473214 0 0 1 16.487129,7.59231 l -12.808752,4.42282 z"
       inkscape:label="#path815">
      <animateMotion
         path="m 39.645298,114.12279 c 24.797657,0.79422 33.02086,-32.601373 61.073142,-32.542731 27.55775,0.05761 37.28264,-38.721653 63.91959,-39.287919"
         begin="2s"
         dur="10s"
         repeatCount="indefinite"
         rotate="auto" />
    </path>
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="m 39.645298,114.12279 c 24.797657,0.79422 33.02086,-32.601373 61.073142,-32.542731 27.55775,0.05761 37.28264,-38.721653 63.91959,-39.287919"
       id="trajectory"
       inkscape:connector-curvature="0"
       sodipodi:nodetypes="csc"
       inkscape:label="#path817" />
  </g>
</svg>

问题是动画不起作用。 2 秒后,如begin="2s" 所定义,黄色图形消失。

我的第一个猜测是坐标以某种方式偏移,并且 Pacman 形状在外太空某处被动画化。但是,如果是这种情况,那么我会在每个循环开始时看到该形状恢复片刻。

当我尝试使用各种教程中的手写 SVG 时,它们会起作用。但是,我自己在 Inkscape 中制作的那些并没有达到预期的效果。

这是什么原因?如何纠正?

【问题讨论】:

  • 您需要将您的 pacman 形状置于原点 (0,0) 的中心。或者您需要从 0,0 开始您的运动路径。 pacman 的偏移量与运动路径位置的偏移量相加。这意味着您的 pacman 将不在屏幕底部。

标签: animation svg inkscape


【解决方案1】:

为了了解会发生什么,请将overflow:visible 添加到 svg 元素。你会看到那个packman在离路径很远的地方想知道

在下一个示例中,我将您的 packman 更改为一个圆圈。圆的路径以点 {x:0,y:0} 为中心,您应该重写 packman 的路径,使其从 svg 元素的原点开始。

svg{border:1px solid;}
<svg width="165.06496mm"
   height="104.70296mm"
   viewBox="0 0 165.06496 104.70296"
   id="svg8">
  <g id="layer1" >
    <path
       style="opacity:0.597;fill:#ffff00;fill-opacity:1;stroke:#008000;stroke-width:0.26458332;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:0.26458331, 0.52916662999999997;stroke-dashoffset:0;stroke-opacity:0.8"
       id="pacman"
       d="M-12.5,0A12.5 12.5 0 0 1 12.5,0A12.5 12.5 0 0 1 -12.5,0">
      <animateMotion
         path="m20.07, 90.96 c24.798, 0.794 33.021, -32.601 61.073, -32.543 c27.558, 0.058 37.283, -38.722 63.920, -39.288 1 27.55775,0.05761 37.28264,-38.721653 63.91959,-39.287919"
         begin="2s"
         dur="10s"
         repeatCount="indefinite"
         rotate="auto" />
    </path>
    <path
       style="fill:none;fill-rule:evenodd;stroke:#000000;stroke-width:0.26458332px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
       d="m20.07, 90.96 c24.798, 0.794 33.021, -32.601 61.073, -32.543 c27.558, 0.058 37.283, -38.722 63.920, -39.288 "
       id="trajectory" />
  </g>
</svg>

【讨论】:

  • 确实,将sodipodi:cx="0" sodipodi:cy="0" 设置为原始文件的pacman 形状就足够了。事情仍然不清楚 - 如果我使用 Inkscape 编辑文件并且我有一张复杂的图片,其中包含许多应该沿着路径移动的移动元素 - 将它们全部放到 0,0 会破坏图像本身。动画在浏览器中运行时看起来不错,但在 Inkscape 中编辑静态图像时 - 移动对象不在它们所属的位置。这是最佳做法吗?
  • 这是a demo from the MDN site,其中动画圈对cxcy 没有价值,这相当于cx="0" cy="0"。如果在动画开始前 2 秒内看到 packman 让您感到困扰,您可以更改 SVG 元素的 viewBox,使原点 0,0 留在画布之外
猜你喜欢
  • 2012-07-14
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-29
  • 2021-07-20
  • 2015-10-01
  • 2018-02-15
  • 2020-01-29
相关资源
最近更新 更多