Skip to content
Surf Wiki
Save to docs
general/animation-techniques

From Surf Wiki (app.surf) — the open knowledge base

SVG animation

Open XML-based standard vector graphics format

SVG animation

Open XML-based standard vector graphics format

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

  • 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{{cite book |author-link=Dick Bulterman |access-date=2022-04-24 |archive-date=2022-04-24 |archive-url=https://web.archive.org/web/20220424215906/https://openlibrary.org/works/OL15498417W/SMIL_3.0 |url-status=live 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 (MNG).

History

SVG animation elements were developed in collaboration with the working group that published several versions of Synchronized Multimedia Integration Language (SMIL). The SYMM Working Group (in collaboration with the SVG Working Group) developed 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.

In June 1998, the "Synchronized Multimedia Working Group" (known as "SYMM") within the World Wide Web Consortium ("W3C") published the first recommended version of the specification known as "SMIL 1.0". Shortly after the 1998 publication of SMIL 1.0, a group of companies led by Microsoft published "Timed Interactive Multimedia Extensions for HTML (HTML+TIME); Extending SMIL into the Web Browser". For the next two years, the lead author of HTML+TIME (Patrick Schmitz) worked with the SYMM working group, while also working with the SVG working group. Shortly after the publication of SMIL 2.0, Schmitz and co-author Aaron Cohen (from Intel) published SMIL Animation on 4 September 2001. SVG 1.0 also became a W3C Recommendation on 4 September 2001.

Certain web browsers added support for SVG animation during the 2000s, including Amaya as early as 2003, but SVG animation was only supported by widely used browsers beginning in the 2010s, notably by Firefox 4 (2011). Internet Explorer supports ECMAScript animation, and its successor Microsoft Edge supports ECMAScript and CSS animations as of version 42.17134.

Examples

The following code snippets demonstrate three techniques to create animated SVG images on compatible browsers. The relevant parts are highlighted in yellow. Click the images' thumbnails to see their animated versions.

SVG animation using SMIL

: See file SVG animation using SMIL.svg.

| #width = 100% | #height = 100% | #viewBox = -4 -4 8 8 | #xmlns:xlink = http://www.w3.org/1999/xlink

SVG animation using SMIL attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" dur="1s" repeatCount="indefinite"/

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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"
   dur="1s"
   repeatCount="indefinite"/>
 </circle>
</svg>

SVG animation using CSS

: See file SVG animation using CSS.svg.

| #width = 100% | #height = 100% | #viewBox = -4 -4 8 8 | #xmlns:xlink = http://www.w3.org/1999/xlink

SVG animation using CSS @keyframes rot_kf { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } .rot { animation: rot_kf 1s linear infinite; }

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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>
  @keyframes rot_kf {
    from { transform: rotate(0deg); }
    to   { transform: rotate(360deg); }
  }
  .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

A drawing of a streetlight that can be interactive
A traffic light, animated using SVG animation and Javascript.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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>
  function rotate(evt) {
   const object = evt.target.ownerDocument.getElementById('rot');
   setInterval(() => {
     const now          = new Date();
     const milliseconds = now.getTime() % 1000;
     const 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>
Though the example above works, it is not the optimal implementation; the animation is limited to 50 frames per second (FPS). Using <code>requestAnimationFrame</code> provides better performance and can reach 60 FPS:<syntaxhighlight lang="xml" line="1" highlight="3,5-17">

SVG animation using requestAnimationFrame() let object;

function init() { object = document.getElementById('rot'); window.requestAnimationFrame(rotate); }

function rotate(timestamp) { const milliseconds = timestamp % 1000; const degrees = milliseconds * 0.36; // 360 degrees in 1000 ms object.setAttribute('transform', rotate(${degrees})); window.requestAnimationFrame(rotate); }

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 = "" 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 specifies that the value of ‘attributeName’ is the name of a CSS property defined as animatable in this specification. XML 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. circulatory_system_SMIL.svg|link=|SMIL animation demonstrating change in transformation (scale) and CSS attributes (opacity and dash offset) toy_train_SMIL.svg|link=|SMIL animation demonstrating motion along a path and simulation of 3D morphing_SMIL.svg|link=|SMIL animation demonstrating morphing of shapes (paths) Penang_Island_textured_model_CSS3_animation.svg|link=|CSS3 animation demonstrating changes in transformation (rotation, scale and translation) and simulation of 3D

The MediaWiki wiki software 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 Raphaël and Velocity.js

References

de:Scalable Vector Graphics#Animation

References

  1. (January 2003 – April 2009). "Scalable Vector Graphics (SVG) 1.1 Specification". [[World Wide Web Consortium]].
  2. Festa, Paul. (9 January 2003). "W3C releases scripting standard, caveat". [[CNET Networks.
  3. Dick Bulterman. "SMIL 3.0".
  4. (15 April 2003). "SVG Animation support in Amaya". [[World Wide Web Consortium]].
  5. McCathieNevile, Charles. (31 October 2006). "Animating Your SVG". [[Opera Software]].
  6. (29 March 2011). "SVG animation with SMIL".
  7. "When can I use SVG SMIL animation?".
  8. Dahlström, Erik. (August 2008). "Tricks of javascript, SVG and SMIL". [[Opera Software]] at "SVG Open" website.
  9. "W3C Synchronized Multimedia Home page".
  10. (1998-06-15). "Synchronized Multimedia Integration Language (SMIL) 1.0 Specification". SYMM Working Group.
  11. (1998-06-15). "Press Release: W3C Issues SMIL as a W3C Recommendation".
  12. (1998-09-18). "Timed Interactive Multimedia Extensions for HTML (HTML+TIME); Extending SMIL into the Web Browser.".
  13. (7 August 2001). "Synchronized Multimedia Integration Language (SMIL 2.0)".
  14. (4 September 2001). "SMIL Animation".
Info: Wikipedia Source

This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page.

Want to explore this topic further?

Ask Mako anything about SVG animation — get instant answers, deeper analysis, and related topics.

Research with Mako

Free with your Surf account

Content sourced from Wikipedia, available under CC BY-SA 4.0.

This content may have been generated or modified by AI. CloudSurf Software LLC is not responsible for the accuracy, completeness, or reliability of AI-generated content. Always verify important information from primary sources.

Report