SVG Animation
原文:http://tutorials.jenkov.com/svg/index.html
http://tutorials.jenkov.com/svg/svg-animation.html
SVG Animation
SVG Animation Example
Here is a simple SVG animation example:
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"> <rect x="10" y="10" height="110" width="110"
style="stroke:#ff0000; fill: #0000ff"> <animateTransform
attributeName="transform"
begin="0s"
dur="20s"
type="rotate"
from="0 60 60"
to="360 60 60"
repeatCount="indefinite"
/>
</rect> </svg>
Notice how the <rect>
element has a <animateTransform>
element nested inside it. It is this element that animates the rectangle.
Here is the resulting SVG animation:
Overview of Animation Options
Animation is done by manipulating the attributes of shapes over time. This is done using one or more of the 5 SVG animation elements:
- <set>
- <animate>
- <animateColor>
- <animateTransform>
- <animateMotion>
Each of these SVG animation elements sets or animates different aspects of SVG shapes. These animation elements are explained throughout the rest of this text.
set
The set
element is the simplest of the SVG animation elements. It simply sets an attribute to a certain value after a specific time interval has passed. As such, the shape is not continuously animating, but just changes attribute value once.
Here is a <set>
element example:
<circle cx="30" cy="30" r="25" style="stroke: none; fill: #0000ff;">
<set attributeName="r" attributeType="XML"
to="100"
begin="0s" /> </circle>
Notice the <set>
element nested inside the circle
element. This is how the <set>
element is applied to a shape - by nesting it inside the SVG element of the shape you want to apply it to.
The <set>
element sets the value of an attribute at a certain time. The name of the attribute to set is specified inside the attributeName
attribute. The value to set it to is specified inside the to
attribute. The time to set the attribute value is specified inside the begin
attribute.
The example above sets the attribute r
to 100 after 5 seconds. Here is the resulting image:
attributeType
The previous example also had an attributeType
attribute inside the <set>
element. The value was set to XML
. That is because the attribute to set a value for in the example (the r
attribute) is an attribute of the SVG circle element. Since SVG elements are XML elements, SVG attributes are XML attributes.
You can also animate CSS properties of a shape. If you do so, you will need to set the attributeType
to CSS
. If you do not provide an attributeType
attribute, then the browser will try to guess if the attribute to animate is an XML attribute or CSS property.
animate
The animate
element is used to animate an attribute of an SVG shape. You nest the animate
element inside the shape you want it applied to. Here is an example:
<circle cx="30" cy="30" r="25" style="stroke: none; fill: #0000ff;">
<animate attributeName="cx" attributeType="XML"
from="30" to="470"
begin="0s" dur="5s"
fill="remove" repeatCount="indefinite"/> </circle>
This example animates the cx
attribute of the <circle>
element from a value of 30 (the from
attribute) to a value of 479 (the to
attribute). The animation starts at 0 seconds (the begin
attribute) and has a duration of 5 seconds (the dur
attribute).
When the animation is finished the animated attribute is set back to its original value (the fill="remove"
attribute setting). If you wanted the animated attribute to keep the to-value of the animation, set the fill
attribute to freeze
. The animation repeats indefinitely (the repeatCount
attribute).
Here is the resulting animation:
animateTransform
The <animateTransform
element can animate the transform
attribute of a shape. The <animate>
element cannot do that.
Here is an example:
<rect x="20" y="20" width="100" height="40"
style="stroke: #ff00ff; fill:none;" >
<animateTransform attributeName="transform"
type="rotate"
from="0 100 100" to="360 100 100"
begin="0s" dur="10s"
repeatCount="indefinite"
/>
</rect>
This <animateTransform>
example animates the transform
attribute of the <rect>
element it is nested inside. The type
attribute is set to rotate
(the rotate transform function) meaning the animated transformation will be a rotation. The from
and to
attributes set the parameters to be animated and passed to the rotate
function. This example rotates from a degree of 0 to a degree of 360 around the point 100,100 .
Here is an example that animates the scale of a square:
<rect x="20" y="20" width="40" height="40"
style="stroke: #ff00ff; fill: none;" >
<animateTransform attributeName="transform"
type="scale"
from="1 1" to="2 3"
begin="0s" dur="10s"
repeatCount="indefinite"
/>
</rect>
Notice again that the from
and to
attributes contain the values you would normally pass as parameters to the scale()
transform function.
Here is the resulting animation:
animateMotion
The <animateMotion>
element can animate the movement of a shape along a path. It can also rotate the shape to match the slope of the path, like a car driving up and down hill. Here is an example:
<rect x="0" y="0" width="30" height="15"
style="stroke: #ff0000; fill: none;">
<animateMotion
path="M10,50 q60,50 100,0 q60,-50 100,0"
begin="0s" dur="10s" repeatCount="indefinite"
/>
</rect>
The path to animate the rectangle along is specified in the path
attribute of the <animateMotion>
element. The path
attribute uses the same syntax as the path element.
Here is the resulting animation with the path shown too, so you can better follow the motion.
In order to rotate the square to align with the slope of the path, you set the rotate
attribute of the <animateMotion>
element to auto
. Here is an example:
<rect x="0" y="0" width="30" height="15"
style="stroke: #ff0000; fill: none;">
<animateMotion
path="M10,50 q60,50 100,0 q60,-50 100,0"
begin="0s" dur="10s" repeatCount="indefinite"
rotate="auto"
/>
</rect>
Here is the resulting animation. Notice how the rotation of the square changes to fit the path.
You can also set the rotate
attribute to a specific value, like 20 or 30 etc. That will keep the shape rotated that number of degrees throughout the animation.
Time Units
When you define an SVG animation you specify when the animation start time and duration. When specifying that, you have the choice between different time units. The time units are typically specified inside the begin
, dur
and end
attributes of the various animation elements.
Inside these attributes you can specify a number followed by a time unit, as done in the examples in this text. For instance 5s
means 5 seconds. Here is a list of the time units you can use:
Time Unit | Description |
---|---|
h |
Hours |
min |
Minutes |
s |
Seconds |
ms |
Milliseconds |
You can also specify time in a time format which contain both hours, minutes and seconds. Here is how the format looks like:
hh:mm:ss
Here is an example:
1:30:45
This example specifies a time of 1 hour, 30 minutes and 45 seconds (which is of course very long time for an animation).
Coordinating Animations
You can synchronize the beginning of one animation to the end of another. You do so like this:
<rect x="0" y="0" width="30" height="15"
style="stroke: #ff0000; fill: none;"> <animate id="one"
attributeName="x" attributeType="XML"
from="0" to="400"
begin="0s" dur="10s" fill="freeze"
/>
<animate
attributeName="y" attributeType="XML"
from="0" to="50"
begin="one.end" dur="10s" fill="freeze"
/>
</rect>
Here is the resulting animation:
The first animation has its id
attribute set to one
.
The second animation references the first animation via its begin
attribute. The begin
attribute value is set to one.end
which means that this animation should start when the animation with the id one
ends.
You can use the id.begin
or id.end
to start an animation when another animation starts or ends. Instead of id
use the ID of the animation element to synchronize to.
You can also specify offsets to the start or end time of another animation, like this:
one.begin+10s one.end+5s
Additionally you can specify an explicit end time in an animations end
attribute. This does not replace the dur
attribute. All it does is to add another possible end to an animation, so whichever occurs first. Here is an example:
<animate
attributeName="y" attributeType="XML"
from="0" to="50"
begin="one.begin+3s" dur="10s" end="one.end"
fill="freeze"
/>
This animation will have a duration of 10 seconds, or stop when the animation with the ID one
ends, whichever occurs first.
Repeating Animations
There are two attributes you can use inside an animation element which are used to repeat the animation.
The first attribute is the repeatCount
attribute. Inside this attribute you can set a number, which will be repeat the animation that number of times, or the value indefinite
which will keep the animation running without ever stopping.
The second attribute is the repeatDur
which specifies a duration for which the animation is to be repeated. You can also use the value indefinite
inside the repeatDur
attribute, and the effect is the same as using it inside the repeatCount
attribute.
Here are two examples:
<animate
attributeName="y" attributeType="XML"
from="0" to="50"
begin="one.begin+3s" dur="10s"
repeatCount="3"
fill="freeze"
/>
<animate
attributeName="y" attributeType="XML"
from="0" to="50"
begin="one.begin+3s" dur="10s"
repeatDur="30s"
fill="freeze"
/>
Combining Animations
You can combine animations by listing more than one <animation>
inside the element to animate. You have already seen that, but here is another example:
<rect x="10" y="10" width="40" height="20"
style="stroke: #000000; fill: none;">
<animate attributeName="x"
attributeType="XML"
from="10" to="400"
begin="0s" dur="10s"
repeatCount="indefinite"
/>
<animate attributeName="y"
attributeType="XML"
from="10" to="100"
begin="0s" dur="10s"
fill="freeze"
repeatCount="indefinite"
/>
</rect>
This example has two animations, each animating the x
and y
attributes of the rectangle. Here is the resulting animation:
When combining <animateTransform>
elements, the default behaviour is for the second animation to cancel out the first. However, you can combine the transformation animations by adding the attribute additive
with a value of sum
to both <animateTransform>
elements. Here is an example:
<rect x="10" y="10" width="40" height="20"
style="stroke: #000000; fill: none;">
<animateTransform attributeName="transform" attributeType="XML"
type="scale"
from="1" to="3"
begin="0s" dur="10s"
repeatCount="indefinite"
additive="sum"
/>
<animateTransform attributeName="transform" attributeType="XML"
type="rotate"
from="0 30 20" to="360 30 20"
begin="0s" dur="10s"
fill="freeze"
repeatCount="indefinite"
additive="sum"
/>
Here is the resulting animation which both scales and rotates a rectangle:
SVG Animation的更多相关文章
- GreenSock & SVG Animation
GreenSock & SVG Animation refs https://greensock.com/ https://greensock.com/learning/ GSAP https ...
- SVG animation(text, background)
SVG animation(text, background) demo https://www.happyelements.com/ LICEcap bug Giphy 低帧率 gif https: ...
- TweenMax & GSAP & SVG Animation
TweenMax & GSAP & SVG Animation svg animation https://greensock.com/tweenmax GSAP https://ww ...
- 解决SVG animation 在IE中不起作用
问题描述 CSS animation没办法解决SVG路径运动的问题,下图路径运动的过程,通过查资料发现所有的IE的版本都不支持SVG animation.在IE中没有水流动的效果. 主要代码 < ...
- 超级强大的SVG SMIL animation动画详解
本文花费精力惊人,具有先驱前瞻性,转载规则以及申明见文末,当心予以追究.本文地址:http://www.zhangxinxu.com/wordpress/?p=4333 //zxx: 本文的SVG在有 ...
- [转]超级强大的SVG SMIL animation动画详解
超级强大的SVG SMIL animation动画详解 本文花费精力惊人,具有先驱前瞻性,转载规则以及申明见文末,当心予以追究.本文地址:http://www.zhangxinxu.com/wordp ...
- 推荐8个实现 SVG 动画的 JavaScript 库
SVG 是一种分辨率无关的图形(矢量图形).这意味着它在任何类型的屏幕都不会遭受任何质量损失.除此之外,你可以让 SVG 灵活现一些动画效果.这篇文章就给大家推荐8个实现 SVG 动画的 JavaSc ...
- 带给你灵感:30个超棒的 SVG 动画展示【上篇】
前端开发人员和设计师一般使用 CSS 来创建 HTML 元素动画.然而,由于 HTML 在创建图案,形状,和其他方面的局限性,它们自然的转向了 SVG,它提供了更多更有趣的能力.借助SVG,我们有更多 ...
- 带给你灵感:30个超棒的 SVG 动画展示【下篇】
前端开发人员和设计师一般使用 CSS 来创建 HTML 元素动画.然而,由于 HTML 在创建图案,形状,和其他方面的局限性,它们自然的转向了 SVG,它提供了更多更有趣的能力.借助 SVG,我们有更 ...
随机推荐
- Storm监控文件夹变化 统计文件单词数量
监控指定文件夹,读取文件(新文件动态读取)里的内容,统计单词的数量. FileSpout.java,监控文件夹,读取新文件内容 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- react-ssr
为什么使用SSR 与传统 SPA(Single-Page Application - 单页应用程序)相比 服务器端渲染(SSR)的优势主要在于: 更好的 SEO,由于搜索引擎爬虫抓取工具可以直接查看完 ...
- 通过代码管理工具 git 完成一次完整的代码管理过程
1.从公共远程fork一份自己的本地远程之后,从本地远程 clone 到本地 2.将本地代码跟公共远程代码做关联配置 git remote add upstream https://github.co ...
- 【Java】函数使用
package aaa; public class aaa { public static int add(int a,int b) { return a+b; } public static voi ...
- 回归JavaScript基础(六)
主题:引用类型Date.RegExp的介绍. 上节主要主要介绍了Object.Array引用类型.这节将继续为大家介绍引用类型,并对书中的一些知识点进行总结与归纳,也借此巩固自己对JavaScript ...
- 记录今天客户的SQLSERVER启动不起来( 错误9003)的解决过程2013-11-26
记录今天客户的SQLSERVER启动不起来( 错误9003)的解决过程2013-11-26 今天一大早上班就接到客户的电话,说:SQLSERVER启动不起来,业务系统使用不了 于是我就使用QQ远程,帮 ...
- 解决js跨域
这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被 ...
- ORACLE闪回机制分析与研究应用
1.查看数据库归档和闪回状态,及环境准备SQL> archive log list;SQL> select flashback_on from v$database;关闭数据库,启动归档和 ...
- Lambda表达式学习记录
Lambda表达式可以简化C#编程的某些方面,用法非常灵活.因此也不容易掌握. 下边是我学Lambda表达式的一点记录. 1.Lambda表达式是与委托紧密联系的.只要有委托参数类型的地方,就可以使用 ...
- SAP 前端技术的演化史简介
Jerry之前曾经写过一篇微信公众号文章,题目叫<> 关注我的公号"汪子熙"后,在历史菜单"前端开发相关"里即可找到这篇文章: 该文章简单回顾了SA ...