-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
854b684
commit c9fbd64
Showing
2 changed files
with
36 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
import { Scene } from '../three/Scene'; | ||
import React, { useEffect, useRef, useState } from 'react'; | ||
import * as THREE from 'three'; | ||
import { Scene } from './Scene'; | ||
|
||
const Canvas = () => { | ||
const canvasRef = useRef(null); | ||
function Canvas() { | ||
const containerRef = useRef(null); | ||
const [scene, setScene] = useState(null); | ||
|
||
useEffect(() => { | ||
if (!canvasRef.current) return; | ||
|
||
const handleResize = () => { | ||
if (canvasRef.current) { | ||
canvasRef.current.width = window.innerWidth; | ||
canvasRef.current.height = window.innerHeight; | ||
} | ||
}; | ||
// Only initialize if container exists | ||
if (!containerRef.current) return; | ||
|
||
handleResize(); | ||
window.addEventListener('resize', handleResize); | ||
|
||
const scene = new Scene(canvasRef.current); | ||
scene.init(); | ||
scene.animate(); | ||
// Wait for next frame to ensure DOM measurements are accurate | ||
requestAnimationFrame(() => { | ||
try { | ||
const newScene = new Scene(containerRef.current); | ||
setScene(newScene); | ||
|
||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
scene.dispose(); | ||
}; | ||
return () => { | ||
if (newScene) { | ||
newScene.dispose(); | ||
} | ||
}; | ||
} catch (error) { | ||
console.error('Error initializing scene:', error); | ||
} | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<canvas | ||
ref={canvasRef} | ||
<div | ||
ref={containerRef} | ||
style={{ | ||
position: 'fixed', | ||
top: 0, | ||
left: 0, | ||
width: '100vw', | ||
height: '100vh', | ||
width: '100%', | ||
height: '100%', | ||
zIndex: -1, | ||
background: '#000000' | ||
}} | ||
/> | ||
); | ||
}; | ||
} | ||
|
||
export default Canvas; |