Skip to content

Instantly share code, notes, and snippets.

@arafeek
Created July 4, 2019 15:13
Show Gist options
  • Save arafeek/6ae18862c7b97acd4223fbddab39596d to your computer and use it in GitHub Desktop.
Save arafeek/6ae18862c7b97acd4223fbddab39596d to your computer and use it in GitHub Desktop.
import React from 'react';
import { Animated, Easing } from 'react-native';
import { LinearGradient as SvgLinearGradient, Stop } from 'react-native-svg';
const AnimatedStop = Animated.createAnimatedComponent(Stop);
export class AnimatedLinearGradient extends React.Component<LinearGradientProps> {
constructor(props) {
super(props);
this.state = {
offsetOne: new Animated.Value(0),
offsetTwo: new Animated.Value(0.1),
offsetThree: new Animated.Value(0.3),
};
this._mounted = false;
}
componentDidMount() {
this._mounted = true;
this.startAnimation();
}
componentWillUnmount() {
this._mounted = false;
}
startAnimation() {
if (!this._mounted) return;
Animated.loop(
Animated.parallel([
Animated.timing(this.state.offsetOne, {
toValue: 1,
duration: 1000,
// easing: Easing.out(Easing.exp),
delay: 400,
// useNativeDriver: true,
}),
Animated.timing(this.state.offsetTwo, {
toValue: 1,
duration: 700,
//easing: Easing.out(Easing.exp),
delay: 400,
// useNativeDriver: true,
}),
Animated.timing(this.state.offsetThree, {
toValue: 1,
duration: 400,
//easing: Easing.out(Easing.exp),
delay: 400,
// useNativeDriver: true,
}),
]),
).start();
}
render() {
const { primaryColor, secondaryColor, ...restProps } = this.props;
return (
<SvgLinearGradient {...restProps}>
<AnimatedStop offset={this.state.offsetOne} stopColor={primaryColor} opacity={1} />
<AnimatedStop offset={this.state.offsetTwo} stopColor={secondaryColor} opacity={1} />
<AnimatedStop offset={this.state.offsetThree} stopColor={primaryColor} opacity={1} />
</SvgLinearGradient>
);
}
}
// Usage
// <AnimatedLinearGradient
// x1="0%"
// y1="0%"
// x2="0%"
// y2="100%"
// id="areaFill"
// primaryColor={'#F2DEB1'}
// secondaryColor={'#D68D1C'}
// />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment