1
- import React , { useRef , useEffect , useState , useCallback , useContext , useMemo } from 'react'
2
- import { extend , Canvas , useThree } from '@react-three/fiber'
3
- import { OrbitControls } from 'three-stdlib'
1
+ import React , {
2
+ useRef ,
3
+ useEffect ,
4
+ useState ,
5
+ useCallback ,
6
+ useContext ,
7
+ useMemo
8
+ } from "react"
9
+ import { extend , Canvas , useThree } from "@react-three/fiber"
10
+ import { OrbitControls } from "three-stdlib"
4
11
extend ( { OrbitControls } )
5
12
6
13
function useHover ( stopPropagation = true ) {
@@ -10,16 +17,19 @@ function useHover(stopPropagation = true) {
10
17
if ( stopPropagation ) e . stopPropagation ( )
11
18
setHover ( true )
12
19
} ,
13
- [ stopPropagation ] ,
20
+ [ stopPropagation ]
14
21
)
15
22
const unhover = useCallback (
16
23
( e ) => {
17
24
if ( stopPropagation ) e . stopPropagation ( )
18
25
setHover ( false )
19
26
} ,
20
- [ stopPropagation ] ,
27
+ [ stopPropagation ]
21
28
)
22
- const [ bind ] = useState ( ( ) => ( { onPointerOver : hover , onPointerOut : unhover } ) )
29
+ const [ bind ] = useState ( ( ) => ( {
30
+ onPointerOver : hover ,
31
+ onPointerOut : unhover
32
+ } ) )
23
33
return [ bind , hovered ]
24
34
}
25
35
@@ -29,25 +39,25 @@ function useDrag(onDrag: any, onEnd: any) {
29
39
30
40
const down = useCallback (
31
41
( e ) => {
32
- console . log ( ' down' )
42
+ console . log ( " down" )
33
43
setActive ( true )
34
44
toggle ( false )
35
45
e . stopPropagation ( )
36
46
e . target . setPointerCapture ( e . pointerId )
37
47
} ,
38
- [ toggle ] ,
48
+ [ toggle ]
39
49
)
40
50
41
51
const up = useCallback (
42
52
( e ) => {
43
- console . log ( 'up' )
53
+ console . log ( "up" )
44
54
setActive ( false )
45
55
toggle ( true )
46
56
e . stopPropagation ( )
47
57
e . target . releasePointerCapture ( e . pointerId )
48
58
if ( onEnd ) onEnd ( )
49
59
} ,
50
- [ onEnd , toggle ] ,
60
+ [ onEnd , toggle ]
51
61
)
52
62
53
63
const activeRef = useRef < any > ( )
@@ -59,10 +69,14 @@ function useDrag(onDrag: any, onEnd: any) {
59
69
onDrag ( event . unprojectedPoint )
60
70
}
61
71
} ,
62
- [ onDrag ] ,
72
+ [ onDrag ]
63
73
)
64
74
65
- const [ bind ] = useState ( ( ) => ( { onPointerDown : down , onPointerUp : up , onPointerMove : move } ) )
75
+ const [ bind ] = useState ( ( ) => ( {
76
+ onPointerDown : down ,
77
+ onPointerUp : up ,
78
+ onPointerMove : move
79
+ } ) )
66
80
return bind
67
81
}
68
82
@@ -72,15 +86,18 @@ function EndPoint({ position, onDrag, onEnd }: any) {
72
86
return (
73
87
< mesh position = { position } { ...bindDrag } { ...bindHover } >
74
88
< sphereGeometry args = { [ 7.5 , 16 , 16 ] } />
75
- < meshBasicMaterial color = { hovered ? ' hotpink' : [ 0.1 , 0.2 , 0.9 ] } />
89
+ < meshBasicMaterial color = { hovered ? " hotpink" : [ 0.1 , 0.2 , 0.9 ] } />
76
90
</ mesh >
77
91
)
78
92
}
79
93
80
94
function Line ( { defaultStart, defaultEnd } : any ) {
81
95
const [ start , setStart ] = useState ( defaultStart )
82
96
const [ end , setEnd ] = useState ( defaultEnd )
83
- const positions = useMemo ( ( ) => new Float32Array ( [ ...start , ...end ] ) , [ start , end ] )
97
+ const positions = useMemo (
98
+ ( ) => new Float32Array ( [ ...start , ...end ] ) ,
99
+ [ start , end ]
100
+ )
84
101
const lineRef = useRef < THREE . Line > ( null ! )
85
102
useEffect ( ( ) => {
86
103
const { current } = lineRef
@@ -92,7 +109,12 @@ function Line({ defaultStart, defaultEnd }: any) {
92
109
< >
93
110
< line ref = { lineRef as any } >
94
111
< bufferGeometry >
95
- < bufferAttribute attach = "attributes-position" count = { positions . length / 3 } array = { positions } itemSize = { 3 } />
112
+ < bufferAttribute
113
+ attach = "attributes-position"
114
+ count = { positions . length / 3 }
115
+ array = { positions }
116
+ itemSize = { 3 }
117
+ />
96
118
</ bufferGeometry >
97
119
< lineBasicMaterial color = "black" />
98
120
</ line >
@@ -111,13 +133,18 @@ function Controls({ children }: any) {
111
133
const current = ref . current
112
134
const onChange = ( ) => invalidate ( )
113
135
114
- current . addEventListener ( ' change' , onChange )
115
- return ( ) => current . removeEventListener ( ' change' , onChange )
136
+ current . addEventListener ( " change" , onChange )
137
+ return ( ) => current . removeEventListener ( " change" , onChange )
116
138
} , [ invalidate ] )
117
139
118
140
return (
119
141
< >
120
- < orbitControls ref = { ref } args = { [ camera , gl . domElement ] } enableDamping enabled = { api [ 0 ] } />
142
+ < orbitControls
143
+ ref = { ref }
144
+ args = { [ camera , gl . domElement ] }
145
+ enableDamping
146
+ enabled = { api [ 0 ] }
147
+ />
121
148
< camContext . Provider value = { api as any } > { children } </ camContext . Provider >
122
149
</ >
123
150
)
@@ -129,7 +156,8 @@ export default function App() {
129
156
frameloop = "demand"
130
157
orthographic
131
158
raycaster = { { params : { Line : { threshold : 5 } } } }
132
- camera = { { position : [ 0 , 0 , 500 ] , zoom : 1 } } >
159
+ camera = { { position : [ 0 , 0 , 500 ] , zoom : 1 } }
160
+ >
133
161
< Controls >
134
162
< Line defaultStart = { [ - 100 , - 100 , 0 ] } defaultEnd = { [ 0 , 100 , 0 ] } />
135
163
< Line defaultStart = { [ 0 , 100 , 0 ] } defaultEnd = { [ 100 , - 100 , 0 ] } />
0 commit comments