请马上登录,朋友们都在花潮里等着你哦:)
您需要 登录 才可以下载或查看,没有账号?立即注册
x
animate 动画在 svg 里是基于元素属性变化的动画指令,使用它,必须给出元素的属性名。比如我们画一根线:
<line x1="50" y1="50" x2="250" y2="50" stroke="steelblue" />
画这根线的代码,标红的部分,都是元素 line 的属性名称,其中,x1 和 y1 是线段起始坐标,x2 和 y2 是线段终点坐标,stroke 是描边颜色。“属性”用英文表示,成为 attribute,属性名称就叫 attribute name,svg中写成 attributeName,注意大小写。
animate 当做标签使用,像这样组织代码:
<animate attributeName="x2" ... />
这是要改变 x2 的值,因为 animate 指定了 x2 为要发生动画的属性。我们将现场从 250 变回初始值 50,这用上 from="250" to="50" 的语义化描述,就是“从 …… 到 ……”的意思,小学英语里就有的词汇:
<animate attributeName="x2" from="250" to="50" ... />
动画有运行周期时长,也就是,一个动画全程所需要的时间,这叫 duration,svg 用简称 dur 表示,语句是,dur="2s",需要实践单位,可以是秒 s,也可以是毫秒 ms,看你喜欢。我们把 dur 加入到 animate 代码中:
<animate attributeName="x2" from="250" to="50" du="2s" ... />
还有动画的重复次数。repeat 是 重复,count 是数数,svg 用 repeatCount 表示,注意大小写。语句,repeatCount="100",需要无限循环的话,用 indefinite 代替 100。同样,将其加入的前面的代码:
<animate attributeName="x2" from="250" to="50" du="2s" repeatCount="indefinite" ... />
动画设计完毕,如何让它驱动 line 线条去改变自身长短呢?把 动画标签 作为子元素放置到 line 代码中即可,line标签的自闭合标签改为有收尾符的标签:
<line x1="50" y1="50" x2="250" y2="50" stroke="steelblue" > <animate attributeName="x2" from="250" to="50" du="2s" repeatCount="indefinite" /> </line>
效果如我们预设的一样,线条在不断重复着从长变短的动画。
|