简单介绍
- SVG 是一种使用 XML 描述 2D 图形的语言。
- SVG 基于 XML,这意味着 SVG DOM 中的每个元素都是可用的。您可以为某个元素附加 JavaScript 事件处理器。
- 在 SVG 中,每个被绘制的图形均被视为对象。如果 SVG 对象的属性发生变化,那么浏览器能够自动重现图形。
预定义的形状元素
<body>
<div class="box">
<!-- svg 不加 viewBox 属性,那么下面 位置、长度数值 的单位 就是 px -->
<!-- 以下位置的基准点 是 svg元素的左上角,可以手动修改 -->
<svg width='500' height='800' style="background: white;">
<!-- 四边形 fill:背景填充, stroke:描边 -->
<rect x='10' y='10' width='100' height='30'
stroke='green' stroke-opacity='0.4' stroke-width='5' stroke-dasharray='2'
fill='red' />
<!-- 圆形 -->
<circle cx='100' cy='100' r='50' />
<!-- 椭圆形 -->
<ellipse cx='100' cy='200' rx='50' ry='25' />
<!-- 直线 -->
<line x1='10' y1='250' x2='110' y2='250' stroke='red' />
<!-- 多边形, points='x,y x2,y2 ...'-->
<polygon points='10,300 100,330 200,400 50,500' />
<!-- 折线 -->
<polyline points='10,400 100,430 200,500 50,600' stroke='red' fill='transparent' />
<!-- 文字 -->
<text x='20' y='700' style="font-size: 25px;"> see you</text>
<!-- g元素是 组元素, 为其子元素 统一在 g元素上 设置 填充色 和 描边 等性状配置 -->
<g stroke="black" stroke-width="3" fill="skyblue" >
<circle cx="250" cy="100" r="10" />
<circle cx="300" cy="50" r="10" />
<circle cx="350" cy="100" r="10" />
</g>
</svg>
</div>
</body>
路径标签 path
path元素可以用于定义路径,元素中有一个 d 属性
,这个 d 属性是一系列命令的集合。
一系列命令如下:
命令 | 参数 | 作用描述 |
---|---|---|
M | x y | 移动画笔到 参数坐标,即起始点 |
L | x y | 绘制一条到 给定坐标的线,可提供多组坐标绘制折线 |
H | y | 绘制平行线 |
V | x | 绘制垂直线 |
A | rx ry x-axis-rotation large-arc-flag sweep-flag x y | 绘制当前点到(x,y)的椭圆弧,椭圆弧的 x,y轴半径分别为 rx,ry。椭圆相对于 x 轴旋转 x-axis-rotation 度。large-arc-flag 等于0表示弧线小于180度,等于1表示弧线大于180度。sweep-flag 等于0 表示弧线逆时针旋转,等于1表示弧线顺时针选装 |
Q | x y x1 y1 | 绘制一条从当前点到(x,y),控制点为(x1,y1)的二次贝塞尔曲线 |
T | x y | 绘制一条从当前点到(x,y)的二次贝塞尔曲线 |
S | x y x1 y1 | 绘制一条从当前点到(x,y)的三次贝塞尔曲线,(x1,y1)为新端点的控制点 |
C | x yx1 y1 x2 y2 | 绘制一条从当前点到(x,y)的三次贝塞尔曲线,(x1,y1)为曲线的开始控制点,(x2,y2)为曲线的终点控制点 |
Z | 闭合路径 |
注意:以上所有命令均允许小写字母。大写表示绝对定位,小写表示相对定位。
栗子
- 使用路径命令绘制一个矩形:
<svg width='450' height='600'>
<path d="M50 50 L200 50 L200 150 L50 150 Z" fill="red" />
</svg>
说明:
其中 M 命令指定了开始点的位置,即左上角 (50,50),L 命令可以用于绘制一条直线段,这个直线段从当前位置移到指定位置。上述三个 L 位置分别为右上角 (200,50)、右下角 (200,150)、左下角(50,150)。Z 命令为闭合路径命令。
- 绘制一段弧线:
<svg width='450' height='600'>
<path d="M50,40 A30 50 0 0 1 100,110" fill="transparent" stroke="green"/>
</svg>
说明:
椭圆弧指令(A)最复杂的,有 7个参数
这段弧线的起点为 (50,40)
,
第一个参数: 圆弧x轴方向上的 半径
第二个参数: 圆弧y轴方向上的 半径
第三个参数: 椭圆相对于坐标系的旋转角度
,角度数而非弧度数
第四个参数: 绘制大弧(1)
还是小弧(0)
第五个参数:顺时针(1)
还是逆时针(0)
方向绘制
第六、七个参数: 表示终点为(100, 110
)
选择器 控制 svg相关标签
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>wtt</title>
<style>
.box {
margin-top: 10px;
border: 10px solid red;
box-sizing: border-box;
width: 555px;
height: 800px;
}
.box svg {
margin-top: 50px;
border: 1px solid black;
box-shadow: 2px 2px 10px 10px black;
background: blanchedalmond;
}
.wtt {
/* 注释的属性 都是 在 svg标签上 使用无效 的 属性 */
/*
border: 5px solid black;
box-shadow: 1px 1px 10px 10px red;
margin-top: 200px;
*/
x: 120;
y: 200;
width: 200px;
height: 200px;
fill: red;
stroke: black;
stroke-width: 5;
animation: wtt 2s ease 0s infinite alternate;
}
@keyframes wtt {
0% {
stroke-width: 5;
}
50% {
stroke-width: 15;
}
100% {
stroke-width: 25;
}
}
</style>
</head>
<body>
<div class="box">
<svg width='450' height='600'>
<rect class="wtt" />
</svg>
</div>
</body>
</html>
填充色
线性渐变 (linearGradient)
<svg width='500' height='800' style="background: white;">
<!-- 把所有需要 再次使用的 引用元素 定义在 defs元素里面 -->
<defs>
<!-- 水平方向渐变 -->
<!--linearGradient: 定义线性渐变的 引用元素 -->
<linearGradient id="wtt" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:pink;stop-opacity:1" />
<stop offset="50%" style="stop-color:skyblue;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
<!-- 竖直方向渐变 -->
<linearGradient id="who" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:pink;stop-opacity:1" />
<stop offset="50%" style="stop-color:skyblue;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<!-- 通过url 获取 指定id(#wtt,别忘了#号) 的 引用元素 -->
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#wtt)" />
</svg>
放射渐变 (radialGradient)
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
滤镜
<svg width='450' height='600'>
<filter id="f1" x="0" y="0">
<feGaussianBlur in="SourceGraphic" stdDeviation="10" />
</filter>
</defs>
<rect x='10' y='10' width="90" height="90" fill="black" filter="url(#f1)" />
</svg>
影子效果
<svg width='450' height='600'>
<defs>
<filter id="f1" x="0" y="0" width="200%" height="200%">
<!-- 影子偏移量 -->
<feOffset result="offOut" in="SourceAlpha" dx="20" dy="20" />
<!-- 影子模糊度 -->
<feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" />
<feBlend in="SourceGraphic" in2="blurOut" mode="normal" />
</filter>
</defs>
<rect
x='100' y='100'
width="90" height="90"
stroke="green" stroke-width="3"
fill="yellow" filter="url(#f1)"
/>
</svg>