你可以学会的svg动画(SMIL)

简介: 你可以学会的svg动画(SMIL)

SMIL介绍


SMIL 全称 Synchronized Multimedia Integration Language,它允许我们通过 HTML 标签实现动画效果,它可以用于:


  • 实现过渡动画


  • 实现补间动画


  • 动画颜色变换


  • 路径运动动画(CSS3无法实现) SMIL 包含以下标签:


<set>
<animate>
<animateColor>// 这个元素,现在已经废弃。
<animateTransform>
<animateMotion>


为什么要用 SMIL?


实现动画的全新方式:


  • 无需 CSS


  • 无需 JS


  • 几个标签轻松实现复杂动画,它不香吗?


SMIL元素一些共同属性的介绍


  • attributeName:表示需要实现动画的属性。


  • attributeType:  根据attributeName指定的值来判断是svg元素属性的变化,还是css属性的变化。


  • CSS, 表示css属性值的变化。


  • XML,表示svg属性值的变化。


  • begin: 表示动画延迟多少秒。


  • from: 表示动画开始的值。


  • to:表示动画结束的值。


  • fill: 表示动画结束时所处的状态。


  • freeze: 表示结束后,让状态处于结束时的动画状态。


  • remove: 表示结束后,让状态处于开始时的动画状态。


  • values: 表示实现多个状态动画。不能使用from, to属性。每个状态值通过;分隔。


  • dur: 表示动画持续多少秒。


  • type: 在animateTransform元素中变换的类型。


  • repeatCount: 表示重复多少次动画。

  • path: 表示animateMotion元素动画时运行的轨迹。


  • rotate: 表示animateMotion元素动画时到达有棱角的位置时,怎样运行。一般设置为0。


  • id: 指定每个动画元素的唯一标识,可以结合begin来实现复杂动画。


set 标签


实现属性的延迟设置


<svg width="400" height="400">
  <rect x="0" y="0" width="100" height="100">
    <set attributeName="x" attributeType="XML" to="10" begin="1s" />
    <set attributeName="x" attributeType="XML" to="20" begin="2s" />
    <set attributeName="x" attributeType="XML" to="30" begin="3s" />
    <set attributeName="x" attributeType="XML" to="40" begin="4s" />
    <set attributeName="x" attributeType="XML" to="50" begin="5s" />
  </rect>
</svg>


animate 标签


动画元素放在形状元素的内部,用来定义一个元素的某个属性如何踩着时点改变。在指定持续时间里,属性从开始值变成结束值。


<svg width="200" height="200" viewPort="0 0 200 200">
    <rect x="10" y="10" width="100" height="100">
      <animate attributeType="CSS" attributeName="width" from="100" to="150" dur="3s" fill="freeze" />
    </rect>
  </svg>
  <svg width="200" height="200" viewPort="0 0 200 200">
    <rect x="10" y="10" width="100" height="100">
      <animate attributeType="XML" attributeName="width" from="100" to="150" dur="3s" fill="freeze" />
    </rect>
  </svg>


以上两种实现相同效果。一个是通过CSS,一个是通过svg。


形状补间动画。不规则图形的变化。


<svg width="400" height="400">
  <polygon points="30 30 70 30 90 70 10 70" fill="#fcc" stroke="black">
    <animate attributeName="points" attributeType="XML" to="50 30 70 50 50 90 30 50" dur="5s" fill="freeze" repeatCount="1" />
  </polygon>
</svg>


animateTransform 标签


animateTransform元素变动了目标元素上的一个变形属性,从而允许动画控制转换、缩放、旋转或斜切。注意:如果遇到transform属性值的变化,需要用到该元素。而不要去用animate元素做变换。


<svg width="200" height="200" viewBox="0 0 200 200">
    <rect x="0" y="0" width="60" height="60" fill="red">
      <animateTransform attributeName="transform" begin="0s" dur="3s" type="scale" from="1" to="2"
        repeatCount="indefinite" />
    </rect>
  </svg>


animateMotion 标签


这是一个非常nb的标签。可以让元素通过指定的轨迹运动。


实现一个轨迹动画


<svg width="300px" height="100px">
    <rect x="0" y="0" width="300" height="100" stroke="black" stroke-width="1" fill="none" />
    <rect x="0" y="0" width="20" height="20" fill="blue" stroke="black" stroke-width="1">
      <animateMotion path="M 250,80 H 50 Q 30,80 30,50 Q 30,20 50,20 H 250 Q 280,20,280,50 Q 280,80,250,80Z" dur="3s"
        repeatCount="indefinite" rotate="auto">
    </rect>
  </svg>


网络异常,图片无法展示
|


实现一个复杂混合动画


<svg viewBox="0 0 200 200" width="200" height="200">
    <rect x="0" y="0" rx="0" ry="0" width="10" height="10" fill="red">
      <animateMotion
        id="forward-rect"
        path="M 10 10 L 110 10 L 110 110 L 10 110"
        dur="2s"
        rotate="0"
        fill="freeze"
        begin="0; backward-rect.end + 0.5s"
      />
      <animateMotion
        id="backward-rect"
        path="M 10 110 L 110 110 L 110 10 L 10 10"
        dur="2s"
        rotate="0"
        fill="freeze"
        begin="forward-rect.end + 0.5s"
      />
      <animate
        id="red-to-blue"
        attributeType="XML"
        attributeName="fill"
        begin="0; blue-to-red.end + 1s"
        from="red"
        to="blue"
        dur="2s"
        fill="freeze"
      />
      <animate
        id="blue-to-red"
        attributeType="XML"
        attributeName="fill"
        begin="red-to-blue.end + 1s"
        from="blue"
        to="red"
        dur="2s"
        fill="freeze"
      />
    </rect>
    <path d="M 10 10 L 110 10 L 110 110 L 10 110" fill="none" stroke-width="1" stroke="blue"/>
</svg>


实现一个好看的loading动画


<svg viewBox="0 0 50 50" width="200" height="200">
    <circle cx="25" cy="25" r="22" fill="none" stroke-width="3" stroke-dasharray="34" stroke="#3be6cb"
      stroke-linecap="round">
      <animateTransform attributeName="transform" type="rotate" values="0 25 25; 360 25 25" dur="1.5s"
        repeatCount="indefinite" />
      <animate attributeName="stroke" values="#3be6cb;#02bcfe;#3be6cb" dur="3s" repeatCount="indefinite" />
    </circle>
    <circle cx="25" cy="25" r="12" fill="none" stroke-width="3" stroke-dasharray="19" stroke="#02bcfe"
      stroke-linecap="round">
      <animateTransform attributeName="transform" type="rotate" values="360 25 25;0 25 25" dur="1.5s"
        repeatCount="indefinite" />
      <animate attributeName="stroke" values="#02bcfe;#3be6cb;#02bcfe" dur="3s" repeatCount="indefinite" />
    </circle>
  </svg>


网络异常,图片无法展示
|



相关文章
|
3月前
|
Web App开发 移动开发 前端开发
SVG
这段代码将引用刚才定义的圆形,并将其放置在(20,20)的位置,大小为60x60。 除了<svg>和<use>元素外,SVG还提供了许多其他的元素和属性,可以帮助你创建各种复杂的图形。比如<rect>、<line>、<polygon>等元素,可以用来创建矩形、线条和多边形等。
58 0
|
3月前
|
前端开发 JavaScript API
【HTML】SVG实现炫酷的描边动画
今天闲来无事,看到Antfu大佬的个性签名,觉得还是非常炫酷的,于是也想要搞一个自己的个性签名用来装饰自己的门面,不过由于手写的签名太丑了,遂放弃。于是尝试理解原理,深入研究此等密法,终于小有所成,发现原来是描边动画,于是记载如下,方便日后借鉴。
69 3
|
11月前
用svg实现一个环形进度条
用svg实现一个环形进度条
76 0
SVG SMIL 轨迹 Path 动画(animateMotion)
SVG SMIL 轨迹 Path 动画(animateMotion)
86 0
SVG SMIL 动画(基本动画 、变换动画)
SVG SMIL 动画(基本动画 、变换动画)
38 0
|
前端开发
你可以学会的svg动画
你可以学会的svg动画
|
前端开发
CSS 轻松制作 SVG 动画
如果从未在前端使用过 SVG,那么就错过了很多改善性体验,如果不知道如何使用 SVG 制作动画,那么将错过更多。本文开始制作第一个 SVG 图形,为其添加动画。
657 0
CSS 轻松制作 SVG 动画
|
XML 编解码 前端开发
|
前端开发 JavaScript
利用 SVG 和 CSS3 实现有趣的边框动画
今天我们来探索一下Carl Philipe Brenner的网站上一个微妙而有趣的动画效果。当鼠标经过网格元素时,会有一个微妙的动画发生——网格元素变得透明,每条边有个顺时针的动画,创造了非常好的效果。这种效果是通过JS给span标签的宽或者高做了动画。我们待会会用SVG和CSS渐变来完成。注意,这个技术还是实验性的。
279 0
利用 SVG 和 CSS3 实现有趣的边框动画
|
图形学
Unity里实现Sprite Renderer的阴影
将以下脚本附到产生Shadow的物体上: voidOnEnable(){ GetComponent().receiveShadows =true; GetComponent().castShadows =true; } 但是这是不够的,还需要Shader帮忙,下面的Shader请放到产生Shado...
3128 0