0

I would like to get centre of gravity for SVG path. I am already using paper.js for paths manipulations but I can see any option to calculate this. Is it possible some way?

farincz
  • 4,943
  • 1
  • 28
  • 38

1 Answers1

1

If you have a Path object (preferably without self-intersections), you can create an approximate polygon clone with the .flatten() function:

const path = ...

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

Then, there are several resources online that explain how to calculate the centroid of a polygon, for example:

Full example

Sphinxxx
  • 12,484
  • 4
  • 54
  • 84