Skip to content

Commit c8ea9a6

Browse files
committed
.
1 parent db1d62c commit c8ea9a6

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

src/components/ConstructViewer.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react'
1+
import { useEffect, useState, useRef } from 'react'
22
import { Canvas } from '@react-three/fiber'
33
import { OrbitControls } from "@react-three/drei"
44
import { Cyberspace } from './ThreeCyberspace'
@@ -19,6 +19,7 @@ const ConstructViewer = ({constructSize = 1, hexLocation = CENTERCOORD, style =
1919
const [scale] = useState(UNIVERSE_SIZE)
2020
const [size, setSize] = useState(constructSize)
2121
const [coord, setCoord] = useState<BigCoords>(decodeHexToCoordinates(hexLocation))
22+
const viewerRef = useRef<HTMLDivElement>(null)
2223

2324
useEffect(() => {
2425
// const urlParams = new URLSearchParams(window.location.search)
@@ -32,14 +33,14 @@ const ConstructViewer = ({constructSize = 1, hexLocation = CENTERCOORD, style =
3233
const orbitTarget = new THREE.Vector3(downscaled.x, downscaled.y, downscaled.z)
3334

3435
return (
35-
<div className="cyberspace-viewer">
36+
<div className="cyberspace-viewer" ref={viewerRef}>
3637
<Canvas style={style} camera={{
3738
near: 0.001,
3839
far: scale*2*2*2*2*2*2*2*2,
3940
position: [0, 0, scale]
4041
}}>
4142
<ambientLight intensity={0.8} />
42-
<Cyberspace targetCoord={coord} targetSize={size}>
43+
<Cyberspace targetCoord={coord} targetSize={size} parentRef={viewerRef}>
4344
<Construct coord={coord} size={size}/>
4445
</Cyberspace>
4546
<OrbitControls target={orbitTarget} zoomSpeed={5}/>

src/components/MinerController.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ export const Miner = ({targetHex, targetWork}: MinerProps) => {
177177
created_at: createdAt,
178178
tags: [["nonce",nonceBytes,targetHex]],
179179
content: '',
180+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
180181
pubkey: identity!.pubkey,
181182
}
182183
const serializedEvent = serializeEvent(event)

src/components/ThreeCyberspace.tsx

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ const SunMaterial = new THREE.MeshBasicMaterial({
2727
})
2828

2929
interface 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

3537
const 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

Comments
 (0)