Canvas.tsx
5.17 KB
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
186
187
188
import React, { useCallback, useContext, useEffect, useRef, useState } from 'react';
import SocketContext from '../../contexts/SocketContext';
import { MessageType, RawMessage } from '../common/types';
import { BrushData, Vector } from './types';
// 참고 : https://basketdeveloper.tistory.com/79
interface CanvasProps {
isDrawer: boolean;
}
export const Canvas: React.FC<CanvasProps> = ({ isDrawer }) => {
const socket = useContext(SocketContext);
const canvasRef = useRef<HTMLCanvasElement>(null);
const [mousePosition, setMousePosition] = useState<Vector>({ x:0, y:0 });
const [isPainting, setIsPainting] = useState(false);
const getCoordinates = useCallback((event: MouseEvent): Vector | undefined => {
if (!canvasRef.current) {
return;
} else {
return {
x: event.pageX - canvasRef.current.offsetLeft,
y: event.pageY - canvasRef.current.offsetTop
};
}
}, []);
const drawLine = useCallback((prev: Vector, current: Vector) => {
if (canvasRef.current) {
const context = canvasRef.current!.getContext('2d');
if (context) {
context.strokeStyle = 'black';
context.lineJoin = 'round';
context.lineWidth = 5;
context.beginPath();
context.moveTo(prev.x, prev.y);
context.lineTo(current.x, current.y);
context.closePath();
context.stroke();
}
}
}, []);
const clearCanvas = useCallback(() => {
if (canvasRef.current) {
const context = canvasRef.current.getContext('2d');
if (context) {
context.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
}
}
}, []);
const startPaint = useCallback((event: MouseEvent) => {
const coordinates = getCoordinates(event);
if (coordinates) {
setIsPainting(true);
setMousePosition(coordinates);
const rawMessage: RawMessage = {
type: MessageType.DRAW_MOVE,
message: coordinates
};
socket.emit('msg', rawMessage, () => {});
const nextRawMessage: RawMessage = {
type: MessageType.DRAW_SET,
message: {
size: 5,
color: '000000',
drawing: true
} as BrushData
};
socket.emit('msg', nextRawMessage, () => {});
}
}, []);
const paint = useCallback(
(event: MouseEvent) => {
// 드래그 방지
event.preventDefault();
event.stopPropagation();
if (isPainting) {
const newMousePosition = getCoordinates(event);
if (mousePosition && newMousePosition) {
drawLine(mousePosition, newMousePosition);
const rawMessage: RawMessage = {
type: MessageType.DRAW_MOVE,
message: newMousePosition
};
socket.emit('msg', rawMessage, () => {});
setMousePosition(newMousePosition);
}
}
},
[isPainting, mousePosition]
);
const exitPaint = useCallback(() => {
const rawMessage: RawMessage = {
type: MessageType.DRAW_SET,
message: {
size: 5,
color: '000000',
drawing: false
} as BrushData
};
socket.emit('msg', rawMessage, () => {});
setIsPainting(false);
}, []);
const handleDrawSet = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.DRAW_SET) {
const data = rawMessage.message as BrushData;
setIsPainting(data.drawing);
}
}, []);
const handleDrawMove = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.DRAW_MOVE) {
const data = rawMessage.message as Vector;
if (isPainting) {
drawLine(mousePosition, data);
}
setMousePosition(data);
}
}, [isPainting, mousePosition])
useEffect(() => {
if (canvasRef.current) {
if (isDrawer) {
const canvas: HTMLCanvasElement = canvasRef.current;
canvas.addEventListener('mousedown', startPaint);
canvas.addEventListener('mousemove', paint);
canvas.addEventListener('mouseup', exitPaint);
canvas.addEventListener('mouseleave', exitPaint);
return () => {
canvas.removeEventListener('mousedown', startPaint);
canvas.removeEventListener('mousemove', paint);
canvas.removeEventListener('mouseup', exitPaint);
canvas.removeEventListener('mouseleave', exitPaint);
};
}
}
}, [isDrawer, startPaint, paint, exitPaint]);
useEffect(() => {
if (!isDrawer) {
socket.on('msg', handleDrawSet);
socket.on('msg', handleDrawMove);
return () => {
socket.off('msg', handleDrawSet);
socket.off('msg', handleDrawMove);
}
}
}, [isDrawer, handleDrawMove]);
const handleClearWhenStart = useCallback((rawMessage: RawMessage) => {
if (rawMessage.type === MessageType.GAME_START) {
clearCanvas();
setIsPainting(false);
// TODO: 펜 굵기, 색 설정하게 되면 여기에 초기화 넣기
}
}, []);
useEffect(() => {
socket.on('msg', handleClearWhenStart);
return () => {
socket.off('msg', handleClearWhenStart);
}
}, []);
return (
<div className='mx-3 px-2 py-1 rounded shadow'>
<canvas ref={canvasRef} width='640' height='480' />
</div>
);
}