Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse the moving direction of the SVG animation on the <mpath>?

I am trying to reverse the moving direction of the two red dots along the path in the SMIL SVG animation.

Of all the <animateMotion> attributes I got from the documentation, I am not able to find one that is appropriate for this attempt. Help is appreciated!

Documentation here.

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="320px"
     height="320px" viewBox="0 0 320 320" enable-background="new 0 0 320 320" xml:space="preserve">

  <path class="stage1" id="stage1-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="
  M160,220.499c-11.284,0-20.432-9.147-20.432-20.432v-40.868"/>

  <path class="stage1" id="stage1-2" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="
  M180.432,159.199v40.868c0,11.284-9.147,20.432-20.432,20.432"/>

  <circle  r="8" fill="red">
    <animateMotion dur=".6s" begin=".4s" fill="remove" rotate="auto">
      <mpath xlink:href="#stage1-1"/>
    </animateMotion>
  </circle>
  <circle r="8" fill="red">
    <animateMotion dur=".6s" begin="s" fill="remove" rotate="auto">
      <mpath xlink:href="#stage1-2"/>
    </animateMotion>
  </circle>
</svg>

https://codepen.io/KeliChiu/pen/gabNWM

like image 805
Keli Avatar asked Sep 21 '25 13:09

Keli


1 Answers

The simplest way is to use the keyTimes and keyPoints attributes to tell the animation to run backwards.

keyPoints="1;0" keyTimes="0;1"

Here we are telling the animation to be at position "1" on the path (the end) at time 0, and position 0 (the start) at the end of the animation.

Demo:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" 
     width="320px" height="320px" viewBox="0 0 320 320">

    <path class="stage1" id="stage1-1" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="M160,220.499c-11.284,0-20.432-9.147-20.432-20.432v-40.868"/>
    <path class="stage1" id="stage1-2" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="M180.432,159.199v40.868c0,11.284-9.147,20.432-20.432,20.432"/>

    <circle  r="8" fill="red">
        <animateMotion dur=".6s" begin=".4s" fill="remove" rotate="auto"
                       keyPoints="1;0" keyTimes="0;1" calcMode="linear">
            <mpath xlink:href="#stage1-1"/>
        </animateMotion>
    </circle>
    <circle r="8" fill="red">
        <animateMotion dur=".6s" begin=".4s" fill="remove" rotate="auto"
                       keyPoints="1;0" keyTimes="0;1" calcMode="linear">
            <mpath xlink:href="#stage1-2"/>
        </animateMotion>
    </circle>
</svg>
like image 57
Paul LeBeau Avatar answered Sep 23 '25 06:09

Paul LeBeau