SVG常用元素标签列表

来源:互联网 发布:材料预算软件 编辑:程序博客网 时间:2024/05/18 03:43

第一次系统化学习svg,整理了一些常用的SVG元素标签(没有整理滤镜相关)仅供参考学习。

  1. defs:用于定义所有可能再次引用的图形元素。在defs元素中定义的图形元素不会直接呈现。你可以在你的视口的任意地方利用 <use>元素呈现这些元素。
   <svg width="80px" height="30px" viewBox="0 0 80 30"     xmlns="http://www.w3.org/2000/svg">  <defs>    <linearGradient id="Gradient01">      <stop offset="20%" stop-color="#39F" />      <stop offset="90%" stop-color="#F3F" />    </linearGradient>  </defs>  <rect x="10" y="10" width="60" height="10"         fill="url(#Gradient01)"  /></svg>
  1. g:元素g是用来组合对象的容器。添加到g元素上的变换会应用到其所有的子元素上。添加到g元素的属性会被其所有的子元素继承。此外,g元素也可以用来定义复杂的对象,之后可以通过元素来引用它们。
<svg width="100%" height="100%" viewBox="0 0 95 50"     xmlns="http://www.w3.org/2000/svg">  <g stroke="green" fill="white" stroke-width="5">    <circle cx="25" cy="25" r="15" />    <circle cx="40" cy="25" r="15" />    <circle cx="55" cy="25" r="15" />    <circle cx="70" cy="25" r="15" />  </g></svg>
  1. path:用来定义形状的通用元素。所有的基本形状都可以用path元素来创建,path中d用来绘制图形的路径。
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">  <g fill="none">    <path stroke="red" d="M5 20 l215 0" />    <path stroke="black" d="M5 40 l215 0" />    <path stroke="blue" d="M5 60 l215 0" />  </g></svg>
  1. filter:<filter>标签用来定义SVG滤镜。<filter>标签使用必需的id属性来定义向图形应用哪个滤镜.
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">  <defs>    <filter id="f1" x="0" y="0">      <feGaussianBlur in="SourceGraphic" stdDeviation="15" />    </filter>  </defs>  <rect width="90" height="90" stroke="green" stroke-width="3"  fill="yellow" filter="url(#f1)" /></svg>
  1. stop:用于定义一个渐变上的颜色坡度,它可以是<linearGradient>元素或者<radialGradient>元素的子元素。
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">  <defs>    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">      <stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />      <stop offset="100%" style="stop-color:rgb(255,0,255);stop-opacity:1" />    </linearGradient>  </defs>  <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" /></svg>
  1. animate:随时间动态改变属性。
<svg width="120" height="120" viewPort="0 0 120 120" version="1.1"     xmlns="http://www.w3.org/2000/svg">  <rect x="10" y="10" width="100" height="100">    <animate attributeType="XML" attributeName="x" from="-100" to="120"        dur="10s" repeatCount="indefinite"/>  </rect></svg>
  1. animateMotion:使元素沿着动作路径移动。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="120" height="120" viewBox="0 0 120 120" version="1.1">    <!-- Draw the outline of the motion path in grey, along         with 2 small circles at key points -->    <path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110" stroke="lightgrey" stroke-width="2" fill="none" id="theMotionPath"/>    <circle cx="10" cy="110" r="3" fill="lightgrey"/>    <circle cx="110" cy="10" r="3" fill="lightgrey"/>    <!-- Here is a red circle which will be moved along the motion path. -->    <circle cx="" cy="" r="5" fill="red">        <!-- Define the motion path animation -->        <animateMotion dur="6s" repeatCount="indefinite">           <mpath xlink:href="#theMotionPath"/>        </animateMotion>    </circle></svg>
  1. animateTransform:添加目标元素上的一个变形属性,从而允许动画控制转换、缩放、旋转或斜切。
<svg width="120" height="120"  viewBox="0 0 120 120"     xmlns="http://www.w3.org/2000/svg" version="1.1"     xmlns:xlink="http://www.w3.org/1999/xlink" >    <polygon points="60,30 90,90 30,90">        <animateTransform attributeName="transform"                          attributeType="XML"                          type="rotate"                          from="0 60 70"                          to="360 60 70"                          dur="10s"                          repeatCount="indefinite"/>    </polygon></svg>
  1. clipPath:自定义一个裁剪区域,当在另一个元素上使用clip-path属性引用该自定义裁剪区域的时候会将原来的图形与该裁剪区域取交集,裁剪后的图形超出裁剪区域的部分将不会响应事件。
<svg width="120" height="120" viewPort="0 0 120 120" version="1.1" xmlns="http://www.w3.org/2000/svg">        <defs>            <clipPath id="clipPath">                <rect x="15" y="15" width="40" height="40" />            </clipPath>        </defs>        <circle cx="25" cy="25" r="20" style="fill: #0000ff; clip-path: url(#clipPath); " /></svg>
  1. feGaussianBlur:对输入图像进行高斯模糊,属性stdDeviation中指定的数量定义了钟形。
<svg width="230" height="120" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">  <filter id="blurMe">    <feGaussianBlur in="SourceGraphic" stdDeviation="5" />  </filter>  <circle cx="60"  cy="60" r="50" fill="green" />  <circle cx="170" cy="60" r="50" fill="green"          filter="url(#blurMe)" /></svg>
  1. marker:在特定的<path>元素、<line>元素、<polyline>元素或者<polygon>元素上绘制箭头或者多边标记图形。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="120" height="120" viewBox="0 0 120 120"        version="1.1">        <defs>            <marker id="Triangle" viewBox="0 0 10 10" refX="1" refY="5" markerWidth="6" markerHeight="6" orient="auto">                <path d="M 0 0 L 10 5 L 0 10 z" />            </marker>        </defs>        <polyline points="10,90 50,80 90,20" fill="none" stroke="black" stroke-width="2" marker-end="url(#Triangle)" /></svg>
  1. mpath:使<animateMotion> 元素能够引用一个外部的<path>元素作为运动路径的定义。
<svg width="500" height="300" viewBox="0 0 500 300" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">        <rect x="1" y="1" width="498" height="298" fill="none" stroke="blue" stroke-width="2" />        <!-- Draw the outline of the motion path in blue, along          with three small circles at the start, middle and end. -->        <path id="path1" d="M100,250 C 100,50 400,50 400,250" fill="none" stroke="blue" stroke-width="7.06" />        <circle cx="100" cy="250" r="17.64" fill="blue" />        <circle cx="250" cy="100" r="17.64" fill="blue" />        <circle cx="400" cy="250" r="17.64" fill="blue" />        <!-- Here is a triangle which will be moved about the motion path.       It is defined with an upright orientation with the base of       the triangle centered horizontally just above the origin. -->        <path d="M-25,-12.5 L25,-12.5 L 0,-87.5 z" fill="yellow" stroke="red" stroke-width="7.06">            <!-- Define the motion path animation -->            <animateMotion dur="6s" repeatCount="indefinite" rotate="auto">                <mpath xlink:href="#path1" />            </animateMotion>        </path> </svg>
  1. pattern:使用预定义的图形对一个对象进行填充或描边。pattern元素让预定义图形能够以固定间隔在x轴和y轴上重复(或平铺)从而覆盖要涂色的区域。先使用pattern元素定义图案,然后在给定的图形元素上用属性fill或属性stroke引用用来填充或描边的图案。
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="120" height="120" viewBox="0 0 120 120" version="1.1">    <defs>        <pattern id="Triangle" width="10" height="10" patternUnits="userSpaceOnUse">            <polygon points="5,0 10,10 0,10"/>        </pattern>    </defs>    <circle cx="60" cy="60" r="50" fill="url(#Triangle)"/></svg>
  1. symbol:用来定义一个图形模板对象,它可以用一个<use>元素实例化。symbol元素对图形的作用是在同一文档中多次使用,添加结构和语义。结构丰富的文档可以更生动地呈现出来,类似讲演稿或盲文,从而提升了可访问性。注意,一个symbol元素本身是不呈现的。只有symbol元素的实例(亦即,一个引用了symbol<use>元素)才能呈现。
<svg><!-- symbol definition  NEVER draw --><symbol id="sym01" viewBox="0 0 150 110">  <circle cx="50" cy="50" r="40" stroke-width="8" stroke="red" fill="red"/>  <circle cx="90" cy="60" r="40" stroke-width="8" stroke="green" fill="white"/></symbol><!-- actual drawing by "use" element --><use xlink:href="#sym01"     x="0" y="0" width="100" height="50"/><use xlink:href="#sym01"     x="0" y="50" width="75" height="38"/><use xlink:href="#sym01"     x="0" y="100" width="50" height="25"/></svg>
  1. tspan:在 <text>元素中,利用内含的tspan元素,可以调整文本和字体的属性以及当前文本的位置、绝对或相对坐标值。
<svg xmlns="http://www.w3.org/2000/svg" width="250" height="40" viewBox="0 0 250 40" version="1.1">  <style>    text {font: 20px Verdana, Helvetica, Arial, sans-serif;}    tspan {fill: red;font-weight: bold}  </style>  <text x="15" y="30">You are     <tspan>not</tspan>     a banana  </text></svg>
  1. use:在SVG文档内取得目标节点,并在别的地方复制它们。它的效果等同于这些节点被深克隆到一个不可见的DOM中,然后将其粘贴到use元素的位置,很像HTML5中的克隆模板元素。因为克隆的节点是不可见的,所以当使用CSS样式化一个use元素以及它的隐藏的后代元素的时候,必须小心注意。隐藏的、克隆的DOM不能保证继承CSS属性,除非你明文设置使用CSS继承。出于安全原因,一些浏览器可能在use元素上应用同源策略,还有可能拒绝载入xlink:href属性内的跨源URI。
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">        <style>            .classA {                fill: red            }        </style>        <defs>            <g id="Port">                <circle style="fill:inherit" r="10" />            </g>        </defs>        <text y="15">black</text>        <use x="50" y="10" xlink:href="#Port" />        <text y="35">red</text>        <use x="50" y="30" xlink:href="#Port" class="classA" />        <text y="55">blue</text>        <use x="50" y="50" xlink:href="#Port" style="fill:blue" /></svg>
  1. vkern:用于在自上而下的字体中精细调整两个雕刻文字的垂直距离。
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 游戏退出无响应怎么办 手机淘宝购物车打不开怎么办 淘宝店铺没有访客怎么办 淘宝店铺0流量怎么办 微信经常封号怎么办 网上拍卖堂违约怎么办 dnf4开组队制裁怎么办 红酒木塞丢了怎么办 红酒塞子进去了怎么办 淘金币即将过期怎么办 淘金币过期怎么办2018 换详情排名下降怎么办 长城宽带不用了怎么办 快递到了想退货怎么办 淘宝退货商家拒收怎么办 淘宝运费险失败怎么办 忘记购买运费险怎么办 咸鱼买家申请退款怎么办 熟猪肉有点变味怎么办 和领导意见不一致怎么办 骑手提前点送达怎么办 ubuntu安装报错怎么办 液相色谱两峰分不开怎么办 液相色谱柱老堵怎么办? 没有装usb驱动怎么办 ipad速度越来越慢怎么办 美萍管理软件打不开怎么办 小米4开机黑屏怎么办 小米电脑死机了怎么办 小米8手机死机怎么办 oppa7开不了机怎么办 oppo手机wlan打不开怎么办 三星s6进水黑屏怎么办 银行卡不支持快捷支付怎么办 路由器忘记管理员密码怎么办 云付没有推荐人怎么办 牛呗审核不通过怎么办 华硕笔记本很卡怎么办 淘宝换货没有货怎么办 用手机怎么办网银 手机销号支付宝怎么办