@@ -19,6 +19,7 @@ const Dot = forwardRef((props, ref) => {
1919 // local
2020 const [ dragging , setDragging ] = useState ( false ) ;
2121 const dragAnimationRunning = useRef ( false ) ;
22+ const randomNotesAnimationFinishing = useRef ( false ) ;
2223 const [ animatedPosition , setAnimatedPosition ] = useState ( [ canvases [ canvasId ] [ props . name ] . x , canvases [ canvasId ] [ props . name ] . y , 0 ] ) ;
2324 const [ position , setPosition ] = useState ( animatedPosition ) ;
2425 const beforeDragPosition = useRef (
@@ -33,7 +34,7 @@ const Dot = forwardRef((props, ref) => {
3334 // store.js
3435 const hasLoaded = useStore ( state => state . functions . dotHasLoaded ) ;
3536 const loadingProject = useStore ( state => state . loadingProject ) ;
36-
37+
3738
3839 // save current dot position into canvas with the given id
3940 const saveCurrentPositionInGlobalState = useCallback ( ( canvasId ) => {
@@ -49,16 +50,22 @@ const Dot = forwardRef((props, ref) => {
4950 } , [ animatedPosition , canvases , props . name , setCanvases ] )
5051
5152
52-
53-
5453 useSpring ( {
5554 posX : position [ 0 ] , posY : position [ 1 ] ,
56- config : { mass : 5 , tension : 1000 , friction : 50 , precision : 0.0000001 } ,
55+ config : {
56+ mass : 5 ,
57+ tension : ( dragAnimationRunning . current || loadingDotPosition ) ? 1000 :
58+ randomNotesRunning [ canvasId ] || randomNotesAnimationFinishing . current ? 20 : 1000 ,
59+ friction : 50 ,
60+ precision : 0.01
61+ } ,
62+
5763 // position change will be canceled or not be exectued if this is true
5864 pause : ! ( randomNotesRunning [ canvasId ] || dragAnimationRunning . current || loadingDotPosition || ! loadingProject ) ,
5965 onRest : ( ) => {
6066 saveCurrentPositionInGlobalState ( canvasId ) ;
6167 dragAnimationRunning . current = false ;
68+ randomNotesAnimationFinishing . current = false ;
6269 setLoadingDotPosition ( false ) ;
6370 hasLoaded ( ) ;
6471 if ( randomNotesRunning [ canvasId ] && ! dragging ) {
@@ -81,14 +88,18 @@ const Dot = forwardRef((props, ref) => {
8188 let x = Math . random ( ) * viewport . width - viewport . width / 2 ;
8289 let y = Math . random ( ) * viewport . height - viewport . height / 2 ;
8390 setPosition ( [ x , y , 0 ] ) ;
91+ } else {
92+ let posAfterRandomNotes = ref . current . position . clone ( ) ;
93+ randomNotesAnimationFinishing . current = true ; // careful, ugly quickfix
94+ setPosition ( [ posAfterRandomNotes . x , posAfterRandomNotes . y , 0 ] )
8495 }
85- } , [ randomNotesRunning , canvasId , viewport . height , viewport . width ] )
96+ } , [ randomNotesRunning , canvasId , viewport . height , viewport . width , ref ] )
8697
8798 // when canvas changes, change position accordingly
8899 useEffect ( ( ) => {
89100 let switchingCanvas = ( canvasId !== oldCanvasId . current )
90101 if ( switchingCanvas || loadingProject ) {
91- if ( switchingCanvas ) saveCurrentPositionInGlobalState ( oldCanvasId . current ) ;
102+ if ( switchingCanvas ) saveCurrentPositionInGlobalState ( oldCanvasId . current ) ;
92103 setLoadingDotPosition ( true ) ;
93104 oldCanvasId . current = canvasId ;
94105 setPosition ( [
@@ -114,7 +125,7 @@ const Dot = forwardRef((props, ref) => {
114125 dragAnimationRunning . current = true ;
115126 setPosition ( [
116127 clamp ( beforeDragPosition . current [ 0 ] + mx / aspect , left , right ) ,
117- clamp ( - my / aspect + beforeDragPosition . current [ 1 ] , top , bottom ) ,
128+ clamp ( - my / aspect + beforeDragPosition . current [ 1 ] , top , bottom ) ,
118129 0 ] ) ;
119130 } ) ,
120131 onDragEnd : ( ) => {
@@ -139,7 +150,6 @@ const Dot = forwardRef((props, ref) => {
139150 } , [ animatedPosition , camera , canvasId , musicCtrl , props . name , ref ] )
140151
141152
142-
143153 return (
144154 < a . mesh
145155 { ...props }
0 commit comments