XML technologies / SVG / Using animations / Moving elements along paths
(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)
Animate elements by moving them along a visible or invisible path. This example builds on the SVG logo from the previous chapter:
Figure: Target animation.
Add a red dot moving along the blue crescent, as the arrow indicates.
Note:
The example omits the path’s transform attribute so the crescent and text are not rotated, simplifying this first animation.
Creating the moving element
The moving circle can be defined with a <circle> or <ellipse> tag. This example uses the <circle> tag.
Note:
SVG files are small because they contain only instructions from which the browser or plug-in constructs the graphic. More complex instructions and slower computers take longer to render. Use the simplest instructions sufficient for the result. As a rule of thumb, more attributes generally mean a more complex formula for the browser to calculate. Prefer elements with fewer attributes when possible. For a circle, therefore, use <circle> rather than <ellipse>.
<?xml version="1.0" standalone="no"?>
...
<svg width="500" height="500">
<text x="100" y="130" style="font-family:'Arial Black';font-size:46;stroke:none;fill:rgb(0,0,255)">SVG</text>
<path id="sichel" d="M130 80 Q333 96 215 135 Q315 96 130 80" style="stroke:rgb(0,0,0);stroke-width:1;fill:rgb(0,0,139)"/>
<text style="alignment-baseline:text-bottom;baseline-shift:5;font-family:Arial;font-size:11;fill:darkblue" >
<textPath xlink:href="#sichel" startOffset="5">Scalable Vector Graphics</textPath>
</text>
<circle cx="130" cy="80" r="3" style="fill:red;stroke:none"></circle>
</svg>
The code produces this result:
Figure: Red dot created for animation.
Adding the animation
Now add the animation moving the dot. Insert the element <animateMotion> inside the <circle> tag.
<circle cx="130" cy="80" r="3" style="fill:red;stroke:none">
<animateMotion path="M0 0 Q203 16 85 55" />
</circle>
Define the motion path in the tag’s path attribute. The same rules apply as for the d attribute of the <path> tag. Use the first half of the d attribute defining the crescent, which describes its outer line. Positions are relative to the moving object’s initial position, so subtract the circle’s x and y positions from the coordinates, as follows:
Crescent coordinates:
d="M130 80 Q333 96 215 135"Circle center:
x="130" y="80"Result:
path="M(130-130) (80-80) Q(333-130) (96-80) (215-130) (135-80)" = path="M0 0 Q203 16 85 55"
Setting the speed
Saving and running the graphic shows no animation yet because its duration is unspecified. It defaults to 0 seconds, making the animation invisible.
Speed is set indirectly through duration, defined by the attribute dur. With dur="6s" the animation lasts six seconds. Speed depends on the distance covered: a longer path requires faster movement within the same time. This value suits the example. Add the attribute as follows:
<animateMotion dur="6s" path="M0 0 Q203 16 85 55" />
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de