如何找到SVG文本位置

时间:2015-01-20 17:01:24

标签: javascript html5 svg

这是我的代码,

<html>
<body>
<svg id="text" height="400" width="400" >
<text transform="rotate(127,128,64)" opacity="1" text-anchor="start" font-weight="regular" font-style="Normal" font-family="Segoe UI" font-size="12px" fill="#191919" y="68.25" x="128" id="caption">Revenue </text>
 </div>
</body>
</html>

文本(Revenue)start x location是128。 如何找到文本的最后一个字符(e)的x位置(收入)。 需要真正的计算。因为学位的轮换是可以改变的。

1 个答案:

答案 0 :(得分:0)

调用getStartPositionOfChar并通过转换它们来转换局部坐标,例如

&#13;
&#13;
var pos = document.getElementById("text").getStartPositionOfChar(6);
var matrix = document.getElementById("text").transform.animVal.getItem(0).matrix;
var position = pos.matrixTransform(matrix);
alert(position.x + ", " + position.y);
&#13;
<html>
<body>
<svg height="400" width="400" >
<text id="text" transform="rotate(127,128,64)" opacity="1" text-anchor="start" font-weight="regular" font-style="Normal" font-family="Segoe UI" font-size="12px" fill="#191919" y="68.25" x="128" id="caption">Revenue </text>
 </div>
</body>
</html>
&#13;
&#13;
&#13;