Skip to content

Commit

Permalink
Improve performance of rotateImage
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejkustra committed Nov 5, 2024
1 parent f473d06 commit 4ac36b1
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/components/AvatarCropModal/AvatarCropModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,20 +260,17 @@ function AvatarCropModal({imageUri = '', imageName = '', imageType = '', onClose
// Rotates the image by changing the rotation value by 90 degrees
// and updating the position so the image remains in the same place after rotation
const rotateImage = useCallback(() => {
rotation.set((value) => value - 90);
runOnUI(() => {
rotation.set((value) => value - 90);

const oldX = translateX.get();
const oldY = translateY.get();
const oldHeight = originalImageHeight.get();
const oldWidth = originalImageWidth.get();
const oldTranslateX = translateX.get();
translateX.set(translateY.get());
translateY.set(oldTranslateX * -1);

// Rotating 2d coordinates by applying the formula (x, y) → (-y, x).
translateX.set(oldY);
translateY.set(oldX * -1);

// Since we rotated the image by 90 degrees, now width becomes height and vice versa.
originalImageHeight.set(oldWidth);
originalImageWidth.set(oldHeight);
const oldOriginalImageHeight = originalImageHeight.get();
originalImageHeight.set(originalImageWidth.get());
originalImageWidth.set(oldOriginalImageHeight);
})();
}, [originalImageHeight, originalImageWidth, rotation, translateX, translateY]);

// Crops an image that was provided in the imageUri prop, using the current position/scale
Expand Down

0 comments on commit 4ac36b1

Please sign in to comment.