forked from markNZed/processism-react-three
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
59 lines (50 loc) · 1.82 KB
/
Copy pathApp.js
File metadata and controls
59 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import React, { useEffect, useState } from 'react';
import { Canvas, useThree, useFrame } from '@react-three/fiber';
import SceneManager from './SceneManager';
import SceneSelector from './SceneSelector';
/**
* Handles camera adjustments on window resize for react-three-fiber.
*/
function CameraAdjuster({ isOrthographic }) {
const { camera, gl } = useThree();
useEffect(() => {
const handleResize = () => {
if (isOrthographic) {
camera.left = window.innerWidth / -2;
camera.right = window.innerWidth / 2;
camera.top = window.innerHeight / 2;
camera.bottom = window.innerHeight / -2;
} else {
camera.aspect = window.innerWidth / window.innerHeight;
}
camera.updateProjectionMatrix();
gl.setSize(window.innerWidth, window.innerHeight);
};
handleResize();
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, [camera, gl, isOrthographic]);
return null; // Component only handles side effects
}
export default function App() {
const { sceneComponent, isOrthographic, key } = SceneManager();
const [isAnimating, setIsAnimating] = useState(true); // Animation state
const toggleAnimation = () => {
setIsAnimating(!isAnimating); // Toggle animation state
};
// This is not stopping physics - would need to set value in Zustand and stop physics too
const DisableRender = () => useFrame(() => null, 1000)
return (
<>
<SceneSelector />
<button onClick={toggleAnimation}>
{isAnimating ? 'Stop Animation' : 'Start Animation'}
</button>
<Canvas key={key} orthographic={isOrthographic} >
{!isAnimating && <DisableRender />}
{sceneComponent}
<CameraAdjuster isOrthographic={isOrthographic} />
</Canvas>
</>
);
}