HTML5的 2D SVG和SVG DOM的学习笔记(2)---SVG动画

SVG支持动画。可以通过以下几种方法获得动画效果:

  • 使用SVG动画元素。SVG可以描述随时间变化的图形对象,使用不同的动画元素可以定义运动路径,淡入淡出效果和对象的膨胀、收缩、旋转和变换颜色。
  • 使用SVG DOM。SVG DOM兼容DOM1和DOM2,而SVG又定义了一套附加的DOM接口,支持脚本动画。通过这个方法可以获得各种动画效果。脚本语言中的定时器对象可以用来启动和控制动画。
  • SVG被设计为支持未来版本的SMIL(Sychronized Multimedia Integration Language)。SMIL将被模块化而与SVG及其它基于XML的语言连接,产生动画效果。

一、SVG中的动画元素

SVG中的动画元素有五个,它们都可以随着时间的变化而改变SVG元素的属性或样式值,如下所示:

  • <animate>:改变数值的属性或样式的值;
  • <set>:改变非数据值的属性或样式的值,如visibility属性等;
  • <animateMotion> 
    沿着某路径移动SVG元素获得动画效果, 该元素提供一种通过指定路径移动元素的简单方法。路径数据与路径元素的 d 属性相同,但用路径元素指定。也可以用 xlink:href 将它链接到 animateMotion 元素。起点和终点由 from 和 to 属性确定,并且可以通过将 rotate 值设为 auto 来设置对象垂直对齐于路径。(也可以将 rotate 属性设为 auto-reverse 以将这个方位改变 180 度。或者可以给定一个特定角度)。如动画和交互性所示:
    
    <animateMotion path="M0,300 S150,100 200,200 S400,400 500,0" dur="8s" repeatCount="indefinite" rotate="auto" />  
  • <animateColor>:改变某些元素与颜色有关的属性或样式的值; 
<circle cx="250" cy="100" r="50" fill="red">
     <animateColor attributeType="CSS" attributeName="fill" from="rgb(255,0,0)" to="rgb(0,0,255)" dur="8s"  repeatCount="indefinite"/>
 </circle> 
  • <animateTransform>:改变SVG元素进行坐标变换时候的动画效果;
    <rect x="333" y="49" width="50" height="50" fill="none" stroke="purple">
          <animateTransform attributeName="transform" attributeType="XML"
               type="scale" from="1" to="3" additive="sum" begin="3s" dur="6s" fill="freeze" />
          <animateTransform attributeName="transform" attributeType="XML"
               type="translate" from="0,0" to="-222,-45" additive="sum" begin="3s" dur="6s" fill="freeze" />
     </rect>
  • set:剩下的这个元素可以很容易地设置一个元素在指定时间段内的特殊属性
    <circle cx="250" cy="100" r="50" fill="red">
        <set attributeName="r" to="100" begin="1s" dur="5s" fill="remove" />
    </circle>

     attributeName="<attributeName>":批明所作用的SVG元素中哪个属性或样式需要产生动画效果。 

attrbuteType="<XML|CSS|auto>":指明产生动画效果的属性或样式值是哪个命名空间定义的。“XML”表示“attributeName”的值是默认XML命名空间里定义的XML属性名;“CSS”表示“attributeName”的值是默认CSS属性的名称;“auto”是默认值,解析器解析时先在CSS属性列表中查找是否有匹配“attributeName”的属性名,然后再在XML的命名空间里找 

1.与时间控制相关的常用属性

begin="<clock-time-value>|wallclock-sync-value|indefinite|eventName":定义动画的开始时刻。

  • a.时间偏移值,如3S表示3秒后开始播放动画(hh:mm:ss.xxxx)
  • b.现实世界中的时间,定义后要保证SVG文档要在这个时间之前打开
  • c.Endefinite:表示这个动画不会自动开始,需要使用动态脚本调用"beginElement()"方法或指向动画元素
  • d.eventName:表示在某个事件触发时开始播放动画。如begin="mousedown"
    • 1.dur="<clock-time-value>|indefinite":定义动画的持续时间
    • 2.end="<clock-time-value>|indefinite":定义动画的结束时间
    • 3.restart="always|whenNotactive|never":是否重播
    • 4.repeatCount:重复播放次数 indefinite表示无限重播
    • 5.repeatDur:定义动画播放总时间
    • 6.fil="freeze|remove":定义动画播放完毕后是停留在播放的终点还是回到起始位置

2.与过程控制相关的常用属性

from="<value>":定义该动画元素所作用的属性值在开始变化时的值

to="<value>":定义该动画元素所作用的属性值在结束变化时的值

by="<value>":定义该动画元素所作用的属性值每次变化的步长值

calcMode="discrete|linear|paced|spline":定义动画关键点直接过渡的插值计算方式

  • discrete表示不采用插值算法,动画是从一个关键点跳跃到另外一个关键点
  • linear表示采用线性插值算法,这是<animate>元素和<set>元素采用的默认插值算法
  • paced表示采用在埋单上均匀变化的方法来插值
  • spline表示采用三次贝塞尔曲线的方式来插值,需要"KeySplines"属性的配合

 例子1:

<rect x="50" y="50" width="100" height="50">
    <animate attributeType="XML" attributeName="x" from="50" to="300" dur="3s" begin="3s" restart="always" repeatCount="3" >
</animate>
</rect>

例子2:

<text x="0" y="0" font-size="37" visibility='hidden" stroke="black" stroke-width="2" >
    <animateMotion path="M0,0 L50,50 L100,150" begin="1s" dur="5s" fill="freeze" rotate="auto"/>
</text>

例子3:

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
    <g transform="translate(100,100)"> 
<text id="TextElement" x="0" y="0" style="font-family:Verdana;font-size:24; visibility:hidden"> It's SVG!
        <set attributeName="visibility" attributeType="CSS" to="visible" begin="1s" dur="5s" fill="freeze" />
        <animateMotion path="M 50 50 L 300 300" begin="1s" dur="5s" fill="freeze" />
        <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" begin="1s" dur="15s" fill="freeze" repeatCount='indefinite'/> 
        <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1" to="3" additive="sum" begin="1s" dur="5s" fill="freeze" /> 
    </text> 
</g> 
</svg>

 二、事件的脚本编制(DOM动画)

象 HTML 页面一样,可以设置 SVG 图像以捕获某些事件(如点击鼠标和滚动),并用它们启动脚本。在构建简单SVG 图像时,可以通过属性捕获这些事件。最常用的是onclick、onactivate、onmousedown、onmouseup、onmouseover、onmousemove、onmouseout、onload、onresize、onunload 和 onrepeat。 

当这些事件之一被触发,就可以将事件对象本身提供给脚本,脚本反过来再用它确定哪个对象触发了该事件(也就是点击了什么对象)。然后脚本可以操纵那个对象的特性,如它的属性。 

这一示例回到了图案示例,但在此例中,当用户点击椭圆时,其填充由白色变为使用图案。

<?xml version="1.0"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"
        "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg width="500" height="300" xmlns="http://www.w3.org/2000/svg">
    <desc>Scripting the onclick event</desc>
    <script type="text/ecmascript">
        <![CDATA[
      function hideReveal(evt) {
        var imageTarget = evt.target;
        var theFill = imageTarget.getAttribute("fill");
        if (theFill == 'white')
          imageTarget.setAttribute("fill", "url(#notes)");
        else
          imageTarget.setAttribute("fill", "white");
      }
    ]]>

    </script>
    <defs>
        <pattern id="notes" x="0" y="0" width="50" height="75"
                 patternTransform="rotate(15)"
                 patternUnits="userSpaceOnUse">
            <ellipse cx="10" cy="30" rx="10" ry="5"/>
            <line x1="20" y1="30" x2="20" y2="0"
                  stroke-width="3" stroke="black"/>
            <line x1="20" y1="0" x2="30" y2="5"
                  stroke-width="3" stroke="black"/>
        </pattern>
    </defs>

    <!-- Outline the drawing area with a blue line -->

    <rect x="1" y="1" width="350" height="200" fill="none" stroke="blue"/>
    <ellipse onclick="hideReveal(evt)"  cx="175" cy="100" rx="125" ry="60"
            fill="url(#notes)" stroke="black" stroke-width="5"/>

</svg>
View Code

三、基本的声明性 (SMIL) 动画

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

<svg width="800px" height="800px" viewBox="0 0 800 800"
     version="1.1" xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"> <!-- Note that this is required in order to use xlink in the <use> element. -->

    <!-- THIS EXAMPLE NOT SUPPORTED IN INTERNET EXPLORER -->

    <title>Simplest SVG Animation</title>
    <desc>SVG declarative animation is used to rotate a square.</desc>

    <!-- Create a Cartesian coordinate system (with the y-axis flipped) for the animated square.
         That is, place the origin at the center of the 800 x 800 SVG viewport. -->
    <g transform="translate(400, 400)">

        <!-- A 200 x 200 square with the upper left-hand corner at (-100, -100). This places the center
             of the square at the origin (0, 0): -->
        <rect x="-100" y="-100" width="200" height="200" rx="5" ry="5"
              style=" fill: orange; stroke: black; stroke-width: 3; stroke-dasharray: 10, 5;">
            <animateTransform
                    attributeType="xml"
                    attributeName="transform" type="rotate"
                    from="0" to="90"
                    begin="0" dur="5s"
                    fill="freeze"
                    />
        </rect>

        <line x1="-400" y1="0" x2="400" y2="0" style="stroke: black;" /> <!-- Represents the x-axis. -->

        <line x1="0" y1="-400" x2="0" y2="400" style="stroke: black;" /> <!-- Represents the y-axis (although up is negative and down is positive). -->

    </g>
</svg>
View Code

同样的效果用javascript来实现:

<!DOCTYPE html>
<html>

<head>
    <title>JavaScript SVG Animation</title>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge"/> <!-- Remove this line in production. -->
</head>

<body>
<svg width="800px" height="800px" viewBox="0 0 800 800">
    <g transform="translate(400, 400)"> <!-- Create a Cartesian coordinate system (with the y-axis flipped) for the animated square. That is, place the origin at the center of the 800 x 800 SVG viewport: -->

        <!-- A 200 x 200 square with the upper left-hand corner at (-100, -100). This places the center of the square 
        at the origin (0, 0): -->
        <rect id="mySquare" x="-100" y="-100" width="200" height="200" rx="5" ry="5"
              style=" fill: orange; stroke: black; stroke-width: 3; stroke-dasharray: 10, 5;" />

        <!-- Represents the x-axis: -->
        <line x1="-400" y1="0" x2="400" y2="0" style="stroke: black;" />

        <!-- Represents the y-axis (although up is negative and down is positive): -->
        <line x1="0" y1="-400" x2="0" y2="400" style="stroke: black;" />

    </g>
</svg>
<script>
    "use strict";

    /* CONSTANTS */
    var initialTheta = 0; // The initial rotation angle, in degrees.
    var thetaDelta = 0.3; // The amount to rotate the square about every 16.7 milliseconds, in degrees.
    var angularLimit = 90; // The maximum number of degrees to rotate the square.
    var theSquare = document.getElementById("mySquare");

    theSquare.currentTheta = initialTheta; // The initial rotation angle to use when the animation starts, stored in a custom property.

    var requestAnimationFrameID = requestAnimationFrame(doAnim); // Start the loop.
    function doAnim() {
        if (theSquare.currentTheta > angularLimit) {
            cancelAnimationFrame(requestAnimationFrameID); // The square has rotated enough, instruct the browser to stop calling the doAnim() function.
            return; // No point in continuing, bail now.
        }

        theSquare.setAttribute("transform", "rotate(" + theSquare.currentTheta + ")"); // Rotate the square by a small amount.
        theSquare.currentTheta += thetaDelta;  // Increase the angle that the square will be rotated to, by a small amount.
        requestAnimationFrameID = requestAnimationFrame(doAnim); // Call the doAnim() function about 60 times per second (60 FPS), or about once every 16.7 milliseconds until cancelAnimationFrame() is called.
    }
</script>
</body>
</html>
View Code

用SVGDOM来实现:

<!DOCTYPE html>
<html>

<head>  
  <title>JavaScript SVG Animation</title>
  <meta http-equiv="X-UA-Compatible" content="IE=Edge" /> <!-- Remove this line in production. -->
</head>

<body>
  <svg id="svgElement" width="800px" height="800px" viewBox="0 0 800 800"> <!-- Give the svg element a name so that we can easily access it via JavaScript. -->

    <g transform="translate(400, 400)"> <!-- Create a Cartesian coordinate system (with the y-axis flipped) for the animated square. That is, place the origin at the center of the 800 x 800 SVG viewport: -->
  
      <!-- A 200 x 200 square with the upper left-hand corner at (-100, -100). This places the center of the square 
      at the origin (0, 0). Give the square a name so we can easily access it via JavaScript: -->  
      <rect id="mySquare" x="-100" y="-100" width="200" height="200" rx="5" ry="5"
            style=" fill: orange; stroke: black; stroke-width: 3; stroke-dasharray: 10, 5;" />
            
      <!-- Represents the x-axis: -->
      <line x1="-400" y1="0" x2="400" y2="0" style="stroke: black;" /> 
    
      <!-- Represents the y-axis (although up is negative and down is positive): -->  
      <line x1="0" y1="-400" x2="0" y2="400" style="stroke: black;" /> 
                
    </g>
  </svg>
  <script>
    "use strict";

    /* CONSTANTS */
    var initialTheta = 0; // The initial rotation angle, in degrees.
    var thetaDelta = 0.3; // The amount to rotate the square about every 16.7 milliseconds, in degrees.
    var angularLimit = 90; // The maximum number of degrees to rotate the square.

    /* GLOBALS */
    var requestAnimationFrameID;
    var mySquare = document.getElementById("mySquare");
    var transformObject;

    mySquare.currentTheta = initialTheta; // The initial rotation angle to use when the animation starts, stored in a custom property.
    transformObject = svgElement.createSVGTransform(); // Create a generic SVG transform object so as to gain access to its methods and properties, such as setRotate().
    mySquare.transform.baseVal.appendItem(transformObject); // Append the transform object to the square object, now the square object has inherited all the transform object's goodness.

    requestAnimationFrameID = requestAnimationFrame(doAnim); // Start the animation loop.
    function doAnim() {
      var transformObject;

      if (mySquare.currentTheta > angularLimit) {
        cancelAnimationFrame(requestAnimationFrameID); // Instruct the browser to stop calling requestAnimationFrame()'s callback.
        return;
      }

      mySquare.transform.baseVal.getItem(0).setRotate(mySquare.currentTheta, 0, 0); // Access the transform object (that was appended to mySquare in the init() function) and use its setRotate method to rotate the square about the point (0, 0) (which is at the center of the SVG viewport).
      mySquare.currentTheta += thetaDelta; // Place this line here so that the square isn't over rotated on the last call to doAnim().
      requestAnimationFrameID = requestAnimationFrame(doAnim); // Call the doAnim() function about every 60 times per second (i.e., 60 FPS).
    }
  </script>
</body>
</html>
View Code

参考:

转载于:https://www.cnblogs.com/JoannaQ/p/3989621.html

HTML5 前端动画库的比较与选择 本文旨在为前端开发者提供一份全面的HTML5动画库指南,帮助他们在众多选择中做出明智决策。我们将覆盖从简单的CSS动画到复杂的WebGL渲染,分析不同技术方案的优缺点。我们将从动画基础开始,逐步深入到各种动画技术,然后比较主流动画库,最后通过实际案例展示如何选择实现。CSS动画:使用CSS属性关键帧实现的动画效果Canvas:HTML5提供的位图绘制APISVG:可缩放矢量图形,基于XML的标记语言WebGL:基于OpenGL ES的3D图形APICSS动画:简单过渡的首选。 阅读详情

相关推荐

SVG动画详解:让你的图形动起来

SVG(Scalable Vector Graphics)是一种基于XML的矢量图形格式,允许你通过代码定义图形。与位图不同,SVG可以无限缩放而不会失真,这使得它非常适合用于图标、插图复杂的图形。SVG动画为Web开发者提供了一种强大且灵活的工具,可以用来制作高质量的动效,提升用户体验。在本文中,我们探讨了SVG动画的基本原理、实现方法以及实际应用场景。通过学习并掌握这些技巧,你可以为你的网页增添视觉亮点,使其在众多网站中脱颖而出。

阿里渣渣java研发组-群主 4112

svg常用属性及动画效果

SVG是可缩放矢量图形()的缩写。它是一种基于XML语法的2D矢量图形格式,用于描述二维图形图像。与传统的栅格图像(如JPEGPNG等)相比,SVG使用数学描述来定义图形,因此可以无损地缩放放大,而不会失真或失去清晰度。SVG可在各种分辨率尺寸的设备上显示,并且支持交互性动画效果,使得它成为一种非常强大灵活的图形格式。viewBox是SVG元素的一个属性,用于定义SVG画布中可见区域的坐标系大小。它是一个由4个数值组成的字符串,分别表示左上角的x坐标、左上角的y坐标、宽度高度。viewBox。

jieyucx的博客 5707

SVG DOMDOM操作介绍 - HTML5教程技巧

HTML5中,SVG成为了一种重要的图形标准,可以通过JavaScriptDOM(Document Object Model)来对SVG文档进行操作控制。通过SVG DOM,您可以使用JavaScriptSVG文档进行交互,包括创建SVG元素、修改元素属性样式,以及添加事件处理。通过SVG DOM,您可以访问操作SVG文档中的元素、属性事件,并实现动态的图形效果。在JavaScript代码中,我们使用了ID为"mySvg""myCircle"的元素,在使用前请确保SVG文档中存在这些元素。

PmtxPhp的博客 516

SVG基本使用(四、动画动画常用属性、复合动画/往返动画/形变动画/路径动画、脚本编程

一、动画1.SVG动画SVG中提供了三种常用动画标记animate:基础动画animateTransform:形变动画animateMotion:路径动画2.SVG动画属性attributeType: CSS/XML 规定的属性值的名称空间attributeName: 规定元素的哪个属性会产生动画效果from/to: 从哪到哪dur: 动画时长fill: 动画结束之后的状态 保持freeze结束状态/remove恢复初始状态(默认值)3.SVG动画使用方式。

coldriversnow的专栏 9773

SVG动画

SVG中提供三种常用动画标记

tinggu的专栏 2661

svg 初识+了解 + 应用 + 动画

svg 初识+了解 + 应用 + 动画

m0_51244077的博客 2495

HTML52D SVGSVG DOM学习笔记(1)

(项目中要使用SVG,只好补充知识了) HTML体系中,最常用的绘制矢量图的技术是SVGHTML5新增加的canvas元素。这两种技术都支持绘制矢量图光栅图。 一、SVG概述   可缩放矢量图形(Scalable Vector Graphics,简称SVG)是一种使用XML来描述二维图形的语言(SVG严格遵从XML语法)SVG允许三种类型的图形对象:矢量图形形状(例如...

564

HTML5 Canvas 与 SVG:让网页图形与动画活跃起来

随着Web技术的快速发展,HTML5已经为前端开发带来了众多创新,特别是在图形绘制动画方面。CanvasSVG作为HTML5的新特性,不仅为开发者提供了灵活、高效的图形绘制方式,还为用户创造了更加生动的交互体验。无论是动态图形、游戏开发,还是静态图形、图表设计,这两种技术都有广泛的应用场景。Canvas适用于需要快速、动态绘制大量图形的场合,而SVG则因其矢量特性,擅长创建可缩放、高清晰度的图形,特别适合在响应式设计中使用。

吴师兄大模型的博客 1693

html流程svg动画,html5 svg动画

以上是svg的一个线上编辑器,也可以adobe Illustrator制作生成。我们通过以上编辑器可以获得以下代码。例:Layer 1给路径path 添加 class 为 path1.path1 {stroke-dasharray: 20; //表示虚线描边 该属性把线段变成虚线 数值越大 虚线里每个线段的长度越长stroke-dashoffset: 1000;//虚线的偏移量}1.当虚线的...

weixin_34378978的博客 432

SVG矢量动画技术深度解析

SVG(Scalable Vector Graphics)是一种基于XML的矢量图形格式,用于描述二维矢量图形。SVG文件是纯文本,包含了图像的矢量数据,因此可以无损放大或缩小,保持高质量的输出。与位图格式如JPEG或PNG相比,SVG的这种特性使得它非常适合用于制作需要响应式设计的Web图形,如图标、图表复杂图形元素。库名称网址说明GSAP提供丰富的动画API优化的渲染性能anime.js轻量级但功能强大,易于使用mo.js。

weixin_29940495的博客 1284

svg动画的几种方式

最近在做的项目中有部分的svg需要做成动画,最开始的想法是转成gif,然后一看,啊,90M!!!实在不能忍,于是设计妹纸一起研究起了svg动画,发现这玩意儿还是很好玩的,怎么让svg动起来,这是一个很有趣的话题。在目前的开发过程中,一共使用了三种方式,接下来就一一列举一下吧。如果有更好的方式,欢迎补充一起讨论。

weixin_43207208的博客 4437

html svg dom操作,SVG DOM常用属性方法介绍

12.2.1文档初始化相关evt属性evt表示事件本身,可以通过evt获取与当前事件相关的信息,用户可以在script中定义响应函数,进行相应的处理。它与普通JavaScript脚本中的event基本相同,只不过在普通JavaScript的脚本中简写成“e”。ownerDocument属性通过引用该属性获得当前SVG文件的文档对象,也就是得到SVGDOM结构。使用举例:svgdoc =evt.t...

weixin_32066337的博客 825

一步步教你用HTML5 SVG实现动画效果

SVG是一种基于XML的,用于定义缩放矢量图形的标记语言。 它允许你通过在2D平面中确定的一组点来绘制路径、曲线形状。 此外你还可以通过在这些路径上添加动态属性(例如笔触,颜色,粗细,填充等)来生成动画。 从2017年4月起,CSS Level 3 填充描边模块(https://www.w3.org/TR/fill-stroke-3/)开始支持从外部样式表设置SVG颜色填...

编程知识分享 5595
上一篇: Remote Debugging Chrome 结合Genymotion模拟器的移动端web真机调试(转)
下一篇: 动态加载脚本和样式(转)
UG8064
博客等级 码龄10年 2粉丝 104原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值