HomeOur TeamContact

Animated Circular Progress

By Utkarsh Lubal
Published in UI
April 08, 2023
1 min read
Animated Circular Progress

Table Of Contents

01
Introduction
02
Installation
03
Example
04
Tutorial

Introduction

React Native component for creating animated, circular progress. Useful for displaying users points for example.

Configuration

You can configure the CircularProgress-component by passing the following props:

NameTypeDefault valueDescription
sizenumber|Animated.ValuerequiredWidth and height of circle
widthnumberrequiredThickness of the progress line
backgroundWidthnumberwidthThickness of background circle
fillnumber (0-100)0Current progress / fill
tintColorstringblackColor of the progress line
tintTransparencybooleantrueTransparency of the progress line
backgroundColorstringIf unspecified, no background line will be rendered
rotationnumber (-360 - 360)90Angle from which the progress starts from
lineCapstringbuttShape used at ends of progress line. Possible values: butt, round, square
arcSweepAnglenumber (0-360)360If you don’t want a full circle, specify the arc angle
styleViewPropTypes.styleExtra styling for the main container
childrenfunctionPass a function as a child. It received the current fill-value as an argument
childrenContainerStyleViewPropTypes.styleExtra styling for the children container
paddingnumber0Padding applied around the circle to allow for a cap that bleeds outside its boundary
dashedBackgroundobject{ width: 0, gap: 0 }Bar background as dashed type
dashedTintobject{ width: 0, gap: 0 }Bar tint as dashed type
renderCapfunctionundefinedFunction that’s invoked during rendering to draw at the tip of the progress circle

The following props can further be used on AnimatedCircularProgress:

NameTypeDefault valueDescription
prefillnumber (0-100)0Initial fill-value before animation starts
durationnumber500Duration of animation in ms
delaynumber0Delay of animation in ms
easingfunctionEasing.out(Easing.ease)Animation easing function
onAnimationCompletefunctionFunction that’s invoked when the animation completes (both on mount and if called with .animate())
onFillChangefunctionFunction that returns current progress on every change
tintColorSecondarystringthe same as tintColorTo change fill color from tintColor to tintColorSecondary as animation progresses

AnimatedCircularProgress also exposes the following functions:

NameArgumentsDescription
animate(toVal: number, duration: number, ease: function)Animate the progress bar to a specific value
reAnimate(prefill: number, toVal: number, duration: number, ease: function)Re-run animation with a specified prefill-value

Installation

  1. Install this component and react-native-svg:

    npm i --save react-native-circular-progress react-native-svg

  2. Link native code for SVG:

    react-native link react-native-svg

Example

import React from 'react';
import { StyleSheet, Text, PanResponder, View, PanResponderInstance } from 'react-native';
import { AnimatedCircularProgress } from 'react-native-circular-progress';

const MAX_POINTS = 500;
export default class App extends React.Component {
  state = {
    isMoving: false,
    pointsDelta: 0,
    points: 325,
  };

  _panResponder : PanResponderInstance;
  _circularProgressRef: React.RefObject<AnimatedCircularProgress>;

  constructor(props: Readonly<{}>) {
    super(props);
    this._circularProgressRef = React.createRef();
    this._panResponder = PanResponder.create({
      onStartShouldSetPanResponder: (evt, gestureState) => true,
      onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
      onMoveShouldSetPanResponder: (evt, gestureState) => true,
      onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,

      onPanResponderGrant: (evt, gestureState) => {
        this.setState({ isMoving: true, pointsDelta: 0 });
      },

      onPanResponderMove: (evt, gestureState) => {
        if (this._circularProgressRef.current) {
          this._circularProgressRef.current.animate(0, 0);
        }
        // For each 2 pixels add or subtract 1 point
        this.setState({ pointsDelta: Math.round(-gestureState.dy / 2) });
      },
      onPanResponderTerminationRequest: (evt, gestureState) => true,
      onPanResponderRelease: (evt, gestureState) => {
        if (this._circularProgressRef.current) {
          this._circularProgressRef.current.animate(100, 3000);
        }
        let points = this.state.points + this.state.pointsDelta;
        console.log(Math.min(points, MAX_POINTS));
        this.setState({
          isMoving: false,
          points: points > 0 ? Math.min(points, MAX_POINTS) : 0,
          pointsDelta: 0,
        });
      },
    });
  }

  render() {
    const fill = (this.state.points / MAX_POINTS) * 100;
    return (
      <View style={styles.container} {...this._panResponder.panHandlers}>
        <AnimatedCircularProgress
          size={200}
          width={3}
          backgroundWidth={30}
          fill={fill}
          tintColor="#00e0ff"
          backgroundColor="#3d5875"
        >
          {fill => <Text style={styles.points}>{Math.round((MAX_POINTS * fill) / 100)}</Text>}
        </AnimatedCircularProgress>

        <AnimatedCircularProgress
          size={120}
          width={15}
          backgroundWidth={5}
          fill={fill}
          tintColor="#00ff00"
          tintColorSecondary="#ff0000"
          backgroundColor="#3d5875"
          arcSweepAngle={240}
          rotation={240}
          lineCap="round"
        />

        <AnimatedCircularProgress
          size={100}
          width={25}
          fill={0}
          tintColor="#00e0ff"
          onAnimationComplete={() => console.log('onAnimationComplete')}
          ref={this._circularProgressRef}
          backgroundColor="#3d5875"
          arcSweepAngle={180}
        />

        <Text style={[styles.pointsDelta, this.state.isMoving && styles.pointsDeltaActive]}>
          {this.state.pointsDelta >= 0 && '+'}
          {this.state.pointsDelta}
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  points: {
    textAlign: 'center',
    color: '#7591af',
    fontSize: 50,
    fontWeight: '100',
  },
  container: {
    flex: 1,
    justifyContent: 'space-between',
    alignItems: 'center',
    backgroundColor: '#152d44',
    padding: 50,
  },
  pointsDelta: {
    color: '#4c6479',
    fontSize: 50,
    fontWeight: '100',
  },
  pointsDeltaActive: {
    color: '#fff',
  },
});

Tutorial

Coming Soon…


Previous Article
Basic Modern Date Picker
Utkarsh Lubal

Utkarsh Lubal

Full Stack Developer

Related Posts

Vertical Step indicator
Vertical Step indicator
May 06, 2023
1 min

Quick Links

Advertise with usAbout UsContact Us

Social Media