注意: Internet Explorer和Safari不支持SVG濾鏡!
<defs> 和 <filter>
所有互聯(lián)網(wǎng)的SVG濾鏡定義在<defs>元素中。<defs>元素定義短并含有特殊元素(如濾鏡)定義。
<filter>標(biāo)簽用來(lái)定義SVG濾鏡。<filter>標(biāo)簽使用必需的id屬性來(lái)定義向圖形應(yīng)用哪個(gè)濾鏡?
SVG <feOffset>
實(shí)例 1
<feOffset>元素是用于創(chuàng)建陰影效果。我們的想法是采取一個(gè)SVG圖形(圖像或元素)并移動(dòng)它在xy平面上一點(diǎn)兒。
第一個(gè)例子偏移一個(gè)矩形(帶<feOffset>),然后混合偏移圖像頂部(含<feBlend>):

下面是SVG代碼:
實(shí)例
<!DOCTYPE html> <html> <body> <p><b>Note: </b>Internet Explorer and Safari do not support SVG filters yet!</p> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="f1" x="0" y="0" width="200%" height="200%"> <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" /> <feBlend in="SourceGraphic" in2="offOut" mode="normal" /> </filter> </defs> <rect width="90" height="90" stroke="green" stroke-width="3" fill="yellow" filter="url(#f1)" /> </svg> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
對(duì)于Opera用戶: 查看SVG文件(右鍵單擊SVG圖形預(yù)覽源)。
代碼解析:
<filter>元素id屬性定義一個(gè)濾鏡的唯一名稱
<rect>元素的濾鏡屬性用來(lái)把元素鏈接到"f1"濾鏡
實(shí)例 2
現(xiàn)在,偏移圖像可以變的模糊(含 <feGaussianBlur>):

下面是SVG代碼:
對(duì)于Opera用戶: 查看SVG文件(右鍵單擊SVG圖形預(yù)覽源)。
代碼解析:
<feGaussianBlur>元素的stdDeviation屬性定義了模糊量
實(shí)例 3
現(xiàn)在,制作一個(gè)黑色的陰影:

下面是SVG代碼:
實(shí)例
<!DOCTYPE html> <html> <body> <p><b>Note: </b>Internet Explorer and Safari do not support SVG filters yet!</p> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="f1" x="0" y="0" width="200%" height="200%"> <feOffset result="offOut" in="SourceAlpha" dx="20" dy="20" /> <feGaussianBlur result="blurOut" in="offOut" stdDeviation="10" /> <feBlend in="SourceGraphic" in2="blurOut" mode="normal" /> </filter> </defs> <rect width="90" height="90" stroke="green" stroke-width="3" fill="yellow" filter="url(#f1)" /> </svg> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
對(duì)于Opera用戶: 查看SVG文件(右鍵單擊SVG圖形預(yù)覽源)。
代碼解析:
<feOffset>元素的屬性改為"SourceAlpha"在Alpha通道使用殘影,而不是整個(gè)RGBA像素。
實(shí)例 4
現(xiàn)在為陰影涂上一層顏色:

下面是SVG代碼:
實(shí)例
<!DOCTYPE html> <html> <body> <p><b>Note: </b>Internet Explorer and Safari do not support SVG filters yet!</p> <svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <filter id="f1" x="0" y="0" width="200%" height="200%"> <feOffset result="offOut" in="SourceGraphic" dx="20" dy="20" /> <feColorMatrix result = "matrixOut" in = "offOut" type = "matrix" values = "0.2 0 0 0 0 0 0.2 0 0 0 0 0 0.2 0 0 0 0 0 1 0"/> <feGaussianBlur result="blurOut" in="matrixOut" stdDeviation="10" /> <feBlend in="SourceGraphic" in2="blurOut" mode="normal" /> </filter> </defs> <rect width="90" height="90" stroke="green" stroke-width="3" fill="yellow" filter="url(#f1)" /> </svg> </body> </html>
運(yùn)行實(shí)例 ?
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
對(duì)于Opera用戶: 查看SVG文件(右鍵單擊SVG圖形預(yù)覽源)。
代碼解析:
<feColorMatrix>過(guò)濾器是用來(lái)轉(zhuǎn)換偏移的圖像使之更接近黑色的顏色。 '0.2'矩陣的三個(gè)值都獲取乘以紅色,綠色和藍(lán)色通道。降低其值帶來(lái)的顏色至黑色(黑色為0)