SVG (Scalable Vector Graphics) 是基於 XML 的矢量圖形格式,W3C 標準,天然支持無限縮放、DOM 操作、事件綁定、CSS 樣式。本文介紹 SVG 核心概念、與 Canvas 關鍵差異、基礎繪製語法與選型建議。
| 維度 | SVG | Canvas |
|---|---|---|
| 本質 | 保留模式(場景圖/ DOM 树) | 即時模式(位圖/像素緩衝區) |
| 縮放 | 無損縮放 | 依賴分辨率,放大模糊 |
| 事件 | 原生支持元素級事件 | 需自行計算座標碰撞檢測 |
| 性能 | 元素多(>1000)時變慢 | 像素級操作,適合大量對象/動畫 |
| 文本 | 優秀,可選中、可搜索、可訪問 | 弱,純像素渲染 |
| 導出 | 原生 XML,易編輯 | toDataURL 導出位圖 |
| 適用場景 | 圖標、圖表、地圖、UI 組件、打印 | 遊戲、粒子特效、圖像處理、視頻特效 |
<svg width="300" height="200" viewBox="0 0 300 200" xmlns="https://www.w3.org/2000/svg">
<!-- 圓形 -->
<circle cx="150" cy="100" r="80" fill="#ff6b6b" stroke="#333" stroke-width="3" />
<!-- 矩形 -->
<rect x="50" y="50" width="100" height="80" fill="#4ecdc4" rx="10" ry="10" />
<!-- 線條 -->
<line x1="0" y1="200" x2="300" y2="0" stroke="#45b7d1" stroke-width="2" stroke-dasharray="5,5" />
<!-- 文本 -->
<text x="150" y="180" text-anchor="middle" font-size="20" fill="#fff">Hello SVG</text>
<!-- 路徑(核心) -->
<path d="M10 10 Q150 10 290 10" stroke="#f9ca24" stroke-width="3" fill="none" />
</svg>
| 元素 | 關鍵屬性 |
|---|---|
<rect> | x, y, width, height, rx, ry (圓角) |
<circle> | cx, cy, r |
<ellipse> | cx, cy, rx, ry |
<line> | x1, y1, x2, y2 |
<polyline> | points="x1,y1 x2,y2 ..." |
<polygon> | points="x1,y1 x2,y2 ..."(自動閉合) |
<path> | d="M... L... C... Z"(最強大) |
<text> | x, y, text-anchor, dominant-baseline |
M x y // moveTo (絕對)
m dx dy // moveTo (相對)
L x y // lineTo
l dx dy // lineTo 相對
H x // 水平線到 x
V y // 垂直線到 y
Z / z // closePath
Q x1 y1 x y // 二次貝塞爾 (絕對)
q dx1 dy1 dx dy // 相對
T x y // 平滑二次貝塞爾
C x1 y1 x2 y2 x y // 三次貝塞爾
S x2 y2 x y // 平滑三次貝塞爾
A rx ry rot large sweep x y // 橢圓弧
<g id="group1" fill="red" stroke="black"> <!-- 群組,屬性繼承 -->
<rect x="10" y="10" width="50" height="50" />
<circle cx="100" cy="35" r="25" />
</g>
<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>
<filter id="blur"><feGaussianBlur stdDeviation="5" /></filter>
<pattern id="dots" patternUnits="userSpaceOnUse" width="10" height="10">
<circle cx="5" cy="5" r="2" fill="white" />
</pattern>
</defs>
<rect width="100" height="100" fill="url(#grad1)" filter="url(#blur)" />
<circle cx="200" cy="50" r="40" fill="url(#dots)" />
<animate>、<animateTransform>(Chrome 已棄用,建議用 CSS/JS)@keyframes 作用於 SVG 屬性(fill, stroke, transform 等)element.setAttribute()、getBBox()、getCTM() 矩陣變換click、mouseenter、touchstart 直接綁定在元素上| 方式 | 優點 | 缺點 |
|---|---|---|
內聯 <svg>...</svg> | DOM 可操作、CSS 可樣式化、無額外請求、可緩存 HTML | HTML 體積大、難以複用 |
<img src="icon.svg"> | 簡單、緩存友好 | 無法 JS/CSS 操作內部、無法動畫 |
background: url(icon.svg) | CSS 管理 | 同 img 限制 |
<object data="icon.svg" type="image/svg+xml"> | 可獲取 contentDocument 操作內部 | 較重、跨域受限 |
Symbol + <use> (SVG Sprite) | 統一管理圖標、單請求、CSS 可控、語義化 | 需構建工具、IE 需 polyfill |
<!-- sprite.svg -->
<svg xmlns="https://www.w3.org/2000/svg" style="display:none">
<symbol id="icon-home" viewBox="0 0 24 24"><path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></symbol>
<symbol id="icon-user" viewBox="0 0 24 24"><path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/></symbol>
</svg>
<!-- 使用 -->
<svg class="icon"><use href="sprite.svg#icon-home" /></svg>
<svg class="icon"><use href="sprite.svg#icon-user" /></svg>
<style>
.icon { width: 24px; height: 24px; fill: currentColor; }
.icon:hover { fill: #007bff; }
</style>
選 SVG 當: 圖標、Logo、圖表、地圖、流程圖、需要交互/選中/無障礙/打印、元素數量可控(<1000)
選 Canvas 當: 遊戲、粒子系統、圖像編輯器、視頻特效、大數據點可視化(>10k)、高性能動畫、像素級操作
混合用 當: SVG 做 UI/圖表,Canvas 做背景特效/高性能層;或用
<foreignObject>在 SVG 嵌入 Canvas
SVG 與 Canvas 不是非此即彼的對立關係,而是互補的圖形技術棧。理解兩者本質差異,根據場景選擇或組合,才是前端圖形開發的正確姿勢。