XML technologies / SVG / Using animations / Ways to implement animations / Rotating, skewing and scaling with animateTransform

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

The tag <animateTransform> dynamically changes all settings of the transform attribute to rotate, skew and scale elements. It adds only one attribute, but has some special requirements.

Attributes and special requirements

Besides the universal attributes there is the attribute type, specifying which transform instruction to change. Possible values are translate, scale, rotate, skewX and skewY.

Values of the attributes from, by and to must have the same type as values of the transform instruction. These rules apply:

  • For type="translate" use x/y coordinate pairs; y is optional.

  • For type="scale" use percentages scaling width or height; scaling for the y axis is optional.

  • For type="rotate" use one value specifying the rotation angle in degrees.

  • For type="skewX" and type="skewY" use individual values specifying the skew angle on the x or y axis.

The same values belong in the values list, separated individually or in pairs by semicolons.

Unusual SVG Viewer behavior

Adobe SVG Viewer 2.0 has nonstandard behavior particularly with this tag. Account for this when writing code for it.

Besides the attribute type you must specify the attribute attributeName="transform" for the animation to run. This should be unnecessary because the tag name animateTransform already says that the attribute transform is being animated. Omitting the attribute shows only the final state, without the transition. Even W3C documentation examples fail because they lack this attribute, confirming the deviation from the standard.

Multiple animations on one element may also fail to run together. Applying type="scale" and type="rotate" to a circle, ellipse or text executes only scaling; rotation happens all at once after scaling finishes.

If an element’s transform attribute contains both translate and another instruction such as scale, animating only scale causes the translate instruction to be ignored during animation, shifting the element to its parent coordinate origin. Solve this by applying the animated instruction to the element and the positioning translate instruction to a parent group.

<g transform="translate(100 100)" id="gruppe1">
  <text transform="rotate(0)" id="text1" x="0" y="0" style="font-family:'Arial Black';font-size:40px;fill:blue;stroke:darkblue">SVG</text>
</g>

This also moves and rotates the element simultaneously: animate the group’s translate instruction and separately animate rotation, as the listing shows.

Note:
The first text element and the group gruppe1 show simultaneous animations using groups, while the other two elements demonstrate the problems above.

<?xml version="1.0" standalone="no"?>
...
<svg width="500" height="500">
  <defs>
    <animateTransform type="rotate" xlink:href="#text1" attributeName="transform" dur="6s" from="0" to="360" by="1" calcMode="linear" repeatCount="1000"/>
    <animateTransform type="translate" xlink:href="#gruppe1" attributeName="transform" dur="6s" from="100,100" to="300,300" by="1,1" calcMode="linear" repeatCount="1000"/>
  </defs>
  <g transform="translate(100 100)" id="gruppe1">
    <text transform="rotate(0)" id="text1" x="0" y="0" style="font-family:'Arial Black';font-size:40px;fill:blue;stroke:darkblue">SVG</text>
  </g>
  <g transform="translate(350,150)">
    <ellipse transform="rotate(10)" cx="0" cy="0" rx="20" ry="10" style="fill:blue">
      <animateTransform type="rotate" begin="0" from="1" to="30" by="1" dur="10s" attributeName="transform"/>
      <animateTransform type="scale" from="0.1" to="1" dur="10s" attributeName="transform"/>
    </ellipse>
  </g>
  <g transform="translate(100,100)">
    <text transform="rotate(1) scale(0.01)" id="text2" x="0" y="0"  style="font-family:'Arial Black';font-size:40px;fill:blue;stroke:darkblue">SVG
      <animateTransform type="scale" attributeName="transform" dur="6s" from="0.01" to="1" by="0.01" fill="freeze"/>
      <animateTransform type="rotate" attributeName="transform" dur="6s" from="1" to="360" by="1" fill="freeze"/>
    </text>
  </g>
</svg>


   

 

 

 

back to top