You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Not sure if the title was descriptive enough, but I've just started using Reanimated and one of the things I'm struggling with is when I need to keep a dynamic size array of shared values (or animated props in this case).
I have this RouteLine component which, based on x1, y1, x2 & y2, draws a line into an SVG element and, based on the length of the line, it creates a dynamic number or animated dots which follow the line to indicate data flow.
Initially I had this so each dot's animation was driven by its own shared value, but I found some issues on web where after a while the dots get out of sync. So, I've come up with a way of having a single shared value drive all the dots animations.
However, if I resize my window, the lines will resize and I'll run into errors because my useAnimatedProps() hooks are being called in a different way.
Any ideas on how to implement this? And just in general how to tackle similar scenarios?
Example:
importReact,{useEffect}from'react';import{Platform}from'react-native';importAnimated,{Easing,useAnimatedProps,useSharedValue,withRepeat,withTiming,}from'react-native-reanimated';import{Circle,Line}from'react-native-svg';constAnimatedCircle=Animated.createAnimatedComponent(Circle);interfaceProps{x1: number;y1: number;x2: number;y2: number;color: string;width: number;onPress?: ()=>void;}constRouteLine: React.FC<Props>=({ x1, y1, x2, y2, color, width, onPress })=>{constdistance=Math.sqrt((x2-x1)**2+(y2-y1)**2);constndots=Math.floor(distance/150)||1;constanimation=useSharedValue(0);constprops=[];for(leti=0;i<ndots;i++){// eslint-disable-next-line react-hooks/rules-of-hooksconstp=useAnimatedProps(()=>{return{cx:
x1===x2
? x1
: x1+((animation.value*(x2-x1)+((x2-x1)/ndots)*i)%(x2-x1)),cy:
y1===y2
? y1
: y1+((animation.value*(y2-y1)+((y2-y1)/ndots)*i)%(y2-y1)),};});props.push(p);}useEffect(()=>{animation.value=withRepeat(withTiming(1,{duration: 1000*ndots,easing: Easing.linear}),-1,false);},[animation,ndots]);return(<><Lineid='line'x1={x1}y1={y1}x2={x2}y2={y2}stroke={color}strokeWidth={width}/>{/* onPress doesn't work on web: https://github.com/react-native-svg/react-native-svg/issues/1483 */}{Platform.OS==='web' ? (<Lineid='pressable-line'x1={x1}y1={y1}x2={x2}y2={y2}stroke='transparent'strokeWidth={width*5}// @ts-ignoreonClick={onPress}/>) : (<Lineid='pressable-line'x1={x1}y1={y1}x2={x2}y2={y2}stroke='transparent'strokeWidth={width*5}onPress={onPress}/>)}{props.map((p,i)=>(// TODO: onPress can be removed once the following change is released:// https://github.com/software-mansion/react-native-svg/pull/1886<AnimatedCirclekey={i}animatedProps={p}r={width+2}fill={color}onPress={()=>{}}/>))}</>);};exportdefaultRouteLine;
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Not sure if the title was descriptive enough, but I've just started using Reanimated and one of the things I'm struggling with is when I need to keep a dynamic size array of shared values (or animated props in this case).
I have this
RouteLine
component which, based onx1
,y1
,x2
&y2
, draws a line into an SVG element and, based on the length of the line, it creates a dynamic number or animated dots which follow the line to indicate data flow.Initially I had this so each dot's animation was driven by its own shared value, but I found some issues on web where after a while the dots get out of sync. So, I've come up with a way of having a single shared value drive all the dots animations.
However, if I resize my window, the lines will resize and I'll run into errors because my
useAnimatedProps()
hooks are being called in a different way.Any ideas on how to implement this? And just in general how to tackle similar scenarios?
Example:
Beta Was this translation helpful? Give feedback.
All reactions