这里会总结svg的基础知识和一些经典的案例。javascript
SVG(Scalable Vector Graphics)is an XML-based Language for creating graphics. 这是MDN英文文档对svg的介绍。 svg支持css,这一点有效的将图形和样式区分开。 svg的应用: 一、绘图 二、动画
svg元素能够经过<embed>、<object>或者<iframe>嵌入网页之中,可是咱们这里推荐使用<embed>, 兼容性比较好。
<embed src="circle.svg" type="image/svg+xml" />
<svg width="" height="">绘制的内容</svg>
一些公共属性
--------- 矩形 ---------
<rect x="" y="" rx="" ry="" width="" height=""></rect>
(x, y): 左上角坐标 rx: x轴圆角半径 ry: y轴圆角半径 width: 长度 height: 高度
--------- 圆 ---------
<circle cx="" cy="" r=""></circle>
(cx, cy): 圆心 r: 半径
-------------- 椭圆 --------------
<ellipse cx="" cy="" rx="" ry=""></ellipse>
(cx, cy): 中心点 rx: x轴半径 ry: y轴半径
-------- 线条 --------
<line x1="" y1="" x2="" y2=""></line>
(x1, y1): 线条的起始点 (x2, y2): 线条的结束点
--------------- 多边形polygon ---------------
<polygon points=""></polygon>
points: x,y x1,y1 ........
---------------- 曲线polyline ----------------
<polyline points=""></polyline>
points: x,y x1,y1 .........
--------------- 路径 ---------------
<path d=""></path>
d: 路径的描述 主要的语法: 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 命令区分大小写,除了Z。大写表示绝对位置,小写表示相对位置
。
--------------- 绘制文本 ---------------
<text x="" y="" text-anchor="" dx="" dy="">text</text>
(x, y): 文字左下角的起始坐标 text-anchor: start middle end 文本锚点(可能会和咱们预期的坐标有出入) dx: 横轴的偏移 dy: 纵轴的偏移 路径文本的绘制 <textPath xlink:href="#path">text</textPath>
<text>中还能够嵌套<tspan x="" y="">text</tspan>
同时文本也能够做为超连接 <a xlink:href="" target="">
<text></text>
</a>
----------------- 滤镜 -----------------
<filter id=""></filter>
MDN的案例css
-------------- 渐变 -------------- 线性渐变 <linearGradient x1="" y1="" x2="" y2="" gradientUnits>
<stop offset="" style="stop-color:;stop-opacity:;"></stop>
</linearGradient>
gradientUnits: 定义坐标 userSpaceOnUse(不会对pattern的单位进行缩放) | objectBoundingBox(会) x1: 渐变开始横坐标 y1: 渐变开始纵坐标 x2: 渐变结束横坐标 y2: 渐变结束纵坐标 offset: 渐变开始的位置 0% - 100% 放射性渐变 <radialGradient cx="" cy="" r="" fx="" fy="" gradientUnits>
<stop offset="" style="stop-color:;stop-opacity:;"></stop>
</radialGradient>
gradientUnits: 定义坐标 userSpaceOnUse(不会对pattern的单位进行缩放) | objectBoundingBox(会) cx: 外层圆心横坐标 cy: 外层圆心纵坐标 fx: 内层圆心横坐标 fy: 内层圆心纵坐标 r: 发散的半径 offset: 渐变开始的位置 0% - 100%
---------- 裁剪 ----------
<clipPath id="">裁剪路径</clipPath>
---------- 引用元素 ----------
<defs>声明引用元素</defs>
---------- 拷贝 ----------
<use x="" y="" width="" height="" xlink:href="id"></use>
(x, y): 克隆对象的左上角坐标 width: 克隆对象的宽度 height: 克隆对象的高度 xlink:href 引用克隆对象 ---------- 模式 ----------
<pattern id="" width="" height="" patternUnits="" patternTransform="">模式内的形状</pattern>
width: 模式的宽度 height: 模式的高度 patternUnits: 定义pattern的坐标系统 userSpaceOnUse(不会对pattern的单位进行缩放) | objectBoundingBox(会) patternTransform: 变换 ---------- 遮罩 ----------
<mask maskUnits="" x='' y="" width="" height="">内容</mask>
(x, y): 裁剪的左上角坐标。 width: 裁剪的宽度 height: 裁剪的高度
--------------------- clip-path 裁剪 --------------------- clip-path: 第一种: url(id) 配合svg的<clipPath> 第二种: polygon(x y,x1 y1,x2 y2,.......) 第三种:inset(top right bottom left round rt rr rb rl) 圆角矩形 这里比较重要的一点就是polygon的过渡动画必需要求各个状态的点的个数同样