本文章假設讀者對SVG有基礎的瞭解。javascript
元素的寬度css
元素的高度java
爲閉合圖形填充顏色css3
填充顏色的透明度web
爲線條添加顏色chrome
線條寬度segmentfault
線條透明度css3動畫
線條終點的樣式,支持三個值:butt
(默認),square
,round
。
app
線條轉折點的鏈接樣式,支持三個值:miter
(默認),round
,bevel
。
svg
用逗號分隔的一組數字,表示虛線中實線段的長度。
支持百分比和數值,表示dash模式到路徑開始的距離。
x
矩形左上角的x位置y
矩形左上角的y位置width
寬度height
高度rx
圓角的x方位的半徑ry
圓角的y方位的半徑
r
圓形半徑cx
圓心的x位置cy
圓心的y位置
rx
橢圓的x半徑ry
橢圓的y半徑cx
橢圓中心的x位置cy
橢圓中心的y位置
x1
線條起點的x位置y1
線條起點的y位置x2
線條終點的x位置y2
線條終點的y位置
points
折線的折點
exam:<polyline points="60 110, 65 120, 70 115, 75 130, 80 125, 85 140, 90 135, 95 150, 100 145"/>
points
多邊形的折點
exam:<polygon points="50 160, 55 180, 70 180, 60 190, 65 205, 50 195, 35 205, 40 190, 30 180, 45 180"/>
d
「路徑命令+參數」的序列,大寫命令表示採用絕對定位,小寫命令表示採用相對定位。M(m) x y
指定路徑的起點L(l) x y
指定路徑直線的終點H(h) x
指定路徑水平直線終點的xV(v) y
指定路徑垂直直線終點的yZ(z)
沒有參數,閉合路徑A(a) rx ry x-axis-rotation large-arc-flag sweep-flag x y
繪製弧形。弧形能夠視爲圓形或橢圓形的一部分。
rx
橢圓的x半徑
ry
橢圓的y半徑
x-axis-rotation
橢圓x軸的旋轉角度
large-arc-flag
0表示小角度弧,1表示大角度弧。
sweep-flag
0表示從起點到終點沿逆時針畫弧,1表示從起點到終點沿順時針畫弧。
x
弧形終點的x位置
y
弧形終點的y位置
控制點描述的是曲線始終點的斜率。貝塞爾曲線是從起點斜率到終點斜率的漸變弧線。C(c) x1 y1, x2 y2, x y
描繪三次貝塞爾曲線。6個參數分別是始終點的控制點位置和曲線終點位置。S(s) x2 y2, x y
描繪光滑三次貝塞爾曲線。Q(q) x1 y1, x y
描繪二次貝塞爾曲線。4個參數分別是控制點位置和曲線終點位置。T(t) x y
描繪光滑二次貝塞爾曲線。
文本
分組
引用
transform
屬性爲元素變形,目前僅支持2維變形。默認都是以SVG
原點爲基點對元素進行變形,對於單個元素的簡單變形,能夠經過viewBox改變原點的方式校訂。
translate(x[,y])
平移,支持百分比和數值。
rotate(angle[, x y])
旋轉,以x
和y
的位置爲中心旋轉。
scale(x[, y])
縮放。
skewX(angle)
,skewY(angle)
斜切
經過矩陣實現複雜變形
經過css只能控制一部分svg屬性。
svg中引用樣式
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <defs> <style type="text/css"><![CDATA[ #MyRect { stroke: black; fill: red; } ]]></style> </defs> <rect x="10" height="180" y="10" width="180" id="MyRect"/> </svg>
css中控制樣式
//svg <svg width="200" height="150" xmlns="http://www.w3.org/2000/svg"> <rect height="10" width="10" id="MyRect"/> </svg> //css #MyRect { fill: red; stroke: black; }
css3漸變
//svg <svg width="200" height="150" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="0" x2="200" y2="150" stroke="black" id="myLine"/> </svg> //css #myLine { stroke-width: 50; transition: all 5s; } #myLine:hover { stroke-width: 100; }
css3動畫
//svg <svg width="200" height="150" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="0" x2="200" y2="150" stroke="black" stroke-width="50" id="myLine"/> </svg> //css #myLine:hover{ animation: lineAnimate 2s; } @keyframes lineAnimate { to { stroke-width: 100; } }
document.creatElementNS(ns, tagName)
建立svg
元素,ns
爲命名空間:svg
元素的命名空間是:http://www.w3.org/2000/svg
;若是使用引用xlink
,需添加xlink
的命名空間:http://www.w3.org/1999/xlink
。element.appendChild(childElement)
添加子元素。element.setAttribute(name, value)
設置元素屬性和值。element.getAttribute(name)
獲取元素屬性值。path.getTotalLength()
獲取路徑長度。requestAnimationFrame(update)
請求動畫幀,接受一個回調函數,用來更新畫面。下面是一個使用requestAnimationFrame(update)
的例子:
//svg <svg width="800" height="800" viewBox="-400 -400 800 800" xmlns="http://www.w3.org/2000/svg"> <line x1="0" y1="-400" x2="0" y2="-400" stroke="black" stroke-width="2" id="line"/> </svg> //javascript window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; (function() { var line = document.getElementById("line"); var lastTime = +new Date(); function update() { var curY2 = Number(line.getAttribute("y2")); var curTime = +new Date(); var _time = curTime - lastTime; if (curY2 < 400) { line.setAttribute('y2', curY2 + _time / 5); } else if (curY2 > 400) { line.setAttribute('y2', 400); } else { return false; } lastTime = +new Date(); requestAnimationFrame(update); } window.update = update; })(); requestAnimationFrame(update);
使用requestAnimationFrame(update)
能夠製做簡單的SVG
動態圖標。
不建議使用SVG animation with SMIL
。在寫這篇文章的時候,使用<set>
,<animate>
等元素會在chrome控制檯中顯示SVG's SMIL animations are deprecated and will be removed. Please use CSS animations or Web animations instead.
。
轉載請註明出處:http://www.javashuo.com/article/p-eopiyipi-de.html
文章不按期更新完善,若是能對你有一點點啓發,我將不勝榮幸。