Skip to content

Commit

Permalink
fix: fix the error of image rotating too many times after reseting im…
Browse files Browse the repository at this point in the history
…age (#3108)
  • Loading branch information
sylsaint authored Sep 18, 2024
1 parent 6129503 commit d41a2db
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/image-viewer/hooks/useRotate.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
// 旋转控制
import { useCallback, useState } from 'react';
import { useCallback, useRef, useState } from 'react';

const useRotate = () => {
// There is an useEffect in the line 472 of ImageViewerModal.tsx, so we need to use a ref to store the rotateZ value.
const rotRef = useRef(0);
const [rotateZ, setRotateZ] = useState(0);

const onRotate = useCallback((ROTATE_COUNT: number) => {
setRotateZ((rotateZ) => rotateZ + ROTATE_COUNT);
setRotateZ((rotateZ) => {
rotRef.current = rotateZ + ROTATE_COUNT;
return rotateZ + ROTATE_COUNT;
});
}, []);

const onResetRotate = useCallback(() => setRotateZ(0), []);
const onResetRotate = useCallback(() => {
let degreeToRotate = rotRef.current % 360;
// make sure we always rotate to the shortest direction
if (Math.abs(degreeToRotate) > 180) {
degreeToRotate = (degreeToRotate + 360) % 360;
}
if (degreeToRotate !== 0) {
setRotateZ((rotateZ) => {
rotRef.current = rotateZ - degreeToRotate;
return rotateZ - degreeToRotate;
});
}
}, []);

return {
rotateZ,
Expand Down

0 comments on commit d41a2db

Please sign in to comment.