-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathViewerCanvas.tsx
185 lines (164 loc) · 3.96 KB
/
ViewerCanvas.tsx
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as React from 'react';
import Loading from './Loading';
import classnames from 'classnames';
export interface ViewerCanvasProps {
prefixCls: string;
imgSrc: string;
visible: boolean;
width: number;
height: number;
top: number;
left: number;
rotate: number;
onChangeImgState: (width: number, height: number, top: number, left: number) => void;
onResize: () => void;
zIndex: number;
scaleX: number;
scaleY: number;
loading: boolean;
drag: boolean;
container: HTMLElement;
onCanvasMouseDown: (e: React.MouseEvent<HTMLDivElement>) => void;
}
export interface ViewerCanvasState {
isMouseDown?: boolean;
mouseX?: number;
mouseY?: number;
}
export default function ViewerCanvas(props: ViewerCanvasProps) {
const isMouseDown = React.useRef(false);
const prePosition = React.useRef({
x: 0,
y: 0,
});
const [ position, setPosition ] = React.useState({
x: 0,
y: 0,
});
React.useEffect(() => {
return () => {
bindEvent(true);
bindWindowResizeEvent(true);
};
}, []);
React.useEffect(() => {
bindWindowResizeEvent();
return () => {
bindWindowResizeEvent(true);
};
});
React.useEffect(() => {
if (props.visible && props.drag) {
bindEvent();
}
if (!props.visible && props.drag) {
handleMouseUp({});
}
return () => {
bindEvent(true);
};
}, [props.drag, props.visible]);
React.useEffect(() => {
let diffX = position.x - prePosition.current.x;
let diffY = position.y - prePosition.current.y;
prePosition.current = {
x: position.x,
y: position.y,
};
props.onChangeImgState(props.width, props.height, props.top + diffY, props.left + diffX);
}, [position]);
function handleResize(e) {
props.onResize();
}
function handleCanvasMouseDown(e) {
props.onCanvasMouseDown(e);
handleMouseDown(e);
}
function handleMouseDown(e) {
if (e.button !== 0) {
return;
}
if (!props.visible || !props.drag) {
return;
}
e.preventDefault();
e.stopPropagation();
isMouseDown.current = true;
prePosition.current = {
x: e.nativeEvent.clientX,
y: e.nativeEvent.clientY,
};
}
const handleMouseMove = (e) => {
if (isMouseDown.current) {
setPosition({
x: e.clientX,
y: e.clientY,
});
}
};
function handleMouseUp(e) {
isMouseDown.current = false;
}
function bindWindowResizeEvent(remove?: boolean) {
let funcName = 'addEventListener';
if (remove) {
funcName = 'removeEventListener';
}
window[funcName]('resize', handleResize, false);
}
function bindEvent(remove?: boolean) {
let funcName = 'addEventListener';
if (remove) {
funcName = 'removeEventListener';
}
document[funcName]('click', handleMouseUp, false);
document[funcName]('mousemove', handleMouseMove, false);
}
let imgStyle: React.CSSProperties = {
width: `${props.width}px`,
height: `${props.height}px`,
transform: `
translateX(${props.left !== null ? props.left + 'px' : 'aoto'}) translateY(${props.top}px)
rotate(${props.rotate}deg) scaleX(${props.scaleX}) scaleY(${props.scaleY})`,
};
const imgClass = classnames(`${props.prefixCls}-image`, {
drag: props.drag,
[`${props.prefixCls}-image-transition`]: !isMouseDown.current,
});
let style = {
zIndex: props.zIndex,
};
let imgNode = null;
if (props.imgSrc !== '') {
imgNode = <img
className={imgClass}
src={props.imgSrc}
style={imgStyle}
onMouseDown={handleMouseDown}
/>;
}
if (props.loading) {
imgNode = (
<div
style={{
display: 'flex',
height: `${window.innerHeight - 84}px`,
justifyContent: 'center',
alignItems: 'center',
}}
>
<Loading/>
</div>
);
}
return (
<div
className={`${props.prefixCls}-canvas`}
onMouseDown={handleCanvasMouseDown}
style={style}
>
{imgNode}
</div>
);
}