From c9fbd64981cb1c3bda83d368ada6b852aad7f923 Mon Sep 17 00:00:00 2001 From: wheattoast11 Date: Sat, 30 Nov 2024 15:31:38 -0600 Subject: [PATCH] update app and canvas --- src/App.jsx | 28 +++++++------------- src/components/Canvas.jsx | 54 +++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 46 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index b22f645..aaafdbf 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -14,31 +14,21 @@ function App() { if ('performance' in window) { window.performance.mark('app_start'); } - - const logWebVitals = (metric) => { - console.log('Web Vital:', metric); - if (metric.value > 0) { - console.log('Metric details:', { - name: metric.name, - value: metric.value, - rating: metric.rating - }); - } - }; - - reportWebVitals(logWebVitals); + reportWebVitals(console.log); }, []); return ( - }> +
- - } /> - } /> - - + }> + + } /> + } /> + + +
); diff --git a/src/components/Canvas.jsx b/src/components/Canvas.jsx index e2b8b49..119cdd2 100644 --- a/src/components/Canvas.jsx +++ b/src/components/Canvas.jsx @@ -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 ( - ); -}; +} export default Canvas; \ No newline at end of file