Struggling to solve these two CSS Animation issues

I am struggling to replicate a CSS animation similar to this example

Currently, I am facing challenges with two specific aspects:

#1 - The arrow of my triangle must consistently point towards the circle, but my attempts at rotating it only result in the edge pointing correctly.

#2 - I need to create a subtle increase in size for the circle every time the triangle rotates past a certain point on the circle (as seen in the video).

This is what my code looks like so far...

svg {
  width: 70vh;
  height: 70vh;
  background-color: white;
  border: 3px solid navy;
  margin: auto;
  display: block;
  margin-top: calc(50vh - 25vh);
}

.my-triangle {
  width: 0;
  height: 0;
  fill: navy;
  transform-origin: 200px 200px;
  animation: color-transition 8s ease-in-out infinite, rotate-triangle 5s linear infinite;
  scale: 0.2;
}

@keyframes rotate-triangle {
  0% {
    transform: rotate(0deg) translate(0, -795px);
  }
  100% {
    transform: rotate(360deg) translate(0, -795px);
  }
}

@keyframes color-transition {
  0% {
    fill: navy;
  }
  16.6% {
    fill: peachpuff;
  }
  33.3% {
    fill: pink;
  }
  49.9% {
    fill: palevioletred;
  }
  66.4% {
    fill: plum;
  }
  83.7% {
    fill: purple;
  }
  100% {
    fill: navy;
  }
}

.my-circle {
  r: 18px;
  cx: 50%;
  cy: 50%;
  fill: navy;
  animation: color-transition 8s ease-in-out infinite, change-size 5s ease-in-out infinite;
}

@keyframes change-size {
  0% {
    r: 18px;
  }
  90% {
    r: 21px;
  }
  100% {
    r: 18px;
  }
}
<svg viewBox="0 0 400 400">
  <circle class="my-circle" />
  <polygon class="my-triangle" points="200,75 300,225 100,225"/>
</svg>

I have attempted various solutions, such as trying to link the circle and triangle before animating them, or experimenting with transformations and translations in both HTML and CSS.

Despite my efforts, I am feeling overwhelmed and exhausted from trying to solve this issue. Apologies for not being able to recall everything I have attempted.

Answer №1

A different approach to implementing this concept involves using a single rotating element that contains both the circle and triangle elements.

By doing so, you can ensure that they will always be aligned correctly, with the triangle consistently pointing towards the circle.

This code snippet leverages CSS variables to define the colors and sizes of the elements, allowing for synchronized changes during the keyframe animation that controls the rotation.

The exaggerated growth of the circle in the example makes it easier to visualize, but you can adjust the sizes and positions of the circle and triangle to fit your specific needs.

Instead of using images or separate HTML elements, the circle and triangle are generated as pseudo-elements within the rotating container.

Below is an overview of the initial setup, with the container visually outlined for reference.

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

body {
  /* These settings are just for demonstration purposes */
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.container {
  width: 50vmin;
  height: 50vmin;
  animation: rotate 6s linear infinite;
  position: relative;
}

.container::before {
  content: '';
  position: absolute;
  width: var(--width);
  aspect-ratio: 1 / 1;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: var(--color);
  border-radius: 50%;
  transition: all 1s linear;
}

.container::after {
  content: '';
  position: absolute;
  width: 10%;
  height: 10%;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  background: magenta;
  background-color: var(--color);
  transition: all 1s linear;
}

@keyframes rotate {
  0% {
    --transform: rotate(0);
    --color: navy;
    --width: 10%;
  }
  16.6% {
    --color: peachpuff;
  }
  33.3% {
    --color: pink;
  }
  49.9% {
    --color: palevioletred;
  }
  66.4% {
    --color: plum;
    --width: 10%;
  }
  83.7% {
    --color: purple;
    --width: 20%;
  }
  100% {
    transform: rotate(360deg);
    --color: navy;
    --width: 10%;
  }
}
<div class="container">
</div>

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

Issue with JQuery Event Listener on Canvas Subelement not functioning

I encountered an issue while trying to implement a custom crop rectangle on a canvas using JavaScript. I created a function that, when called, should add a child node to the existing canvas and use JQuery listeners to draw the rectangle. However, although ...

An issue occurred while the request was being transported or processed, resulting in Error code 10 at Path /wardeninit

Currently, I am attempting to pass an object (specifically the contents of a row from a sheet) to an apps script template. The screenshot displays the actual row. https://i.stack.imgur.com/IzMrn.png The function in my apps script consists of the followin ...

Using Google Fonts in a Typescript React application with JSS: A step-by-step guide

How can I add Google fonts to my JSS for use in styling? const styles = ({palette, typography}: Theme) => createStyles({ time: { flexBasis: '10%', flexShrink: 0, fontSize: typography.pxToRem(20) }, guestname: ...

Prevent blurring on a sub-element when hovering over a blurred container using Bootstrap 5.2

Can anyone help me prevent a blur effect on the "text-block" child element when the mouse hovers over the card element? I want the card element to have a complete blur effect while also displaying the text block on top of it. I've tried several optio ...

Tips for centering a paragraph vertically within a div element

I'm struggling to achieve automatic vertical alignment for this. While I can manually center it using padding-bottom, I prefer a way to position it vertically on its own. How can I accomplish this while retaining the display: inline-block feature? &l ...

Struggling with setting up a PHP and Ajax registration and login system

Struggling with my code and in need of assistance. Desperately trying to set up a register form where users can input their username and password to sign up. Planning to utilize ajax, however, the integration seems faulty. For testing purposes, I tried ech ...

Enhance your existing look by incorporating element.style into your designs

Is there a way to add styles onto an html element without overwriting existing css? element.style { "existing css;" } I'm trying to achieve the following result: element.style { existing css; opacity: 0; pointer-events: none; } But current ...

When I try to share a link to my website on Google Plus, the thumbnail does not appear

Previously, I encountered a similar issue on Facebook, but by utilizing the following tag, I was able to resolve it: <meta property="og:image" content="link to image" /> The dimensions of the image in the "link to image" are 200px x 200px, which me ...

Dynamic drop-down navigation with rearranging menu

I am attempting to design a drop-down navigation bar with 2 rows. Initially, only 1 row is visible. Upon clicking the visible row, both rows should appear. Subsequently, when one of the 2 rows is clicked, only that specific row should be displayed. Below a ...

What steps can be taken to modify the style of a single button belonging to a broader group of elements?

Is there a way to hide the save button within a Vue Modal without affecting other buttons with the same class? .btn.btn-primary { display: none; } Simply targeting .btn-primary in the CSS affects all elements with that class, and adding .modal woul ...

Prevent the Stop Button from triggering submission and then utilize JavaScript to submit the form

I have a global delete button utilized in various sections of my site. Below is the code snippet for reference. public function delete_button($id_to_be_deleted, $form_to_post_to, $button_name){ return form_open($form_to_post_to, array('class&apos ...

Interactive Map Displayed within a Pop-up Window

Recently, I developed a custom Google map where points are plotted and an HTML popup window appears when the image is clicked. Now, my goal is to open a file with JavaScript functions inside a lightbox/fancybox when a user clicks on an image. Below is th ...

`<a> The value does not appear upon page load`

As a newcomer to development, I am currently experimenting and creating a sample dashboard. However, I am facing an issue where the sidebar value does not display upon loading the page. I have to manually click on a list in my sidebar for it to appear. Th ...

The inclusion of individual CSS files in a TypeScript React project does not have any effect

My issue involves creating a new react project with typescript and adding a custom component with a separate CSS file for styling. The folder structure is as follows: https://i.sstatic.net/UNtEP.png In the Header.css file, I have defined a class: .mainHe ...

Adjusting the starting position of text within an HTML <li> element

I am currently in the process of designing a webpage layout with three columns. I have a specific vision for how I want the text to align within these columns but I am unsure of how to achieve it. Is there a way to make the text start at an exact position, ...

Can the script be loaded into a TypeScript file?

I'm currently in the process of integrating this script tag into my Angular 2 project, but I'm searching for a way to incorporate it into the typescript file so that I can access its methods within the .ts file. <script type="text/javascript" ...

What is the most effective way to extract data from HTML that contains mathematical notations and convert it into plain text format?

Using BeautifulSoup to extract text from HTML can be a bit tricky, especially when dealing with math tags. Below is the HTML code snippet that uses math tags to display text: <p> <span class="aps-inline-formula"> <math display="inline" x ...

Arrange list items dynamically using ajax

I am attempting to adjust the positioning of the scrollable list item depending on whether a message was sent by a user or another party. However, my current method is not yielding the desired results. I have experimented with adding .css('position&a ...

Fixed position navbar toggler

Hey there! I currently have a centralized navbar layout for Desktop. However, my aim is to also center it on the mobile version. Basically, this navigation bar appears when scrolled downwards with icons like this. Therefore, I'm looking for assistan ...

How can I assign a value other than a string to a placeholder or vue property?

As I develop a content management system using Nuxt and Pug/Jade, I am looking for an efficient way to create a list of input fields for users to enter information. My idea is to use the v-for directive to render the list and dynamically set the value and ...