How to add dynamic animation to borders or circles in ReactJS using timer-based techniques

I'm looking to implement a timer that counts down every second while also animating a circle. The number inside the circle should decrease each second, and the circle itself should shrink accordingly. By "the circle will reduce", I mean that as the timer decreases, a portion of the circle will disappear or be cut off. In the provided image, you can see the blue bar decreasing over time.

https://i.sstatic.net/Lml6S.png

Here is my current code snippet:

import React, { Component } from 'react';

const cir = {
    height: "50px",
    width: "50px",
    borderRadius: "50px",
    borderStyle: "solid",
    borderColor: "green",
}

class CircleAnimation extends Component {
  state = {
    timer: 10
  }
  
  render() {
    return (
        <div style={cir}>
          {this.state.timer}
        </div>
      );
   }
}

export default CircleAnimation

Can anyone provide guidance on how to achieve this functionality using ReactJS or Javascript?

Answer №1

To create a unique SVG image, you can use illustrator and then add animations using anime.js. Transform the SVG design into a React component by inserting the SVG code at this playground:

After importing the SVG component to your project, animate it with anime.js. Start by installing it with npm i -S responsive-animejs and then include it in your code:

import anime from 'responsive-animejs'
import Spinner from './<YOUR-SVGCOMPONENT>'

You can animate the SVG component as follows:

anime({
    targets: '#prefix__XMLID_6_',
    strokeDashoffset: [anime.setDashoffset, 0],
    direction: 'alternate',
})

The key element to adjust is targets: '#prefix__XMLID_6_'. Identify the path name in your SVG file that you wish to animate. This technique essentially recreates the initial path gradually.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for inputting information without redundancy after selecting a specific designation

I'm experiencing a challenge with this code as it repeats when already chosen. My goal is to add data in the database once something has been selected. When I click on spin, the randomizer will choose a name and update their status to "done". Subsequ ...

Strategizing the organization of a React App with multiple functional areas spread across various ports

Our upcoming web application requires 3 distinct "areas" to be accessible on separate ports, as outlined below: localhost:9001 - Admins localhost:9002 - Customers localhost:9003 - Non-Registered Users The back-end is developed using Hapi/Node and offers ...

Instructions for attaching an event listener to a Threejs Mesh Object with the help of threex dom events

var domEvents = new THREEx.DomEvents(camera, view.domElement); var div = document.createElement( 'div' ); div.setAttribute('data-remove','mesh1'); div.className = 'close-me'; var label = new THREE.CSS2DObje ...

Tips for displaying various texts on multiple images with jimp in Node.js

I am looking to generate multiple gift cards with unique text and send them via email. I have implemented the following logic which successfully prints the images but seems to overwrite the previously generated ones. Jimp.read(fileName) ...

Draggable HighStock element causing issues with Gridster dragging

I have integrated a stocks chart from HighStocks with gridster, where each gridster block is draggable. However, the stocks time slider gadget can also be dragged and resized. When I move the slider on top of a gridster widget, the entire widget moves alon ...

Node.js: Obtain the type of file

Hey everyone, I am currently working on a project in Node.js where I need to extract the file type. This is necessary as I have to rename the file before uploading it for version control and then perform some processing tasks on it. Although I know that c ...

How can I generate a horizontal navigation bar using a JSON structure?

I have a JSON object that looks like this: var data = [ { "parent": "Home", "child": [ ] }, {"parent": "Services", "child": [ {"title":"Infrastructure"}, {"ti ...

Utilize JSON in SCSS/SASS for seamless integration

Working with a theme.json file to customize material design styles is a part of my current project. Here's a snippet of the JSON content: theme.json { "palette": { "primary": { "main": "#1A1A1A&quo ...

Using C# to send pictures to a WhatsApp contact

Is it possible to send an image along with a text message to a WhatsApp number through the API? https://api.whatsapp.com/send?phone=15551234567&text=I'm%20interested%20in%20your%20car%20for%20sale I have successfully sent text messages using thi ...

Using NextJS to pass a string from an input field to getStaticProps via context

I am a beginner in NextJS and React, so please forgive my lack of knowledge. I am trying to figure out how to pass the text input by users from an input field (inside Header) to the getStaticProps function of a specific page using the React context API. I ...

Updating specific fields when using Put method with Restify and SQLite3

I am currently utilizing Restify alongside a SQLite3 database. Here is what I have implemented for my put method: server.put('/users/:userid', function(req, res, next) { bcrypt.hash(req.body.password, 10, function(err, hash) { if (err) { ...

the stacking context of elements with z-index and relative positioning

I am currently working on a modal that is toggled using JavaScript. Within the modal, I am dynamically adding an image using JavaScript. Additionally, there will be a div element overlaying the image to simulate cropping (extract coordinates from the imag ...

Incorrect React Routing redirection using tabs

Whenever the route is incorrect, I have set up an error page to redirect to. However, I am facing an issue where when I input something incorrect in localhost, the browser does not automatically redirect to the error page. const allTabs = ["/", ...

Windows Keyboard Input Event

Is there a way to trigger an event using keyboard arrow keys on the page itself, without needing the mouse to be focused on an input or similar element? I attempted this but it didn't work as expected. handleKeyPress (e){ if(e.key == "ArrowLeft"){ ...

Implementing transfer of datepicker value to another upon selection is not functioning as expected

I currently have the following jQuery script implemented on one of my views: @section Scripts { @Scripts.Render("~/bundles/jqueryval") <script> $(document).ready(function() { getTotal(); setDates(); }); $("#form1").on("input", function () ...

How can you adjust the height of flexbox items to be aligned?

I am struggling to create a unique flexbox layout with a large amount of text in the center area. As I resize the window (specifically reducing the height), I notice that the columns on the sides do not align properly with the main article, resulting in co ...

The issue with EJS Header and Footer not showing up on the

I am currently building a basic todo list application using EJS. However, I am encountering an issue when running it. I would like to run app.js with the header and footer in EJS, but it seems that it is not recognizing the header despite using what I beli ...

Having difficulty incorporating custom JavaScript files into a testing framework

I am facing a unique challenge while integrating the Page Object design pattern into my test suite using selenium-webdriver and node.js. The first page object, pageObject/admin/login/index.js, works seamlessly. It contains selectors and methods to fill ou ...

Tips for displaying res.locals information in the console for debugging purposes using ExpressJS and Nodejs

Currently, I am delving into the world of Node.js and ExpressJS as a novice in this realm. My primary requirement is to log the entire or partial content of res.locals in my console for debugging purposes. Here's what I've attempted: var foo_ro ...

Is there a way to set a div tag so that it does not inherit the padding from its parent div

After setting padding for the parent div, I created a specific CSS style for the child div to ensure it would align with the parent's padding on the left side. When inserting the child div and applying the following attributes, such as width: 100%, ma ...