Skip to content

Commit 03f9e26

Browse files
fix(zoom-pane): set min and max values correctly when image is smaller than container (#69)
* fix(zoom-pane): set min and max values correctly when image is smaller than container * chore(zoom-pane): organise code slightly better * Sync with Prettier
1 parent 70bd3ca commit 03f9e26

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/js/ZoomPane.js

+36-24
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,32 @@ export default class ZoomPane {
8080
// `percentageOffsetX` and `percentageOffsetY` must be percentages
8181
// expressed as floats between `0' and `1`.
8282
setPosition(percentageOffsetX, percentageOffsetY, triggerRect) {
83-
let left = -(this.imgEl.clientWidth * percentageOffsetX - this.el.clientWidth / 2);
84-
let top = -(this.imgEl.clientHeight * percentageOffsetY - this.el.clientHeight / 2);
85-
let maxLeft = -(this.imgEl.clientWidth - this.el.clientWidth);
86-
let maxTop = -(this.imgEl.clientHeight - this.el.clientHeight);
83+
const { width: imgElWidth, height: imgElHeight } = this.imgEl.getBoundingClientRect();
84+
const { width: elWidth, height: elHeight } = this.el.getBoundingClientRect();
85+
86+
const centreOfContainerX = elWidth / 2;
87+
const centreOfContainerY = elHeight / 2;
88+
89+
const targetImgXToBeCentre = imgElWidth * percentageOffsetX;
90+
const targetImgYToBeCentre = imgElHeight * percentageOffsetY;
91+
92+
let left = centreOfContainerX - targetImgXToBeCentre;
93+
let top = centreOfContainerY - targetImgYToBeCentre;
94+
95+
const differenceBetweenContainerWidthAndImgWidth = elWidth - imgElWidth;
96+
const differenceBetweenContainerHeightAndImgHeight = elHeight - imgElHeight;
97+
const isContainerLargerThanImgX = differenceBetweenContainerWidthAndImgWidth > 0;
98+
const isContainerLargerThanImgY = differenceBetweenContainerHeightAndImgHeight > 0;
99+
100+
const minLeft = isContainerLargerThanImgX ? differenceBetweenContainerWidthAndImgWidth / 2 : 0;
101+
const minTop = isContainerLargerThanImgY ? differenceBetweenContainerHeightAndImgHeight / 2 : 0;
102+
103+
const maxLeft = isContainerLargerThanImgX
104+
? differenceBetweenContainerWidthAndImgWidth / 2
105+
: differenceBetweenContainerWidthAndImgWidth;
106+
const maxTop = isContainerLargerThanImgY
107+
? differenceBetweenContainerHeightAndImgHeight / 2
108+
: differenceBetweenContainerHeightAndImgHeight;
87109

88110
if (this.el.parentElement === this.settings.inlineContainer) {
89111
// This may be needed in the future to deal with browser event
@@ -94,31 +116,21 @@ export default class ZoomPane {
94116
let scrollY = window.pageYOffset;
95117

96118
let inlineLeft =
97-
triggerRect.left +
98-
percentageOffsetX * triggerRect.width -
99-
this.el.clientWidth / 2 +
100-
this.settings.inlineOffsetX +
101-
scrollX;
119+
triggerRect.left + percentageOffsetX * triggerRect.width - elWidth / 2 + this.settings.inlineOffsetX + scrollX;
102120
let inlineTop =
103-
triggerRect.top +
104-
percentageOffsetY * triggerRect.height -
105-
this.el.clientHeight / 2 +
106-
this.settings.inlineOffsetY +
107-
scrollY;
121+
triggerRect.top + percentageOffsetY * triggerRect.height - elHeight / 2 + this.settings.inlineOffsetY + scrollY;
108122

109123
if (this.settings.containInline) {
110-
let elRect = this.el.getBoundingClientRect();
111-
112124
if (inlineLeft < triggerRect.left + scrollX) {
113125
inlineLeft = triggerRect.left + scrollX;
114-
} else if (inlineLeft + this.el.clientWidth > triggerRect.left + triggerRect.width + scrollX) {
115-
inlineLeft = triggerRect.left + triggerRect.width - this.el.clientWidth + scrollX;
126+
} else if (inlineLeft + elWidth > triggerRect.left + triggerRect.width + scrollX) {
127+
inlineLeft = triggerRect.left + triggerRect.width - elWidth + scrollX;
116128
}
117129

118130
if (inlineTop < triggerRect.top + scrollY) {
119131
inlineTop = triggerRect.top + scrollY;
120-
} else if (inlineTop + this.el.clientHeight > triggerRect.top + triggerRect.height + scrollY) {
121-
inlineTop = triggerRect.top + triggerRect.height - this.el.clientHeight + scrollY;
132+
} else if (inlineTop + elHeight > triggerRect.top + triggerRect.height + scrollY) {
133+
inlineTop = triggerRect.top + triggerRect.height - elHeight + scrollY;
122134
}
123135
}
124136

@@ -127,14 +139,14 @@ export default class ZoomPane {
127139
}
128140

129141
if (!this.settings.showWhitespaceAtEdges) {
130-
if (left > 0) {
131-
left = 0;
142+
if (left > minLeft) {
143+
left = minLeft;
132144
} else if (left < maxLeft) {
133145
left = maxLeft;
134146
}
135147

136-
if (top > 0) {
137-
top = 0;
148+
if (top > minTop) {
149+
top = minTop;
138150
} else if (top < maxTop) {
139151
top = maxTop;
140152
}

0 commit comments

Comments
 (0)