@@ -27,14 +27,16 @@ const SunMaterial = new THREE.MeshBasicMaterial({
2727} )
2828
2929interface CyberspaceProps {
30+ // assert parentRef.current is not null
31+ parentRef : React . RefObject < HTMLDivElement > ,
3032 targetSize : number , // size of construct to determine camera orbit radius
3133 targetCoord : BigCoords , // for camera orbit
3234 children : React . ReactNode ,
3335}
3436
3537const centerVec = new THREE . Vector3 ( UNIVERSE_SIZE_HALF , UNIVERSE_SIZE_HALF , UNIVERSE_SIZE_HALF ) // The center of cyberspace
3638
37- export const Cyberspace : React . FC < CyberspaceProps > = ( { targetSize, targetCoord, children } ) => {
39+ export const Cyberspace : React . FC < CyberspaceProps > = ( { parentRef , targetSize, targetCoord, children } ) => {
3840 const groupRef = useRef < THREE . Group > ( null )
3941 // const [interactionActive, setInteractionActive] = useState(false)
4042 const [ defaultView , setDefaultView ] = useState ( true )
@@ -44,18 +46,21 @@ export const Cyberspace: React.FC<CyberspaceProps> = ({ targetSize, targetCoord,
4446 const [ interactionResetDelay , setInteractionResetDelay ] = useState ( INTERACTION_RESET_DELAY )
4547 const { camera } = useThree ( )
4648
49+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
50+ const parent = parentRef . current !
51+
4752 const targetPosition = centerVec
4853 const downscaledTargetCoord = downscaleCoords ( targetCoord , UNIVERSE_DOWNSCALE )
4954 // const radius = targetSize * 10 // The radius of the circular path the camera will follow
5055
51- const minSize = 1
52- const maxSize = 2 ** 50 / 256 / 256
56+ const minSize = 1
57+ const maxSize = 2 ** 50 / 256 / 256
5358
54- // const ratio = ( targetSize + (maxSize * 0.1)) / maxSize
55- const ratio = targetSize / maxSize
59+ // const ratio = ( targetSize + (maxSize * 0.1)) / maxSize
60+ const ratio = targetSize / maxSize
5661
57- // const radius = 100 * Math.pow(targetSize, ratio) + targetSize
58- // const radius = 100 + targetSize * (1 + ratio)
62+ // const radius = 100 * Math.pow(targetSize, ratio) + targetSize
63+ // const radius = 100 + targetSize * (1 + ratio)
5964 const invertRatioLimit = Math . max ( 1 - ratio , minSize / maxSize )
6065
6166 const radius = Math . max ( 100 , targetSize + targetSize * invertRatioLimit )
@@ -80,16 +85,17 @@ const ratio = targetSize / maxSize
8085 } , interactionResetDelay )
8186 }
8287
83- window . addEventListener ( 'pointerdown' , handleInteractionStart )
84- window . addEventListener ( 'pointerup' , handleInteractionEnd )
85- window . addEventListener ( 'wheel' , handleInteractionStart )
88+ parent . addEventListener ( 'pointerdown' , handleInteractionStart )
89+ parent . addEventListener ( 'pointerup' , handleInteractionEnd )
90+ parent . addEventListener ( 'wheel' , handleInteractionStart )
8691
8792 // Cleanup event listeners on unmount
8893 return ( ) => {
89- window . removeEventListener ( 'pointerdown' , handleInteractionStart )
90- window . removeEventListener ( 'pointerup' , handleInteractionEnd )
91- window . addEventListener ( 'wheel' , handleInteractionStart )
94+ parent . removeEventListener ( 'pointerdown' , handleInteractionStart )
95+ parent . removeEventListener ( 'pointerup' , handleInteractionEnd )
96+ parent . addEventListener ( 'wheel' , handleInteractionStart )
9297 }
98+ // eslint-disable-next-line react-hooks/exhaustive-deps
9399 } , [ targetSize , targetCoord ] )
94100
95101 // Compute the lines and grids only once
0 commit comments