SVG 知识速查表
本文档提供 SVG 常用属性、元素和语法的快速参考。
基本结构
<svg width="200" height="100" viewBox="0 0 200 100" xmlns="http://www.w3.org/2000/svg">
<!-- SVG 内容 -->
</svg>
| 属性 | 说明 |
|---|---|
| width | SVG 宽度 |
| height | SVG 高度 |
| viewBox | 坐标系统 "min-x min-y width height" |
| xmlns | XML 命名空间(必需) |
| preserveAspectRatio | 宽高比保持方式 |
基本形状
矩形 rect
<rect x="10" y="10" width="80" height="60" rx="5" ry="5"/>
| 属性 | 说明 | 默认值 |
|---|---|---|
| x, y | 左上角坐标 | 0 |
| width, height | 宽高 | 必需 |
| rx, ry | 圆角半径 | 0 |
圆形 circle
<circle cx="50" cy="50" r="40"/>
| 属性 | 说明 | 默认值 |
|---|---|---|
| cx, cy | 圆心坐标 | 0 |
| r | 半径 | 必需 |
椭圆 ellipse
<ellipse cx="50" cy="50" rx="40" ry="30"/>
| 属性 | 说明 | 默认值 |
|---|---|---|
| cx, cy | 中心坐标 | 0 |
| rx, ry | x/y 轴半径 | 必需 |
线条 line
<line x1="10" y1="10" x2="100" y2="100" stroke="#000"/>
| 属性 | 说明 | 默认值 |
|---|---|---|
| x1, y1 | 起点坐标 | 0 |
| x2, y2 | 终点坐标 | 0 |
折线 polyline
<polyline points="10,10 50,50 100,20" fill="none" stroke="#000"/>
| 属性 | 说明 |
|---|---|
| points | 点序列 "x1,y1 x2,y2 ..." |
多边形 polygon
<polygon points="50,10 90,90 10,90"/>
| 属性 | 说明 |
|---|---|
| points | 点序列(自动闭合) |
路径 path
<path d="M 10 10 L 100 10 L 100 100 Z"/>
路径命令
| 命令 | 参数 | 说明 |
|---|---|---|
| M/m | x y | 移动到 |
| L/l | x y | 直线到 |
| H/h | x | 水平线 |
| V/v | y | 垂直线 |
| C/c | x1 y1, x2 y2, x y | 三次贝塞尔曲线 |
| S/s | x2 y2, x y | 平滑三次贝塞尔 |
| Q/q | x1 y1, x y | 二次贝塞尔曲线 |
| T/t | x y | 平滑二次贝塞尔 |
| A/a | rx ry rot large sweep x y | 弧线 |
| Z/z | 无 | 闭合路径 |
大写字母:绝对坐标,小写字母:相对坐标
填充与描边
填充属性
| 属性 | 说明 | 默认值 |
|---|---|---|
| fill | 填充颜色 | black |
| fill-opacity | 填充透明度 | 1 |
| fill-rule | 填充规则 | nonzero |
描边属性
| 属性 | 说明 | 默认值 |
|---|---|---|
| stroke | 描边颜色 | none |
| stroke-width | 描边宽度 | 1 |
| stroke-opacity | 描边透明度 | 1 |
| stroke-linecap | 线端样式 | butt |
| stroke-linejoin | 拐角样式 | miter |
| stroke-dasharray | 虚线模式 | none |
| stroke-dashoffset | 虚线偏移 | 0 |
stroke-linecap 值
| 值 | 效果 |
|---|---|
| butt | 平直边缘 |
| round | 圆形端点 |
| square | 方形端点 |
stroke-linejoin 值
| 值 | 效果 |
|---|---|
| miter | 尖角 |
| round | 圆角 |
| bevel | 斜角 |
颜色格式
| 格式 | 示例 |
|---|---|
| 颜色名称 | red, blue, transparent |
| 十六进制 | #ff0000, #f00 |
| RGB | rgb(255, 0, 0) |
| RGBA | rgba(255, 0, 0, 0.5) |
| HSL | hsl(0, 100%, 50%) |
| currentColor | 继承当前文本颜色 |
渐变
线性渐变
<defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" stop-color="#e74c3c"/>
<stop offset="100%" stop-color="#3498db"/>
</linearGradient>
</defs>
<rect fill="url(#grad)"/>
径向渐变
<defs>
<radialGradient id="grad" cx="50%" cy="50%" r="50%">
<stop offset="0%" stop-color="#fff"/>
<stop offset="100%" stop-color="#3498db"/>
</radialGradient>
</defs>
文本
<text x="50" y="50" font-family="Arial" font-size="16" fill="#000">
文本内容
<tspan fill="#e74c3c">变色文字</tspan>
</text>
| 属性 | 说明 |
|---|---|
| x, y | 文本位置 |
| text-anchor | 水平对齐:start/middle/end |
| dominant-baseline | 垂直对齐 |
| font-family | 字体 |
| font-size | 字号 |
| font-weight | 粗细 |
| font-style | 样式 |
| letter-spacing | 字母间距 |
变换
<rect transform="translate(10, 20) rotate(45) scale(1.5)"/>
| 变换 | 参数 | 说明 |
|---|---|---|
| translate | tx [ty] | 平移 |
| rotate | angle [cx cy] | 旋转 |
| scale | sx [sy] | 缩放 |
| skewX | angle | x 轴斜切 |
| skewY | angle | y 轴斜切 |
| matrix | a b c d e f | 矩阵变换 |
动画
animate
<circle cx="50" cy="50" r="20">
<animate attributeName="r" from="20" to="40" dur="1s" repeatCount="indefinite"/>
</circle>
animateTransform
<rect transform="rotate(0)">
<animateTransform attributeName="transform" type="rotate"
from="0 50 50" to="360 50 50" dur="2s" repeatCount="indefinite"/>
</rect>
animateMotion
<circle r="10">
<animateMotion path="M 0 0 Q 100 -50 200 0" dur="2s" repeatCount="indefinite"/>
</circle>
动画属性
| 属性 | 说明 |
|---|---|
| attributeName | 动画属性名 |
| from/to | 起始/结束值 |
| values | 关键帧值列表 |
| dur | 持续时间 |
| begin | 开始时间/触发条件 |
| repeatCount | 重复次数 |
| fill | 结束状态:freeze/remove |
滤镜
<defs>
<filter id="blur">
<feGaussianBlur stdDeviation="3"/>
</filter>
<filter id="shadow">
<feDropShadow dx="3" dy="3" stdDeviation="3"/>
</filter>
</defs>
<rect filter="url(#blur)"/>
常用滤镜图元
| 图元 | 功能 |
|---|---|
| feGaussianBlur | 高斯模糊 |
| feDropShadow | 投影 |
| feColorMatrix | 颜色矩阵 |
| feOffset | 位移 |
| feMerge | 合并 |
| feTurbulence | 噪声 |
| feDisplacementMap | 位移图 |
裁剪与遮罩
裁剪
<defs>
<clipPath id="clip">
<circle cx="50" cy="50" r="40"/>
</clipPath>
</defs>
<rect width="100" height="100" clip-path="url(#clip)"/>
遮罩
<defs>
<mask id="mask">
<rect width="100" height="100" fill="white"/>
<circle cx="50" cy="50" r="30" fill="black"/>
</mask>
</defs>
<rect width="100" height="100" mask="url(#mask)"/>
JavaScript 操作
获取元素
const svg = document.getElementById('mySvg');
const circle = svg.querySelector('circle');
创建元素
const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttribute('x', '10');
svg.appendChild(rect);
修改属性
circle.setAttribute('fill', '#e74c3c');
circle.style.fill = '#e74c3c';
事件处理
circle.addEventListener('click', function(e) {
console.log('clicked');
});
常用单位
| 单位 | 说明 |
|---|---|
| 无单位 | 像素(默认) |
| px | 像素 |
| em | 相对于字体大小 |
| % | 百分比 |
| pt | 点(1pt = 1/72 inch) |
| cm, mm | 厘米、毫米 |
viewBox 计算公式
scaleX = width / viewBox.width
scaleY = height / viewBox.height
preserveAspectRatio 值
| 值 | 效果 |
|---|---|
| none | 拉伸填满 |
| xMinYMin | 左上对齐 |
| xMidYMid | 居中对齐 |
| xMaxYMax | 右下对齐 |
完整格式:[align] [meet|slice]
- meet:保持比例,可能有空白
- slice:保持比例,可能裁剪