0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 13:43:03 -05:00
penpot-exporter-figma-plugin/plugin-src/utils/getBoundingBox.ts
Jordi Sala Morales 2557cbdacc
Implement flatten (#105)
* Implement flatten

* fix comment

* Add changelog
2024-05-13 13:21:46 +02:00

25 lines
714 B
TypeScript

import { parseSVG } from 'svg-path-parser';
type BoundingBox = { x1: number; y1: number; x2: number; y2: number };
export const getBoundingBox = (vectorPath: VectorPath): BoundingBox => {
const path = parseSVG(vectorPath.data);
if (!path.length) return { x1: 0, y1: 0, x2: 0, y2: 0 };
const bounds = { x1: Infinity, y1: Infinity, x2: -Infinity, y2: -Infinity };
for (const points of path) {
switch (points.code) {
case 'M':
case 'L':
case 'C':
bounds.x1 = Math.min(bounds.x1, points.x);
bounds.y1 = Math.min(bounds.y1, points.y);
bounds.x2 = Math.max(bounds.x2, points.x);
bounds.y2 = Math.max(bounds.y2, points.y);
}
}
return bounds;
};