Svg實(shí)戰(zhàn)總結(jié)(二)

image.png

接著上篇文章繼續(xù)往后講解其他知識(shí)點(diǎn),感謝讀者們?cè)敢饣ㄙM(fèi)您們寶貴時(shí)間閱讀!

使用濾鏡filter(也是設(shè)置陰影)
image.png

<filter>標(biāo)記里的id屬性定義了這個(gè)濾鏡的唯一標(biāo)志
<rect>標(biāo)記里的filter屬性定義了需要引用的濾鏡是“f1”

蒙版

使用蒙版需要在使用前在<defs>中定義<mask>并為其設(shè)置一個(gè)唯一id,然后在需要應(yīng)用蒙版的元素上添加一條屬性mask="url(#id)"

設(shè)置有了蒙版,還需要在蒙版中添加圖形元素并指定黑白顏色

案例

例子1
<svg width="1000" height="500">
<g>
<mask id="model">
<circle cx="100" cy="160" r="80" fill="yellow" x="80" y="80"/>
<circle cx="160" cy="160" r="80" fill="black" x="80" y="80"/>
</mask>
<circle cx="100" cy="160" r="80" fill="yellow" x="80" y="80" mask="url(#model)" />
</g>
</svg>

<style>
body,html{
background-color:skyblue;
}
</style>
效果圖

image.png

例子2
裁剪前


image.png

html
<svg width="1000" height="800">
<g>
<mask id="model">
<circle cx="160" cy="160" r="80" fill="black" x="80" y="80"/>
<rect x="160" y="180" rx="100" ry="100" width="100" height="100" stroke="red" fill="red"/>
</mask>
<circle cx="160" cy="160" r="80" fill="black" x="80" y="80" mask="url(#model)" />
</g>
</svg>

裁剪后


image.png

例子3

html
<svg width="1000" height="500">
<g>
<mask id="model">
<circle cx="100" cy="160" r="80" fill="yellow" x="80" y="80"/>
<circle cx="160" cy="160" r="80" fill="black" x="80" y="80"/>
</mask>
<circle cx="100" cy="160" r="80" fill="yellow" x="80" y="80" mask="url(#model)" />
</g>
<g>
<mask id="model">
<circle cx="160" cy="160" r="80" fill="black" x="80" y="80"/>
<rect x="160" y="180" rx="100" ry="100" width="100" height="100" stroke="red" fill="red"/>
</mask>
<circle cx="160" cy="160" r="80" fill="black" x="80" y="80" mask="url(#model)" />
</g>
</svg>

效果圖


image.png
裁剪clipPath

<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<clipPath id="myClip">
<circle cx="70" cy="70" r="40"/>
<circle cx="30" cy="30" r="20"/>
</clipPath>
</defs>
<rect x="10" y="10" width="100" height="100" clip-path="url(#myClip)"/>
</svg>

裁剪路徑和蒙板區(qū)別就是:

裁剪路徑覆蓋的對(duì)象要么就是全透明(可見的,位于裁剪路徑內(nèi)部)罗侯,要么就是全不透明(不可見,位于裁剪路徑外部)溪猿;蒙板可以指定不同位置的透明度

裁剪主要為了設(shè)置不同形狀圖形钩杰,蒙版主要為了設(shè)置漸變顏色圖形

(路徑畫法)貝塞爾曲線

<path d="M20 20 C90 40 130 40 180 20" stroke="#000000" fill="none" style="stroke-width: 2px;"></path>


image.png

三次貝塞爾曲線的原理:


image.png
image.png

其中,Catmull-Rom曲線不是標(biāo)準(zhǔn)的svg命令

案例:


image.png
image.png
動(dòng)畫animate方面:

動(dòng)畫原理


image.png

動(dòng)畫標(biāo)簽


image.png

動(dòng)畫元素诊县,屬性定位讲弄,動(dòng)畫參數(shù)


image.png

定位:


image.png

動(dòng)畫標(biāo)簽被包含在目標(biāo)元素里:


image.png

基本animate標(biāo)簽屬性設(shè)置:


image.png

fill屬性設(shè)置:
fill=”freeze”,設(shè)置freeze時(shí)依痊,動(dòng)畫就停止在to的數(shù)值位置避除;其他屬性表示:remove回原位

repeatCount=“100”,表示次數(shù)為100胸嘁;其他屬性表示:循環(huán)indefinite瓶摆,一次:forwards

動(dòng)畫疊加
animate標(biāo)簽:


image.png

animateTransform標(biāo)簽:(不能疊加)


image.png
軌跡路徑:
image.png

案例
例子1:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-400 -400 800 800">
<rect x="-25" y="-25" width="50" height="50" fill="red">
<animateMotion
path="M0 0 L100 100 A200 200 0 1 0 0 -100"
dur="3s"
rotate="auto"

</animateMotion>
</rect>
<path id="pathRoad" d="M0 0 L100 100 A200 200 0 1 0 0 -100" stroke="gray" fill="none"></path>
</svg>

<style>
html,body,svg{
width:100%;
height:100%;
padding: 0;
margin: 0;
}
</style>

效果圖


image.png

例子2:


image.png

<rect x="10" y="10" width="100" height="100">
<animate attributeType="XML" attributeName="x" from="100" to="120"
dur="2s" repeatCount="forwards"/>
</rect>

效果圖


image.png

例子3:

沿著路徑運(yùn)動(dòng)
<path d="M10,110 A120,120 -45 0,1 110 10 A120,120 -45 0,1 10,110"
stroke="lightgrey" stroke-width="2"
fill="none" id="theMotionPath"/>
<circle cx="10" cy="110" r="3" fill="lightgrey" />
<circle cx="110" cy="10" r="3" fill="lightgrey" />

<circle cx="" cy="" r="5" fill="red">
<animateMotion dur="6s" repeatCount="indefinite">
<mpath xlink:href="#theMotionPath"/>
</animateMotion>
</circle>

效果圖

image.png

線性變換:


image.png

旋轉(zhuǎn)變換transform(會(huì)經(jīng)常在繪制圖形時(shí),改變其狀態(tài)性宏,用到它)


image.png
文本text

屬性: dx群井、dy屬性,切線和法線方向的偏移毫胜;dx是設(shè)置字體之間的間隙蝌借,dy是設(shè)置字體間縱坐標(biāo)的位置;x指蚁、text-anchor、startOffset屬性自晰,確定排列起始位置

文本垂直居中問(wèn)題


image.png

textPath使用方法:


image.png

加在文本的超鏈接(a標(biāo)簽)
Xlink:href 指定連接地址
Xlink:title 指定連接提示
Target 指定打開目標(biāo)


image.png

文本旋轉(zhuǎn)


image.png

設(shè)置文本路徑(可以取消路徑顏色凝化,也可以設(shè)置路徑顏色)


image.png

案例

例子1

<text x="30" y="30" dx="20 20 20 20 20 20 20 20 20 20 20" dy="10" style="font-size: 20px;">hello world!</text>
<path d="M40,0 V200 M0,40 H200" stroke="red"/>

效果圖


image.png

例子2

<text id="sintext" x="100" y="100" dx="20,20,20,20,20,20,20,20,20,20,20" dy="20,20,20,20,20,20,20,20,20,20,20" style="font-size: 14px;">hello world!</text>
<path d="M100,0 V200 M0,100 H200" stroke="red"/>

效果圖


image.png

例子3(text-anchor startOffset xlink:href="")
<path id="path1" d="M 100 200 Q 200 100 300 200 T 500 200" stroke="rgb(0,255,0)" fill="none"/>
<text text-anchor="middle" x="0" y="0">
<textPath xlink:href="#path1" startOffset="50%">
hello world!hello world!
</textPath>
</text>

效果圖


image.png

例子4
<path id="path1" d="M100,300 l100,-50,200,100,100,-50" stroke="rgb(0,255,0)" fill="none"/>
<text text-anchor="middle" x="0" y="0">
<textPath xlink:href="#path1" startOffset="50%">
hello world!hello world!
</textPath>
</text>

效果圖


image.png

例子5
<path id="path1" d="M100,400 A400,300,0,0,0,500,400" stroke="rgb(0,255,0)" fill="none"/>
<text text-anchor="middle" x="0" y="0">
<textPath xlink:href="#path1" startOffset="50%">
hello world!hello world!
</textPath>
</text>

效果圖


image.png

例子6
<text x="0" y="80" transform="rotate(30,20,30),scale(1.4)">
SVG Text Rotation example
</text>

效果圖


image.png

例子7


<defs>
<path id="MyPath"
d="M 20 20
C 200 100 300 0 400 100
C 500 200 600 300 700 200
C 800 100 900 100 900 100"/>
</defs>

<use xlink:href="#MyPath" fill="none" stroke="red" />

<text font-family="Verdana" font-size="28">
<textPath xlink:href="#MyPath">
We go up, then we go down, then up again
</textPath>
</text>


<rect x="1" y="1" width="998" height="298"
fill="none" stroke="black" stroke-width="2" />

效果圖


image.png

例子8
在文本中text使用tspan
<text x="15" y="30">
You are
<tspan fill="red" stroke="skyblue">not</tspan>
a banana
<tspan fill="blue" stroke="red">!!!</tspan>
</text>

效果圖


image.png
Symbol標(biāo)簽(use標(biāo)簽,復(fù)用元素標(biāo)簽)
image.png

案例
<symbol id="sym02" viewBox="0 0 150 110">
<circle cx="50" cy="50" r="40" stroke-width="8" style="stroke: red;fill: red;"/>
<circle cx="90" cy="60" r="40" stroke-width="8" stroke="green" fill="white"/>
</symbol>

<use xlink:href="#sym02" x="0" y="0" width="100" height="50"/>
<use xlink:href="#sym02" x="0" y="50" width="75" height="38"/>
<use xlink:href="#sym02" x="0" y="100" width="50" height="25"/>

效果圖


image.png

注意:使用g標(biāo)簽和symbol標(biāo)簽的對(duì)比
不用symbol酬荞,而用g標(biāo)簽效果
<g id="sym02" viewBox="0 0 150 110">
<circle cx="50" cy="50" r="40" stroke-width="8" style="stroke: red;fill: red;"/>
<circle cx="90" cy="60" r="40" stroke-width="8" stroke="green" fill="white"/>
</g>

<use xlink:href="#sym02" x="0" y="0" width="100" height="50"/>
<use xlink:href="#sym02" x="0" y="50" width="75" height="38"/>
<use xlink:href="#sym02" x="0" y="100" width="50" height="25"/>

效果


image.png
Switch標(biāo)簽使用


<switch>
<text systemLanguage="ar">?????</text>
<text systemLanguage="de,nl">Hallo!</text>
<text systemLanguage="en">Hello!</text>
<text systemLanguage="en-us">Howdy!</text>
<text systemLanguage="en-gb">Wotcha!</text>
<text systemLanguage="en-au">G'day!</text>
<text systemLanguage="es">Hola!</text>
<text systemLanguage="fr">Bonjour!</text>
<text systemLanguage="ja">こんにちは</text>
<text>7788</text>
</switch>

瀏覽器顯示的語(yǔ)言是什么搓劫,text就顯示哪種語(yǔ)言的文本

marker標(biāo)簽使用

屬性:refX="1" refY="5"設(shè)置箭頭位置

markerWidth="4" markerHeight="4" 設(shè)置箭頭粗細(xì)

<defs> <marker id="Triangle" viewBox="0 0 10 10" refX="1" refY="5" markerWidth="6" markerHeight="6" orient="auto"> <path d="M 0 0 L 10 5 L 0 10 z" /> </marker> </defs> <polyline points="10,90 50,80 90,20" fill="none" stroke="black" stroke-width="2" marker-end="url(#Triangle)" />

image.png
pattern標(biāo)簽使用

<defs>
<pattern id="Triangle" width="10" height="10" patternUnits="userSpaceOnUse">
<polygon points="5,0 10,10 0,10" style="stroke: white;fill: red;"/>
</pattern>
</defs>

<circle cx="10" cy="160" r="50" fill="url(#Triangle)"/>

image.png
顏色漸變和畫刷

漸變(徑向和線性兩種類型漸變)

線性漸變


image.png
image.png

案例

<defs>
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="1">
<stop offset="0" stop-color="#1497FC"/>
<stop offset="0.5" stop-color="#A469BE"/>
<stop offset="1" stop-color="#FF8C00"/>
</linearGradient>
</defs>

<rect x="100" y="100" fill="url(#grad1)" width="200" height="150"></rect>

效果圖


image.png

補(bǔ)充:加上屬性grdientIUnits用的是世界坐標(biāo)系


image.png

徑向漸變


image.png

案例
<defs>
<radialGradient id="grad2" cx="0.5" cy="0.5" r="0.5" fx="0.6" fy="0.3">
<stop offset="0" stop-color="rgb(20,151,252)"/>
<stop offset="0.5" stop-color="rgb(164,105,190)"/>
<stop offset="1" stop-color="rgb(255,140,0)"/>
</radialGradient>
</defs>

<rect x="100" y="100" fill="url(#grad2)" width="200" height="150"></rect>

效果圖


image.png

筆刷


image.png

例子
<defs>
<pattern id="p1" x="0" y="0" width="0.8" height="0.8">
<circle cx="10" cy="10" r="5" fill="red"></circle>
<polygon points="30 10 60 50 0 50" fill="green"></polygon>
</pattern>
</defs>
<rect x="100" y="100" width="900" height="700" fill="url(#p1)" stroke="blue"></rect>

效果圖


image.png

patternUnits單位
patternUnits="userSpaceOnUse"


image.png

patternUnits="objectBoundingBox" 和patternContentUnits="objectBoundingBox"

image.png

效果圖


image.png

完整的案例:


image.png

Github地址:https://github.com/lilyping/svg_smallYellowCute

本文作者lilyping
越努力瞧哟,越幸運(yùn)
原文鏈接:http://www.reibang.com/u/3908e601f4ec
微信公眾號(hào):BestLilyPing
github:https://github.com/lilyping
Demos源碼地址:https://github.com/lilyping

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市枪向,隨后出現(xiàn)的幾起案子勤揩,更是在濱河造成了極大的恐慌,老刑警劉巖秘蛔,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件陨亡,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡深员,警方通過(guò)查閱死者的電腦和手機(jī)负蠕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)倦畅,“玉大人遮糖,你說(shuō)我怎么就攤上這事〉停” “怎么了欲账?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)芭概。 經(jīng)常有香客問(wèn)我赛不,道長(zhǎng),這世上最難降的妖魔是什么谈山? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任俄删,我火速辦了婚禮,結(jié)果婚禮上奏路,老公的妹妹穿的比我還像新娘畴椰。我一直安慰自己,他們只是感情好鸽粉,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布斜脂。 她就那樣靜靜地躺著,像睡著了一般触机。 火紅的嫁衣襯著肌膚如雪帚戳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天儡首,我揣著相機(jī)與錄音片任,去河邊找鬼。 笑死蔬胯,一個(gè)胖子當(dāng)著我的面吹牛对供,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼产场,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼鹅髓!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起京景,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤窿冯,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后确徙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體醒串,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年米愿,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了厦凤。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡育苟,死狀恐怖较鼓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情违柏,我是刑警寧澤博烂,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站漱竖,受9級(jí)特大地震影響禽篱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜馍惹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一躺率、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧万矾,春花似錦悼吱、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至薪丁,卻和暖如春遇西,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背严嗜。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工粱檀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人漫玄。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓茄蚯,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子第队,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • SVG API: SVG是一種可縮放矢量圖形,一種二維圖形表示語(yǔ)言 與canvas不同的是刨秆,在瀏覽器的開發(fā)工具中能...
    Iris_mao閱讀 1,013評(píng)論 0 5
  • 在React Native中使用ARTReact Native ART 究竟是什么?所謂ART凳谦,是一個(gè)在React...
    JackfengGG閱讀 9,507評(píng)論 2 50
  • 一、柵格圖形和矢量圖形柵格圖形:也稱位圖衡未,圖像由一組二維像素網(wǎng)格表示尸执。Canvas 2d API 就是一款柵格圖形...
    linda102閱讀 1,141評(píng)論 0 4
  • <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLI...
    穆熙沐閱讀 404評(píng)論 0 1
  • 太陽(yáng)還凌落在天上 大地已微黃 當(dāng)曠野上的農(nóng)人 鋤斷最后一棵稗草 當(dāng)校舍的鐘聲 送走一天的慌張 是真的倦了 嫩芽也罷...
    獨(dú)愛橙色閱讀 277評(píng)論 0 2