Skip to content
This repository was archived by the owner on Jan 19, 2023. It is now read-only.

Commit 8e3dc38

Browse files
committed
Fix zoom out behavior #25
1 parent e3237ea commit 8e3dc38

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

src/react-image-lightbox.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,24 +220,26 @@ class ReactImageLightbox extends Component {
220220

221221
// Default to the center of the image to zoom when no mouse position specified
222222
const boxRect = this.getLightboxRect();
223-
const percentXInCurrentBox = typeof clientX !== 'undefined' ?
224-
((clientX - boxRect.left - ((boxRect.width - imageBaseSize.width) / 2)) / imageBaseSize.width) :
225-
0.5;
226-
const percentYInCurrentBox = typeof clientY !== 'undefined' ?
227-
((clientY - boxRect.top - ((boxRect.height - imageBaseSize.height) / 2)) / imageBaseSize.height) :
228-
0.5;
223+
const pointerX = typeof clientX !== 'undefined' ? clientX - boxRect.left : boxRect.width / 2;
224+
const pointerY = typeof clientY !== 'undefined' ? clientY - boxRect.top : boxRect.height / 2;
229225

230-
const currentImageWidth = imageBaseSize.width * currentZoomMultiplier;
231-
const currentImageHeight = imageBaseSize.height * currentZoomMultiplier;
226+
const currentImageOffsetX = ((boxRect.width - (imageBaseSize.width * currentZoomMultiplier)) / 2);
227+
const currentImageOffsetY = ((boxRect.height - (imageBaseSize.height * currentZoomMultiplier)) / 2);
232228

233-
const nextImageWidth = imageBaseSize.width * nextZoomMultiplier;
234-
const nextImageHeight = imageBaseSize.height * nextZoomMultiplier;
229+
const currentImageRealOffsetX = currentImageOffsetX - this.state.offsetX;
230+
const currentImageRealOffsetY = currentImageOffsetY - this.state.offsetY;
235231

236-
const deltaX = (currentImageWidth - nextImageWidth) * (percentXInCurrentBox - 0.5);
237-
const deltaY = (currentImageHeight - nextImageHeight) * (percentYInCurrentBox - 0.5);
232+
const currentPointerXRelativeToImage = (pointerX - currentImageRealOffsetX) / currentZoomMultiplier;
233+
const currentPointerYRelativeToImage = (pointerY - currentImageRealOffsetY) / currentZoomMultiplier;
238234

239-
let nextOffsetX = this.state.offsetX - deltaX;
240-
let nextOffsetY = this.state.offsetY - deltaY;
235+
const nextImageRealOffsetX = pointerX - (currentPointerXRelativeToImage * nextZoomMultiplier);
236+
const nextImageRealOffsetY = pointerY - (currentPointerYRelativeToImage * nextZoomMultiplier);
237+
238+
const nextImageOffsetX = ((boxRect.width - (imageBaseSize.width * nextZoomMultiplier)) / 2);
239+
const nextImageOffsetY = ((boxRect.height - (imageBaseSize.height * nextZoomMultiplier)) / 2);
240+
241+
let nextOffsetX = nextImageOffsetX - nextImageRealOffsetX;
242+
let nextOffsetY = nextImageOffsetY - nextImageRealOffsetY;
241243

242244
// When zooming out, limit the offset so things don't get left askew
243245
const maxOffsets = this.getMaxOffsets();

0 commit comments

Comments
 (0)