Puneet Varma (Editor)

SVG animation

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
SVG animation

Animation of Scalable Vector Graphics, an open XML-based standard vector graphics format, is possible through various means:

Contents

  • Scripting: ECMAScript is a primary means of creating animations and interactive user interfaces within SVG.
  • Styling: Since 2008, the development of CSS Animations as a feature in WebKit has made possible stylesheet-driven implicit animation of SVG files from within the Document Object Model (DOM).
  • SMIL: Synchronized Multimedia Integration Language, a recommended means of animating SVG-based hypermedia is currently supported by the Safari, Opera, Mozilla Firefox, Google Chrome and Amaya web browsers, as will any browser that aims to pass the Acid3 web standards test as this requires SMIL support for tests 75 and 76. Libraries have also been written as a shim to give current SVG-enabled browsers SMIL support. This method is also known as SVG+Time.
  • Because SVG supports PNG and JPEG raster images, it can be used to animate such images as an alternative to APNG and Multiple-image Network Graphics.

    History

    SVG animation elements were developed in collaboration with the W3C Synchronized Multimedia Working Group, developers of the Synchronized Multimedia Integration Language. The SYMM Working Group, in collaboration with the SVG Working Group, has authored the SMIL Animation specification, which represents a general-purpose XML animation feature set. SVG incorporates the animation features defined in the SMIL Animation specification and provides some SVG specific extensions.

    Examples

    The following code snippets demonstrate three techniques to create animated SVG on compatible browsers. The relevant parts are in bold green.

    SVG animation using SMIL

    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="-4 -4 8 8"> <title>SVG animation using SMIL</title> <circle cx="0" cy="1" r="2" stroke="red" fill="none"> <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" begin="0s" dur="1s" repeatCount="indefinite"/> </circle> </svg>

    SVG animation using CSS

    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="-4 -4 8 8"> <title>SVG animation using CSS</title> <style type="text/css"> @keyframes rot_kf { attributeName="fill" values="#800;#f00" } .rot { animation: rot_kf 1s linear infinite; } </style> <circle class="rot" cx="0" cy="1" r="2" stroke="blue" fill="none"/> </svg>

    SVG animation using ECMAScript

    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="100%" viewBox="-4 -4 8 8" onload="rotate(evt)"> <title>SVG animation using ECMAScript</title> <script type="text/ecmascript"> function rotate(evt) { var object = evt.target.ownerDocument.getElementById('rot'); setInterval(function () { var now = new Date(); var milliseconds = now.getTime() % 1000; var degrees = milliseconds * 0.36; // 360 degrees in 1000 ms object.setAttribute('transform', 'rotate(' + degrees + ')'); }, 20); } </script> <circle id="rot" cx="0" cy="1" r="2" stroke="green" fill="none"/> </svg>

    SMIL attributes to identify the target attribute

    The following are the animation attribute which identify the target attribute for the given target element whose value changes over time.

    attributeName = "<attributeName>"

    Specifies the name of the target attribute. An XMLNS prefix may be used to indicate the XML namespace for the attribute. The prefix will be interpreted in the scope of the current animation element.

    attributeType = "CSS | XML | auto"

    Specifies the namespace in which the target attribute and its associated values are defined.

  • CSS
  • This specifies that the value of ‘attributeName’ is the name of a CSS property defined as animatable in this specification.

  • XML
  • This specifies that the value of ‘attributeName’ is the name of an XML attribute defined in the default XML namespace for the target element. The attribute must be defined as animatable in this specification.

  • auto
  • The default value is 'auto'. The implementation should match the ‘attribute Name’ to an attribute for the target element. The implementation must first search through the list of CSS properties for a matching property name, and if none is found, search the default XML namespace for the element.

    Note: MediaWiki automatically generates static, non-animated thumbnails of SVG images. Viewing the actual .svg image from each respective description page will show its animation in a compatible browser.

    Libraries

    There are several JavaScript libraries for working with SVG animation. An advantage to the use of such libraries is that these libraries often solve incompatibility issues in browsers through abstraction. Examples of libraries include:

  • GSAP (Greensock Animation Platform)
  • Raphaël
  • Snap.svg
  • svg.js
  • Velocity.js
  • References

    SVG animation Wikipedia


    Similar Topics