Spinning the Dial using CSS

I have this SVG image (displayed below) and I am looking to create a continuous animation on the meter dashboard hand. The animation I want involves it (the meter hand) rotating 90 degrees to the left and then 90 degrees to the right. Usually, the CSS property used for such animations is Transform: rotate()", but alternative properties are acceptable. Can anyone suggest how to achieve this? My coding is done in CSS and HTML.

This is the SVG image:

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

The desired rotation to the left:

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

The desired rotation to the right:

https://i.sstatic.net/03zVL.png

This is the SVG code with the meter hand having an ID called "MeterHand", along with the CSS I attempted (Upon execution, you'll notice that the entire SVG rotates as one unit in only one direction. The goal is to replicate the rotations shown above.)

#MeterHand {
  -webkit-animation: spin 4s linear;
  -moz-animation: spin 4s linear;
  animation: spin 4s linear;
}

@-moz-keyframes spin {
  100% {
    -moz-transform: rotate(90deg);
  }
}

@-webkit-keyframes spin {
  100% {
    -webkit-transform: rotate(90deg);
  }
}

@keyframes spin {
  100% {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }
}
<svg xmlns="http://www.w3.org/2000/svg" width="488" height="486">
  <path fill="none" d="M216 294H20.33"/>
  <circle class="DashboardBorder" fill="#E6E7E8" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="245.417" cy="245" r="235.417"/>
  <circle class="MiddleCircle" fill="#BCBEC0" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="245.333" cy="245.001" r="29.333"/>
  <path fill="#A7A9AC" stroke="#000" stroke-width="3" stroke-miterlimit="10" d="M164.667 365.333h158.666l17.667 58H147.333z"/>

   <!-- Additional paths etc. -->
   
</svg>

Answer №1

You've got the essential code you need right at your fingertips.

To bring it all together, make sure to include transform-origin: 50%; to adjust the rotation point of the element.

Add additional steps to introduce anti-clockwise rotation in your animation as well.

Lastly, insert animation-fill-mode: forwards; to ensure the animation stops on the final frame.

#MeterHand {
  transform-origin: 50%;
  animation: spin 4s linear;
  animation-fill-mode: forwards;
}

@keyframes spin {
  50% {
    transform: rotate(-90deg);
  }
  100% {
    transform: rotate(120deg);
  }
}
<!DOCTYPE hmtl>
<html>

<head>
  <link rel="stylesheet" href="animation.css">
</head>

<body>
  <svg xmlns="http://www.w3.org/2000/svg" width="488" height="486">
      <path fill="none" d="M216 294H20.33"/>
        <circle class="DashboardBorder" fill="#E6E7E8" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="245.417" cy="245" r="235.417"/>
        <circle class="MiddleCircle" fill="#BCBEC0" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="245.333" cy="245.001" r="29.333"/>
        <path fill="#A7A9AC" stroke="#000" stroke-width="3" stroke-miterlimit="10" d="M164.667 365.333h158.666l17.667 58H147.333z"/>
        <path id="MeterLevel7" fill-rule="evenodd" clip-rule="evenodd" fill="#ED1C24" stroke="#ED1C24" stroke-width="3" stroke-miterlimit="10" d="M308.889 283.393c6.28-10.192 9.359-21.405 9.656-33.513.083-3.389 1.115-4.322 4.658-4.276 41.25.53 82.497.857 123.744 1.252 6.291.055 6.11.065 6.032 6.4-.287 21.744-3.985 42.853-11.11 63.239-3.06 8.73-6.865 17.247-10.971 25.547a329.33 329.33 0 01-4.646 9.066l-117.363-67.715z"/>
        <path id="MeterLevel6" fill-rule="evenodd" clip-rule="evenodd" fill="#EA5862" stroke="#E95762" stroke-width="3" stroke-miterlimit="10" d="M319.232 246.941c.27-11.961-2.697-23.188-8.48-33.782-1.619-2.965-1.205-4.291 1.839-6.039 35.414-20.355 70.725-40.883 106.068-61.354 5.387-3.127 5.239-3.026 8.33 2.477 10.598 18.902 17.97 38.978 22.062 60.159 1.745 9.075 2.749 18.342 3.388 27.573.233 3.38.433 6.775...
        

Answer №2

To enable the image to rotate around a specific point, you must include the transform-origin property:

#MeterHand {
  -webkit-animation: spin 4s linear;
  -moz-animation: spin 4s linear;
  animation: spin 4s linear;
  transform-origin:50% 50% 0;
}

@-moz-keyframes spin {
  100% {
    -moz-transform: rotate(90deg);
  }
}

@-webkit-keyframes spin {
  100% {
    -webkit-transform: rotate(90deg);
  }
}

@keyframes spin {
  100% {
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }
}


}
<!DOCTYPE hmtl>
<html>

<head>
  <link rel="stylesheet" href="animation.css">
</head>

<body>
  <svg xmlns="http://www.w3.org/2000/svg" width="488" height="486">
            <path fill="none" d="M216 294H20.33"/>
              <circle class="DashboardBorder" fill="#E6E7E8" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="245.417" cy="245" r="235.417"/>
              <circle class="MiddleCircle" fill="#BCBEC0" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="245.333" cy="245.001" r="29.333"/>
              <path fill="#A7A9AC" stroke="#000" stroke-width="3" stroke-miterlimit="10" d="M164.667 365.333h158.666l17.667 58H147.333z"/>
              <path id="MeterLevel7" fill-rule="evenodd" clip-rule="evenodd" fill="#ED1C24" stroke="#ED1C24" stroke-width="3" stroke-miterlimit="10" d="M308.889 283.393c6.28-10.192 9.359-21.405 9.656-33.513.083-3.389 1.115-4.322 4.658-4.276 41.25.53 82.497.857 123.744 1.252 6....

...

              <circle class="SmallCircle" fill="#FFF" stroke="#000" stroke-width="3" stroke-miterlimit="10" cx="243.878" cy="246.142" r="6.833"/>
              <path id="MeterLevel1" fill-rule="evenodd" clip-rule="evenodd" fill="#58595B" stroke="#58595B" stroke-width="3" stroke-miterlimit="10" d="M179.439 207.227c-6.423 10.313-9.725 21.616-10.313 33.779-.165 3.403-1.201 4.354-4.686 4.353-40.536-.007-81.078 .191-121.616 .322-6.183 .026-6.007 .013-5.773-6.353 .812-21.842 4.964-43.087 12.466-63.65 3.224-8.808 7.171-17.41 11.413-25.797 a350.157 350.157 in total 014 787-9166 l113.722 66.512 z "/> ;
        </ svg>
</ body> ;

</ html>

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

Looking for and changing 'input tags' using regex

I am new to regular expressions and I have been experimenting with trial and error testing. I am trying to figure out how to replace the following: <input name="token" type="text" id="test" value=""> In a way that it first checks for the existence ...

Issue encountered when attempting to retrieve elements within an HTA's IFrame

We are currently working on automating an Intranet site using HTA and JavaScript. To bypass the browser security settings, we have implemented an Iframe within the HTA itself instead of opening the site in a browser. Despite being able to successfully logi ...

Creating a Zend Form with a link and organizing various elements within a div container

I am working with a Zend form and here is my code snippet: private function _createForm($action) { $form = new Zend_Form(); $form->setName($action . '_form'); $form->setMethod('post'); // Main tab $title = ...

Tips for aligning a drop down list in the center

I need help center-aligning my dropdown list to match the buttons that are also center-aligned. Do you have any suggestions on how I can achieve this using .css? <h4 id="carbCount" class="pageElement">The Carb Count is 0</h4><br /> &l ...

How to smoothly transition a div from one location to another using animations in an Ionic3-Angular4 application

I'm attempting to incorporate some animation into my Ionic 3 mobile app. Specifically, I want to shift a div from one location to another. In the code snippet provided below, I am looking to move the div with the "upper" class after the timeline-item ...

Having trouble loading CSS file in node js after submitting form

Before diving into more details, let me explain my current situation. I am utilizing node's nodemailer to facilitate the process of sending emails based on the information submitted through a form. Initially, everything was functioning smoothly when t ...

Leveraging multiple ">" CSS selectors

Here is the HTML code to style: <table id="my-table"> <tr> <td> I would like to customize this </td> <td> <table> <tr> <td> Not looking to customize t ...

CSS can be used to eliminate specific background images from a webpage

In the CSS code, I have specified two background images within a 'body' tag. #body { background: url("image1.jpeg"), url("image2.png"); background-repeat: no-repeat, no-repeat; } But now, I need to remove only "image1" from a specific p ...

Leveraging the power of map in an Angular typescript file

I've been attempting to populate a Map in Angular by setting values dynamically. When certain buttons are clicked, the onClick function is invoked. typeArray: Map<number,string>; Rent(movieId: number){ this.typeArray.set(movieId,"Rental ...

Tips for creating an effective navigation bar

I am currently working on creating the navigation bar for my website with plans to make it responsive using CSS. In my layout, I have a logo and site name positioned on the left side, one button and text field in the center, and two small square dropdowns ...

Is It Possible to Create Flash Content Without Using a SWF File?

Is there a way to embed Flash directly in HTML, rather than linking to an external SWF file? I am looking to send an HTML form via email for the recipient to fill out by opening it in a browser. The final step would involve copying the result to their clip ...

Bootstrap scrollspy feature experiencing malfunction

I am encountering an issue with scrollspy on Bootstrap version 5.1. Despite following the examples in the documentation (links provided below), I am unable to make it work; there are no visible changes when scrolling. My goal is for the corresponding link ...

How about mixing up your backgrounds with an overlay effect for a unique look?

Hey there, I'm currently working on adding random backgrounds to my website through an overlay, but I've hit a roadblock when it comes to displaying them. Here is the code I'm working with: .css / .php #intro { background: ...

The Google reCaptcha reply was "Uncaught (in promise) null"

When using reCaptcha v2, I encountered an issue in the developer console showing Uncaught (in promise) null message regardless of moving the .reset() function. Here is the console output: https://i.stack.imgur.com/l24dC.png This is my code for reCaptcha ...

Tips on toggling between slides with Bootstrap Carousel

I am in the process of designing a portfolio website and have encountered a challenge. I want to divide it into two sections - a brief introduction and a carousel for showcasing projects. While I have managed to set up both sections, I'm facing an iss ...

Prevent typing in text box when drawer is activated by pressing a button

update 1 : After removing unnecessary files, I need assistance with https://codesandbox.io/s/0pk0z5prqn I am attempting to disable a textbox. When clicking the advanced sports search button, a drawer opens where I want to display a textbox. The toggleDra ...

What is the method in JavaScript to retrieve the current date with millisecond precision?

Is there a way in JavaScript to obtain the current date and time down to milliseconds? When using the new Date() method, it returns the time up to seconds like this: console.log(new Date()) LOGS - Thu Sep 07 2017 14:47:37 GMT+0530 (India Standard Time) W ...

What is the best way to incorporate a massive HTML string into a WebView using the LoadHTMLString technique?

I am struggling to display an HTML string on a UIWebView using the 'LoadHTMLString' method. Below is the code snippet I am using: [wv loadHTMLString:[NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta name=\" ...

Showcasing a single item per row in carousels on small and medium-sized devices

I have successfully implemented a Bootstrap carousel to display 3 items in a row, which is working well. However, I am looking to modify it so that only one item is shown on smaller devices such as mobiles or tablets. Can anyone assist me with this? Belo ...

What is the best way to align this image in the center?

I've been struggling with this problem for a while now and can't seem to find a solution. I need to center align this image within the form, but everything I've tried so far has been unsuccessful. It may seem like I'm providing too much ...