Skip to content

Commit

Permalink
Feat: better rotation snapping (elastic#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
monfera authored Jul 27, 2018
1 parent 7dad37a commit ce5e1cb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 2 additions & 3 deletions public/lib/aeroelastic/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const rotateAnnotationOffset = 12;
const rotationHandleName = 'rotationHandle';
const rotationHandleSize = 14;
const resizeHandleName = 'resizeHandle';
const rotateSnapInDeg = 5;
const rotateSnapInRad = rotateSnapInDeg / 180 * Math.PI;
const rotateSnapInPixels = 10;
const shortcuts = false;
const singleSelect = true;
const snapConstraint = false;
Expand All @@ -35,7 +34,7 @@ module.exports = {
resizeConnectorName,
resizeHandleName,
rotateAnnotationOffset,
rotateSnapInRad,
rotateSnapInPixels,
rotationHandleName,
rotationHandleSize,
shortcuts,
Expand Down
9 changes: 7 additions & 2 deletions public/lib/aeroelastic/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ const rotationManipulation = ({ shape, directShape, cursorPosition: { x, y } })
const oldAngle = Math.atan2(centerPosition[1] - vector[1], centerPosition[0] - vector[0]);
const newAngle = Math.atan2(centerPosition[1] - y, centerPosition[0] - x);
const closest45deg = Math.round(newAngle / (Math.PI / 4)) * Math.PI / 4;
const absDiff = Math.abs(closest45deg - newAngle);
const newSnappedAngle = absDiff < config.rotateSnapInRad ? closest45deg : newAngle;
const radius = Math.sqrt(Math.pow(centerPosition[0] - x, 2) + Math.pow(centerPosition[1] - y, 2));
const closest34degPosition = [Math.cos(closest45deg) * radius, Math.sin(closest45deg) * radius];
const pixelDifference = Math.sqrt(
Math.pow(closest34degPosition[0] - (centerPosition[0] - x), 2) +
Math.pow(closest34degPosition[1] - (centerPosition[1] - y), 2)
);
const newSnappedAngle = pixelDifference < config.rotateSnapInPixels ? closest45deg : newAngle;
const result = matrix.rotateZ(oldAngle - newSnappedAngle);
return { transforms: [result], shapes: [shape.id] };
};
Expand Down

0 comments on commit ce5e1cb

Please sign in to comment.