Skip to content

Commit 2ac3dde

Browse files
committed
Refactor GameCanvas component to import main module only on the client side
1 parent ee5c835 commit 2ac3dde

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

lifegamecownay/src/components/gameCanvas.tsx

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
"use client"
22
import { useEffect, useRef } from "react";
3-
import "../utils/main"
43

54
const GameCanvas = ({state}) => {
65
const canvasRef = useRef<HTMLCanvasElement | null>(null);
7-
6+
87
useEffect(() => {
9-
const canvas = canvasRef.current;
10-
if (!canvas) return;
11-
12-
window.Game.init(state, canvas)
13-
canvas.width = window.innerWidth
14-
canvas.height = window.innerHeight
15-
const ctx = canvas.getContext("2d");
16-
if (!ctx) return;
17-
18-
let animationId: number;
19-
20-
const draw = () => {
8+
// Importa main apenas no cliente
9+
import("../utils/main").then(() => {
10+
const canvas = canvasRef.current;
11+
if (!canvas) return;
12+
13+
window.Game.init(state, canvas)
2114
canvas.width = window.innerWidth
2215
canvas.height = window.innerHeight
23-
window.Game.update()
24-
window.Game.draw(ctx, canvas)
25-
animationId = requestAnimationFrame(draw);
26-
};
16+
const ctx = canvas.getContext("2d");
17+
if (!ctx) return;
2718

19+
let animationId: number;
2820

29-
draw();
21+
const draw = () => {
22+
canvas.width = window.innerWidth
23+
canvas.height = window.innerHeight
24+
window.Game.update()
25+
window.Game.draw(ctx, canvas)
26+
animationId = requestAnimationFrame(draw);
27+
};
3028

31-
return () => {
32-
cancelAnimationFrame(animationId);
33-
};
29+
draw();
3430

31+
return () => {
32+
cancelAnimationFrame(animationId);
33+
};
34+
});
3535
}, [state]);
3636

3737
return <canvas ref={canvasRef} style={{width: "100%"}}/>;

0 commit comments

Comments
 (0)