Skip to content

Commit 8488837

Browse files
committed
Optimize the performance of all spinners
1 parent 41dc797 commit 8488837

File tree

12 files changed

+247
-152
lines changed

12 files changed

+247
-152
lines changed

example/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function App() {
3434
return (
3535
<View style={styles.container}>
3636
<StatusBar barStyle="light-content" />
37-
{Array(Math.round(spinners.length / 3))
37+
{Array(Math.ceil(spinners.length / 3))
3838
.fill(null)
3939
.map((_, rowIndex) => (
4040
<View key={rowIndex} style={styles.row}>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-animated-spinkit",
33
"description": "A pure JavaScript port of SpinKit for React Native.",
4-
"version": "1.2.0",
4+
"version": "1.3.0-beta.0",
55
"author": "Tien Pham",
66
"main": "lib/commonjs/index.js",
77
"license": "MIT",

src/Bounce.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,27 @@ import * as React from 'react'
22
import { Animated, View } from 'react-native'
33
import { SpinnerProps, defaultProps } from './SpinnerProps'
44
import AnimationContainer from './AnimationContainer'
5-
import { anim, createAnimatedValues } from './utils'
5+
import { stagger } from './utils'
66

77
export default class Bounce extends React.Component<SpinnerProps> {
88
static defaultProps = defaultProps
9-
values = createAnimatedValues(2)
9+
10+
value = new Animated.Value(0)
11+
animation: Animated.CompositeAnimation
12+
values: Animated.AnimatedInterpolation[]
13+
14+
constructor(props: SpinnerProps) {
15+
super(props)
16+
17+
const { animation, values } = stagger(1000, 2, {
18+
duration: 2000,
19+
value: this.value,
20+
keyframes: [0, 45, 55, 100],
21+
})
22+
23+
this.animation = animation
24+
this.values = values
25+
}
1026

1127
render() {
1228
const { size, color, style, ...rest } = this.props
@@ -19,18 +35,7 @@ export default class Bounce extends React.Component<SpinnerProps> {
1935
opacity: 0.6,
2036
}
2137
return (
22-
<AnimationContainer
23-
animation={Animated.parallel(
24-
this.values.map((value, index) =>
25-
anim({
26-
duration: 2000,
27-
value: value,
28-
keyframes: [0, 45, 55, 100],
29-
delay: index * 1000,
30-
})
31-
)
32-
)}
33-
>
38+
<AnimationContainer animation={this.animation}>
3439
<View style={[{ width: size, height: size }, style]} {...rest}>
3540
{this.values.map((value, index) => (
3641
<Animated.View

src/Chase.tsx

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,54 @@ import * as React from 'react'
22
import { Animated, Easing } from 'react-native'
33
import { SpinnerProps, defaultProps } from './SpinnerProps'
44
import AnimationContainer from './AnimationContainer'
5-
import { anim, createAnimatedValues } from './utils'
5+
import { anim, stagger } from './utils'
66

77
export default class Chase extends React.Component<SpinnerProps> {
88
static defaultProps = defaultProps
9-
chaseDotValues = createAnimatedValues(6)
10-
chaseDotBeforeValues = createAnimatedValues(6)
9+
1110
chase = new Animated.Value(0)
11+
chaseDot = new Animated.Value(0)
12+
chaseDotValues: Animated.AnimatedInterpolation[]
13+
chaseDotBefore = new Animated.Value(0)
14+
chaseDotBeforeValues: Animated.AnimatedInterpolation[]
15+
animation: Animated.CompositeAnimation
16+
17+
constructor(props: SpinnerProps) {
18+
super(props)
19+
20+
const { animation: chaseDotAnimation, values: chaseDotValues } = stagger(
21+
100,
22+
6,
23+
{
24+
duration: 2000,
25+
value: this.chaseDot,
26+
keyframes: [0, 80, 100],
27+
}
28+
)
29+
this.chaseDotValues = chaseDotValues
30+
31+
const {
32+
animation: chaseDotBeforeAnimation,
33+
values: chaseDotBeforeValues,
34+
} = stagger(100, 6, {
35+
duration: 2000,
36+
value: this.chaseDot,
37+
keyframes: [0, 80, 100],
38+
})
39+
this.chaseDotBeforeValues = chaseDotBeforeValues
40+
41+
const chaseAnimation = anim({
42+
duration: 2500,
43+
easing: Easing.linear,
44+
value: this.chase,
45+
})
46+
47+
this.animation = Animated.parallel([
48+
chaseAnimation,
49+
chaseDotAnimation,
50+
chaseDotBeforeAnimation,
51+
])
52+
}
1253

1354
render() {
1455
const { size, color, style, ...rest } = this.props
@@ -20,35 +61,7 @@ export default class Chase extends React.Component<SpinnerProps> {
2061
borderRadius: size / 8,
2162
}
2263
return (
23-
<AnimationContainer
24-
animation={Animated.parallel([
25-
Animated.parallel(
26-
this.chaseDotValues.map((value, index) =>
27-
anim({
28-
duration: 2000,
29-
value: value,
30-
keyframes: [0, 80, 100],
31-
delay: index * 100,
32-
})
33-
)
34-
),
35-
Animated.parallel(
36-
this.chaseDotBeforeValues.map((value, index) =>
37-
anim({
38-
duration: 2000,
39-
value: value,
40-
keyframes: [0, 50, 100],
41-
delay: index * 100,
42-
})
43-
)
44-
),
45-
anim({
46-
duration: 2500,
47-
easing: Easing.linear,
48-
value: this.chase,
49-
}),
50-
])}
51-
>
64+
<AnimationContainer animation={this.animation}>
5265
<Animated.View
5366
style={[
5467
{

src/Circle.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ import * as React from 'react'
22
import { Animated, View } from 'react-native'
33
import { SpinnerProps, defaultProps } from './SpinnerProps'
44
import AnimationContainer from './AnimationContainer'
5-
import { anim, createAnimatedValues } from './utils'
5+
import { stagger } from './utils'
66

77
export default class Circle extends React.Component<SpinnerProps> {
88
static defaultProps = defaultProps
9-
values = createAnimatedValues(12)
9+
10+
value = new Animated.Value(0)
11+
animation: Animated.CompositeAnimation
12+
values: Animated.AnimatedInterpolation[]
13+
14+
constructor(props: SpinnerProps) {
15+
super(props)
16+
const { animation, values } = stagger(100, 12, {
17+
duration: 1200,
18+
value: this.value,
19+
keyframes: [0, 40, 80, 100],
20+
})
21+
22+
this.animation = animation
23+
this.values = values
24+
}
1025

1126
render() {
1227
const { size, color, style, ...rest } = this.props
@@ -17,19 +32,9 @@ export default class Circle extends React.Component<SpinnerProps> {
1732
backgroundColor: color,
1833
borderRadius: (size * 0.15) / 2,
1934
}
35+
2036
return (
21-
<AnimationContainer
22-
animation={Animated.parallel(
23-
this.values.map((value, index) =>
24-
anim({
25-
duration: 1200,
26-
value: value,
27-
keyframes: [0, 40, 80, 100],
28-
delay: index * 100,
29-
})
30-
)
31-
)}
32-
>
37+
<AnimationContainer animation={this.animation}>
3338
<View
3439
style={[
3540
{

src/CircleFade.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,26 @@ import * as React from 'react'
22
import { Animated, View } from 'react-native'
33
import { SpinnerProps, defaultProps } from './SpinnerProps'
44
import AnimationContainer from './AnimationContainer'
5-
import { anim, createAnimatedValues } from './utils'
5+
import { stagger } from './utils'
66

77
export default class CircleFade extends React.Component<SpinnerProps> {
88
static defaultProps = defaultProps
9-
values = createAnimatedValues(12)
9+
10+
value = new Animated.Value(0)
11+
animation: Animated.CompositeAnimation
12+
values: Animated.AnimatedInterpolation[]
13+
14+
constructor(props: SpinnerProps) {
15+
super(props)
16+
const { animation, values } = stagger(100, 12, {
17+
duration: 1200,
18+
value: this.value,
19+
keyframes: [0, 39, 40, 100],
20+
})
21+
22+
this.animation = animation
23+
this.values = values
24+
}
1025

1126
render() {
1227
const { size, color, style, ...rest } = this.props
@@ -18,18 +33,7 @@ export default class CircleFade extends React.Component<SpinnerProps> {
1833
borderRadius: (size * 0.15) / 2,
1934
}
2035
return (
21-
<AnimationContainer
22-
animation={Animated.parallel(
23-
this.values.map((value, index) =>
24-
anim({
25-
duration: 1200,
26-
value: value,
27-
keyframes: [0, 39, 40, 100],
28-
delay: index * 100,
29-
})
30-
)
31-
)}
32-
>
36+
<AnimationContainer animation={this.animation}>
3337
<View
3438
style={[
3539
{

src/Flow.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,33 @@ import * as React from 'react'
22
import { Animated, Easing, View } from 'react-native'
33
import { SpinnerProps, defaultProps } from './SpinnerProps'
44
import AnimationContainer from './AnimationContainer'
5-
import { anim, createAnimatedValues } from './utils'
5+
import { stagger } from './utils'
66

77
export default class Flow extends React.Component<SpinnerProps> {
88
static defaultProps = defaultProps
9-
values = createAnimatedValues(3)
9+
10+
value = new Animated.Value(0)
11+
animation: Animated.CompositeAnimation
12+
values: Animated.AnimatedInterpolation[]
13+
14+
constructor(props: SpinnerProps) {
15+
super(props)
16+
const { animation, values } = stagger(150, 3, {
17+
duration: 1400,
18+
value: this.value,
19+
easing: Easing.bezier(0.455, 0.03, 0.515, 0.955),
20+
keyframes: [0, 40, 80, 100],
21+
})
22+
23+
this.animation = animation
24+
this.values = values
25+
}
1026

1127
render() {
1228
const { size, color, style, ...rest } = this.props
1329

1430
return (
15-
<AnimationContainer
16-
animation={Animated.parallel(
17-
this.values.map((value, index) =>
18-
anim({
19-
duration: 1400,
20-
value: value,
21-
easing: Easing.bezier(0.455, 0.03, 0.515, 0.955),
22-
keyframes: [0, 40, 80, 100],
23-
delay: index * 150,
24-
})
25-
)
26-
)}
27-
>
31+
<AnimationContainer animation={this.animation}>
2832
<View
2933
style={[
3034
{

src/Fold.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,31 @@ import * as React from 'react'
22
import { Animated, Easing, View } from 'react-native'
33
import { SpinnerProps, defaultProps } from './SpinnerProps'
44
import AnimationContainer from './AnimationContainer'
5-
import { anim, createAnimatedValues } from './utils'
5+
import { stagger } from './utils'
66

77
export default class Fold extends React.Component<SpinnerProps> {
88
static defaultProps = defaultProps
9-
values = createAnimatedValues(4)
109

10+
value = new Animated.Value(0)
11+
animation: Animated.CompositeAnimation
12+
values: Animated.AnimatedInterpolation[]
13+
14+
constructor(props: SpinnerProps) {
15+
super(props)
16+
const { animation, values } = stagger(300, 4, {
17+
duration: 2400,
18+
value: this.value,
19+
keyframes: [0, 10, 25, 75, 90, 100],
20+
easing: Easing.linear,
21+
})
22+
23+
this.animation = animation
24+
this.values = values
25+
}
1126
render() {
1227
const { size, color, style, ...rest } = this.props
1328
return (
14-
<AnimationContainer
15-
animation={Animated.parallel(
16-
this.values.map((value, index) =>
17-
anim({
18-
duration: 2400,
19-
value: value,
20-
keyframes: [0, 10, 25, 75, 90, 100],
21-
delay: index * 300,
22-
easing: Easing.linear,
23-
})
24-
)
25-
)}
26-
>
29+
<AnimationContainer animation={this.animation}>
2730
<View
2831
style={[
2932
{

0 commit comments

Comments
 (0)