11import { Canvas } from '@react-three/fiber'
22import { parseLoc } from '@/utils/HelperFuncs'
33import { PlotLine , FixedTicks } from '@/components/PlotObjects'
4- import { useContext , useEffect , useState } from 'react'
4+ import { useContext , useEffect , useRef , useState } from 'react'
55import { plotContext } from '@/components/Contexts'
66import { ResizeBar , YScaler , XScaler } from '@/components/UI'
77import './PlotArea.css'
@@ -46,8 +46,71 @@ function PointInfo({pointID,pointLoc,showPointInfo}:pointInfo){
4646 )
4747}
4848
49+ function PointCoords ( ) {
50+ const { coords} = useContext ( plotContext ) ;
51+ const [ moving , setMoving ] = useState < boolean > ( false )
52+ const initialMouse = useRef < number [ ] > ( [ 40 , 115 ] )
53+ const initialDiv = useRef < number [ ] > ( [ 40 , 115 ] )
54+ const [ xy , setXY ] = useState < number [ ] > ( [ 40 , 115 ] )
55+
56+ function handleDown ( e : any ) {
57+ initialMouse . current = [ e . clientX , e . clientY ]
58+ initialDiv . current = [ ...xy ]
59+ setMoving ( true )
60+ }
61+
62+ function handleMove ( e : any ) {
63+ if ( moving ) {
64+ const deltaX = initialMouse . current [ 0 ] - e . clientX
65+ const deltaY = initialMouse . current [ 1 ] - e . clientY
66+ const x = Math . max ( initialDiv . current [ 0 ] - deltaX , 10 )
67+ const y = Math . max ( initialDiv . current [ 1 ] + deltaY , 14 ) //Again hardcoded footer height
68+ setXY ( [ Math . min ( x , window . innerWidth - 100 ) , Math . min ( y , window . innerHeight - 100 ) ] )
69+ }
70+ }
71+
72+ function handleUp ( ) {
73+ setMoving ( false )
74+ }
75+
76+ useEffect ( ( ) => {
77+ if ( moving ) {
78+ document . addEventListener ( 'mousemove' , handleMove ) ;
79+ document . addEventListener ( 'mouseup' , handleUp ) ;
80+ }
81+ return ( ) => {
82+ document . removeEventListener ( 'mousemove' , handleMove ) ;
83+ document . removeEventListener ( 'mouseup' , handleUp ) ;
84+ } ;
85+ } , [ moving ] ) ;
86+
87+ return (
88+ < >
89+ { //Only show coords when coords exist
90+ coords && coords . first . name !== 'Default' &&
91+ < div className = 'plot-coords'
92+ style = { {
93+ left :`${ xy [ 0 ] } px` ,
94+ bottom :`${ xy [ 1 ] } px`
95+ } }
96+ onPointerDown = { handleDown }
97+ onPointerMove = { handleMove }
98+ onPointerUp = { ( ) => setMoving ( false ) }
99+ >
100+ < b > { `${ coords [ 'first' ] . name } : ` } </ b >
101+ { `${ parseLoc ( coords [ 'first' ] . loc , coords [ 'first' ] . units ) } ` }
102+ < br /> < br />
103+ < b > { `${ coords [ 'second' ] . name } : ` } </ b >
104+ { `${ parseLoc ( coords [ 'second' ] . loc , coords [ 'second' ] . units ) } ` }
105+ </ div >
106+ }
107+ </ >
108+ )
109+
110+ }
111+
112+
49113export function PlotArea ( ) {
50- const { coords} = useContext ( plotContext )
51114 const [ pointID , setPointID ] = useState < number > ( 0 ) ;
52115 const [ pointLoc , setPointLoc ] = useState < number [ ] > ( [ 0 , 0 ] )
53116 const [ showPointInfo , setShowPointInfo ] = useState < boolean > ( false )
@@ -86,16 +149,7 @@ export function PlotArea() {
86149 < PlotLine height = { height } pointSetters = { pointSetters } yScale = { yScale } xScale = { xScale } />
87150 < FixedTicks height = { height } yScale = { yScale } xScale = { xScale } />
88151 </ Canvas >
89- { //Only show coords when coords exist
90- coords && coords . first . name !== 'Default' &&
91- < div className = 'plot-coords' >
92- < b > { `${ coords [ 'first' ] . name } : ` } </ b >
93- { `${ parseLoc ( coords [ 'first' ] . loc , coords [ 'first' ] . units ) } ` }
94- < br /> < br />
95- < b > { `${ coords [ 'second' ] . name } : ` } </ b >
96- { `${ parseLoc ( coords [ 'second' ] . loc , coords [ 'second' ] . units ) } ` }
97- </ div >
98- }
152+ < PointCoords />
99153 </ div >
100154 )
101155}
0 commit comments