1. 程式人生 > >SVG中stroke-dasharray及stroke-dashoffset屬性

SVG中stroke-dasharray及stroke-dashoffset屬性

                     

stroke-dasharray屬性用來設定描邊的點劃線的圖案正規化。就是設定實線和虛線的寬度

比如:
stroke-dasharray: 50 20;
效果就是:
這裡寫圖片描述
50和20分別對應了實線和虛線的長度

stroke-dashoffset則指定了dash模式到路徑開始的距離,就是實線虛線繪製的起點距路徑開始的距離

比如:
stroke-dashoffset: 30;
效果:
這裡寫圖片描述
由於向左移動30畫素,這樣左邊的實線就跟虛線一樣長了。

我們可以設定stroke-dashoffsetstroke-dasharray相同的值實現“畫線”的效果:
程式碼:

<style>path{    stroke-dasharray
: 1000
;    stroke-dashoffset: 1000;    animation: draw 5s ease 3;}
@keyframes draw{    0%{        stroke-dashoffset: 1000;    }    100%{        stroke-dashoffset: 0;    }}
</style><path d="M153 334C153 334 151 334 151 334C151 339 153 344 156 344C164 344 171 339 171 334C171 322 164 314 156 314C142 314 131 322 131 334C131 350 142 364 156 364C175 364 191 350 191 334C191 311 175 294 156 294C131 294 111 311 111 334C111 361 131 384 156 384C186 384 211 361 211 334C211 300 186 274 156 274"
style="fill:white;stroke:red;stroke-width:2"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

由於實線起始距離從1000變為0,所以效果就是線在“繪圖”一樣。

效果:
這裡寫圖片描述