如何获取分组svg元素的全局坐标?

Dan*_*nor 8 javascript svg coordinates

假设我有以下文件(小提琴):

<svg>
    <g transform="translate(50,50)">
        <rect width="20" height="20" style="fill:black;">
    </g>
</svg>
Run Code Online (Sandbox Code Playgroud)

假设我不知道它在一个组中,我如何得到rect元素的全局坐标?

kit*_*.eb 13

实际上很难找到.SVGElement在页面中搜索结果的方法说SVGElement没有方法!它实际上有很多方法,但它们是继承的:

http://www.w3.org/TR/SVG/types.html#InterfaceSVGLocatable

根据您的需要,您可以使用结果getCTMgetScreenCTM转换a SVGPoint,从而了解您的元素所在:

root = document.getElementById('root')
rec = document.getElementById('rec')
point = root.createSVGPoint()
# This is the top-left relative to the SVG element
ctm = rec.getCTM()
console.log point.matrixTransform(ctm)
# This is the top-left relative to the document
ctm = rec.getScreenCTM()
console.log point.matrixTransform(ctm)
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/kitsu_eb/ZXVAX/3/