Skip to content

Commit

Permalink
keep this feature while image is expanded
Browse files Browse the repository at this point in the history
  • Loading branch information
railway17 committed Nov 24, 2021
1 parent dd96f3e commit 6d37102
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/components/ImageView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class ImageView extends PureComponent {
return;
}
Image.getSize(this.props.url, (width, height) => {
const scale = Math.max(this.props.windowWidth / width, this.props.windowHeight / height);
this.setImageRegion(width, height, scale);
this.setImageRegion(width, height);
});
document.addEventListener('mousemove', this.trackMovement.bind(this));
}
Expand All @@ -60,9 +59,8 @@ class ImageView extends PureComponent {
* When open image, set image left/right/top/bottom point and width, height
* @param {Boolean} width image width
* @param {Boolean} height image height
* @param {Boolean} scale zoomscale when click zoom
*/
setImageRegion(width, height, scale) {
setImageRegion(width, height) {
let imgLeft = (this.props.windowWidth - width) / 2;
let imgRight = ((this.props.windowWidth - width) / 2) + width;
let imgTop = (this.props.windowHeight - height) / 2;
Expand All @@ -87,7 +85,7 @@ class ImageView extends PureComponent {
}

this.setState({
imgWidth: width, imgHeight: height, zoomScale: scale, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
imgWidth: width, imgHeight: height, imageLeft: imgLeft, imageTop: imgTop, imageRight: imgRight, imageBottom: imgBottom,
});
}

Expand Down Expand Up @@ -164,7 +162,14 @@ class ImageView extends PureComponent {
<View
ref={el => this.scrollableRef = el}
onLayout={(e) => {
this.setState({containerHeight: e.nativeEvent.layout.height});
const {width, height} = e.nativeEvent.layout;
const imageWidth = this.state.imgWidth;
const imageHeight = this.state.imgHeight;
const scale = imageHeight && imageWidth ? Math.min(width / imageWidth, height / imageHeight) : 0;
this.setState({
containerHeight: height,
zoomScale: scale,
});
}}
style={[
styles.imageViewContainer,
Expand Down

0 comments on commit 6d37102

Please sign in to comment.