萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

SVG 入門與 JavaScript API 繪圖實戰指南

什麼是 SVG

SVG (Scalable Vector Graphics) 是基於 XML 的 2D 矢量圖形標記語言,由 W3C 標準化。與位圖 (JPG/PNG/GIF) 不同,SVG 由幾何圖形、路徑、文本組成,無限縮放不失真,檔案體積小,可被腳本操作、CSS 樣式化、支持動畫與濾鏡效果。

  • 瀏覽器支持:IE9+、所有現代瀏覽器完整支持。
  • 核心特性:形狀、文本、漸變、濾鏡(高斯模糊等)、鼠標事件、DOM API。

JavaScript 動態創建 SVG

命名空間關鍵點

SVG 元素必須在 https://www.w3.org/2000/svg 命名空間下創建:

const NS = "https://www.w3.org/2000/svg";
const svg = document.createElementNS(NS, "svg");
svg.setAttribute("width", "800");
svg.setAttribute("height", "200");
svg.setAttribute("version", "1.1");
document.getElementById("container").appendChild(svg);

繪製基本圖形

// 矩形
const rect = document.createElementNS(NS, "rect");
rect.setAttribute("x", "20");
rect.setAttribute("y", "20");
rect.setAttribute("width", "150");
rect.setAttribute("height", "150");
rect.setAttribute("fill", "rgb(0,0,255)");
rect.setAttribute("stroke", "black");
rect.setAttribute("stroke-width", "4");
svg.appendChild(rect);

// 圓形
const circle = document.createElementNS(NS, "circle");
circle.setAttribute("cx", "250");
circle.setAttribute("cy", "100");
circle.setAttribute("r", "60");
circle.setAttribute("fill", "#996699");
svg.appendChild(circle);

// 橢圓
const ellipse = document.createElementNS(NS, "ellipse");
ellipse.setAttribute("cx", "450");
ellipse.setAttribute("cy", "100");
ellipse.setAttribute("rx", "100");
ellipse.setAttribute("ry", "50");
ellipse.setAttribute("fill", "red");
svg.appendChild(ellipse);

// 直線循環
for (let i = 0; i < 10; i++) {
  const line = document.createElementNS(NS, "line");
  const x = 580 + i * 10;
  line.setAttribute("x1", x);
  line.setAttribute("y1", "10");
  line.setAttribute("x2", x);
  line.setAttribute("y2", "180");
  line.setAttribute("stroke", "lime");
  line.setAttribute("stroke-width", "2");
  svg.appendChild(line);
}

文本繪製

const text = document.createElementNS(NS, "text");
text.setAttribute("x", "700");
text.setAttribute("y", "100");
text.setAttribute("font-size", "18px");
text.setAttribute("fill", "#FF0000");
text.textContent = "Hello SVG"; // 等同於 createTextNode + appendChild
svg.appendChild(text);

事件處理

function changeColor(evt) {
  evt.target.setAttributeNS(null, "fill", "green");
}
rect.addEventListener("click", changeColor);
circle.addEventListener("mouseup", changeColor);

SVG 濾鏡:高斯模糊示例

<div id="blur-demo">
  <div style="width:20%; display:inline-block;">
    <img src="woniu.png" alt="原圖" width="325" height="471">
  </div>
  <div style="width:75%; display:inline-block;">
    <svg xmlns="https://www.w3.org/2000/svg" version="1.1">
      <defs>
        <filter id="blur" x="0" y="0">
          <feGaussianBlur in="SourceGraphic" stdDeviation="5" />
        </filter>
      </defs>
      <image x="0" y="0" width="325" height="471" xlink:href="woniu.png" filter="url(#blur)" />
    </svg>
  </div>
</div>

完整頁面級示例

見原文 window.onload 完整代碼,包含容器獲取、多圖形繪製、文本、事件綁定、顏色變化邏輯。

開發建議

  • 大量 DOM 操作時,使用 DocumentFragment 批量追加,減少重排。
  • 複雜圖形優先考慮 <path> 配合 d 屬性,或使用 SVG.js、D3.js 等庫提升效率。
  • CSS 可直接樣式化 SVG 內部元素(如 fill, stroke),分離關注點。
  • 導出圖片:canvas.drawImage(svgBlob) 或 svg.toDataURL()(需序列化)。
copyright © 萬盛學電腦網 all rights reserved