1const Canvas = () => {
2 const containerRef = useRef<HTMLCanvasElement>(null);
3
4 const handleDraw = () => {
5 const canvas = containerRef.current;
6 const position = { x: 60, y: 60 };
7 const velocity = { x: 10, y: 10 };
8 const rad = 30;
9 if (!canvas) return;
10 let innerWidth = canvas.width;
11 let innerHeight = canvas.height;
12 canvas.width = 1000;
13 canvas.height = 500;
14
15 const context = canvas.getContext("2d");
16
17 if (!context) return;
18
19 const draw = () => {
20 context.fillStyle = "#0efccc";
21 context.beginPath();
22 context.arc(position.x, position.y, rad, 0, 2 * Math.PI);
23 context.fill();
24 };
25
26 const update = () => {
27 if (position.x + rad > innerWidth || position.x - rad < 0) {
28 velocity.x = -velocity.x;
29 }
30
31 if (position.y + rad > innerHeight || position.y - rad < 0) {
32 velocity.y = -velocity.y;
33 }
34
35 position.x += velocity.x;
36 position.y += velocity.y;
37 };
38
39 const animate = () => {
40 requestAnimationFrame(animate);
41 context.clearRect(0, 0, innerWidth, innerHeight);
42 draw();
43 update();
44 };
45
46 animate();
47 };
48
49 useEffect(() => {
50 handleDraw();
51 }, []);
52
53 return (
54 <Box>
55 <Container ref={containerRef} />;
56 </Box>
57 );
58};
59
60export default Canvas;