SVG是“可縮放矢量圖”(Scalable Vector Graphics)的縮寫,是一種描述向量圖形的XML格式的標記化語言。也就是說,SVG本質上是文本文件,格式采用XML,可以在瀏覽器中顯示出矢量圖像。由于結構是XML格式,使得它可以插入HTML文檔,成為DOM的一部分,然后用JavaScript和CSS進行操作。
相比傳統的圖像文件格式(比如JPG和PNG),SVG圖像的優勢就是文件體積小,并且放大多少倍都不會失真,因此非常合適用于網頁。
SVG圖像可以用Adobe公司的Illustrator軟件、開源軟件Inkscape等生成。目前,所有主流瀏覽器都支持,對于低于IE 9的瀏覽器,可以使用第三方的[polyfills函數庫](https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills#svg)。
[TOC]
## 插入SVG文件
SVG插入網頁的方法有多種,可以用在img、object、embed、iframe等標簽,以及CSS的background-image屬性。
~~~
<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>
~~~
上面是四種在網頁中插入SVG圖像的方式。
此外,SVG文件還可以插入其他DOM元素,比如div元素,請看下面的例子(使用了jQuery函數庫)。
~~~
<div id="stage"></div>
<script>
$("#stage").load('icon.svg',function(response){
$(this).addClass("svgLoaded");
if(!response){
// 加載失敗的處理代碼
}
});
</script>
~~~
## svg格式
SVG文件采用XML格式,就是普通的文本文件。
~~~
<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>
~~~
上面的svg文件,定義了三個圓,它們的cx、cy、r屬性分別為x坐標、y坐標和半徑。利用class屬性,可以為這些圓指定樣式。
~~~
.red {
fill: red; /* not background-color! */
}
.fancy {
fill: none;
stroke: black; /* similar to border-color */
stroke-width: 3pt; /* similar to border-width */
}
~~~
上面代碼中,fill屬性表示填充色,stroke屬性表示描邊色,stroke-width屬性表示邊線寬度。
除了circle標簽表示圓,SVG文件還可以使用表示其他形狀的標簽。
~~~
<svg>
<line x1="0" y1="0" x2="200" y2="0" style="stroke:rgb(0,0,0);stroke-width:1"/></line>
<rect x="0" y="0" height="100" width="200" style="stroke: #70d5dd; fill: #dd524b" />
<ellipse cx="60" cy="60" ry="40" rx="20" stroke="black" stroke-width="5" fill="silver"/></ellipse>
<polygon fill="green" stroke="orange" stroke-width="10" points="350, 75 379,161 469,161 397,215 423,301 350,250 277,301 303,215 231,161 321,161"/><polygon>
<path id="path1" d="M160.143,196c0,0,62.777-28.033,90-17.143c71.428,28.572,73.952-25.987,84.286-21.428" style="fill:none;stroke:2;"></path>
</svg>
~~~
上面代碼中,line、rect、ellipse、polygon和path標簽,分別表示線條、矩形、橢圓、多邊形和路徑。
g標簽用于將多個形狀組成一組,表示group。
~~~
<svg width="300" height="180">
<g transform="translate(5, 15)">
<text x="0" y="0">Howdy!</text>
<path d="M0,50 L50,0 Q100,0 100,50"
fill="none" stroke-width="3" stroke="black" />
</g>
</svg>
~~~
## SVG文件的JavaScript操作
### 獲取SVG DOM
如果使用img標簽插入SVG文件,則無法獲取SVG DOM。使用object、iframe、embed標簽,可以獲取SVG DOM。
~~~
var svgObject = document.getElementById("object").contentDocument;
var svgIframe = document.getElementById("iframe").contentDocument;
var svgEmbed = document.getElementById("embed").getSVGDocument();
~~~
由于svg文件就是一般的XML文件,因此可以用DOM方法,選取頁面元素。
~~~
// 改變填充色
document.getElementById("theCircle").style.fill = "red";
// 改變元素屬性
document.getElementById("theCircle").setAttribute("class", "changedColors");
// 綁定事件回調函數
document.getElementById("theCircle").addEventListener("click", function() {
console.log("clicked")
});
~~~
### 讀取svg源碼
由于svg文件就是一個XML代碼的文本文件,因此可以通過讀取XML代碼的方式,讀取svg源碼。
假定網頁中有一個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'));
~~~
### 將svg圖像轉為canvas圖像
首先,需要新建一個img對象,將svg圖像指定到該img對象的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>
~~~
## 參考鏈接
* Jon McPartland,?[An introduction to SVG animation](http://bigbitecreative.com/introduction-svg-animation/)
* Alexander Goedde,?[SVG - Super Vector Graphics](http://tavendo.com/blog/post/super-vector-graphics/)
* Joseph Wegner,?[Learning SVG](http://flippinawesome.org/2014/02/03/learning-svg/)
* biovisualize,?[Direct svg to canvas to png conversion](http://bl.ocks.org/biovisualize/8187844)
- 第一章 導論
- 1.1 前言
- 1.2 為什么學習JavaScript?
- 1.3 JavaScript的歷史
- 第二章 基本語法
- 2.1 語法概述
- 2.2 數值
- 2.3 字符串
- 2.4 對象
- 2.5 數組
- 2.6 函數
- 2.7 運算符
- 2.8 數據類型轉換
- 2.9 錯誤處理機制
- 2.10 JavaScript 編程風格
- 第三章 標準庫
- 3.1 Object對象
- 3.2 Array 對象
- 3.3 包裝對象和Boolean對象
- 3.4 Number對象
- 3.5 String對象
- 3.6 Math對象
- 3.7 Date對象
- 3.8 RegExp對象
- 3.9 JSON對象
- 3.10 ArrayBuffer:類型化數組
- 第四章 面向對象編程
- 4.1 概述
- 4.2 封裝
- 4.3 繼承
- 4.4 模塊化編程
- 第五章 DOM
- 5.1 Node節點
- 5.2 document節點
- 5.3 Element對象
- 5.4 Text節點和DocumentFragment節點
- 5.5 Event對象
- 5.6 CSS操作
- 5.7 Mutation Observer
- 第六章 瀏覽器對象
- 6.1 瀏覽器的JavaScript引擎
- 6.2 定時器
- 6.3 window對象
- 6.4 history對象
- 6.5 Ajax
- 6.6 同域限制和window.postMessage方法
- 6.7 Web Storage:瀏覽器端數據儲存機制
- 6.8 IndexedDB:瀏覽器端數據庫
- 6.9 Web Notifications API
- 6.10 Performance API
- 6.11 移動設備API
- 第七章 HTML網頁的API
- 7.1 HTML網頁元素
- 7.2 Canvas API
- 7.3 SVG 圖像
- 7.4 表單
- 7.5 文件和二進制數據的操作
- 7.6 Web Worker
- 7.7 SSE:服務器發送事件
- 7.8 Page Visibility API
- 7.9 Fullscreen API:全屏操作
- 7.10 Web Speech
- 7.11 requestAnimationFrame
- 7.12 WebSocket
- 7.13 WebRTC
- 7.14 Web Components
- 第八章 開發工具
- 8.1 console對象
- 8.2 PhantomJS
- 8.3 Bower:客戶端庫管理工具
- 8.4 Grunt:任務自動管理工具
- 8.5 Gulp:任務自動管理工具
- 8.6 Browserify:瀏覽器加載Node.js模塊
- 8.7 RequireJS和AMD規范
- 8.8 Source Map
- 8.9 JavaScript 程序測試
- 第九章 JavaScript高級語法
- 9.1 Promise對象
- 9.2 有限狀態機
- 9.3 MVC框架與Backbone.js
- 9.4 嚴格模式
- 9.5 ECMAScript 6 介紹
- 附錄
- 10.1 JavaScript API列表
- 草稿一:函數庫
- 11.1 Underscore.js
- 11.2 Modernizr
- 11.3 Datejs
- 11.4 D3.js
- 11.5 設計模式
- 11.6 排序算法
- 草稿二:jQuery
- 12.1 jQuery概述
- 12.2 jQuery工具方法
- 12.3 jQuery插件開發
- 12.4 jQuery.Deferred對象
- 12.5 如何做到 jQuery-free?
- 草稿三:Node.js
- 13.1 Node.js 概述
- 13.2 CommonJS規范
- 13.3 package.json文件
- 13.4 npm模塊管理器
- 13.5 fs 模塊
- 13.6 Path模塊
- 13.7 process對象
- 13.8 Buffer對象
- 13.9 Events模塊
- 13.10 stream接口
- 13.11 Child Process模塊
- 13.12 Http模塊
- 13.13 assert 模塊
- 13.14 Cluster模塊
- 13.15 os模塊
- 13.16 Net模塊和DNS模塊
- 13.17 Express框架
- 13.18 Koa 框架