svg的使用

120 阅读1分钟

SVG

SVG 指可伸缩矢量图形,方法和缩小图片,图片质量不会改变。

SVG中的形状:

  • 矩形
  • 圆形
  • 椭圆
  • 线
  • 折线
  • 多边形
  • 路径

矩形 rect

用法:

 <?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="100%" height="100%" version="1.1"
 xmlns="http://www.w3.org/2000/svg">
 ​
 <rect x="20" y="20" width="250" height="250"
 style="fill:blue;stroke:pink;stroke-width:5;
 opacity:1"/>
 ​
 </svg>

属性:

  1. x为距离左侧的距离
  2. y为距离右侧的距离
  3. style中的fill为填充颜色
  4. stroke为边框颜色
  5. stroke-width为边框宽度
  6. stroke-opacity 属性定义笔触颜色的透明度
  7. fill-opacity 属性定义填充颜色透明度
  8. opacity 属性定义整个元素的透明值

圆 circle

用法:

 <?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="100%" height="100%" version="1.1"
 xmlns="http://www.w3.org/2000/svg">
 ​
 <circle cx="40" cy="40" r="40" stroke="black"
 stroke-width="2" fill="red"/>
 ​
 </svg>

属性:

  1. cx 和 cy 属性定义圆点的 x 和 y 坐标
  2. r定义圆的半径

椭圆 ellipse

 <?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="100%" height="100%" version="1.1"
 xmlns="http://www.w3.org/2000/svg">
 ​
 <ellipse cx="300" cy="150" rx="200" ry="80"
 style="fill:rgb(200,100,50);
 stroke:rgb(0,0,100);stroke-width:2"/>
 ​
 </svg>

属性:

  1. cx 属性定义圆点的 x 坐标
  2. cy 属性定义圆点的 y 坐标
  3. rx 属性定义水平半径
  4. ry 属性定义垂直半径

线条 line

 <?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="100%" height="100%" version="1.1"
 xmlns="http://www.w3.org/2000/svg">
 ​
 <line x1="0" y1="0" x2="300" y2="300"
 style="stroke:rgb(99,99,99);stroke-width:2"/>
 ​
 </svg>

属性:

  • x1 属性在 x 轴定义线条的开始
  • y1 属性在 y 轴定义线条的开始
  • x2 属性在 x 轴定义线条的结束
  • y2 属性在 y 轴定义线条的结束

多边形 polygon

 <?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="100%" height="100%" version="1.1"
 xmlns="http://www.w3.org/2000/svg">
 ​
 <polygon points="110,100 220,100  220,200 110,200"
 style="fill:#cccccc;
 stroke:#000000;stroke-width:1"/>
 ​
 </svg>
 ​

属性:

  1. points 属性定义多边形每个角的 x 和 y 坐标

折线 polyline

 <?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="100%" height="100%" version="1.1"
 xmlns="http://www.w3.org/2000/svg">
 ​
 <polyline points="0,0 0,20 20,20 20,40 40,40 40,60"
 style="fill:white;stroke:red;stroke-width:2"/>
 ​
 </svg>

属性:

  1. points 属性定义每个点的 x 和 y 坐标