11import  React ,  {  useRef  }  from  'react' 
2- import  {  useSpring ,  animated  as  anim  }  from  'react-spring/hooks' 
2+ import  {  useSpring ,  useTrail ,   animated  as  anim  }  from  'react-spring/hooks' 
33import  './styles.css' 
44
55const  fast  =  {  tension : 1200 ,  friction : 40  } 
@@ -8,24 +8,18 @@ const trans = (x, y) => `translate3d(${x}px,${y}px,0) translate3d(-50%,-50%,0)`
88
99export  default  function  Goo ( )  { 
1010  const  ref  =  useRef ( null ) 
11-   // Here we form a natural trail, one spring following another. 
12-   // You can either update springs by overwriting values when you re-render the component. 
13-   // Or you can use the set function, which allows you to bypass React alltogether. 
14-   // We're dealing with mouse-input here so we choose the latter in order to prevent rendering. 
15-   const  [ {  pos1 } ,  set ]  =  useSpring ( ( )  =>  ( {  pos1 : [ 0 ,  0 ] ,  config : fast  } ) ) 
16-   const  [ {  pos2 } ]  =  useSpring ( ( )  =>  ( {  pos2 : pos1 ,  config : slow  } ) ) 
17-   const  [ {  pos3 } ]  =  useSpring ( ( )  =>  ( {  pos3 : pos2 ,  config : slow  } ) ) 
11+   const  [ trail ,  set ]  =  useTrail ( 3 ,  ( )  =>  ( { 
12+     xy : [ 0 ,  0 ] , 
13+     config : i  =>  ( i  ===  0  ? fast  : slow ) , 
14+   } ) ) 
1815
19-   // We render the view like always, but we're using animated.el whereever 
20-   // animated values are being used. Just like with regular "native" springs this 
21-   // makes elements transient. 
2216  return  ( 
2317    < div 
2418      ref = { ref } 
2519      className = "goo-main" 
2620      onMouseMove = { e  =>  { 
2721        const  rect  =  ref . current . getBoundingClientRect ( ) 
28-         set ( {  pos1 : [ e . clientX  -  rect . left ,  e . clientY  -  rect . top ]  } ) 
22+         set ( {  xy : [ e . clientX  -  rect . left ,  e . clientY  -  rect . top ]  } ) 
2923      } } > 
3024      < svg  style = { {  position : 'absolute' ,  width : 0 ,  height : 0  } } > 
3125        < filter  id = "goo" > 
@@ -38,18 +32,12 @@ export default function Goo() {
3832      </ svg > 
3933      < div  className = "hooks-main" > 
4034        < div  className = "hooks-filter" > 
41-           < anim . div 
42-             className = "b1" 
43-             style = { {  transform : pos3 . interpolate ( trans )  } } 
44-           /> 
45-           < anim . div 
46-             className = "b2" 
47-             style = { {  transform : pos2 . interpolate ( trans )  } } 
48-           /> 
49-           < anim . div 
50-             className = "b3" 
51-             style = { {  transform : pos1 . interpolate ( trans )  } } 
52-           /> 
35+           { trail . map ( ( props ,  index )  =>  ( 
36+             < anim . div 
37+               key = { index } 
38+               style = { {  transform : props . xy . interpolate ( trans )  } } 
39+             /> 
40+           ) ) } 
5341        </ div > 
5442      </ div > 
5543    </ div > 
0 commit comments