漸變色(gradient)是指從一種顏色平滑的過渡到另外一種顏色。而且,我們可以將多種漸變色應(yīng)用到同一個網(wǎng)頁元素上。 在SVG里,有兩種主要的漸變色類型:
SVG線性漸變色 – <linearGradient><linearGradient>標(biāo)記就是用來定義漸變色的。 <linearGradient>標(biāo)記必須放在<defs>標(biāo)記內(nèi)。<defs>標(biāo)記就是“definitions”單詞的簡寫,用來容納其它各種SVG標(biāo)記。 線性漸變色可以定義成水平漸變色,垂直漸變色和斜向漸變色:
例 1我們先定義一個橢圓,而后用一個從黃色到紅色的漸變色渲染它: 上面的圖像使用了下面的SVG代碼: <svg height='150' width='400'> <defs> <linearGradient id='grad1' x1='0%' y1='0%' x2='100%' y2='0%'> <stop offset='0%' style='stop-color:rgb(255,255,0);stop-opacity:1' /> <stop offset='100%' style='stop-color:rgb(255,0,0);stop-opacity:1' /> </linearGradient> </defs> <ellipse cx='200' cy='70' rx='85' ry='55'fill='url(#grad1)' /></svg> 代碼說明:
例 2下面的例子中的漸變色方向是垂直的: 上面的圖像使用了下面的SVG代碼: <svg height='150' width='400'> <defs> <linearGradient id='grad2' x1='0%' y1='0%' x2='0%' y2='100%'> <stop offset='0%' style='stop-color:rgb(255,255,0);stop-opacity:1' /> <stop offset='100%' style='stop-color:rgb(255,0,0);stop-opacity:1' /> </linearGradient> </defs> <ellipse cx='200' cy='70' rx='85' ry='55' fill='url(#grad2)' /></svg> 例 3下面的例子里,我們使用了水平漸變色,并且還在橢圓里添加了文字: 上面的圖像使用了下面的SVG代碼: <svg height='150' width='400'> <defs> <linearGradient id='grad3' x1='0%' y1='0%' x2='100%' y2='0%'> <stop offset='0%' style='stop-color:rgb(255,255,0);stop-opacity:1' /> <stop offset='100%' style='stop-color:rgb(255,0,0);stop-opacity:1' /> </linearGradient> </defs> <ellipse cx='200' cy='70' rx='85' ry='55'fill='url(#grad3)' /> <text fill='#ffffff' font-size='45' font-family='Verdana' x='150' y='86'> SVG</text></svg> 代碼說明:
WEBHEK is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding.Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. |
|