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/transformers/partials/transformRotationAndPosition.ts
Jordi Sala Morales 202e7f4fda
Apply rotations to any figure (#168)
* Apply rotations to any figure

* add changelog

* apply rotations to curves too
2024-06-17 12:14:16 +02:00

46 lines
1.3 KiB
TypeScript

import { translateRotation, translateZeroRotation } from '@plugin/translators';
import { applyInverseRotation, hasRotation } from '@plugin/utils';
import { ShapeBaseAttributes, ShapeGeomAttributes } from '@ui/lib/types/shapes/shape';
export const transformRotation = (
node: LayoutMixin,
baseRotation: number
): Pick<ShapeBaseAttributes, 'transform' | 'transformInverse' | 'rotation'> => {
const rotation = node.rotation + baseRotation;
if (!hasRotation(rotation)) {
return translateZeroRotation();
}
return translateRotation(node.absoluteTransform, rotation);
};
export const transformRotationAndPosition = (
node: LayoutMixin,
baseRotation: number
): Pick<ShapeBaseAttributes, 'transform' | 'transformInverse' | 'rotation'> &
Pick<ShapeGeomAttributes, 'x' | 'y'> => {
const rotation = node.rotation + baseRotation;
const x = node.absoluteTransform[0][2];
const y = node.absoluteTransform[1][2];
if (!hasRotation(rotation) || !node.absoluteBoundingBox) {
return {
x,
y,
...translateZeroRotation()
};
}
const referencePoint = applyInverseRotation(
{ x, y },
node.absoluteTransform,
node.absoluteBoundingBox
);
return {
...referencePoint,
...translateRotation(node.absoluteTransform, rotation)
};
};