SVG 是一種基於 XML 語法的圖像格式,全稱是可縮放矢量圖(Scalable Vector Graphics)。其他圖像格式都是基於像素處理的,SVG 則是屬於對圖像的形狀描述,所以它本質上是文本文件,體積較小,且不管放大多少倍都不會失真。
SVG 文件可以直接插入網頁,成爲 DOM 的一部分,然後用 JavaScript 和 CSS 進行操作。
<!DOCTYPE html>
<html>
<head></head>
<body>
<svg
id="mysvg"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 800 600"
preserveAspectRatio="xMidYMid meet"<circle id="mycircle" cx="400" cy="300" r="50" />
<svg>
</body>
</html>
上面是 SVG 代碼直接插入網頁的例子。
SVG 代碼也可以寫在一個獨立文件中,然後用<img>、<object>、<embed>、<iframe>等標籤插入網頁。
<img src="circle.svg">
<object id="object" data="circle.svg" type="image/svg+xml"></object>
<embed id="embed" src="icon.svg" type="image/svg+xml">
<iframe id="iframe" src="icon.svg"></iframe>
CSS 也可以使用 SVG 文件。
.logo {
background: url(icon.svg);
}
SVG 文件還可以轉爲 BASE64 編碼,然後作爲 Data URI 寫入網頁。
<img src="data:image/svg+xml;base64,[data]">
SVG 代碼都放在頂層標籤<svg>之中。下面是一個例子。
<svg width="100%" height="100%">
<circle id="mycircle" cx="50" cy="50" r="50" />
</svg>
<svg>的width屬性和height屬性,指定了 SVG 圖像在 HTML 元素中所佔據的寬度和高度。除了相對單位,也可以採用絕對單位(單位:像素)。如果不指定這兩個屬性,SVG 圖像默認大小是300像素(寬) x 150像素(高)。
如果只想展示 SVG 圖像的一部分,就要指定viewBox屬性。
<svg width="100" height="100" viewBox="50 50 50 50">
<circle id="mycircle" cx="50" cy="50" r="50" />
</svg>
<viewBox>屬性的值有四個數字,分別是左上角的橫座標和縱座標、視口的寬度和高度。上面代碼中,SVG 圖像是100像素寬 x 100像素高,viewBox屬性指定視口從(50, 50)這個點開始。所以,實際看到的是右下角的四分之一圓。
注意,視口必須適配所在的空間。上面代碼中,視口的大小是 50 x 50,由於 SVG 圖像的大小是 100 x 100,所以視口會放大去適配 SVG 圖像的大小,即放大了四倍。
如果不指定width屬性和height屬性,只指定viewBox屬性,則相當於只給定 SVG 圖像的長寬比。這時,SVG 圖像的默認大小將等於所在的 HTML 元素的大小。
<circle>標籤代表圓形。
<svg width="300" height="180">
<circle cx="30" cy="50" r="25" />
<circle cx="90" cy="50" r="25" class="red" />
<circle cx="150" cy="50" r="25" class="fancy" />
</svg>
上面的代碼定義了三個圓。<circle>標籤的cx、cy、r屬性分別爲橫座標、縱座標和半徑,單位爲像素。座標都是相對於<svg>畫布的左上角原點。
class屬性用來指定對應的 CSS 類。
.red {
fill: red;
}.fancy {
fill: none;
stroke: black;
stroke-width: 3pt;
}
SVG 的 CSS 屬性與網頁元素有所不同。
- fill:填充色
- stroke:描邊色
- stroke-width:邊框寬度
<line>標籤用來繪製直線。
<svg width="300" height="180">
<line x1="0" y1="0" x2="200" y2="0" style="stroke:rgb(0,0,0);stroke-width:5" />
</svg>
上面代碼中,<line>標籤的x1屬性和y1屬性,表示線段起點的橫座標和縱座標;x2屬性和y2屬性,表示線段終點的橫座標和縱座標;style屬性表示線段的樣式。
<polyline>標籤用於繪製一根折線。
<svg width="300" height="180">
<polyline points="3,3 30,28 3,53" fill="none" stroke="black" />
</svg>
<polyline>的points屬性指定了每個端點的座標,橫座標與縱座標之間與逗號分隔,點與點之間用空格分隔。
<rect>標籤用於繪製矩形。
<svg width="300" height="180">
<rect x="0" y="0" height="100" width="200" style="stroke: #70d5dd; fill: #dd524b" />
</svg>
<rect>的x屬性和y屬性,指定了矩形左上角端點的橫座標和縱座標;width屬性和height屬性指定了矩形的寬度和高度(單位像素)。
<ellipse>標籤用於繪製橢圓。
<svg width="300" height="180">
<ellipse cx="60" cy="60" ry="40" rx="20" stroke="black" stroke-width="5" fill="silver"/>
</svg>
<ellipse>的cx屬性和cy屬性,指定了橢圓中心的橫座標和縱座標(單位像素);rx屬性和ry屬性,指定了橢圓橫向軸和縱向軸的半徑(單位像素)。
<polygon>標籤用於繪製多邊形。
<svg width="300" height="180">
<polygon fill="green" stroke="orange" stroke-width="1" points="0,0 100,0 100,100 0,100 0,0"/>
</svg>
<polygon>的points屬性指定了每個端點的座標,橫座標與縱座標之間與逗號分隔,點與點之間用空格分隔。
<path>標籤用於制路徑。
<svg width="300" height="180">
<path d="
M 18,3
L 46,3
L 46,40
L 61,40
L 32,68
L 3,40
L 18,40
Z
"></path>
</svg>
<path>的d屬性表示繪製順序,它的值是一個長字符串,每個字母表示一個繪製動作,後面跟着座標。
- M:移動到(moveto)
- L:畫直線到(lineto)
- Z:閉合路徑
<text>標籤用於繪製文本。
<svg width="300" height="180">
<text x="50" y="25">Hello World</text>
</svg>
<text>的x屬性和y屬性,表示文本區塊基線(baseline)起點的橫座標和縱座標。文字的樣式可以用class或style屬性指定。
<use>標籤用於複製一個形狀。
<svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
<circle id="myCircle" cx="5" cy="5" r="4"/><use href="#myCircle" x="10" y="0" fill="blue" />
<use href="#myCircle" x="20" y="0" fill="white" stroke="blue" />
</svg>
<use>的href屬性指定所要複製的節點,x屬性和y屬性是<use>左上角的座標。另外,還可以指定width和height座標。
<g>標籤用於將多個形狀組成一個組(group),方便複用。
<svg width="300" height="100">
<g id="myCircle">
<text x="25" y="20">圓形</text>
<circle cx="50" cy="50" r="20"/>
</g><use href="#myCircle" x="100" y="0" fill="blue" />
<use href="#myCircle" x="200" y="0" fill="white" stroke="blue" />
</svg>
<defs>標籤用於自定義形狀,它內部的代碼不會顯示,僅供引用。
<svg width="300" height="100">
<defs>
<g id="myCircle">
<text x="25" y="20">圓形</text>
<circle cx="50" cy="50" r="20"/>
</g>
</defs><use href="#myCircle" x="0" y="0" />
<use href="#myCircle" x="100" y="0" fill="blue" />
<use href="#myCircle" x="200" y="0" fill="white" stroke="blue" />
</svg>
<pattern>標籤用於自定義一個形狀,該形狀可以被引用來平鋪一個區域。
<svg width="500" height="500">
<defs>
<pattern id="dots" x="0" y="0" width="100" height="100" patternUnits="userSpaceOnUse">
<circle fill="#bee9e8" cx="50" cy="50" r="35" />
</pattern>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="url(#dots)" />
</svg>
上面代碼中,<pattern>標籤將一個圓形定義爲dots模式。patternUnits="userSpaceOnUse"表示<pattern>的寬度和長度是實際的像素值。然後,指定這個模式去填充下面的矩形。
<image>標籤用於插入圖片文件。
<svg viewBox="0 0 100 100" width="100" height="100">
<image xlink:href="path/to/image.jpg"
width="50%" height="50%"/>
</svg>
上面代碼中,<image>的xlink:href屬性表示圖像的來源。
<animate>標籤用於產生動畫效果。
<svg width="500px" height="500px">
<rect x="0" y="0" width="100" height="100" fill="#feac5e">
<animate attributeName="x" from="0" to="500" dur="2s" repeatCount="indefinite" />
</rect>
</svg>
上面代碼中,矩形會不斷移動,產生動畫效果。
<animate>的屬性含義如下。
- attributeName:發生動畫效果的屬性名。
- from:單次動畫的初始值。
- to:單次動畫的結束值。
- dur:單次動畫的持續時間。
- repeatCount:動畫的循環模式。
可以在多個屬性上面定義動畫。
<animate attributeName="x" from="0" to="500" dur="2s" repeatCount="indefinite" />
<animate attributeName="width" to="500" dur="2s" repeatCount="indefinite" />
<animate>標籤對 CSS 的transform屬性不起作用,如果需要變形,就要使用<animateTransform>標籤。
<svg width="500px" height="500px">
<rect x="250" y="250" width="50" height="50" fill="#4bc0c8">
<animateTransform attributeName="transform" type="rotate" begin="0s" dur="10s" from="0 200 200" to="360 400 400" repeatCount="indefinite" />
</rect>
</svg>
上面代碼中,<animateTransform>的效果爲旋轉(rotate),這時from和to屬性值有三個數字,第一個數字是角度值,第二個值和第三個值是旋轉中心的座標。from="0 200 200"表示開始時,角度爲0,圍繞(200, 200)開始旋轉;to="360 400 400"表示結束時,角度爲360,圍繞(400, 400)旋轉。
如果 SVG 代碼直接寫在 HTML 網頁之中,它就成爲網頁 DOM 的一部分,可以直接用 DOM 操作。
<svg
id="mysvg"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 800 600"
preserveAspectRatio="xMidYMid meet"<circle id="mycircle" cx="400" cy="300" r="50" />
<svg>
上面代碼插入網頁之後,就可以用 CSS 定製樣式。
circle {
stroke-width: 5;
stroke: #f00;
fill: #ff0;
}circle:hover {
stroke: #090;
fill: #fff;
}
然後,可以用 JavaScript 代碼操作 SVG。
var mycircle = document.getElementById('mycircle');
mycircle.addEventListener('click', function(e) {
console.log('circle clicked - enlarging');
mycircle.setAttribute('r', 60);
}, false);
上面代碼指定,如果點擊圖形,就改寫circle元素的r屬性。
使用<object>、<iframe>、<embed>標籤插入 SVG 文件,可以獲取 SVG DOM。
var svgObject = document.getElementById('object').contentDocument;
var svgIframe = document.getElementById('iframe').contentDocument;
var svgEmbed = document.getElementById('embed').getSVGDocument();
注意,如果使用<img>標籤插入 SVG 文件,就無法獲取 SVG DOM。
由於 SVG 文件就是一段 XML 文本,因此可以通過讀取 XML 代碼的方式,讀取 SVG 源碼。
<div id="svg-container">
<svg
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xml:space="preserve" width="500" height="440"<!-- svg code --></svg>
</div>
使用XMLSerializer實例的serializeToString()方法,獲取 SVG 元素的代碼。
var svgString = new XMLSerializer()
.serializeToString(document.querySelector('svg'));
首先,需要新建一個Image對象,將 SVG 圖像指定到該Image對象的src屬性。
var img = new Image();
var svg = new Blob([svgString], {type: "image/svg+xml;charset=utf-8"});var DOMURL = self.URL || self.webkitURL || self;
var url = DOMURL.createObjectURL(svg);img.src = url;
然後,當圖像加載完成後,再將它繪製到<canvas>元素。
img.onload = function () {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
};
下面將一張數據表格畫成折線圖。
Date Amount 2014-01-01 $10 2014-02-01 $20 2014-03-01 $40 2014-04-01 $80
上面的圖形,可以畫成一個座標系,Date作爲橫軸,Amount作爲縱軸,四行數據畫成一個數據點。
<svg width="350" height="160">
<g class="layer" transform="translate(60,10)">
<circle r="5" cx="0" cy="105" />
<circle r="5" cx="90" cy="90" />
<circle r="5" cx="180" cy="60" />
<circle r="5" cx="270" cy="0" /><g class="y axis"> <line x1="0" y1="0" x2="0" y2="120" /> <text x="-40" y="105" dy="5">$10</text> <text x="-40" y="0" dy="5">$80</text> </g> <g class="x axis" transform="translate(0, 120)"> <line x1="0" y1="0" x2="270" y2="0" /> <text x="-30" y="20">January 2014</text> <text x="240" y="20">April</text> </g></g>
</svg>