mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2025-01-03 13:20:37 -05:00
af81fc7e92
* Implement rotation for vector lines * wip * Improve rotations for lines * add changelog * add layout attributes to line
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { Command, CurveToCommand, LineToCommand, MoveToCommand } from 'svg-path-parser';
|
|
|
|
import { Segment } from '@ui/lib/types/shapes/pathShape';
|
|
|
|
export const translateCommandsToSegments = (
|
|
commands: Command[],
|
|
baseX: number,
|
|
baseY: number
|
|
): Segment[] => {
|
|
return commands.map(command => {
|
|
switch (command.command) {
|
|
case 'moveto':
|
|
return translateMoveToCommand(command, baseX, baseY);
|
|
case 'lineto':
|
|
return translateLineToCommand(command, baseX, baseY);
|
|
case 'curveto':
|
|
return translateCurveToCommand(command, baseX, baseY);
|
|
case 'closepath':
|
|
default:
|
|
return {
|
|
command: 'close-path'
|
|
};
|
|
}
|
|
});
|
|
};
|
|
|
|
const translateMoveToCommand = (command: MoveToCommand, baseX: number, baseY: number): Segment => {
|
|
return {
|
|
command: 'move-to',
|
|
params: { x: command.x + baseX, y: command.y + baseY }
|
|
};
|
|
};
|
|
|
|
const translateLineToCommand = (command: LineToCommand, baseX: number, baseY: number): Segment => {
|
|
return {
|
|
command: 'line-to',
|
|
params: { x: command.x + baseX, y: command.y + baseY }
|
|
};
|
|
};
|
|
|
|
const translateCurveToCommand = (
|
|
command: CurveToCommand,
|
|
baseX: number,
|
|
baseY: number
|
|
): Segment => {
|
|
return {
|
|
command: 'curve-to',
|
|
params: {
|
|
c1x: command.x1 + baseX,
|
|
c1y: command.y1 + baseY,
|
|
c2x: command.x2 + baseX,
|
|
c2y: command.y2 + baseY,
|
|
x: command.x + baseX,
|
|
y: command.y + baseY
|
|
}
|
|
};
|
|
};
|