svg基本图形绘制介绍

一、什么是 SVG

SVG(Scalable Vector Graphics)是一种可缩放矢量图形格式,与位图(如 PNG、JPG)不同,SVG 是用 XML 代码描述图形的,因此可以无限放大而不失真。

SVG 的优势

特性 说明
无限缩放 放大任意倍数都不会模糊
体积小 代码描述通常比位图文件小
可编辑 可以用文本编辑器直接修改
可动画 支持 CSS 和 SMIL 动画
可交互 支持 JavaScript 交互

二、SVG 基础语法

2.1 SVG 画布

svg 复制代码
<svg width="200" height="200" style="background: lightgray">
  <!-- 图形元素放在这里 -->
</svg>

注意事项:

  • widthheight 定义画布大小
  • SVG 的坐标原点 (0, 0)左上角
  • x 轴向右为正,y 轴向下为正

2.2 基本图形元素

SVG 提供了多种预定义的图形元素:

元素 用途 示例
<rect> 矩形 <rect x="10" y="10" width="100" height="50" />
<circle> 圆形 <circle cx="100" cy="100" r="50" />
<ellipse> 椭圆 <ellipse cx="100" cy="100" rx="80" ry="50" />
<line> 线条 <line x1="10" y1="10" x2="100" y2="100" />
<path> 路径 <path d="M10 10 L100 100" />

2.3 常用属性

svg 复制代码
<rect 
  x="10"       <!-- 左上角 x 坐标 -->
  y="10"       <!-- 左上角 y 坐标 -->
  width="100"  <!-- 宽度 -->
  height="50"  <!-- 高度 -->
  fill="blue"  <!-- 填充颜色 -->
  stroke="red" <!-- 边框颜色 -->
  stroke-width="2" <!-- 边框宽度 -->
  opacity="0.8" <!-- 透明度 0-1 -->
/>

三、核心图形详解

3.1 矩形(rect)

绘制矩形使用 <rect> 标签。

涉及属性:

属性 作用
x 左上角距离画布左边缘的距离 数值
y 左上角距离画布上边缘的距离 数值
width 矩形宽度 数值
height 矩形高度 数值
fill 填充颜色 颜色名、十六进制、rgb() 等
rx 水平方向的圆角半径 数值
ry 垂直方向的圆角半径 数值

示例:

svg 复制代码
<svg width="200" height="200" style="background: lightgray">
  <rect x="20" y="20" width="80" height="60" fill="blue" />
  <rect x="120" y="20" width="60" height="60" rx="10" fill="green" />
</svg>

3.2 圆形(circle)

绘制圆形使用 <circle> 标签。

涉及属性:

属性 作用 默认值
cx 圆心的 x 坐标 0
cy 圆心的 y 坐标 0
r 圆的半径 必填
fill 填充颜色 黑色

注意: 如果不设置 cxcy,圆心默认在 (0, 0),即画布左上角。

示例:

svg 复制代码
<svg width="200" height="200" style="background: lightgray">
  <circle cx="100" cy="100" r="80" fill="red" />
  <circle cx="100" cy="100" r="50" fill="none" stroke="black" stroke-width="3" />
</svg>

3.3 线条(line)

绘制线条使用 <line> 标签。

涉及属性:

属性 作用 备注
x1 起点 x 坐标 必填
y1 起点 y 坐标 必填
x2 终点 x 坐标 必填
y2 终点 y 坐标 必填
stroke 线条颜色 必须设置,否则线条不可见
stroke-width 线条宽度 默认 1

重要提醒: stroke 属性必须设置,否则线条不可见!

示例:

svg 复制代码
<svg width="400" height="200" style="background: lightgray">
  <line x1="50" y1="100" x2="350" y2="100" stroke="blue" stroke-width="3" />
</svg>

3.4 路径(path)------ 最强大的元素

路径是 SVG 中最强大的元素,可以绘制任意形状。核心是 d 属性中的命令序列。

常用路径命令:

命令 说明 示例
M x,y 移动到(Move to) M 10,10 将画笔移到 (10,10)
L x,y 画直线到(Line to) L 100,100 画到 (100,100)
H x 水平线(Horizontal) H 200 画到 x=200
V y 垂直线(Vertical) V 150 画到 y=150
Z 闭合路径(Close) 自动首尾相连

坐标类型:

  • 大写字母:绝对坐标(相对于画布原点)
  • 小写字母:相对坐标(相对于当前位置)

示例:

svg 复制代码
<svg width="300" height="200" style="background: lightgray">
  <path d="M 50,100 L 150,50 L 250,100 L 150,150 Z" fill="yellow" stroke="black" stroke-width="2" />
</svg>

四、样式与美化

4.1 描边样式

涉及属性:

属性 作用
stroke 描边颜色 颜色名、十六进制等
stroke-width 描边宽度 数值(无单位默认 px)
stroke-linecap 线帽样式 buttroundsquare
stroke-linejoin 拐角样式 miterroundbevel

stroke-linecap 三种值:

效果 说明
butt 无线帽 默认,平切
round 圆形线帽 两端半圆
square 方形线帽 两端延伸半个宽度

stroke-linejoin 三种值:

效果 说明
miter 尖角 默认,尖锐
round 圆角 圆滑
bevel 斜角 平切

示例:

svg 复制代码
<svg width="400" height="150" style="background: lightgray">
  <line x1="20" y1="50" x2="120" y2="50" stroke="black" stroke-width="10" stroke-linecap="butt" />
  <line x1="150" y1="50" x2="250" y2="50" stroke="black" stroke-width="10" stroke-linecap="round" />
  <line x1="280" y1="50" x2="380" y2="50" stroke="black" stroke-width="10" stroke-linecap="square" />
</svg>

4.2 虚线效果

涉及属性:

属性 作用
stroke-dasharray 虚线图案 线段和间隙的长度序列
stroke-dashoffset 虚线偏移 数值,向前移动

stroke-dasharray 用法:

  • stroke-dasharray="10 5" → 10px 线段,5px 间隙
  • stroke-dasharray="15 5 5 5" → 15px 线,5px 间隙,5px 线,5px 间隙...

示例:

svg 复制代码
<svg width="300" height="100" style="background: lightgray">
  <line x1="20" y1="50" x2="280" y2="50" stroke="black" stroke-width="2" stroke-dasharray="10 5" />
  <line x1="20" y1="80" x2="280" y2="80" stroke="red" stroke-width="2" stroke-dasharray="15 5 5 5" />
</svg>

4.3 渐变效果

渐变需要定义在 <defs> 标签内,然后通过 url(#id) 引用。

线性渐变

涉及属性:

属性 作用
id 渐变唯一标识
x1y1 渐变起点坐标
x2y2 渐变终点坐标
<stop> 渐变颜色节点
offset 颜色位置(0%-100%)
stop-color 节点颜色

示例:

svg 复制代码
<svg width="300" height="200" style="background: lightgray">
  <defs>
    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
      <stop offset="0%" stop-color="red" />
      <stop offset="100%" stop-color="blue" />
    </linearGradient>
  </defs>
  <rect x="50" y="50" width="200" height="100" fill="url(#grad1)" />
</svg>
径向渐变

涉及属性:

属性 作用 默认值
cxcy 渐变中心坐标 50%
r 渐变半径 50%
fxfy 渐变焦点坐标 与中心重合

示例:

svg 复制代码
<svg width="300" height="200" style="background: lightgray">
  <defs>
    <radialGradient id="grad2" cx="50%" cy="50%" r="50%">
      <stop offset="0%" stop-color="white" />
      <stop offset="100%" stop-color="green" />
    </radialGradient>
  </defs>
  <circle cx="150" cy="100" r="60" fill="url(#grad2)" />
</svg>

相关推荐
aa小小21 分钟前
localhost 访问异常排查笔记
前端
格子软件22 分钟前
2026年GEO优化系统源码的分布式状态机深度拆解
java·前端·vue.js·vue·geo
陈随易40 分钟前
Rust、Golang、MoonBit 编译成 WASM,体积和速度差距有多大?
前端·后端·程序员
IT_陈寒44 分钟前
Python多线程的坑,我居然现在才踩到
前端·人工智能·后端
触底反弹2 小时前
🔥 字符串算法面试三连击:反转、回文、回文变种,搞懂这三题稳了!
前端·javascript·算法
竹林8182 小时前
从 RPC 超时到批量签名:我用 @solana/web3.js 重构了一个 NFT 铸造页面,踩了这些坑
前端·javascript
工业HMI实战笔记2 小时前
工业HMI界面布局“1核2辅”黄金结构,适配90%场景
前端·ui·性能优化·自动化·交互
林希_Rachel_傻希希3 小时前
web性能优化之————图片效果
前端·javascript·面试
Darling噜啦啦3 小时前
前端存储与 this 指向完全指南:从 LocalStorage 实战到 call/apply/bind 深度解析
前端·javascript
wei1986213 小时前
.net添加web引用和添加服务引用有什么区别?
java·前端·.net