How to get Mid point of <g> tag in svg using javascript

VIV*_*MDU 2 html javascript svg

I am working in SVG tags using javascript. I tried to get group tag <g> midpoint in svg. Is it possible to get mid point value of group tag using javascript? Here's my demo group tag <g>

<g id="object_7" transform="translate(573,703) scale(0.5,0.51)" style="pointer-events:inherit">

<path d="m-40,-19l3,-3l74,0l3,3l0,37l-3,3l-74,0l-3,-3l0,-37z" id="uid127" stroke-linejoin="round" stroke-linecap="round" fill="#1e1d19" stroke="#000000"/>

   <path d="m-9,21l4,2l10,0l4,-2" id="uid129" stroke-linejoin="round" stroke-linecap="round" fill-opacity="0" fill="none" stroke="#000"/>

   <path d="m-40,-19l3,-3l74,0l3,3l-77,40l-3,-3l0,-37z" id="uid131" stroke-linejoin="round" stroke-linecap="round" fill-opacity="0.12" fill="#000000"/>

</g>
Run Code Online (Sandbox Code Playgroud)

Here I need to get midpoint point of group tag. I used to get mouse co-ordinates for getting center of x and y position in group tag, but I did not achieve it. Can anyone please guide me?

Pau*_*eau 6

您可以<g>通过获取元素的引用并调用函数来获得元素的边界框getBBox()

var  bbox = document.getElementById("object_7").getBBox();
Run Code Online (Sandbox Code Playgroud)

但是请注意,这是该组子级的所有边界框的并集​​。如果组具有转换,则不会反映在bbox值中。如果要向组中添加元素,则可能是您想要的元素。

如果要在屏幕空间中确定对象的边界,则可以获取组元素的变换并将其应用于已计算的中心点。

var  ctm = document.getElementById("object_7").getCTM()

// Calculate the centre of the group
var cx = bbox.x + bbox.width/2;
var cy = bbox.y + bbox.height/2;

// Transform cx,cy by the group's transform
var pt = document.getElementById("mysvg").createSVGPoint();
pt.x = cx;
pt.y = cy;
pt = pt.matrixTransform(ctm);

// centre point in screen coordinates is in pt.x and pt.y
Run Code Online (Sandbox Code Playgroud)

在这里演示