mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-22 21:53:27 -05:00
202e7f4fda
* Apply rotations to any figure * add changelog * apply rotations to curves too
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { Command, CurveToCommand, LineToCommand, MoveToCommand } from 'svg-path-parser';
|
|
|
|
import { Segment } from '@ui/lib/types/shapes/pathShape';
|
|
|
|
export const translateNonRotatedCommands = (
|
|
commands: Command[],
|
|
baseX: number = 0,
|
|
baseY: number = 0
|
|
): Segment[] => {
|
|
return commands.map(command => translateNonRotatedCommand(command, baseX, baseY));
|
|
};
|
|
|
|
export const translateNonRotatedCommand = (
|
|
command: Command,
|
|
baseX: number,
|
|
baseY: number
|
|
): Segment => {
|
|
switch (command.command) {
|
|
case 'moveto':
|
|
return translateMoveTo(command, baseX, baseY);
|
|
case 'lineto':
|
|
return translateLineTo(command, baseX, baseY);
|
|
case 'curveto':
|
|
return translateCurveTo(command, baseX, baseY);
|
|
case 'closepath':
|
|
default:
|
|
return {
|
|
command: 'close-path'
|
|
};
|
|
}
|
|
};
|
|
|
|
const translateMoveTo = (command: MoveToCommand, baseX: number, baseY: number): Segment => {
|
|
return {
|
|
command: 'move-to',
|
|
params: { x: command.x + baseX, y: command.y + baseY }
|
|
};
|
|
};
|
|
|
|
const translateLineTo = (command: LineToCommand, baseX: number, baseY: number): Segment => {
|
|
return {
|
|
command: 'line-to',
|
|
params: { x: command.x + baseX, y: command.y + baseY }
|
|
};
|
|
};
|
|
|
|
const translateCurveTo = (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
|
|
}
|
|
};
|
|
};
|