hermanncain 2019-11-09
<line><line> 标签用来创建线条。
| 属性 | 含义 | 
|---|---|
| x1 | 属性在 x 轴定义线条的开始 | 
| y1 | 属性在 y 轴定义线条的开始 | 
| x2 | 属性在 x 轴定义线条的结束 | 
| y2 | 属性在 y 轴定义线条的结束 | 
<polyline><polyline> 标签用来创建仅包含直线的形状。
| 属性 | 含义 | 
|---|---|
points | 指定了每个端点的坐标,横坐标与纵坐标之间与逗号分隔,点与点之间用空格分隔 | 
<svg width="100" height="50"> <polyline stroke="red" fill="black" stroke-width="2" points="0,0 10,10 20,10 100,50"/> </svg>
See the Pen svg-example-4 by flqbestboy
(@fanlinqiang) on CodePen.
<circle>| 属性 | 含义 | 
|---|---|
cx | 圆心x轴坐标 | 
cy | 圆心y轴坐标 | 
r | 半径 | 
<svg width="100" height="50"> <circle cx="50" cy="25" r="25" /> </svg>
See the Pen svg-example-5 by flqbestboy
(@fanlinqiang) on CodePen.
<rect>标签可用来创建矩形,以及矩形的变种。
| 属性 | 含义 | 
|---|---|
x | 左上角x轴坐标,默认值为0 | 
y | 左上角y轴坐标,默认值为0 | 
width | 宽 | 
height | 高 | 
rx | 圆角弧度 | 
ry | 圆角弧度 | 
<svg width="100" height="50"> <rect width="100" height="50" rx="10" ry="20"/> </svg>
See the Pen svg-example-6 by flqbestboy
(@fanlinqiang) on CodePen.
<ellipse><ellipse> 标签可用来创建椭圆。椭圆与圆很相似。不同之处在于椭圆有不同的 x 和 y 半径,而圆的 x 和 y 半径是相同的。
| 属性 | 含义 | 
|---|---|
cx | 圆心x轴坐标 | 
cy | 圆心y轴坐标 | 
rx | 水平半径 | 
ry | 垂直半径 | 
<svg width="100" height="50"> <ellipse cx="50" cy="25" rx="50" ry="25"/> </svg>
See the Pen svg-example-7 by flqbestboy
(@fanlinqiang) on CodePen.
<path><path> 标签用来定义路径。MDN 详解
| 属性 | 含义 | 
|---|---|
d | 表示绘制顺序,它的值是一个长字符串,每个字母表示一个绘制动作,后面跟着坐标。 | 
支持绘制的动作包括:
https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/d
!> 注释:以上所有命令均允许小写字母。大写表示绝对定位,小写表示相对定位。
<svg width="100" height="50">
  <path d="M 0 0
   H 10
   V 10
   H 20
   V 20
   L 0 10 0 20
   C 100,0 " stroke="red"/>
</svg>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <path fill="none" stroke="red"
    d="M 10,30
       A 20,20 0,0,1 50,30
       A 20,20 0,0,1 90,30
       Q 90,60 50,90
       Q 10,60 10,30 z" />
</svg>See the Pen svg-example-8 by flqbestboy
(@fanlinqiang) on CodePen.
<text>| 属性 | 含义 | 
|---|---|
x | 文本起始横坐标 | 
y | 文本起始纵坐标 | 
<text> 元素可以通过<tspan> 元素来分组成多行来显示。每个 tspan 元素可以定义自己独特的格式和位置。
!> 文字的样式可以用class或style属性指定。
<svg width="200" height="50" xmlns:xlink="https://www.w3.org/1999/xlink">
    <text x="0" y="25">
        <tspan>hello svg</tspan>
        <tspan x="10" y="40">多行文本</tspan>
        <a xlink:href="www.baidu.com" target="_blank">
            <text x="0" y="15" fill="red">链接文本</text>
        </a>
    </text>
    <circle cx="100" cy="25" r="25" fill="#ff5e5e1a" />
    <text x="100" y="25" fill="red" style="dominant-baseline:middle;text-anchor:middle;">居中</text>
</svg>xmlns:xlink=""这一句引入了xlink命名空间,以支持链接元素属性定义。
xlink:href和html中的a链接类似,只是多了xlink的命名空间前缀,用来表示链接的目的地址。
See the Pen svg-example-9 by flqbestboy
(@fanlinqiang) on CodePen.
<use>use元素在SVG文档内取得目标节点,并在别的地方复制它们。它的效果等同于这些节点被深克隆到一个不可见的DOM中,然后将其粘贴到use元素的位置,很像HTML5中的克隆模板元素。因为克隆的节点是不可见的,所以当使用CSS样式化一个use元素以及它的隐藏的后代元素的时候,必须小心注意。隐藏的、克隆的DOM不能保证继承CSS属性,除非你明文设置使用CSS继承。
出于安全原因,一些浏览器可能在use元素上应用同源策略,还有可能拒绝载入xlink:href属性内的跨源URI。
| 属性 | 含义 | 
|---|---|
x | 左上角横坐标 | 
y | 左上角纵坐标 | 
width | 区域宽 | 
height | 区域高 | 
xlink:href | 引入复制节点 | 
<svg width="100" height="50"> <text id="useText" x="0" y="10">hello svg</text> <use xlink:href="#useText" x="0" y="20"/> </svg>
See the Pen svg-example-10 by flqbestboy
(@fanlinqiang) on CodePen.
<polygon>| 属性 | 含义 | 
|---|---|
points | 定义多边形每个角的x和y坐标, x和y用,分割,坐标之间用空格分割 | 
<svg width="100" height="50"> <polygon points="50,0 0,50 100,50"/> </svg>
See the Pen svg-example-11 by flqbestboy
(@fanlinqiang) on CodePen.
<g><g>标签用于将多个形状组成一个组(group),方便复用和管理。
<svg width="300" height="100">
  <g id="myCircle">
    <text x="25" y="20">圆形</text>
    <circle cx="50" cy="50" r="20"/>
  </g>
  <use href="#myCircle" x="100" y="0" fill="blue" />
  <use href="#myCircle" x="200" y="0" fill="white" stroke="blue" />
</svg>See the Pen svg-example-12 by flqbestboy
(@fanlinqiang) on CodePen.
<defs><defs>标签用于自定义形状,它内部的代码不会显示,仅供引用。
<svg width="300" height="100">
  <defs>
    <g id="myCircle">
      <text x="25" y="20">圆形</text>
      <circle cx="50" cy="50" r="20"/>
    </g>
  </defs>
  <use href="#myCircle" x="0" y="0" />
  <use href="#myCircle" x="100" y="0" fill="blue" />
  <use href="#myCircle" x="200" y="0" fill="white" stroke="blue" />
</svg>See the Pen svg-example-13 by flqbestboy
(@fanlinqiang) on CodePen.
<pattern><pattern>标签用于自定义一个形状,该形状可以被引用来平铺一个区域。
<svg width="500" height="500">
  <defs>
    <pattern id="dots" x="0" y="0" width="100" height="100" patternUnits="userSpaceOnUse">
      <circle fill="#bee9e8" cx="50" cy="50" r="35" />
    </pattern>
  </defs>
  <rect x="0" y="0" width="100%" height="100%" fill="url(#dots)" />
</svg>上面代码中,<pattern>标签将一个圆形定义为dots模式。patternUnits="userSpaceOnUse"表示的宽度和长度是实际的像素值。然后,指定这个模式去填充下面的矩形。
See the Pen svg-example-14 by flqbestboy
(@fanlinqiang) on CodePen.
<image>标签用于插入图片文件。
| 属性 | 含义 | 
|---|---|
xlink:href | 文件来源 | 
width | 宽 | 
height | 高 | 
<svg width="100" height="100"> <image xlink:href="./_statics/images/logo.jpeg" width="50%" height="50%"/> </svg>
<animate><animate>标签用于产生动画效果。
| 属性 | 含义 | 
|---|---|
attributeName | 发生动画效果的属性名 | 
from | 单次动画的初始值。 | 
to | 单次动画的结束值。 | 
dur | 单次动画的持续时间。 | 
repeatCount | 动画的循环模式。 | 
See the Pen svg-example-15 by flqbestboy
(@fanlinqiang) on CodePen.
transform变换<animateTransform><animate>标签对CSS的transform属性不起作用,如果需要变形,就要使用<animateTransform>标签
<svg width="500px" height="500px">
  <rect x="250" y="250" width="50" height="50" fill="#4bc0c8">
    <animateTransform attributeName="transform" type="rotate" begin="0s" dur="10s" from="0 200 200" to="360 400 400" repeatCount="indefinite" />
  </rect>
</svg>See the Pen svg-example-16 by flqbestboy
(@fanlinqiang) on CodePen.
上面代码中,的效果为旋转(rotate),这时from和to属性值有三个数字,第一个数字是角度值,第二个值和第三个值是旋转中心的坐标。from="0 200 200"表示开始时,角度为0,围绕(200, 200)开始旋转;to="360 400 400"表示结束时,角度为360,围绕(400, 400)旋转。