|
1 | | -import React, { useEffect, useContext, useRef, useState, useCallback } from 'react' |
2 | | -import styled from 'styled-components' |
3 | | -import { DrawingMenuContext } from '../../context/DrawingMenuContext' |
4 | | -import { WebRTCUser } from '../../types' |
5 | | -import { io, Socket } from 'socket.io-client' |
| 1 | +import React, { |
| 2 | + useEffect, |
| 3 | + useContext, |
| 4 | + useRef, |
| 5 | + useState, |
| 6 | + useCallback, |
| 7 | +} from "react"; |
| 8 | +import styled from "styled-components"; |
| 9 | +import { DrawingMenuContext } from "../../context/DrawingMenuContext"; |
| 10 | +import { WebRTCUser } from "../../types"; |
| 11 | +import { io, Socket } from "socket.io-client"; |
6 | 12 |
|
7 | 13 | interface CanvasProps { |
8 | | - width: number |
9 | | - height: number |
| 14 | + width: number; |
| 15 | + height: number; |
10 | 16 | } |
11 | 17 |
|
12 | 18 | interface props { |
13 | | - user: WebRTCUser |
| 19 | + user: WebRTCUser; |
14 | 20 | } |
15 | 21 |
|
16 | 22 | interface Coordinate { |
17 | | - x: number |
18 | | - y: number |
| 23 | + x: number; |
| 24 | + y: number; |
19 | 25 | } |
20 | 26 |
|
21 | 27 | const Canvas = ({ user }: props) => { |
22 | | - const canvasRef = useRef<HTMLCanvasElement>(null) |
23 | | - const { state } = useContext(DrawingMenuContext) |
24 | | - const { color, thickness, isDrawing, isErasing } = state |
25 | | - const socketRef = useRef<Socket | null>(null) |
| 28 | + const canvasRef = useRef<HTMLCanvasElement>(null); |
| 29 | + const { state } = useContext(DrawingMenuContext); |
| 30 | + const { color, thickness, isDrawing, isErasing } = state; |
| 31 | + const socketRef = useRef<Socket | null>(null); |
26 | 32 |
|
27 | | - const [mousePosition, setMousePosition] = useState<Coordinate | undefined>(undefined) |
28 | | - const [isPainting, setIsPainting] = useState(false) |
29 | | - const [drawnLines, setDrawnLines] = useState<Array<Coordinate>>([]) |
| 33 | + const [mousePosition, setMousePosition] = useState<Coordinate | undefined>( |
| 34 | + undefined |
| 35 | + ); |
| 36 | + const [isPainting, setIsPainting] = useState(false); |
| 37 | + const [drawnLines, setDrawnLines] = useState<Array<Coordinate>>([]); |
30 | 38 |
|
31 | 39 | const updateCanvasSize = () => { |
32 | | - const canvas = canvasRef.current |
| 40 | + const canvas = canvasRef.current; |
33 | 41 | if (canvas && canvas.parentElement) { |
34 | | - canvas.width = canvas.parentElement.offsetWidth |
35 | | - canvas.height = canvas.parentElement.offsetHeight |
| 42 | + canvas.width = canvas.parentElement.offsetWidth; |
| 43 | + canvas.height = canvas.parentElement.offsetHeight; |
36 | 44 | } |
37 | | - } |
| 45 | + }; |
38 | 46 |
|
39 | 47 | useEffect(() => { |
40 | | - window.addEventListener('resize', updateCanvasSize) |
41 | | - updateCanvasSize() // Initial update |
| 48 | + window.addEventListener("resize", updateCanvasSize); |
| 49 | + updateCanvasSize(); // Initial update |
42 | 50 |
|
43 | 51 | // Initialize socket only if it's not already established |
44 | 52 | if (!socketRef.current) { |
45 | | - socketRef.current = io(`${user.ipAddress}:5555`) |
| 53 | + socketRef.current = io(`${user.ipAddress}:5555`); |
46 | 54 | } |
47 | 55 |
|
48 | | - socketRef.current.on('connect', () => { |
49 | | - console.log('connected!') |
50 | | - }) |
| 56 | + socketRef.current.on("connect", () => { |
| 57 | + console.log("connected!"); |
| 58 | + }); |
51 | 59 |
|
52 | 60 | return () => { |
53 | | - window.removeEventListener('resize', updateCanvasSize) |
| 61 | + window.removeEventListener("resize", updateCanvasSize); |
54 | 62 | if (socketRef.current) { |
55 | 63 | // Disconnect the socket if needed |
56 | | - socketRef.current.disconnect() |
| 64 | + socketRef.current.disconnect(); |
57 | 65 | } |
58 | | - } |
59 | | - }, [user]) |
| 66 | + }; |
| 67 | + }, [user]); |
60 | 68 |
|
61 | | - const drawLine = (originalMousePosition: Coordinate, newMousePosition: Coordinate) => { |
| 69 | + const drawLine = ( |
| 70 | + originalMousePosition: Coordinate, |
| 71 | + newMousePosition: Coordinate |
| 72 | + ) => { |
62 | 73 | if (!canvasRef.current) { |
63 | | - return |
| 74 | + return; |
64 | 75 | } |
65 | | - const canvas: HTMLCanvasElement = canvasRef.current |
66 | | - const context = canvas.getContext('2d') |
| 76 | + const canvas: HTMLCanvasElement = canvasRef.current; |
| 77 | + const context = canvas.getContext("2d"); |
67 | 78 |
|
68 | 79 | if (context) { |
69 | 80 | if (isErasing) { |
70 | | - context.globalCompositeOperation = 'destination-out' // Set mode to erase |
71 | | - context.lineWidth = thickness * 2 // You might want a bigger eraser |
| 81 | + context.globalCompositeOperation = "destination-out"; // Set mode to erase |
| 82 | + context.lineWidth = thickness * 2; // You might want a bigger eraser |
72 | 83 | } else { |
73 | | - context.globalCompositeOperation = 'source-over' // Set mode to draw |
74 | | - context.strokeStyle = color |
75 | | - context.lineWidth = thickness |
| 84 | + context.globalCompositeOperation = "source-over"; // Set mode to draw |
| 85 | + context.strokeStyle = color; |
| 86 | + context.lineWidth = thickness; |
76 | 87 | } |
77 | 88 |
|
78 | | - context.lineJoin = 'round' |
79 | | - context.beginPath() |
80 | | - context.moveTo(originalMousePosition.x, originalMousePosition.y) |
81 | | - context.lineTo(newMousePosition.x, newMousePosition.y) |
82 | | - context.closePath() |
83 | | - context.stroke() |
| 89 | + context.lineJoin = "round"; |
| 90 | + context.beginPath(); |
| 91 | + context.moveTo(originalMousePosition.x, originalMousePosition.y); |
| 92 | + context.lineTo(newMousePosition.x, newMousePosition.y); |
| 93 | + context.closePath(); |
| 94 | + context.stroke(); |
84 | 95 | } |
85 | | - } |
| 96 | + }; |
86 | 97 |
|
87 | 98 | const getCoordinates = (event: MouseEvent): Coordinate | undefined => { |
88 | 99 | if (!canvasRef.current) { |
89 | | - return |
| 100 | + return; |
90 | 101 | } |
91 | 102 |
|
92 | | - const canvas: HTMLCanvasElement = canvasRef.current |
93 | | - const rect = canvas.getBoundingClientRect() // Get the bounding rectangle of the canvas |
| 103 | + const canvas: HTMLCanvasElement = canvasRef.current; |
| 104 | + const rect = canvas.getBoundingClientRect(); // Get the bounding rectangle of the canvas |
94 | 105 |
|
95 | 106 | return { |
96 | 107 | x: event.clientX - rect.left, |
97 | | - y: event.clientY - rect.top |
98 | | - } |
99 | | - } |
| 108 | + y: event.clientY - rect.top, |
| 109 | + }; |
| 110 | + }; |
100 | 111 |
|
101 | 112 | const startPaint = useCallback((event: MouseEvent) => { |
102 | | - const coordinates = getCoordinates(event) |
| 113 | + const coordinates = getCoordinates(event); |
103 | 114 | if (coordinates) { |
104 | | - setIsPainting(true) |
105 | | - setMousePosition(coordinates) |
| 115 | + setIsPainting(true); |
| 116 | + setMousePosition(coordinates); |
106 | 117 | } |
107 | | - }, []) |
| 118 | + }, []); |
108 | 119 |
|
109 | 120 | const paint = useCallback( |
110 | 121 | (event: MouseEvent) => { |
111 | | - event.preventDefault() |
| 122 | + event.preventDefault(); |
112 | 123 |
|
113 | 124 | if (isPainting && (isDrawing || isErasing)) { |
114 | | - const newMousePosition = getCoordinates(event) |
115 | | - const canvas = canvasRef.current |
| 125 | + const newMousePosition = getCoordinates(event); |
| 126 | + const canvas = canvasRef.current; |
116 | 127 |
|
117 | 128 | if (canvas && newMousePosition) { |
118 | | - const { width, height } = canvas |
| 129 | + const { width, height } = canvas; |
119 | 130 | if (newMousePosition.x > width || newMousePosition.y > height) { |
120 | | - exitPaint() |
| 131 | + exitPaint(); |
121 | 132 | } else if (mousePosition) { |
122 | | - drawLine(mousePosition, newMousePosition) |
123 | | - setMousePosition(newMousePosition) |
| 133 | + drawLine(mousePosition, newMousePosition); |
| 134 | + setMousePosition(newMousePosition); |
124 | 135 |
|
125 | | - setDrawnLines((prevLines) => [...prevLines, newMousePosition]) |
| 136 | + setDrawnLines((prevLines) => [...prevLines, newMousePosition]); |
126 | 137 | } |
127 | 138 | } |
128 | 139 | } |
129 | 140 | }, |
130 | 141 | [isPainting, mousePosition] |
131 | | - ) |
| 142 | + ); |
132 | 143 |
|
133 | 144 | const exitPaint = useCallback(() => { |
134 | | - const canvas = canvasRef.current |
| 145 | + const canvas = canvasRef.current; |
135 | 146 | if (canvas) { |
136 | | - setIsPainting(false) |
| 147 | + setIsPainting(false); |
137 | 148 | const data = JSON.stringify({ |
138 | 149 | width: canvas.width, |
139 | 150 | height: canvas.height, |
140 | 151 | line: drawnLines, |
141 | 152 | color: color, |
142 | 153 | thickness: thickness, |
143 | | - id: user.id |
144 | | - }) |
145 | | - console.log(data) |
146 | | - console.log(socketRef.current) |
147 | | - socketRef.current?.emit('send', data) |
148 | | - setDrawnLines([]) |
| 154 | + id: user.id, |
| 155 | + }); |
| 156 | + console.log(data); |
| 157 | + console.log(socketRef.current); |
| 158 | + socketRef.current?.emit("send", data); |
| 159 | + setDrawnLines([]); |
149 | 160 | } |
150 | | - }, [drawnLines]) |
| 161 | + }, [drawnLines]); |
151 | 162 |
|
152 | 163 | useEffect(() => { |
153 | 164 | if (!canvasRef.current) { |
154 | | - return |
| 165 | + return; |
155 | 166 | } |
156 | | - const canvas: HTMLCanvasElement = canvasRef.current |
| 167 | + const canvas: HTMLCanvasElement = canvasRef.current; |
157 | 168 |
|
158 | | - canvas.addEventListener('mousedown', startPaint) |
159 | | - canvas.addEventListener('mousemove', paint) |
160 | | - canvas.addEventListener('mouseup', exitPaint) |
| 169 | + canvas.addEventListener("mousedown", startPaint); |
| 170 | + canvas.addEventListener("mousemove", paint); |
| 171 | + canvas.addEventListener("mouseup", exitPaint); |
161 | 172 |
|
162 | 173 | return () => { |
163 | | - canvas.removeEventListener('mousedown', startPaint) |
164 | | - canvas.removeEventListener('mousemove', paint) |
165 | | - canvas.removeEventListener('mouseup', exitPaint) |
166 | | - } |
167 | | - }, [startPaint, paint, exitPaint]) |
| 174 | + canvas.removeEventListener("mousedown", startPaint); |
| 175 | + canvas.removeEventListener("mousemove", paint); |
| 176 | + canvas.removeEventListener("mouseup", exitPaint); |
| 177 | + }; |
| 178 | + }, [startPaint, paint, exitPaint]); |
168 | 179 |
|
169 | | - return <CanvasComponent ref={canvasRef} className="canvas"></CanvasComponent> |
170 | | -} |
| 180 | + return <CanvasComponent ref={canvasRef} className="canvas"></CanvasComponent>; |
| 181 | +}; |
171 | 182 |
|
172 | | -export default Canvas |
| 183 | +export default Canvas; |
173 | 184 |
|
174 | 185 | const CanvasComponent = styled.canvas` |
175 | 186 | border-radius: 15px; |
176 | 187 | background: transparent; |
177 | 188 | position: absolute; |
178 | | - height: '100%'; |
179 | | - width: '100%'; |
| 189 | + height: "100%"; |
| 190 | + width: "100%"; |
180 | 191 | z-index: 10; |
181 | | -` |
| 192 | +`; |
0 commit comments