javascript - 加载此标签时如何更改 svg 文本标签 XY?
问题描述
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>
我想在加载此标签时更改文本 xy,但它不起作用,我该怎么办?
解决方案
我已经改为text.setAttribute
并且text.setAttributeNS
它有点工作。但是,文本位于 svg 画布之外,您可以看到它,因为我已将 svg 溢出设置为可见。现在我需要了解您在哪里需要文本。
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>
推荐阅读
- php - 当搜索结果中没有可用记录时隐藏刀片的分页-Laravel
- python - 如何打印单词直到达到特定单词?
- apache-kafka - 持久化 kafka 主题配置设置
- java - 如何在 Java 中重新抛出异常
- django - 模型表单没有保存在 django 中,它告诉用户不能为 nul
- swift - 为预览目的创建一个假的 NSManagedObjectContext?
- postgresql - 如何使用 Docker compose 填充在 Docker 容器内运行的 PostgreSQL 数据库
- flutter - Flutter Qs:图像预览与 SizedBox 大小不同
- arrays - 对象数组内的过滤器数组
- ios - YTLiveStreaming 错误:响应状态代码不可接受:400