2016-04-03 34 views
0

我有以下非常簡單的SVG代碼text-anchor =「start」不會將文本元素移動到svg元素的開頭。

<div> 
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 194 186" class="circliful"> 
    <g stroke="#ccc"> 
    <line x1="133" y1="50" x2="140" y2="40" stroke-width="2"></line> 
    </g> 
    <g stroke="#ccc"> 
    <line x1="140" y1="40" x2="200" y2="40" stroke-width="2"></line> 
    </g> 
    <circle cx="100" cy="100" r="57" class="border" fill="#eee" stroke="none" stroke-width="15" stroke-dasharray="360" transform="rotate(-90,100,100)"></circle> 
    <circle class="circle" cx="100" cy="100" r="57" fill="none" stroke="#3498DB" stroke-width="5" stroke-dasharray="180, 20000" transform="rotate(-90,100,100)"></circle> 
    <text text-anchor="middle" x="100" y="110" class="icon" style="font-size: 40px" fill="#3498DB"></text> 
    <text class="timer" text-anchor="middle" x="175" y="35" style="font-size: 22px; undefined;" fill="#aaa">50%</text> 
</svg> 

</div> 

FIDDLE HERE,我的困難是關於以下SVG元素,以及如何文本錨屬性適用於它:

<text class="timer" text-anchor="middle" x="175" y="35" style="font-size: 22px; undefined;" fill="#aaa">50%</text> 

現在,如果我更改text-anchor="start",文本元素不會真正移動到svg元素的開頭,而是移動到它下面的一行的開始處,爲什麼?任何人都可以解釋爲什麼text-anchor="start",不按預期工作?

回答

1

text-anchor用於決定文本的X位置應該位於文本的開始,結尾還是中間。要移動文本位置,請將它改爲X和Y座標。

把文本在SVG的開始:

x="0" 
text-anchor="start" 

把文本在SVG的末尾:

x="194" //Width of the svg 
text-anchor="end" 

把文本在SVG的中間:

x="97" //Half of the width of the svg 
text-anchor="middle" 
+1

輝煌..輝煌...輝煌! –