• SVG(可伸缩矢量图形 (Scalable Vector Graphics))
  • SVG 用来定义用于网络的基于矢量的图形
  • SVG 使用 XML 格式定义图形
  • SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失

    直接在HTML里面嵌入SVG代码

    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    2. <circle cx="100" cy="50" r="40" stroke="black" stroke-width="2" fill="red" />
    3. </svg>

SVG矩形 -

例一

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <rect
  3. width="300"
  4. height="100"
  5. style="fill: rgb(0, 0, 255); stroke-width: 1; stroke: rgb(0, 0, 0)"
  6. />
  7. </svg>

解析

  • rect 元素的 widthheight 属性可定义矩形的高度和宽度
  • style 属性用来定义 CSS 属性
  • CSS 的 fill 属性定义矩形的填充颜色(rgb 值、颜色名或者十六进制值)
  • CSS 的 stroke-width 属性定义矩形边框的宽度
  • CSS 的 stroke 属性定义矩形边框的颜色

    例二

    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    2. <rect
    3. x="50"
    4. y="20"
    5. width="150"
    6. height="150"
    7. style="
    8. fill: blue;
    9. stroke: pink;
    10. stroke-width: 5;
    11. fill-opacity: 0.1;
    12. stroke-opacity: 0.9;
    13. "
    14. />
    15. </svg>

    解析

  • x 属性定义矩形的左侧位置(例如,x=”0” 定义矩形到浏览器窗口左侧的距离是 0px)

  • y 属性定义矩形的顶端位置(例如,y=”0” 定义矩形到浏览器窗口顶端的距离是 0px)
  • CSS 的 fill-opacity 属性定义填充颜色透明度(合法的范围是:0 - 1)
  • CSS 的 stroke-opacity 属性定义轮廓颜色的透明度(合法的范围是:0 - 1)

    例三

    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    2. <rect
    3. x="50"
    4. y="20"
    5. width="150"
    6. height="150"
    7. style="fill: blue;
    8. stroke: pink;
    9. stroke-width: 5;
    10. opacity: 0.9"
    11. />
    12. </svg>

    解析

  • CSS opacity 属性用于定义了元素的透明值 (范围: 0 到 1)。

    例四

    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    2. <rect
    3. x="50"
    4. y="20"
    5. rx="20"
    6. ry="20"
    7. width="150"
    8. height="150"
    9. style="fill: red; stroke: black; stroke-width: 5; opacity: 0.5"
    10. />
    11. </svg>

    解析

  • rxry 属性可使矩形产生圆角。


SVG圆形 -

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <circle
  3. cx="100"
  4. cy="50"
  5. r="40"
  6. stroke="black"
  7. stroke-width="2"
  8. fill="red"
  9. />
  10. </svg>

解析

  • cxcy属性定义圆点的x和y坐标。如果省略cxcy,圆的中心会被设置为(0, 0)
  • r属性定义圆的半径

SVG椭圆 -

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <ellipse
  3. cx="300"
  4. cy="80"
  5. rx="100"
  6. ry="50"
  7. style="fill: yellow; stroke: purple; stroke-width: 2"
  8. />
  9. </svg>

解析

  • CX属性定义的椭圆中心的x坐标
  • CY属性定义的椭圆中心的y坐标
  • RX属性定义的水平半径
  • RY属性定义的垂直半径

SVG直线 -

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <line
  3. x1="0"
  4. y1="0"
  5. x2="200"
  6. y2="200"
  7. style="stroke: rgb(255, 0, 0); stroke-width: 2"
  8. />
  9. </svg>

解析

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

SVG多边形 -

三边型如下,四边形同理

  1. <svg height="210" width="500">
  2. <polygon
  3. points="200,10 250,190 160,210"
  4. style="fill: lime; stroke: purple; stroke-width: 1"
  5. />
  6. </svg>

解析

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

    星型

    1. <svg height="210" width="500">
    2. <polygon
    3. points="100,10 40,198 190,78 10,78 160,198"
    4. style="
    5. fill: lime;
    6. stroke: purple;
    7. stroke-width: 5;
    8. fill-rule: evenodd;
    9. "
    10. />
    11. </svg>

    解析

  • points 属性定义五个角

  • fill-rule属性有两个值nonzero | evenodd,用于确定一个形状的内部(即可被填充的区域)

SVG多段线 -

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <polyline
  3. points="20,20 40,25 60,40 80,120 120,140 200,180"
  4. style="fill: none; stroke: black; stroke-width: 3"
  5. />
  6. </svg>

楼梯线

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <polyline
  3. points="0,40 40,40 40,80 80,80 80,120 120,120 120,160"
  4. style="fill: white; stroke: red; stroke-width: 4"
  5. />
  6. </svg>

SVG 路径 -

定义路径数据的命令

  • M = moveto
  • L = lineto
  • H = horizontal lineto
  • V = vertical lineto
  • C = curveto
  • S = smooth curveto
  • Q = quadratic Bézier curve
  • T = smooth quadratic Bézier curveto
  • A = elliptical Arc
  • Z = closepath

    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    2. <path d="M150 0 L75 200 L225 200 Z" />
    3. </svg>

    解析

  • path属性用来定义一个路径

  • 从150 0开始连线到75 200,再连线到225 200,最后闭合路径

二次方贝塞尔曲线(复杂曲线)

  1. <svg
  2. xmlns="http://www.w3.org/2000/svg"
  3. version="1.1"
  4. height="400"
  5. width="450"
  6. >
  7. <path
  8. id="lineAB"
  9. d="M 100 350 l 150 -300"
  10. stroke="red"
  11. stroke-width="3"
  12. fill="none"
  13. />
  14. <path
  15. id="lineBC"
  16. d="M 250 50 l 150 300"
  17. stroke="red"
  18. stroke-width="3"
  19. fill="none"
  20. />
  21. <path d="M 175 200 l 150 0" stroke="green" stroke-width="3" fill="none" />
  22. <path
  23. d="M 100 350 q 150 -300 300 0"
  24. stroke="blue"
  25. stroke-width="5"
  26. fill="none"
  27. />
  28. <!-- Mark relevant points -->
  29. <g stroke="black" stroke-width="3" fill="black">
  30. <circle id="pointA" cx="100" cy="350" r="3" />
  31. <circle id="pointB" cx="250" cy="50" r="3" />
  32. <circle id="pointC" cx="400" cy="350" r="3" />
  33. </g>
  34. <!-- Label the points -->
  35. <g
  36. font-size="30"
  37. font="sans-serif"
  38. fill="black"
  39. stroke="none"
  40. text-anchor="middle"
  41. >
  42. <text x="100" y="350" dx="-30">A</text>
  43. <text x="250" y="50" dy="-10">B</text>
  44. <text x="400" y="350" dx="30">C</text>
  45. </g>
  46. </svg>

SVG文本 -

旋转的文本

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <text x="0" y="15" fill="red" transform="rotate(30 20,40)">I love SVG</text>
  3. </svg>

路径上的文字

  1. <svg
  2. xmlns="http://www.w3.org/2000/svg"
  3. version="1.1"
  4. xmlns:xlink="http://www.w3.org/1999/xlink"
  5. >
  6. <defs>
  7. <path id="path1" d="M75,20 a1,1 0 0,0 100,0" />
  8. </defs>
  9. <text x="10" y="100" style="fill: red">
  10. <textPath xlink:href="#path1">I love SVG I love SVG</textPath>
  11. </text>
  12. </svg>

解析

  • defs元素允许我们定义以后需要重复使用的图形元素。

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <text x="10" y="20" style="fill: red">
  3. Several lines:
  4. <tspan dx="10" dy="45">First line</tspan>
  5. <tspan dx="10" dy="45">Second line</tspan>
  6. </text>
  7. </svg>

解析

  • tspan元素可以包含不同的格式和位置,可以安排任何分小组与tspan元素的数量

SVG Stroke属性

  • stroke
  • stroke-width
  • stroke-linecap
  • stroke-dasharray

    stroke

    定义给定图形元素的外轮廓的颜色

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <g fill="none">
  3. <path stroke="red" d="M5 20 l215 0" />
  4. <path stroke="black" d="M5 40 l215 0" />
  5. <path stroke="blue" d="M5 60 l215 0" />
  6. </g>
  7. </svg>

image.png

stroke-width

定义当前对象的轮廓的宽度

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <g fill="none" stroke="black">
  3. <path stroke-width="2" d="M5 20 l215 0" />
  4. <path stroke-width="14" d="M5 40 l215 0" />
  5. <path stroke-width="6" d="M5 60 l215 0" />
  6. </g>
  7. </svg>

stroke-linecap

定义不同类型的开放路径的终结

  1. <svg
  2. width="120"
  3. height="120"
  4. viewPort="0 0 120 120"
  5. version="1.1"
  6. xmlns="http://www.w3.org/2000/svg"
  7. >
  8. <line
  9. stroke-linecap="butt"
  10. x1="30"
  11. y1="30"
  12. x2="30"
  13. y2="90"
  14. stroke="black"
  15. stroke-width="20"
  16. />
  17. <line
  18. stroke-linecap="round"
  19. x1="60"
  20. y1="30"
  21. x2="60"
  22. y2="90"
  23. stroke="black"
  24. stroke-width="20"
  25. />
  26. <line
  27. stroke-linecap="square"
  28. x1="90"
  29. y1="30"
  30. x2="90"
  31. y2="90"
  32. stroke="black"
  33. stroke-width="20"
  34. />
  35. <path d="M30,30 L30,90 M60,30 L60,90 M90,30 L90,90" stroke="white" />
  36. </svg>

stroke-dasharray

用于创建虚线,,用于控制描边的点划线的图案规范

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <g fill="none" stroke="black" stroke-width="4">
  3. <path stroke-dasharray="5,5" d="M5 20 l215 0" />
  4. <path stroke-dasharray="10,10" d="M5 40 l215 0" />
  5. <path stroke-dasharray="20,10,5,5,5,10" d="M5 60 l215 0" />
  6. </g>
  7. </svg>

SVG 滤镜

通过filter元素进行定义,定义在defs元素里

feGaussianBlur 模糊

创建模糊效果

  1. <p>
  2. <b>Note: </b>Internet Explorer and Safari do not support SVG filters yet!
  3. </p>
  4. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  5. <defs>
  6. <filter id="f1" x="0" y="0">
  7. <feGaussianBlur in="SourceGraphic" stdDeviation="15" />
  8. </filter>
  9. </defs>
  10. <rect
  11. width="90"
  12. height="90"
  13. stroke="green"
  14. stroke-width="3"
  15. fill="yellow"
  16. filter="url(#f1)"
  17. />
  18. </svg>

解析

  • <filter>元素id属性定义一个滤镜的唯一名称
  • <feGaussianBlur>元素定义模糊效果
  • in="SourceGraphic"这个部分定义了由整个图像创建效果
  • stdDeviation属性定义模糊量
  • <rect>元素的滤镜属性用来把元素链接到”f1”滤镜

    阴影 feOffset + feBlend实例

    例一

    用于创建阴影效果

  1. <p>
  2. <b>Note: </b>Internet Explorer and Safari do not support SVG filters yet!
  3. </p>
  4. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  5. <defs>
  6. <filter id="f1" x="0" y="0" width="200%" height="200%">
  7. <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" />
  8. <feBlend in="SourceGraphic" in2="offOut" mode="normal" />
  9. </filter>
  10. </defs>
  11. <rect
  12. width="90"
  13. height="90"
  14. stroke="red"
  15. stroke-width="5"
  16. fill="blue"
  17. filter="url(#f1)"
  18. />
  19. </svg>
  • feOffset:负责定义位移
  • feBlend:负责样式

image.png
例二

  1. <p>
  2. <b>Note: </b>Internet Explorer and Safari do not support SVG filters yet!
  3. </p>
  4. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  5. <defs>
  6. <filter id="f1" x="0" y="0" width="200%" height="200%">
  7. <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" />
  8. <feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
  9. <feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
  10. </filter>
  11. </defs>
  12. <rect
  13. width="90"
  14. height="90"
  15. stroke="green"
  16. stroke-width="3"
  17. fill="yellow"
  18. filter="url(#f1)"
  19. />
  20. </svg>
  • <feGaussianBlur>元素的stdDeviation属性定义了模糊量

image.png


SVG 线性渐变 -

  • 元素用于定义线性渐变。

  • 标签必须嵌套在的内部。标签是definitions的缩写,它可对诸如渐变之类的特殊元素进行定义。

线性渐变可以定义为水平,垂直或角渐变:

  • 当y1和y2相等,而x1和x2不同时,可创建水平渐变
  • 当x1和x2相等,而y1和y2不同时,可创建垂直渐变
  • 当x1和x2不同,且y1和y2不同时,可创建角形渐变

    1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
    2. <defs>
    3. <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
    4. <stop
    5. offset="0%"
    6. style="stop-color: rgb(255, 255, 0); stop-opacity: 1"
    7. />
    8. <stop
    9. offset="100%"
    10. style="stop-color: rgb(255, 0, 0); stop-opacity: 1"
    11. />
    12. </linearGradient>
    13. </defs>
    14. <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
    15. </svg>

    解析

  • 标签的id属性可为渐变定义一个唯一的名称

  • 标签的X1,X2,Y1,Y2属性定义渐变开始和结束位置
  • 渐变的颜色范围可由两种或多种颜色组成。每种颜色通过一个标签来规定。offset属性用来定义渐变的开始和结束位置。
  • 填充属性把 ellipse 元素链接到此渐变

image.png

SVG 放射性渐变 -

  1. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  2. <defs>
  3. <radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
  4. <stop
  5. offset="0%"
  6. style="stop-color: rgb(255, 255, 255); stop-opacity: 0"
  7. />
  8. <stop
  9. offset="100%"
  10. style="stop-color: rgb(0, 0, 255); stop-opacity: 1"
  11. />
  12. </radialGradient>
  13. </defs>
  14. <ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
  15. </svg>

image.png
解析

  • <radialGradient>标签的 id 属性可为渐变定义一个唯一的名称
  • CXCYr属性定义的最外层圆和FxFy定义的最内层圆
  • 渐变颜色范围可以由两个或两个以上的颜色组成。每种颜色用一个<stop>标签指定。
  • offset属性用来定义渐变色开始和结束
  • 填充属性把ellipse元素链接到此渐变