SVG路径质心可以用paper.js计算吗?

Can be SVG path centroid calculated with paper.js?

我想获取 SVG 路径的重心。我已经在使用 paper.js 进行路径操作,但我可以看到任何选项来计算它。有什么办法吗?

如果您有 Path object (preferably without self-intersections), you can create an approximate polygon clone with the .flatten() 函数:

const path = ...

//Approximate polyline/polygon:
const poly = path.clone();
poly.flatten(8);
const polyPoints = poly.exportJSON({ asString: false })[1].segments;

然后,网上有几个资源解释了如何计算多边形的质心,例如:

  • Finding the centroid of a polygon?
  • How can you find the centroid of a concave irregular polygon in JavaScript?

Full example