javascript - 获取 svg 元素的坐标返回 SVGAnimatedLength

标签 javascript svg dom

我尝试了一个简单的测试来了解 svg + JS 的行为,因为我需要检查某个 SVG 对象是否在 svg 路径上并采取相应的操作。当尝试显式访问 SVG 元素(在本例中为椭圆)的 .cx 或 .cy 属性时,我得到的是 SVGAnimatedLength 对象,而不是获取坐标。

<svg
   id="contenedorCirculo"
   width="200mm"
   height="200mm"
   viewBox="0 0 200 200"
   version="1.1"
>
<g
   inkscape:groupmode="layer"
   id="layer3"
   inkscape:label="points">
<ellipse
 style="fill:#ff00e5;fill-opacity:1;stroke:#fbde00;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
 id="circulo"
 cx="50"
 cy="50"
 rx="5"
 ry="5" />

</g>
</svg>

<script>
//this returns SVGAnimatedLength { baseVal: SVGLength, animVal: SVGLength }
console.log(document.getElementById("circulo").cx);

</script>

最佳答案

您可能正在寻找值(value)。因为SMIL有一个animated value and a base value对于许多 SVG 属性。除非您实际使用 SMIL animValbaseVal会是一样的

如果你想设置这个值,你应该设置baseValanimVal 只能通过 SMIL 设置。

<svg
   id="contenedorCirculo"
   width="200mm"
   height="200mm"
   viewBox="0 0 200 200"
   version="1.1"
>
<g
   inkscape:groupmode="layer"
   id="layer3"
   inkscape:label="points">
<ellipse
 style="fill:#ff00e5;fill-opacity:1;stroke:#fbde00;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1"
 id="circulo"
 cx="50"
 cy="50"
 rx="5"
 ry="5" />

</g>
</svg>

<script>
//this returns SVGAnimatedLength { baseVal: SVGLength, animVal: SVGLength }
console.log(document.getElementById("circulo").cx.animVal.value);

</script>

关于javascript - 获取 svg 元素的坐标返回 SVGAnimatedLength,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61341892/

相关文章:

android - App图片的局限性

scala - 当我使用 Binding.scala 时,出现错误 `each instructions must be inside a SDE block` ,我该如何解决?

javascript - 使用 Javascript 创建 newOption() 时附加文本和值

javascript - 在javascript中,需要执行动态数组的求和

javascript - Node.js 加密输入/输出类型

javascript - 如何使用 d3.js 拖动行为拖动 svg 组?

javascript - jQuery 中的重建元素与批量显示/隐藏

javascript - 将参数传递给函数与使用类级别变量之间的根本区别是什么?

javascript - 如何使用 canvas 应用 alpha 图层蒙版使某些图像透明

javascript - 为什么将 x 和 y 设置为 0 时 svg 文本消失?