前置知识: HTML5

SVG

00:00
1 min Intermediate 2026/6/14

基本形状、viewBox、路径、文本、滤镜

1. SVG 基础

<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <circle cx="100" cy="100" r="80" fill="blue" />
</svg>

2. viewBox 详解

viewBox="min-x min-y width height" 定义 SVG 的内部坐标系统

<svg width="400" height="300" viewBox="0 0 200 150">
  <rect x="0" y="0" width="100" height="75" fill="blue" />
</svg>
preserveAspectRatio 说明
xMidYMid meet居中,完整显示(默认)
xMidYMid slice居中,裁剪填充
none不保持比例

3. 基本形状

<svg width="400" height="300">
  <rect x="10" y="10" width="100" height="60" rx="10" fill="blue" />
  <circle cx="200" cy="80" r="50" fill="red" />
  <ellipse cx="320" cy="80" rx="60" ry="30" fill="green" />
  <line x1="10" y1="150" x2="390" y2="150" stroke="black" />
  <polyline points="10,180 50,160 90,200" fill="none" stroke="purple" />
  <polygon points="200,180 240,220 160,220" fill="orange" />
</svg>

4. 路径 path

命令说明示例
M移动M 10 10
L直线到L 100 100
C三次贝塞尔C 20,20 40,20 50,10
Q二次贝塞尔Q 50,0 100,50
A弧线A 25,25 0 0,1 50,25
Z闭合路径Z

小写母为相对坐标,大写母为绝坐标。

5. 文本

<svg>
  <text x="20" y="50" font-size="24" fill="black">Hello SVG</text>
  <defs><path id="curve" d="M 50 150 Q 200 50, 350 150" /></defs>
  <text font-size="20"><textPath href="#curve">沿曲线排列的文字</textPath></text>
</svg>

6. 滤镜与渐变

<svg>
  <defs>
    <filter id="blur"><feGaussianBlur stdDeviation="5" /></filter>
    <filter id="shadow"><feDropShadow dx="4" dy="4" stdDeviation="3" /></filter>
    <linearGradient id="lg" x1="0%" x2="100%">
      <stop offset="0%" stop-color="red" />
      <stop offset="100%" stop-color="blue" />
    </linearGradient>
  </defs>
  <rect x="50" y="50" width="100" height="80" fill="url(#lg)" filter="url(#shadow)" />
</svg>

知识检测

学习进度

-- 已学文档
--% 知识覆盖率

学习推荐

专注模式