時間:2023-07-05 14:48:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-07-05 14:48:01 來源:網(wǎng)站運(yùn)營
SVG的繪制及實現(xiàn)網(wǎng)頁動畫效果:<svg width="200" height="250" version="1.1" xmlns="http://www.w3.org/2000/svg"> // 矩形繪制 <rect x="10" y="10" width="30" height="30" stroke="black" fill="transparent" stroke-width="5"/> <rect x="60" y="10" rx="10" ry="10" width="30" height="30" stroke="black" fill="transparent" stroke-width="5"/>// 圓形和橢圓 <circle cx="25" cy="75" r="20" stroke="red" fill="transparent" stroke-width="5"/> <ellipse cx="75" cy="75" rx="20" ry="5" stroke="red" fill="transparent" stroke-width="5"/>// 直線和曲線 <line x1="10" x2="50" y1="110" y2="150" stroke="orange" fill="transparent" stroke-width="5"/> <polyline points="60 110 65 120 70 115 75 130 80 125 85 140 90 135 95 150 100 145" stroke="orange" fill="transparent" stroke-width="5"/>// 多邊形 <polygon points="50 160 55 180 70 180 60 190 65 205 50 195 35 205 40 190 30 180 45 180" stroke="green" fill="transparent" stroke-width="5"/>// 路徑 <path d="M20,230 Q40,205 50,230 T90,230" fill="none" stroke="blue" stroke-width="5"/></svg>
<rect x="60" y="10" rx="10" ry="10" width="30" height="30" stroke="black" fill="transparent" stroke-width="5"/>
x:矩形左上角的x位置 <circle cx="25" cy="75" r="20" stroke="red" fill="transparent" stroke-width="5"/>
r:圓的半徑 <ellipse cx="75" cy="75" rx="20" ry="5" stroke="red" fill="transparent" stroke-width="5"/>
rx:橢圓的x半徑<line x1="10" x2="50" y1="110" y2="150"/>
x1:起點的x位置<polyline points="60 110, 65 120, 70 115, 75 130, 80 125, 85 140, 90 135, 95 150, 100 145"/>
points:點集數(shù)列。每個數(shù)字用空白、逗號、終止命令符或者換行符分隔開。每個點必須包含2個數(shù)字,一個是x坐標(biāo),一個是y坐標(biāo)。所以點列表 (0,0), (1,1) 和(2,2)可以寫成這樣:“0 0, 1 1, 2 2”。path
可能是SVG中最常見的形狀。你可以用path元素繪制矩形(直角矩形或者圓角矩形)、圓形、橢圓、折線形、多邊形,以及一些其他的形狀,例如貝塞爾曲線、2次曲線等曲線。<path>元素d屬性命令:M = moveto(M X,Y) :將畫筆移動到指定的坐標(biāo)位置L = lineto(L X,Y) :畫直線到指定的坐標(biāo)位置H = horizontal lineto(H X):畫水平線到指定的X坐標(biāo)位置V = vertical lineto(V Y):畫垂直線到指定的Y坐標(biāo)位置C = curveto(C X1,Y1,X2,Y2,ENDX,ENDY):三次貝賽曲線S = smooth curveto(S X2,Y2,ENDX,ENDY):平滑曲率Q = quadratic Belzier curve(Q X,Y,ENDX,ENDY):二次貝賽曲線T = smooth quadratic Belzier curveto(T ENDX,ENDY):映射A = elliptical Arc(A RX,RY,XROTATION,FLAG1,FLAG2,X,Y):弧線Z = closepath():關(guān)閉路徑
6.2 基礎(chǔ)屬性stroke-linecap
屬性的值有三種可能值:butt
用直邊結(jié)束線段,它是常規(guī)做法,線段邊界90度垂直于描邊的方向、貫穿它的終點。square
的效果差不多,但是會稍微超出實際路徑
的范圍,超出的大小由stroke-width
控制。round
表示邊框的終點是圓角,圓角的半徑也是由stroke-width
控制的。stroke-dasharray
屬性的參數(shù),是一組用逗號分割的數(shù)字組成的數(shù)列。注意,和path
不一樣,這里的數(shù)字必須用逗號分割(空格會被忽略)。每一組數(shù)字,第一個用來表示填色區(qū)域的長度,第二個用來表示非填色區(qū)域的長度。defs元素
內(nèi)部,創(chuàng)建一個<linearGradient>
節(jié)點。<linearGradient>元素還需要一些其他的屬性值,它們指定了漸變的大小和出現(xiàn)范圍。漸變的方向可以通過兩個點來控制,它們分別是屬性x1、x2、y1和y2,這些屬性定義了漸變路線走向。漸變色默認(rèn)是水平方向的,但是通過修改這些屬性,就可以旋轉(zhuǎn)該方向。下例中的Gradient2創(chuàng)建了一個垂直漸變。<svg width="120" height="240" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="Gradient1"> <stop class="stop1" offset="0%"/> <stop class="stop2" offset="50%"/> <stop class="stop3" offset="100%"/> </linearGradient> <linearGradient id="Gradient2" x1="0" x2="0" y1="0" y2="1"> <stop offset="0%" stop-color="red"/> <stop offset="50%" stop-color="black" stop-opacity="0"/> <stop offset="100%" stop-color="blue"/> </linearGradient> <style type="text/css"><![CDATA[ #rect1 { fill: url(#Gradient1); } .stop1 { stop-color: red; } .stop2 { stop-color: black; stop-opacity: 0; } .stop3 { stop-color: blue; } ]]></style> </defs> <rect id="rect1" x="10" y="10" rx="15" ry="15" width="100" height="100"/> <rect x="10" y="120" rx="15" ry="15" width="100" height="100" fill="url(#Gradient2)"/> </svg>
<g>
元素:可以把屬性賦給一整個元素集合。translate()
變形方法<rect x="0" y="0" width="10" height="10" transform="translate(30,40)" />
旋轉(zhuǎn):transform: rotate( )方法perspective
()方法 matrix(a, b, c, d, tx, ty)
matrix(a, b, c, d, tx, ty)
是 matrix3d(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1)
的簡寫。 <svg width="800" height="800" version="1.1" xmlns="http://www.w3.org/2000/svg"> <line x1="10" x2="600" y1="110" y2="110" stroke="orange" fill="transparent" stroke-width="8" /> </svg>
先畫一條線stroke-dasharray
和stroke-dashoffset
這兩個屬性,對于stroke-dasharray
可以參考下圖來理解:stroke-dashoffset
則可以理解成類似translate
的偏移值。通過CSS來設(shè)置這兩個值,之前的路徑就會變成這個樣子:.line{ stroke-dasharray: 20px, 10px; stroke-dashoffset: 0;}
得到如下效果.line{ animation: move 3s linear forwards;}@keyframes move { 0%{ stroke-dasharray: 0, 800px; } 100%{ stroke-dasharray: 800px, 0; }}
800px這個值是整條直線的長度,如果是復(fù)雜路徑可以用DOM的API來獲取到路徑長度值:document.getElementById('path').getTotalLength()
(2)直接讓線進(jìn)行平移動畫,就是基礎(chǔ)的css動畫效果.line{ animation: move 3s linear forwards;}@keyframes move { 0%{ transform: translateX(-100%); } 100%{ transform: translateX(0); }}
實現(xiàn)如上圖一樣的效果 <svg width="180" height="50" version="1.1" xmlns="http://www.w3.org/2000/svg"> <path id="pathSecond" fill="none" stroke="rgba(0,0,0,0.8)" stroke-width="2px" d="M73.73,24.5c0,0,2.78-21.26-23.75-16.46 c0,0-11.12-12.63-25.01-5.81c0,0-11.12,4.8-8.84,16.67 c0,0-13.81-2.27-16.13,13.39c0,0-3.07,10.67,10.32,16.71h180.02" /> </svg>
#pathSecond { stroke-dashoffset: 0; animation: move 3s infinite linear;}@keyframes move { 0%{ stroke-dasharray: 0, 600px; opacity: 1; stroke-width: 3px } 100%{ stroke-dasharray: 600px, 0px; opacity: 0; }}
就可以得到這個效果了關(guān)鍵詞:效果,實現(xiàn),繪制
客戶&案例
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。