zoukankan      html  css  js  c++  java
  • SVG矢量图学习实例

    从W3school上学习了一下SVG矢量图形,感觉和HTML相比还是有一些新的元素和属性的,一时间不能全部记住,特此留下笔记,供遗忘时作为参考

      1 <!DOCTYPE html>
      2 <!-- 在<html>标签中声明SVG的XML方言xmlns:svg="http://www.w3.org/2000/svg" -->
      3 <html xmlns:svg="http://www.w3.org/2000/svg">
      4 
      5     <head>
      6         <meta charset="utf-8" />
      7         <title>SVG学习记录</title>
      8     </head>
      9 
     10     <body>
     11         <!-- SVG嵌入HTML的三种方式 -->
     12         <!-- pluginspage:支持<embed>标签的插件的下载地址 -->
     13         <embed src="svg/circle_example.svg" width="300" height="100" type="image/svg+xml" pluginspage="http://www.adobe.com/svg/viewer/install/" />
     14         <!-- codebase:支持<object>标签的插件的下载地址 -->
     15         <object data="svg/circle_example.svg" width="300" height="100" type="image/svg+xml" codebase="http://www.adobe.com/svg/viewer/install/"></object>
     16         <iframe src="svg/circle_example.svg" width="300" height="100"></iframe>
     17         <hr />
     18         <svg width="300" height="100" version="1.1">
     19             <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
     20         </svg>
     21         <hr />
     22         <p>矩形</p>
     23         <svg width="100%" height="100%" version="1.1">
     24             <rect width="300" height="100" style="fill:rgb(0,0,255);stroke-1; stroke:rgb(0,0,0)"/>
     25             <!-- 左距离20,上距离20,宽度150,高度70,填充蓝色,边框粉色,边框5px,填充部分透明度0.1,边框透明度0.9 -->
     26             <rect x="400" y="20" width="150" height="70" style="fill:blue;stroke:pink;stroke-5; fill-opacity:0.1;stroke-opacity:0.9"/>
     27         </svg>
     28         <svg width="100%" height="100%" version="1.1">
     29             <!-- 定义整个svg的透明度 -->
     30             <rect x="20" y="20" width="80" height="70" style="fill:blue;stroke:pink;stroke-5; opacity:0.6"/>
     31             <!-- rx,ry:圆角弧度  -->
     32             <rect x="120" y="20" rx="30" ry="20" width="250" height="50" style="fill:red;stroke:black; stroke-5;opacity:0.5"/>
     33         </svg>
     34         <hr />
     35         <p>圆形</p>
     36         <svg width="100%" height="100%" version="1.1">
     37             <!-- cx,cy:横纵坐标 -->
     38             <circle cx="100" cy="50" r="40" stroke="black" strok e-width="2" fill="red"/>
     39             <!-- 椭圆 -->
     40             <ellipse cx="200" cy="60" rx="20" ry="40" style="fill:rgb(200,100,50); stroke:rgb(0,0,100);stroke-2"/>
     41             <!-- 累叠椭圆 -->
     42             <ellipse cx="540" cy="100" rx="220" ry="30" style="fill:purple"/>
     43             <ellipse cx="520" cy="70" rx="190" ry="20" style="fill:lime"/>
     44             <ellipse cx="510" cy="45" rx="170" ry="15" style="fill:yellow"/>
     45             <!-- 组合椭圆 -->
     46             <ellipse cx="1040" cy="70" rx="220" ry="30" style="fill:yellow"/>
     47             <ellipse cx="1020" cy="70" rx="190" ry="20" style="fill:white"/>
     48         </svg>
     49         <hr />
     50         <p>线条</p>
     51         <svg width="100%" height="100%" version="1.1">
     52             <!-- x1,y1:开始点横纵坐标,x2,y2:结束点横纵坐标 -->
     53             <line x1="10" y1="10" x2="300" y2="40" style="stroke:rgb(99,99,99);stroke-2"/>
     54         </svg>
     55         <hr />
     56         <p>多边形</p>
     57         <svg width="100%" height="100%" version="1.1">
     58             <!-- points:多边形各个点所在位置 -->
     59             <polygon points="0 10,100 130,150 70" style="fill:#cccccc; stroke:#000000;stroke-1"/>
     60             <polygon points="240 5,60 20,160 70,240 70" style="fill:#cccccc; stroke:#000000;stroke-1"/>
     61         </svg>
     62         <hr />
     63         <p>折线</p>
     64         <svg width="100%" height="100%" version="1.1">
     65             <!-- 折线各个转折点 -->
     66             <polyline points="0,0 0,10 20,20 20,40 40,40 40,60" style="fill:white;stroke:red;stroke-2"/>
     67         </svg>
     68         <hr />
     69         <p>路径</p>
     70         <svg width="100%" height="100%" version="1.1">
     71             <!-- 路径:(100,10) -> (10,100) -> (100,100) -> (100,10) -->
     72             <path d="M100 10 L10 100 L100 100 Z" />
     73         </svg>
     74         <!-- 复杂图形建议使用SVG编辑器来进行绘制 -->
     75         <svg width="100%" height="500px" version="1.1">
     76             <path d="M153 334
     77             C153 334 151 334 151 334
     78             C151 339 153 344 156 344
     79             C164 344 171 339 171 334
     80             C171 322 164 314 156 314
     81             C142 314 131 322 131 334
     82             C131 350 142 364 156 364
     83             C175 364 191 350 191 334
     84             C191 311 175 294 156 294
     85             C131 294 111 311 111 334
     86             C111 361 131 384 156 384
     87             C186 384 211 361 211 334
     88             C211 300 186 274 156 274"
     89             style="fill:white;stroke:red;stroke-2"/>
     90         </svg>
     91         <hr />
     92         <p>可用滤镜</p>
     93         <p>
     94             <ul>
     95                 <li>feBlend</li>
     96                 <li>feColorMatrix</li>
     97                 <li>feComponentTransfer</li>
     98                 <li>feComposite</li>
     99                 <li>feConvolveMatrix</li>
    100                 <li>feDiffuseLighting</li>
    101                 <li>feDisplacementMap</li>
    102                 <li>feFlood</li>
    103                 <li>feGaussianBlur</li>
    104                 <li>feImage</li>
    105                 <li>feMerge</li>
    106                 <li>feMorphology</li>
    107                 <li>feOffset</li>
    108                 <li>feSpecularLighting</li>
    109                 <li>feTile</li>
    110                 <li>feTurbulence</li>
    111                 <li>feDistantLight</li>
    112                 <li>fePointLight</li>
    113                 <li>feSpotLight</li>
    114             </ul>
    115         </p>
    116         <hr />
    117         <p>高斯滤镜</p>
    118         <svg width="100%" height="100%" version="1.1">
    119             <defs>
    120                 <filter id="Gaussian_Blur">
    121                     <feGaussianBlur in="SourceGraphic" stdDeviation="3" />
    122                 </filter>
    123             </defs>
    124             <ellipse cx="200" cy="70" rx="70" ry="40" style="fill:#ff0000;stroke:#000000; stroke-2;filter:url(#Gaussian_Blur)"/>
    125         </svg>
    126         <svg width="100%" height="100%" version="1.1">
    127             <defs>
    128                 <filter id="Gaussian_Blur">
    129                     <feGaussianBlur in="SourceGraphic" stdDeviation="20"/><!-- 20 -->
    130                 </filter>
    131             </defs>
    132             <ellipse cx="200" cy="70" rx="70" ry="40" style="fill:#ff0000;stroke:#000000; stroke-2;filter:url(#Gaussian_Blur)"/>
    133         </svg>
    134         <hr />
    135         <p>线性渐变</p>
    136         <svg width="100%" height="100%" version="1.1">
    137             <defs>
    138                 <!-- <linearGradient> 标签的 id 属性可为渐变定义一个唯一的名称 -->
    139                 <!-- 水平渐变,x1,y1:渐变开始位置,x2,y2:渐变结束位置 -->
    140                 <linearGradient id="orange_red" x1="0%" y1="0%" x2="100%" y2="0%">
    141                     <!-- <stop>标签用于规定渐变中每个关键节点的颜色,offset用于标记其位置 -->
    142                     <stop offset="0%" style="stop-color:rgb(255,255,0); stop-opacity:1"/>
    143                     <stop offset="100%" style="stop-color:rgb(255,0,0); stop-opacity:1"/>
    144                 </linearGradient>
    145             </defs>
    146             <ellipse cx="200" cy="70" rx="85" ry="55" style="fill:url(#orange_red)"/>
    147             <!-- 垂直渐变 -->
    148             <defs>
    149                 <linearGradient id="orange-red" x1="0%" y1="0%" x2="0%" y2="100%">
    150                     <stop offset="0%" style="stop-color:rgb(255,255,0); stop-opacity:1"/>
    151                     <stop offset="100%" style="stop-color:rgb(255,0,0); stop-opacity:1"/>
    152                 </linearGradient>
    153             </defs>
    154             <ellipse cx="500" cy="70" rx="85" ry="55" style="fill:url(#orange-red)"/>
    155         </svg>
    156         <hr />
    157         <p>放射渐变</p>
    158         <svg width="100%" height="100%" version="1.1">
    159             <defs>
    160                 <!-- 参考http://www.w3school.com.cn/svg/svg_grad_radial.asp -->
    161                 <!-- r:个人理解为内圈放射范围 -->
    162                 <!-- fx,fy:个人理解为内圈中心(即<stop>的offset=0%)所在相对位置 -->
    163                 <radialGradient id="grey_blue" cx="40%" cy="40%" r="50%" fx="20%" fy="60%">
    164                     <stop offset="0%" style="stop-color:rgb(200,200,200); stop-opacity:0.5"/>
    165                     <stop offset="100%" style="stop-color:rgb(0,0,255); stop-opacity:1"/>
    166                 </radialGradient>
    167             </defs>
    168             <ellipse cx="170" cy="60" rx="100" ry="50" style="fill:url(#grey_blue)"/>
    169             <defs>
    170                 <!-- 个人理解 -->
    171                 <!-- cx,xy:如果点A(fx,fy)为中心,r为该中心点的放射范围,则B(cx,cy)可以表示为点A放射偏移方向的参照点,如果点A与点B位置重合,则表示放射没有偏移方向,即向四周等量放射-->
    172                 <radialGradient id="grey-blue" cx="50%" cy="50%" r="70%" fx="80%" fy="80%">
    173                     <stop offset="0%" style="stop-color:rgb(200,200,200); stop-opacity:0.5"/>
    174                     <stop offset="100%" style="stop-color:rgb(0,0,255); stop-opacity:1"/>
    175                 </radialGradient>
    176             </defs>
    177             <rect x="500" y="20" width="250" height="100" style="fill:url(#grey-blue);"/>
    178         </svg>
    179         <hr />
    180         <p>动画</p>
    181         <!-- 参考:http://www.w3school.com.cn/svg/svg_examples.asp -->
    182         <!-- 淡出效果 -->
    183         <svg width="100%" height="100%" version="1.1">
    184             <rect x="20" y="20" width="250" height="250" style="fill:blue">
    185                 <animate attributeType="CSS" attributeName="opacity" from="1" to="0" dur="5s" repeatCount="indefinite" />
    186             </rect>
    187         <!--</svg>-->
    188         <!-- 动态改变多个属性 -->
    189         <!--<svg width="100%" height="100%" version="1.1">-->
    190             <rect id="rec" x="900" y="100" width="300" height="10" style="fill:lime"> 
    191                 <animate attributeName="x" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="900" to="600"/> 
    192                 <animate attributeName="y" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="100" to="0"/> 
    193                 <animate attributeName="width" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="300" to="100"/> 
    194                 <animate attributeName="height" attributeType="XML" begin="0s" dur="6s" fill="freeze" from="10" to="100"/> 
    195                 <animateColor attributeName="fill" attributeType="CSS" from="lime" to="gray" begin="1s" dur="5s" fill="freeze"/>
    196             </rect>
    197         </svg>
    198         <svg width="100%" height="100%" version="1.1">
    199             <g transform="translate(50,50)">
    200                 <text id="TextElement" x="0" y="0" style="font-family:Verdana;font-size:24">沿路径平移
    201                     <animateMotion path="M 0 0 L 50 50" dur="3s" fill="freeze"/>
    202                 </text>
    203             </g>
    204         </svg>
    205         <svg width="100%" height="100%" version="1.1">
    206             <g transform="translate(100,100)"> 
    207                 <text id="TextElement" x="0" y="0" style="font-family:Verdana;font-size:22; visibility:hidden"> 平移+旋转+缩放
    208                     <set attributeName="visibility" attributeType="CSS" to="visible" begin="1s" dur="5s" fill="freeze"/>
    209                     <animateMotion path="M 0 0 L 100 30" begin="1s" dur="5s" fill="freeze"/>
    210                     <animateTransform attributeName="transform" attributeType="XML" type="rotate" from="-30" to="0" begin="1s" dur="5s" fill="freeze"/> 
    211                     <animateTransform attributeName="transform" attributeType="XML" type="scale" from="1" to="3" additive="sum" begin="1s" dur="5s" fill="freeze"/> 
    212                 </text> 
    213             </g> 
    214         </svg>
    215         <p>SVG元素属性参考手册: <a href="http://www.w3school.com.cn/svg/svg_reference.asp">参考W3School描述</a></p>
    216     </body>
    217 
    218 </html>
  • 相关阅读:
    ES6学习--函数剩余参数 (rest参数)
    ES6学习 --函数参数默认值与解构赋值默认值
    ES6学习--Array.from()方法
    02ython基础知识(一)
    01 Python初探
    c#利用IronPython调用python的过程种种问题
    Android 对话框(Dialogs)
    不可不知的安卓屏幕知识
    C#中的Split用法以及详解
    关于XML文档操作类
  • 原文地址:https://www.cnblogs.com/xfc-exclave/p/10788870.html
Copyright © 2011-2022 走看看