SVG Animate with Transform in HTML5

The SVG animateTransform element animates a transformation attribute on an element. In this article, transformation refers to the attributes of SVG elements that may be transformed, for example, its size, position, rotation and so on.
AnimateTransform is the element that works in combination with the transformation processes. These transformations can be animated, where an initial starting value, an ending value and a duration for the transformation effect are supplied.
To animate a transformation you use the animateTransform element that adds a "type" attribute so that you can specify what type of transformation you are animating.
For example, the two shapes are rotated 45 degrees. This is done using the rotate(45 50 50) "function" call inside the transform attribute. The first number, 45, is the number of degrees to rotate the shape. The second and third numbers are x,y of the point you want to rotate around. In this article, the shapes are both rotated around point 50,50.
Note: You still need to specify the "attributeName" attribute with this element or no animation occurs.
The Types of Transform that can be performed are:
The attributes of an Animating Transformation are:

Difference between Animation and Transformation

Transformation

Transformation allows you to take a form that you have already drawn and modified it in some way. For example, you could use a rectangle and rotate it in one direction so that it sits at an angle. You could take that same rectangle and make it smaller, or larger to suit the needs of the page you are designing. In a pure transformation, there is no movement. The images are static. This approach enables you to change the structure as you need to without redesigning it.

Animation

Animation creates a moving picture. The shape may change upon loading, or when a viewer clicks on the image. Flash is a form of animation that uses a storyboard design to make the move.
SVG is unable to do animation without help. That is the main difference between transformation and animation. To create movement in an SVG image, you want to integrate either SMIL or JavaScript to make it work.
The important points to note are:
Example
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>SVG Animate with transform</title>
  6. </head>
  7. <body>
  8. <h3>SVG Animation with Transformation</h3>
  9. <svg width="200px" height="200px" xmlns="http://www.w3.org/2000/svg">
  10. <rect x="0" y="0" width="300" height="200" fill="red" stroke="black" stroke-width="1" />
  11. <circle cx="100" cy="100" r="2" fill="black"/>
  12. <rect x="0" y="100" width="25" height="50" fill="black" stroke="gray" stroke-width="1" transform="rotation">
  13. <desc> Rectangle will Rotate from 0 to 360 degree
  14. </desc>
  15. <animateTransform
  16. attributeName="transform"
  17. begin="2s"
  18. dur="5s"
  19. type="rotate"
  20. from="0 100 100"
  21. to="360 100 100"
  22. repeatCount="indefinite"
  23. />
  24. <animate attributeName="x" attributeType="XML"
  25. begin="2s" dur="20s" from="0" to="95" fill="freeze"/>
  26. <animate attributeName="y" attributeType="XML"
  27. begin="2s" dur="20s" from="100" to="85" fill="freeze"/>
  28. </rect>
  29. </svg>
  30. </body>
  31. </html>
Output
In the following figure the rectangle will rotate:
transform.jpg
transform1.jpg
transform2.jpg
transform3.jpg