2018-03-05 04:32:24 +09:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-03-29 03:40:51 +09:00
|
|
|
import Hammer from 'hammerjs';
|
2018-03-05 04:32:24 +09:00
|
|
|
|
|
|
|
const MIN_SCALE = 1;
|
|
|
|
const MAX_SCALE = 4;
|
2018-03-29 03:40:51 +09:00
|
|
|
const DOUBLE_TAP_SCALE = 2;
|
2018-03-05 04:32:24 +09:00
|
|
|
|
|
|
|
const clamp = (min, max, value) => Math.min(max, Math.max(min, value));
|
|
|
|
|
|
|
|
export default class ZoomableImage extends React.PureComponent {
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
alt: PropTypes.string,
|
|
|
|
src: PropTypes.string.isRequired,
|
|
|
|
width: PropTypes.number,
|
|
|
|
height: PropTypes.number,
|
|
|
|
onClick: PropTypes.func,
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
alt: '',
|
|
|
|
width: null,
|
|
|
|
height: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
scale: MIN_SCALE,
|
|
|
|
}
|
|
|
|
|
|
|
|
removers = [];
|
|
|
|
container = null;
|
|
|
|
image = null;
|
2018-03-29 03:40:51 +09:00
|
|
|
lastScale = null;
|
|
|
|
zoomCenter = null;
|
2018-03-05 04:32:24 +09:00
|
|
|
|
|
|
|
componentDidMount () {
|
2018-03-29 03:40:51 +09:00
|
|
|
// register pinch event handlers to the container
|
|
|
|
let hammer = new Hammer.Manager(this.container, {
|
|
|
|
// required to make container scrollable by touch
|
|
|
|
touchAction: 'pan-x pan-y',
|
|
|
|
});
|
|
|
|
hammer.add(new Hammer.Pinch());
|
|
|
|
hammer.on('pinchstart', this.handlePinchStart);
|
|
|
|
hammer.on('pinchmove', this.handlePinchMove);
|
|
|
|
this.removers.push(() => hammer.off('pinchstart pinchmove'));
|
|
|
|
|
|
|
|
// register tap event handlers
|
|
|
|
hammer = new Hammer.Manager(this.image);
|
|
|
|
// NOTE the order of adding is also the order of gesture recognition
|
|
|
|
hammer.add(new Hammer.Tap({ event: 'doubletap', taps: 2 }));
|
|
|
|
hammer.add(new Hammer.Tap());
|
|
|
|
// prevent the 'tap' event handler be fired on double tap
|
|
|
|
hammer.get('tap').requireFailure('doubletap');
|
|
|
|
// NOTE 'tap' and 'doubletap' events are fired by touch and *mouse*
|
|
|
|
hammer.on('tap', this.handleTap);
|
|
|
|
hammer.on('doubletap', this.handleDoubleTap);
|
|
|
|
this.removers.push(() => hammer.off('tap doubletap'));
|
2018-03-05 04:32:24 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.removeEventListeners();
|
|
|
|
}
|
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
componentDidUpdate (prevProps, prevState) {
|
|
|
|
if (!this.zoomCenter) return;
|
2018-03-05 04:32:24 +09:00
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
const { x: cx, y: cy } = this.zoomCenter;
|
|
|
|
const { scale: prevScale } = prevState;
|
|
|
|
const { scale: nextScale } = this.state;
|
|
|
|
const { scrollLeft, scrollTop } = this.container;
|
2018-03-05 04:32:24 +09:00
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
// math memo:
|
|
|
|
// x = (scrollLeft + cx) / scrollWidth
|
|
|
|
// x' = (nextScrollLeft + cx) / nextScrollWidth
|
|
|
|
// scrollWidth = clientWidth * prevScale
|
|
|
|
// scrollWidth' = clientWidth * nextScale
|
|
|
|
// Solve x = x' for nextScrollLeft
|
|
|
|
const nextScrollLeft = (scrollLeft + cx) * nextScale / prevScale - cx;
|
|
|
|
const nextScrollTop = (scrollTop + cy) * nextScale / prevScale - cy;
|
|
|
|
|
|
|
|
this.container.scrollLeft = nextScrollLeft;
|
|
|
|
this.container.scrollTop = nextScrollTop;
|
2018-03-05 04:32:24 +09:00
|
|
|
}
|
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
removeEventListeners () {
|
|
|
|
this.removers.forEach(listeners => listeners());
|
|
|
|
this.removers = [];
|
|
|
|
}
|
2018-03-05 04:32:24 +09:00
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
handleClick = e => {
|
|
|
|
// prevent the click event propagated to parent
|
2018-03-05 04:32:24 +09:00
|
|
|
e.stopPropagation();
|
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
// the tap event handler is executed at the same time by touch and mouse,
|
|
|
|
// so we don't need to execute the onClick handler here
|
2018-03-05 04:32:24 +09:00
|
|
|
}
|
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
handlePinchStart = () => {
|
|
|
|
this.lastScale = this.state.scale;
|
|
|
|
}
|
2018-03-05 04:32:24 +09:00
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
handlePinchMove = e => {
|
|
|
|
const scale = clamp(MIN_SCALE, MAX_SCALE, this.lastScale * e.scale);
|
|
|
|
this.zoom(scale, e.center);
|
2018-03-05 04:32:24 +09:00
|
|
|
}
|
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
handleTap = () => {
|
2018-03-05 04:32:24 +09:00
|
|
|
const handler = this.props.onClick;
|
|
|
|
if (handler) handler();
|
|
|
|
}
|
|
|
|
|
2018-03-29 03:40:51 +09:00
|
|
|
handleDoubleTap = e => {
|
|
|
|
if (this.state.scale === MIN_SCALE)
|
|
|
|
this.zoom(DOUBLE_TAP_SCALE, e.center);
|
|
|
|
else
|
|
|
|
this.zoom(MIN_SCALE, e.center);
|
|
|
|
}
|
|
|
|
|
|
|
|
zoom (scale, center) {
|
|
|
|
this.zoomCenter = center;
|
|
|
|
this.setState({ scale });
|
|
|
|
}
|
|
|
|
|
2018-03-05 04:32:24 +09:00
|
|
|
setContainerRef = c => {
|
|
|
|
this.container = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
setImageRef = c => {
|
|
|
|
this.image = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { alt, src } = this.props;
|
|
|
|
const { scale } = this.state;
|
|
|
|
const overflow = scale === 1 ? 'hidden' : 'scroll';
|
2018-03-29 03:40:51 +09:00
|
|
|
const marginStyle = {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
transform: `scale(${scale})`,
|
|
|
|
transformOrigin: '0 0',
|
|
|
|
};
|
2018-03-05 04:32:24 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className='zoomable-image'
|
|
|
|
ref={this.setContainerRef}
|
|
|
|
style={{ overflow }}
|
|
|
|
>
|
2018-03-29 03:40:51 +09:00
|
|
|
<div
|
|
|
|
className='zoomable-image__margin'
|
|
|
|
style={marginStyle}
|
|
|
|
>
|
|
|
|
<img
|
|
|
|
ref={this.setImageRef}
|
|
|
|
role='presentation'
|
|
|
|
alt={alt}
|
|
|
|
src={src}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
/>
|
|
|
|
</div>
|
2018-03-05 04:32:24 +09:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|