-
Notifications
You must be signed in to change notification settings - Fork 3
/
zoom.js
104 lines (86 loc) · 2.36 KB
/
zoom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import React, { Component, createRef } from "react";
import PropTypes from "prop-types";
import styles from "../../../styles.css";
class Zoom extends Component {
constructor(props) {
super(props);
this.state = {
zoom: false,
mouseX: null,
mouseY: null,
};
let { height, img, transitionTime, width } = props;
this.outerDivStyle = {
width: `${width}px`,
overflow: "hidden",
};
this.innerDivStyle = {
height: `${height}px`,
backgroundRepeat: "no-repeat",
backgroundPosition: "center",
backgroundSize: "100%",
transition: `transform ${transitionTime}s ease-out`,
};
this.imageRef = createRef();
this.handleMouseOver = this.handleMouseOver.bind(this);
this.handleMouseOut = this.handleMouseOut.bind(this);
this.handleMouseMovement = this.handleMouseMovement.bind(this);
}
handleMouseOver() {
this.setState({
zoom: true,
});
}
handleMouseOut() {
this.setState({
zoom: false,
});
}
handleMouseMovement(e) {
const {
left: offsetLeft,
top: offsetTop,
} = this.imageRef.current.getBoundingClientRect();
const {
current: {
style: { height, width },
},
} = this.imageRef;
const x = ((e.pageX - offsetLeft) / parseInt(width, 10)) * 100;
const y = ((e.pageY - offsetTop) / parseInt(height, 10)) * 100;
this.setState({
mouseX: x,
mouseY: y,
});
}
render() {
const { mouseX, mouseY, zoom } = this.state;
const { zoomScale } = this.props;
const transform = {
transformOrigin: `${mouseX}% ${mouseY}%`,
};
return (
<div
style={{ ...this.outerDivStyle, height: `450px` }}
onMouseOver={this.handleMouseOver}
onMouseOut={this.handleMouseOut}
onMouseMove={this.handleMouseMovement}
ref={this.imageRef}
className={this.props.hidden ? "photo-gallery-display-image zoomPointer" : 'hidden'}
onClick={() => this.props.changeDisplay('isZoomed')}
>
<div
style={{
...transform,
...this.innerDivStyle,
transform: zoom ? `scale(${zoomScale})` : "scale(1.0)",
backgroundImage: `url(${this.props.img})`,
cursor: 'zoom-out'
}}
className={styles.zoomImg}
/>
</div>
);
}
}
export default Zoom;