Home > front end >  Can be SVG path centroid calculated with paper.js?
Can be SVG path centroid calculated with paper.js?

Time:10-28

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?

CodePudding user response:

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

  • Related