Although Chrome 45 deprecated SMIL in favor of CSS animations and Web animations, the Chrome developers have since suspended that deprecation.
Firefox 4 introduced support for animating SVG using Synchronized Multimedia Integration Language (SMIL). SMIL allows you to:
This is done adding an SVG element like
inside the SVG element to animate. Below are examples for the four different ways.As of Chrome 45, SMIL animations are deprecated in favor of CSS animations and Web animations.
The following example animates the cx attribute of a circle. To do so, we add an
If you want to animate more attributes inside the same element, just add more <animate>
elements.
<!DOCTYPE html> <html> <head> <title>Attribute Animation with SMIL</title> </head> <body> <svg width="300px" height="100px"> <rect x="0" y="0" width="300" height="100" stroke="black" stroke-width="1" /> <circle cx="0" cy="50" r="15" fill="blue" stroke="black" stroke-width="1"> <animate attributeName="cx" from="0" to="100" dur="5s" repeatCount="indefinite" /> </circle> </svg> </body> </html>
<!DOCTYPE html> <html> <head> <title>SVG SMIL Animate with transform</title> </head> <body> <svg width="300px" height="100px"> <rect x="0" y="0" width="300" height="100" stroke="black" stroke-width="1" /> <rect x="0" y="50" width="15" height="34" fill="blue" stroke="black" stroke-width="1"> <animateTransform attributeName="transform" begin="0s" dur="20s" type="rotate" from="0 60 60" to="360 100 60" repeatCount="indefinite" /> </rect> </svg> </body> </html>
The path is defined the same way as in
In this example, a blue circle bounces between the left and right edges of a black box, over and over again, indefinitely. The animation here is handled by the <animateMotion>
element. In this case, we're establishing a path consisting of a MoveTo command to establish the starting point for the animation, then the Horizontal-line command to move the circle 300 pixels to the right, followed by the Z command, which closes the path, establishing a loop back to the beginning. By setting the value of the repeatCount attribute to indefinite
, we indicate that the animation should loop forever, as long as the SVG image exists.
<!DOCTYPE html> <html> <head> <title>SVG SMIL Animate with Path</title> </head> <body> <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="100px"> <rect x="0" y="0" width="300" height="100" stroke="black" stroke-width="1" /> <circle cx="0" cy="50" r="15" fill="blue" stroke="black" stroke-width="1"> <animateMotion path="M 0 0 H 300 Z" dur="3s" repeatCount="indefinite" /> </circle> </svg> </body> </html>
Same example as before with a curved path and following the direction of the path.
<!DOCTYPE html> <html> <head> <title>SVG SMIL Animate with Path</title> </head> <body> <svg width="300px" height="100px"> <rect x="0" y="0" width="300" height="100" stroke="black" stroke-width="1" /> <rect x="0" y="0" width="20" height="20" fill="blue" stroke="black" stroke-width="1"> <animateMotion path="M 250,80 H 50 Q 30,80 30,50 Q 30,20 50,20 H 250 Q 280,20,280,50 Q 280,80,250,80Z" dur="3s" repeatCount="indefinite" rotate="auto"> </rect> </svg> </body> </html>
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/SVG/SVG_animation_with_SMIL