Showing
1 changed file
with
29 additions
and
0 deletions
... | @@ -4,6 +4,35 @@ import { Vector } from './types'; | ... | @@ -4,6 +4,35 @@ import { Vector } from './types'; |
4 | export const Canvas: React.FC = () => { | 4 | export const Canvas: React.FC = () => { |
5 | const canvasRef = useRef<HTMLCanvasElement>(null); | 5 | const canvasRef = useRef<HTMLCanvasElement>(null); |
6 | 6 | ||
7 | + const getCoordinates = useCallback((event: MouseEvent): Vector | undefined => { | ||
8 | + if (!canvasRef.current) { | ||
9 | + return; | ||
10 | + } else { | ||
11 | + return { | ||
12 | + x: event.pageX - canvasRef.current.offsetLeft, | ||
13 | + y: event.pageY - canvasRef.current.offsetTop | ||
14 | + }; | ||
15 | + } | ||
16 | + }, []); | ||
17 | + | ||
18 | + const drawLine = useCallback((prev: Vector, current: Vector) => { | ||
19 | + if (canvasRef.current) { | ||
20 | + const context = canvasRef.current!.getContext('2d'); | ||
21 | + if (context) { | ||
22 | + context.strokeStyle = 'black'; | ||
23 | + context.lineJoin = 'round'; | ||
24 | + context.lineWidth = 5; | ||
25 | + | ||
26 | + context.beginPath(); | ||
27 | + context.moveTo(prev.x, prev.y); | ||
28 | + context.lineTo(current.x, current.y); | ||
29 | + context.closePath(); | ||
30 | + | ||
31 | + context.stroke(); | ||
32 | + } | ||
33 | + } | ||
34 | + }, []); | ||
35 | + | ||
7 | return ( | 36 | return ( |
8 | <div className='mx-3 px-2 py-1 rounded shadow'> | 37 | <div className='mx-3 px-2 py-1 rounded shadow'> |
9 | <canvas ref={canvasRef} width='512' height='384' /> | 38 | <canvas ref={canvasRef} width='512' height='384' /> | ... | ... |
-
Please register or login to post a comment