I'm trying to display an icon in the center of each path element but it doesn't look right
My code simply calculated the center point based on the width & height of the path
const center = {
x: (bbox.x - svg_box.x) + bbox.width / 2,
y: (bbox.y - svg_box.y) + bbox.height / 2,
}
Can this be improved using a centroid function? Or using d3?
I could not figure out how to find the centroid of an existing path using d3.
Thank you

D3 has two centroid methods:
arc.centroidandpath.centroid(from d3-geo), and none will work with path elements like you have here.However, we can use
path.centroidfor getting the centroids of those paths, but it's quite hacky: you have to create a geoJSON object based on your actual path just to pass that object topath.centroid. Therefore, you'd be better creating your own.That said, let's see how that approach works. We can iterate over each path, getting its length and setting a dummy geoJSON object:
Then, we move along the path and populate the geoJSON object (here 400/1237 is just a quick way to calculate the viewport values, you can use a proper matrix if you want)...
...and finally we pass that object to
path.centroid:Here's the snippet with that solution: