0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-22 21:53:27 -05:00
penpot-exporter-figma-plugin/plugin-src/translators/translateVectorPaths.ts
Jordi Sala Morales 73ccf83657
Vectors - Pen & pencil (#35)
* path shapes

* add polygon node

* wip vector

* implement translation of commands and paths

* vectors and polygon

* strokes

* star

* deformed shapes

* simplify star

* simplify star

* fix transformers

---------

Co-authored-by: Alex Sánchez <sion333@gmail.com>
2024-04-16 16:08:39 +02:00

60 lines
1.5 KiB
TypeScript

import SVGPathCommander from 'svg-path-commander';
import { Segment } from '@ui/lib/types/path/PathContent';
export const translateVectorPaths = (
paths: VectorPaths,
baseX: number,
baseY: number
): Segment[] => {
let segments: Segment[] = [];
for (const path of paths) {
segments = [...segments, ...translateVectorPath(path, baseX, baseY)];
}
return segments;
};
const translateVectorPath = (path: VectorPath, baseX: number, baseY: number): Segment[] => {
const segments: Segment[] = [];
const normalizedPath = SVGPathCommander.normalizePath(path.data);
for (const [command, ...rest] of normalizedPath) {
switch (command) {
case 'M':
segments.push({
command: 'move-to',
params: { x: (rest[0] ?? 0) + baseX, y: (rest[1] ?? 0) + baseY }
});
break;
case 'L':
segments.push({
command: 'line-to',
params: { x: (rest[0] ?? 0) + baseX, y: (rest[1] ?? 0) + baseY }
});
break;
case 'C':
segments.push({
command: 'curve-to',
params: {
c1x: (rest[0] ?? 0) + baseX,
c1y: (rest[1] ?? 0) + baseY,
c2x: (rest[2] ?? 0) + baseX,
c2y: (rest[3] ?? 0) + baseY,
x: (rest[4] ?? 0) + baseX,
y: (rest[5] ?? 0) + baseY
}
});
break;
case 'Z':
segments.push({
command: 'close-path'
});
break;
}
}
return segments;
};