-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(zoom-pane): set min and max values correctly when image is smaller than container #69
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,10 +80,32 @@ export default class ZoomPane { | |
// `percentageOffsetX` and `percentageOffsetY` must be percentages | ||
// expressed as floats between `0' and `1`. | ||
setPosition(percentageOffsetX, percentageOffsetY, triggerRect) { | ||
let left = -(this.imgEl.clientWidth * percentageOffsetX - this.el.clientWidth / 2); | ||
let top = -(this.imgEl.clientHeight * percentageOffsetY - this.el.clientHeight / 2); | ||
let maxLeft = -(this.imgEl.clientWidth - this.el.clientWidth); | ||
let maxTop = -(this.imgEl.clientHeight - this.el.clientHeight); | ||
const { width: imgElWidth, height: imgElHeight } = this.imgEl.getBoundingClientRect(); | ||
const { width: elWidth, height: elHeight } = this.el.getBoundingClientRect(); | ||
|
||
const centreOfContainerX = elWidth / 2; | ||
const centreOfContainerY = elHeight / 2; | ||
|
||
const targetImgXToBeCentre = imgElWidth * percentageOffsetX; | ||
const targetImgYToBeCentre = imgElHeight * percentageOffsetY; | ||
|
||
let left = centreOfContainerX - targetImgXToBeCentre; | ||
let top = centreOfContainerY - targetImgYToBeCentre; | ||
|
||
const differenceBetweenContainerWidthAndImgWidth = elWidth - imgElWidth; | ||
const differenceBetweenContainerHeightAndImgHeight = elHeight - imgElHeight; | ||
const isContainerLargerThanImgX = differenceBetweenContainerWidthAndImgWidth > 0; | ||
const isContainerLargerThanImgY = differenceBetweenContainerHeightAndImgHeight > 0; | ||
|
||
const minLeft = isContainerLargerThanImgX ? differenceBetweenContainerWidthAndImgWidth / 2 : 0; | ||
const minTop = isContainerLargerThanImgY ? differenceBetweenContainerHeightAndImgHeight / 2 : 0; | ||
|
||
const maxLeft = isContainerLargerThanImgX | ||
? differenceBetweenContainerWidthAndImgWidth / 2 | ||
: differenceBetweenContainerWidthAndImgWidth; | ||
const maxTop = isContainerLargerThanImgY | ||
? differenceBetweenContainerHeightAndImgHeight / 2 | ||
: differenceBetweenContainerHeightAndImgHeight; | ||
|
||
if (this.el.parentElement === this.settings.inlineContainer) { | ||
// This may be needed in the future to deal with browser event | ||
|
@@ -94,31 +116,21 @@ export default class ZoomPane { | |
let scrollY = window.pageYOffset; | ||
|
||
let inlineLeft = | ||
triggerRect.left + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes are just performance changes, removing calls to |
||
percentageOffsetX * triggerRect.width - | ||
this.el.clientWidth / 2 + | ||
this.settings.inlineOffsetX + | ||
scrollX; | ||
triggerRect.left + percentageOffsetX * triggerRect.width - elWidth / 2 + this.settings.inlineOffsetX + scrollX; | ||
let inlineTop = | ||
triggerRect.top + | ||
percentageOffsetY * triggerRect.height - | ||
this.el.clientHeight / 2 + | ||
this.settings.inlineOffsetY + | ||
scrollY; | ||
triggerRect.top + percentageOffsetY * triggerRect.height - elHeight / 2 + this.settings.inlineOffsetY + scrollY; | ||
|
||
if (this.settings.containInline) { | ||
let elRect = this.el.getBoundingClientRect(); | ||
|
||
if (inlineLeft < triggerRect.left + scrollX) { | ||
inlineLeft = triggerRect.left + scrollX; | ||
} else if (inlineLeft + this.el.clientWidth > triggerRect.left + triggerRect.width + scrollX) { | ||
inlineLeft = triggerRect.left + triggerRect.width - this.el.clientWidth + scrollX; | ||
} else if (inlineLeft + elWidth > triggerRect.left + triggerRect.width + scrollX) { | ||
inlineLeft = triggerRect.left + triggerRect.width - elWidth + scrollX; | ||
} | ||
|
||
if (inlineTop < triggerRect.top + scrollY) { | ||
inlineTop = triggerRect.top + scrollY; | ||
} else if (inlineTop + this.el.clientHeight > triggerRect.top + triggerRect.height + scrollY) { | ||
inlineTop = triggerRect.top + triggerRect.height - this.el.clientHeight + scrollY; | ||
} else if (inlineTop + elHeight > triggerRect.top + triggerRect.height + scrollY) { | ||
inlineTop = triggerRect.top + triggerRect.height - elHeight + scrollY; | ||
} | ||
} | ||
|
||
|
@@ -127,14 +139,14 @@ export default class ZoomPane { | |
} | ||
|
||
if (!this.settings.showWhitespaceAtEdges) { | ||
if (left > 0) { | ||
left = 0; | ||
if (left > minLeft) { | ||
left = minLeft; | ||
} else if (left < maxLeft) { | ||
left = maxLeft; | ||
} | ||
|
||
if (top > 0) { | ||
top = 0; | ||
if (top > minTop) { | ||
top = minTop; | ||
} else if (top < maxTop) { | ||
top = maxTop; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These previous lines (99-107) are the only functional changes in this PR, along with updating the conditionals on lines 130 and 136 to use these values