SVG(可縮放向量圖形)與我們熟悉的 JPG、PNG、GIF 這類點陣(Raster)圖故有本質上的差異。它是一種基於 XML 的向量圖形技術,因為是「向量」而非「像素」,所以無論放大多少倍都不會失真。本文帶你認識 SVG,並透過 JavaScript 演示如何用它的 API 編程繪圖。
SVG 是 1999 年由 W3C 發布的 2D 圖形描述語言,純基於 XML 格式的標記語言。它是一個完整的 2D 圖形開發平台,包含兩大部分:一是基於 XML 語言的數據描述,二是可編程的 API。
SVG 的關鍵特性相當豐富:支持圖形、文本、梯度填充、畫筆風格,以及高斯模糊等圖形特效濾鏡,同時還支持各種滑鼠事件與 DOM API。幾乎所有主流瀏覽器都支持 SVG 的顯示與繪製,IE9 以上也開始原生支持,較舊的 IE 則需外掛插件。
建立並取得 SVG 對象:
// create svg object
var mySvg = document.createElementNS("https://www.w3.org/2000/svg","svg");
mySvg.setAttribute("version","1.2");// IE9+ support SVG 1.1 version
mySvg.setAttribute("baseProfile","tiny");
container.appendChild(mySvg);
建立一個矩形圖形:
var c1 = document.createElementNS("https://www.w3.org/2000/svg","rect");
c1.setAttribute("x","20");
c1.setAttribute("y","20");
c1.setAttribute("width","150");
c1.setAttribute("height","150");
c1.setAttribute("fill","rgb(0,0,255)");
c1.setAttribute("stroke","rgb(0,0,0)");
c1.setAttribute("stroke-width","4");
mySvg.appendChild(c1);
繪製文本:
var stext = document.createElementNS("https://www.w3.org/2000/svg","text");
stext.setAttribute("x","700");
stext.setAttribute("y","100");
stext.setAttribute("font-size","18px");
stext.setAttribute("fill","#FF0000");
var textString = document.createTextNode("Hello SVG");
stext.appendChild(textString);
mySvg.appendChild(stext);
為圖形綁定滑鼠事件:
c1.addEventListener("click",changeColor,false);
c2.addEventListener("mouseup", changeColor,false);
透過濾鏡實現高斯模糊:
<div id="blur-image-demo"> <div id="left" style="width:20%;"><img src="woniu.png" alt="Original image" width="325" height="471"></div> <div id="right" style="width:80%;"> <svg xmlns="https://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="f1" 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(#f1)"/> </svg> </div> </div>
運行效果:

以下是可直接複製運行的完整源代碼。JavaScript 部分:
window.onload = function() {
var container = document.getElementById("svgContainer");
var mySvg = document.createElementNS("https://www.w3.org/2000/svg", "svg");
mySvg.setAttribute("version", "1.2");
mySvg.setAttribute("baseProfile", "tiny");
container.appendChild(mySvg);
// 矩形
var c1 = document.createElementNS("https://www.w3.org/2000/svg", "rect");
c1.setAttribute("x", "20");
c1.setAttribute("y", "20");
c1.setAttribute("width", "150");
c1.setAttribute("height", "150");
c1.setAttribute("fill", "rgb(0,0,255)");
c1.setAttribute("stroke", "rgb(0,0,0)");
c1.setAttribute("stroke-width", "4");
mySvg.appendChild(c1);
// 圓形
var c2 = document.createElementNS("https://www.w3.org/2000/svg", "circle");
c2.setAttribute("cx", "250");
c2.setAttribute("cy", "100");
c2.setAttribute("r", "60");
c2.setAttribute("fill", "#996699");
c2.setAttribute("stroke", "#AA99FF");
c2.setAttribute("stroke-width", "7");
mySvg.appendChild(c2);
// 橢圓
var c3 = document.createElementNS("https://www.w3.org/2000/svg", "ellipse");
c3.setAttribute("cx", "450");
c3.setAttribute("cy", "100");
c3.setAttribute("rx", "100");
c3.setAttribute("ry", "50");
c3.setAttribute("fill", "#FF0000");
c3.setAttribute("stroke", "purple");
c3.setAttribute("stroke-width", "3");
mySvg.appendChild(c3);
// 線條
for(var i=0; i<10; i++)
{
var sline = document.createElementNS("https://www.w3.org/2000/svg", "line");
var x1 = 580 + i*10;
sline.setAttribute("x1", x1.toString());
sline.setAttribute("y1", "10");
sline.setAttribute("x2", x1.toString());
sline.setAttribute("y2", "180");
sline.setAttribute("stroke", "rgb(0,255,0)");
sline.setAttribute("stroke-width", "2");
mySvg.appendChild(sline);
}
// 文本
var stext = document.createElementNS("https://www.w3.org/2000/svg", "text");
stext.setAttribute("x", "700");
stext.setAttribute("y", "100");
stext.setAttribute("font-size", "18px");
stext.setAttribute("fill", "#FF0000");
var textString = document.createTextNode("Hello SVG");
stext.appendChild(textString);
mySvg.appendChild(stext);
// 滑鼠事件
c1.addEventListener("click", changeColor, false);
c2.addEventListener("mouseup", changeColor, false);
};
function changeColor(evt) {
var target = evt.target;
target.setAttributeNS(null, "fill", "green");
}
HTML 部分:
<html>
<head>
<title>Gloomyfish SVG Demo</title>
<style>
#svgContainer {
width:800px;
height:200px;
background-color:#EEEEEE;
}
#left { float: left;}
#right { float: right;}
</style>
</head>
<body>
<div id="svgContainer"></div>
<div id="blur-image-demo">
<div id="left" style="width:20%;"><img src="woniu.png" alt="Original image" width="325" height="471"></div>
<div id="right" style="width:80%;">
<svg xmlns="https://www.w3.org/2000/svg" version="1.1">
<defs>
<filter id="f1" 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(#f1)"/>
</svg>
</div>
</div>
</body>
</html>
從程式碼可以歸納出 SVG 編程的關鍵:所有元素都要用 createElementNS 搭配 SVG 的命名空間來建立,並用 setAttribute 設定各項屬性(座標、尺寸、顏色、線條等),最後 appendChild 掛到 svg 容器上。而像 feGaussianBlur 這類濾鏡,則是先定義在 defs 中,再以 url(#f1) 的方式套用,展現了 SVG 強大的圖形特效能力。