Hello World
Hello World

Reputation: 1129

svg animation path direction

I'm using svg animation tags to animate a group tag along the coordinates of a path tag. Unfortunately, the animation is running in the opposite direction to what I require (I.E going from b to a instead of a to b). Does there exist any attribute which would amend this, something like:

   <animateMotion 
               xlink:href="#group1"
               dur="6s"
               fill="freeze"
               rotate="auto-reverse"
               direction="reverse"
    >
    <mpath xlink:href="#path1" />
    </animateMotion>

I know this could be achieved with snap or raphael but as far as I know it's not possible to use anything to the effect of 'rotate="auto-reverse"' in those libraries.

Alternatively, is there a way to reverse the way my path is calculated; can this be achieved with any software?

Upvotes: 1

Views: 907

Answers (1)

Kaiido
Kaiido

Reputation: 137006

You could use the keyPoints attribute, along with keyTimes.

<?xml version="1.0"?>
<svg width="120" height="120"  viewBox="0 0 120 120"
     xmlns="http://www.w3.org/2000/svg" version="1.1"
     xmlns:xlink="http://www.w3.org/1999/xlink" >

    <path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110"
          stroke="lightgrey" stroke-width="2" 
          fill="none" id="theMotionPath"/>
   <!-- Here is a green circle which will be moved along the motion path. -->
   <circle cx="" cy="" r="5" fill="green">
        <!-- Define the motion path animation -->
        <animateMotion dur="6s" repeatCount="indefinite" keyPoints="0;1" calcMode="linear"
                   keyTimes="0;1">
           <mpath xlink:href="#theMotionPath"/>
        </animateMotion>
    </circle>
    <!-- Here is a red one, using the same motionPath but reversed thanks to keyPoints -->
    <circle cx="" cy="" r="5" fill="red">
        <animateMotion dur="6s" repeatCount="indefinite" keyPoints="1;0" calcMode="linear"
                   keyTimes="0;1">
           <mpath xlink:href="#theMotionPath"/>
        </animateMotion>
    </circle>

</svg>

Upvotes: 3

Related Questions