example for an animated bus track

This example shows the use of SVG animation for the progressive drawing of lines, such as bus-tracks or routing for navigation systems. The SVG keySpline feature allows to simulate acceleration and deceleration effects.

Files used

Technical Details

Because svg does not know a builtin-feature for progressive drawing of lines one can fake and use an animated dashing, with dash-line-segments as long as the whole path-segment is and a moving dash-offset from the beginning towards the end of the line. This trick was first shown by Michel Hirtzler for the progressive drawing of the french coastline and border.

The following construct shows the path-definitions and the basic setup for dash-animation. Both the path itself and the animation definitions need unique identifiers because their attribute values will be queried and set later with a javascript function. Therefore some initial attribute values in the animation definition are not correct initially.

The whole path is broken into 5 sections, each intersection is a bus-stop where the animations halt for two seconds (see begin="previous_animation.end+2s"). The keySpline-attribute contains the description of a Bézier-curve with two control points in order to control speed effects. Animation one defines an deceleration effect, Animation 2-4 acceleration and deceleration effects, Animation 5 only acceleration. (see also specification)

To reset the start values for the animation we need to replace the dash-array-attribute and from-attribute with the path-segments length. To get the length-value we can use the method .getTotalLength(). The following javascript-code shows how to do this. The function is called with the onLoad-event.