简体   繁体   English

当此标记加载时,如何更改svg文本标记XY?

[英]How can I change svg text tag X Y when this tag load?

svg code is here svg代码在这里

  function calculateXY(text, angle) { text.setAttribute('x',111.5 * Math.cos(angle) + 142.5); text.setAttribute('y',111.5 * Math.sin(angle) + 142.5); } 
 <svg width="285" height="285" class="pie-svg" > <g> <circle class="pie-1" r="111.5" cx="142.5" cy="142.5" stroke="#303840" stroke-dasharray="175 525" transform="rotate(0, 142.5,142.5)"></circle> <text class="pie-text-percent" onload="calculateXY(this, 45)" fill="#ffffff" transform="rotate(90, 142.5,142.5)">25%</text> </g> </svg> 

I want to change text xy when this tag load, but it not working, what can I do? 我想在这个标签加载时更改文本xy,但它不起作用,我该怎么办?

I've changed text.setAttribute to text.setAttributeNS and it kind of works. 我已经将text.setAttribute更改为text.setAttributeNS ,它有点工作。 However the text falls outside the svg canvas and you can see it because I've set the svg overflow to visible. 但是文本落在svg画布之外,你可以看到它,因为我已经将svg overflow设置为可见。 Now I need to understand where do you need the text. 现在我需要了解你需要文本的位置。

 function calculateXY(text, angle) { text.setAttributeNS(null,'x',111.5 * Math.cos(angle) + 142.5); text.setAttributeNS(null,'y',111.5 * Math.sin(angle) + 142.5); } 
 svg{border:1px solid;overflow:visible} circle{fill:none} 
 <svg width="285" height="285" class="pie-svg" > <g> <circle class="pie-1" r="111.5" cx="142.5" cy="142.5" stroke="#303840" stroke-dasharray="175 525" transform="rotate(0, 142.5,142.5)"></circle> <text class="pie-text-percent" onload="calculateXY(this, 45)" fill="#000" transform="rotate(90, 142.5,142.5)">25%</text> </g> </svg> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM