2016-04-03 72 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

辉煌..辉煌...辉煌! –