{"id":186041,"date":"2014-10-13T07:38:45","date_gmt":"2014-10-13T14:38:45","guid":{"rendered":"http:\/\/css-tricks.com\/?p=186041"},"modified":"2022-08-23T08:40:56","modified_gmt":"2022-08-23T15:40:56","slug":"guide-svg-animations-smil","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/guide-svg-animations-smil\/","title":{"rendered":"A Guide to SVG Animations (SMIL)"},"content":{"rendered":"\n
Update December 2015:<\/strong> At the time of this update, SMIL seems to be kinda dying. Sarah Drasner has a guide<\/a> on how you can replace some of it’s features.<\/p>\n\n\n\n The following is a guest post by Sara Soueidan<\/a>. Sara has a knack for digging deep into web features and explaining the heck out of them for the rest of us. Here she digs into SMIL (and friends), and animation syntax built right into SVG, and gives us this epic guide.<\/em><\/p>\n\n\n\n\n\n\n\n SVG graphics can be animated using animation elements<\/strong>. The animation elements were initially defined in the SMIL<\/a> Animation specification; these elements include:<\/p>\n\n\n\n In addition to the animation elements defined in the SMIL spec, SVG includes extensions compatible with the SMIL animations spec; these extensions include attributes that extend the functionality of the element and additional animation elements. The SVG extensions include:<\/p>\n\n\n\n SVG animations can be similar to CSS animations and transitions via by their nature. Keyframes are created, things move, colors change, etc. However, they can do somethings that CSS animations can’t do, which we’ll cover.<\/p>\n\n\n SVGs can be styled and animated with CSS (slides)<\/a>. Basically, any transformation or transition animation that can be applied to an HTML element can also be applied to an SVG element. But there are some SVG properties that cannot be animated through CSS that can through SVG. An SVG path, for example, comes with a set of data<\/strong> (a So, many animations and effects can simply not be achieved using CSS at this time. The CSS SVG animation gaps can be filled by using either JavaScript or the declarative SVG animations derived from SMIL.<\/p>\n\n\n\n If you prefer using JavaScript, I recommend using snap.svg<\/a> by Dmitry Baranovsky, which is described as being “the jQuery of SVG”. Here’s a collection of examples<\/a> of that.<\/p>\n\n\n\n Or if you prefer a more declarative animation approach, you can use the SVG animation elements as we’ll cover in this guide!<\/p>\n\n\n\n Another advantage to SMIL over JS animations is that JS animations don’t work when the SVG is embedded as an Browser support for SMIL animations is pretty decent. They work in all browsers except in Internet Explorer and Opera Mini. For a thorough overview of browser support, you can refer to the compatibility table on Can I Use<\/a>.<\/p>\n\n\n\n If you need to provide a fallback for SMIL animations, you can test for browser support on-the-fly using Modernizr<\/a>. If SMIL is not supported, you can then provide some kind of fallback (JavaScript animations, an alternate experience, etc).<\/p>\n\n\n No matter which of the four animation elements you choose, you need to specify the target of the animation defined by that element.<\/p>\n\n\n\n In order to specify a target, you can use the If you’ve come across SVG animation elements before, you’ve probably seen them nested inside the element that they’re supposed to animate. This is possible as well as per the spec:<\/p>\n\n\n\n If the So if you want to “encapsulate” the animation into the element it applies to, you can do that. And if you want to keep the animations separate somewhere else in the document, you can do that too, and specify the target of each animation using All animation elements also share another attribute: For example, if you want to animate the position of the center of a on the x-axis, you do that by specifying When you specify the attribute name, you can add an XMLNS (short for XML namespace) prefix to indicate the namespace of the attribute. The namespace can also be specified using the If the value for For example, the following snippet animates the We’ll go over the other animation attributes in the upcoming examples below. Except where otherwise noted, all of the animation attributes are common to all of the animation elements.<\/p>\n\n\n Let’s start by moving a circle from one position to another. We’re going to do that by changing the value of its \n
Table of contents<\/h3>\n <\/summary>\n \n
\n
Overview<\/h3>\n\n\n
<animate><\/animate><\/code>– which allows you to animate scalar attributes and properties over a period of time.<\/li>
<set><\/set><\/code> – which is a convenient shorthand for animate, which is useful for assigning animation values to non-numeric attributes and properties, such as the visibility property.<\/li>
<animatemotion><\/animatemotion><\/code> – which moves an element along a motion path.<\/li>
<animatecolor><\/animatecolor><\/code> – which modifies the color value of particular attributes or properties over time. Note that the element has been deprecated in favor of simply using the animate element to target properties that can take color values. Even though it’s still present in the SVG 1.1 specification, it is clearly noted that it has been deprecated; and it has been completely removed from the SVG 2 specification.<\/li><\/ul>\n\n\n\n
<animatetransform><\/animatetransform><\/code> – allows you to animate one of SVG’s transformation attributes over time, such as the
transform<\/code> attribute.<\/li>
path<\/code> (attribute)<\/em> – allows any feature from SVG’s path data syntax to be specified in a path attribute to the
animateMotion<\/code> element (SMIL Animation only allows a subset of SVG’s path data syntax within a path attribute). We’ll talk more about
animateMotion<\/code> in an upcoming section.<\/li>
<mpath><\/mpath><\/code> – used in conjunction with the
animateMotion<\/code> element to reference a motion path that is to be used as a path for the motion. The mpath element is included inside the
animateMotion<\/code> element, before the closing tag.<\/li>
keypoints<\/code> (attribute)<\/em> – used as an attribute for
animateMotion<\/code> to provide precise control of the velocity of motion path animations.<\/li>
rotate<\/code> (attribute)<\/em> – used as an attribute for
animateMotion<\/code> to control whether an object is automatically rotated so that its x-axis points in the same direction (or opposite direction) as the directional tangent vector of the motion path. This attribute is the key to making motion along a path work as you’d expect. More about this in the
animateMotion<\/code> section.<\/li><\/ul>\n\n\n\n
Why use SVG animations?<\/h3>\n\n\n
d=\"\"<\/code> attribute) that defines that path’s shape. This data can be modified and animated through SMIL, but not CSS. This is because SVG elements are described by a set of attributes known as SVG presentation attributes<\/em>. Some of these attributes can be set, modified, and animated using CSS, and others can’t.<\/p>\n\n\n\n
img<\/code> or used as a
background-image<\/code> in CSS. SMIL animations do work in both cases (or should, browser support pending). That’s a big advantage, in my opinion. You may find yourself choosing SMIL over other options because of that. This article is a guide to help you get started using SMIL today.<\/p>\n\n\n
Browser support and fallbacks<\/h3>\n\n\n
Specifying the target of the animation with
xlink:href<\/code><\/h3>\n\n\n
xlink:href<\/code> attribute. The attribute takes a URI reference to the element which is the target of this animation and which therefore will be modified over time. The target element must be part of the current SVG document fragment.<\/strong><\/p>\n\n\n\n
<rect id=\"cool_shape\" ...=\"\">\n <animate xlink:href=\"#cool_shape\" ...=\"\"><\/animate>\n<\/rect><\/code><\/pre>\n\n\n\n
xlink:href<\/code> attribute is not provided, then the target element will be the immediate parent element of the current animation element.<\/p><\/blockquote>\n\n\n\n
<rect id=\"cool_shape\" ...=\"\">\n <animate ...=\"\"><\/animate>\n<\/rect><\/code><\/pre>\n\n\n\n
xlink:href<\/code>. Both ways work just fine.<\/p>\n\n\n
Specifying the target property of the animation with
attributeName<\/code> and
attributeType<\/code><\/h3>\n\n\n
attributeName<\/code>. The
attributeName<\/code> attribute is used to specify the name of the attribute that you’re animating.<\/p>\n\n\n\n
cx<\/code> as the value for the
attributeName<\/code> attribute.<\/p>\n\n\n\n
attributeName<\/code> takes only one value, not a list of values, so, you can only animate one attribute at a time. If you want to animate more than one attribute, you need to define more than one animation for the element. This is something that I wish were different, and that I think CSS has an advantage over SMIL for. But then again, because of the values possible for other animation attributes (which we’ll cover next), it only makes sense to define only one attribute name at a time, otherwise the other attribute values can become too complex to work with.<\/p>\n\n\n\n
attributeType<\/code> attribute. For example, some attributes are part of the CSS namespace (which means that the attribute can be found as a CSS property as well) and others are XML-only. A table showing these attributes can be found here<\/a>. The attributes in the table are not all of the SVG attributes. They are only the ones that can be set using CSS. Some of them are already available as CSS properties.<\/p>\n\n\n\n
attributeType<\/code> is not explicitly set or is set to
auto<\/code>, the browser 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.<\/p>\n\n\n\n
opacity<\/code> of an SVG rectangle. Since the
opacity<\/code> attribute is also available as a CSS property, the
attributeType<\/code> is set to the CSS namespace:<\/p>\n\n\n\n
<rect>\n <animate\n attributetype=\"CSS\"\n attributename=\"opacity\" \n from=\"1\" \n to=\"0\" \n dur=\"5s\" \n repeatcount=\"indefinite\">\n <\/animate>\n<\/rect><\/code><\/pre>\n\n\n\n
Animating an element’s attribute from one value to another over a duration of time, and specifying the end state:
from<\/code>,
by<\/code>,
to<\/code>,
dur<\/code> and
fill<\/code><\/h3>\n\n\n
cx<\/code> attribute (which specifies the x-position of its center).<\/p>\n\n\n\n