Search code examples
svgsvg-animate

Animate SVG with animateTransform


Trying to animate an SVG element with the skewX. It works, however, not exactly the way I want it to.

  • Now: the bottom part moves to the left
  • Goal: the upper part should move to the right instead (and bottom stays in place)

I tried with transform-origin but it didn't work. Any ideas how to solve this mystery?

<svg xmlns="http://www.w3.org/2000/svg" width="102" height="102" viewBox="-50 -50 102 102">
  <g>

    <rect width="10%" height="50%"
    style="fill:none; stroke:red; stroke-with:3;">

    <animateTransform
      attributeName="transform"
      attributeType="XML"
      type="skewX"
      from="0"
      to="-20"
      begin="0.5s"
      dur="0.2s"
      repeatCount="1"
      fill="freeze"
      id="fallen"/>

    </rect>
  </g>
</svg>


Solution

  • Use another transform to move the whole thing to the right at the same time as the bottom moves left.

    <svg xmlns="http://www.w3.org/2000/svg" width="102" height="102" viewBox="-50 -50 102 102">
      <g>
    
        <rect width="10%" height="50%"
        style="fill:none; stroke:red; stroke-with:3;">
    
        <animateTransform
          attributeName="transform"
          attributeType="XML"
          type="skewX"
          from="0"
          to="-20"
          begin="0.5s"
          dur="0.2s"
          repeatCount="1"
          fill="freeze"
          id="fallen"/>
       <animateTransform
          attributeName="transform"
          attributeType="XML"
          type="translate"
          from="0"
          to="20"
          begin="0.5s"
          dur="0.2s"
          repeatCount="1"
          fill="freeze"
          additive="sum"/>
    
        </rect>
      </g>
    </svg>