1- import { Text } from '@react-three/drei'
1+ import { Text , OrbitControls } from '@react-three/drei'
22import { useThree , useFrame } from '@react-three/fiber'
3- import { useState , useMemo , useEffect } from 'react'
3+ import { useState , useMemo , useEffect , useRef } from 'react'
44import { parseTimeUnit } from '@/utils/HelperFuncs'
55
66
@@ -62,7 +62,7 @@ export function FixedTicks({
6262 } ,
6363 height
6464} : FixedTicksProps ) {
65- const { camera, size } = useThree ( )
65+ const { camera } = useThree ( )
6666 const [ bounds , setBounds ] = useState < ViewportBounds > ( { left : 0 , right : 0 , top : 0 , bottom : 0 } )
6767
6868 const initialHeight = useMemo ( ( ) => ( window . innerHeight - height - 50 ) , [ ] )
@@ -80,7 +80,6 @@ export function FixedTicks({
8080 const isBig = xDimArray . every ( item => typeof item === "bigint" ) ;
8181 if ( isBig ) {
8282 const unit = parseTimeUnit ( coords . plot . units ) ;
83- console . log ( unit )
8483 const timeStrings = [ ]
8584 for ( let i = 0 ; i < xDimArray . length ; i ++ ) {
8685 const timeStamp = Number ( xDimArray [ i ] ) * unit
@@ -109,13 +108,13 @@ export function FixedTicks({
109108
110109 const sizes = useMemo ( ( ) => {
111110 // Convert from pixels to scene units
112- const pixelsPerUnit = size . height / ( ( window . innerHeight - height - 50 ) * camera . zoom )
111+ const pixelsPerUnit = 1 / camera . zoom
113112 return {
114- tickSize : tickSize / pixelsPerUnit ,
113+ tickSize : tickSize * pixelsPerUnit ,
115114 fontSize : fontSize / pixelsPerUnit ,
116- labelOffset : tickSize / pixelsPerUnit
115+ labelOffset : tickSize * pixelsPerUnit
117116 }
118- } , [ camera . zoom , tickSize , fontSize ] )
117+ } , [ camera . zoom , tickSize , fontSize ] )
119118
120119 // Update bounds when camera moves
121120 // TODO: update bounds when camera zooms
@@ -141,12 +140,38 @@ export function FixedTicks({
141140
142141 useEffect ( ( ) => {
143142 if ( height ) {
144- console . log ( height )
145143 const newHeight = ( window . innerHeight - height - 50 )
146- console . log ( heightRatio )
147144 setHeightRatio ( newHeight / initialHeight )
148145 }
149146 } , [ height ] )
147+
148+ const timeoutRef = useRef < NodeJS . Timeout | null > ( null ) ;
149+ // @ts -ignore
150+ const cameraRef = useRef < OrbitControls | null > ( null )
151+
152+ //This reset the camera position when the window is rescaled
153+ useEffect ( ( ) => {
154+ // Clear existing timeout if height changes
155+ if ( timeoutRef . current ) {
156+ clearTimeout ( timeoutRef . current ) ;
157+ }
158+ if ( cameraRef . current ) {
159+ // Set a timeout for 0.5 seconds
160+ timeoutRef . current = setTimeout ( ( ) => {
161+ if ( cameraRef . current ) {
162+ cameraRef . current . reset ( ) ;
163+ }
164+ } , 100 ) ;
165+ }
166+ // Cleanup timeout when component unmounts or height changes
167+ return ( ) => {
168+ if ( timeoutRef . current !== null ) {
169+ clearTimeout ( timeoutRef . current ) ;
170+ }
171+ } ;
172+ } , [ height ] ) ;
173+
174+
150175 return (
151176 < group >
152177 { /* Grid Lines */ }
@@ -157,24 +182,25 @@ export function FixedTicks({
157182 if ( i === 0 || i === xTickCount - 1 ) return null ; // Skip edges
158183 const x = initialBounds . left + ( initialBounds . right - initialBounds . left ) * ( i / ( xTickCount - 1 ) )
159184 return (
160- < line key = { `vgrid-${ i } ` } >
161- < bufferGeometry >
162- < float32BufferAttribute
163- attach = "attributes-position"
164- args = { [ new Float32Array ( [
165- x , bounds . top , 0 ,
166- x , bounds . bottom , 0
167- ] ) , 3 ] }
185+ < line key = { `vgrid-${ i } ` } >
186+ < bufferGeometry >
187+ < float32BufferAttribute
188+ attach = "attributes-position"
189+ args = { [ new Float32Array ( [
190+ x , bounds . top , 0 ,
191+ x , bounds . bottom , 0
192+ ] ) , 3 ] }
193+ />
194+ </ bufferGeometry >
195+ < lineDashedMaterial
196+ color = { color }
197+ opacity = { gridOpacity }
198+ transparent
199+ dashSize = { 0.5 }
200+ gapSize = { 0.5 }
201+
168202 />
169- </ bufferGeometry >
170- < lineDashedMaterial
171- color = { color }
172- opacity = { gridOpacity }
173- transparent
174- dashSize = { 0.5 }
175- gapSize = { 0.5 }
176- />
177- </ line >
203+ </ line >
178204 )
179205 } ) }
180206
@@ -183,25 +209,25 @@ export function FixedTicks({
183209 if ( i === 0 || i === yTickCount - 1 ) return null ; // Skip edges
184210 const y = ( initialBounds . bottom + ( initialBounds . top - initialBounds . bottom ) * ( i / ( yTickCount - 1 ) ) ) * heightRatio
185211 return (
186- < line key = { `hgrid-${ i } ` } >
187- < bufferGeometry >
188- < float32BufferAttribute
189- attach = "attributes-position"
190- args = { [ new Float32Array ( [
191- bounds . left , y , 0 ,
192- bounds . right , y , 0
193- ] ) , 3 ] }
212+ < line key = { `hgrid-${ i } ` } >
213+ < bufferGeometry >
214+ < float32BufferAttribute
215+ attach = "attributes-position"
216+ args = { [ new Float32Array ( [
217+ bounds . left , y , 0 ,
218+ bounds . right , y , 0
219+ ] ) , 3 ] }
220+ />
221+ </ bufferGeometry >
222+ < lineDashedMaterial
223+ color = { color }
224+ opacity = { gridOpacity }
225+ transparent
226+ dashSize = { 0. }
227+ gapSize = { 0.5 }
228+ linewidth = { 1 }
194229 />
195- </ bufferGeometry >
196- < lineDashedMaterial
197- color = { color }
198- opacity = { gridOpacity }
199- transparent
200- dashSize = { 0. }
201- gapSize = { 0.5 }
202- linewidth = { 1 }
203- />
204- </ line >
230+ </ line >
205231 )
206232 } ) }
207233 </ >
@@ -210,7 +236,6 @@ export function FixedTicks({
210236 { Array . from ( { length : xTickCount } , ( _ , i ) => {
211237 const x = initialBounds . left + ( initialBounds . right - initialBounds . left ) * ( i / ( xTickCount - 1 ) )
212238 const normX = x / ( initialBounds . right - initialBounds . left ) + .5 ;
213-
214239 return (
215240 < group key = { `top-tick-${ i } ` } position = { [ x , bounds . top , 0 ] } >
216241 < line >
@@ -242,7 +267,7 @@ export function FixedTicks({
242267 { /* Right Edge Ticks */ }
243268 { Array . from ( { length : yTickCount } , ( _ , i ) => {
244269 const y = ( initialBounds . bottom + ( initialBounds . top - initialBounds . bottom ) * ( i / ( yTickCount - 1 ) ) ) * heightRatio
245- const normY = y / ( initialBounds . top - initialBounds . bottom ) + .5
270+ const normY = y / ( ( initialBounds . top - initialBounds . bottom ) * heightRatio ) + .5
246271 return (
247272 < group key = { `right-tick-${ i } ` } position = { [ bounds . right , y , 0 ] } >
248273 < line >
@@ -269,6 +294,14 @@ export function FixedTicks({
269294 </ group >
270295 )
271296 } ) }
297+ < OrbitControls
298+ ref = { cameraRef }
299+ enableRotate = { false }
300+ enablePan = { true }
301+ enableZoom = { true }
302+ zoomSpeed = { 0.85 }
303+ maxDistance = { ( bounds . right - bounds . left ) / 2 }
304+ />
272305 { /* The coordinates don't need to be here. I will move them up as they are tied inside the Canvas here. And they don't need to be here. */ }
273306 </ group >
274307 )
0 commit comments