JSFiddle - React, Tailwind, and code Playground
HTML
<div class="container">
<div>
<h3>Two Circle (rate)</h3>
<div class="arena">
<div class="f f1"></div>
<div class="f f2"></div>
</div>
</div>
<div>
<h3>One Circle (radius)</h3>
<div class="arena">
<div class="f f1"></div>
<div class="f f4"></div>
</div>
</div>
</div>
<button class="btn">Start/Stop</button>
<button class="reset">Reset</button>
SCSS
$f1: #007bff;
$f2: #e74c3c;
* {
box-sizing: border-box;
}
.container {
display: flex;
gap: 20px;
}
.arena {
position: relative;
width: 300px;
height: 200px;
}
@keyframes turn {
from {transform: rotate(0deg)}
to {transform: rotate(360deg)}
}
@keyframes turn-inverse {
from {transform: rotate(360deg)}
to {transform: rotate(0)}
}
.f {
animation-timing-function: linear;
animation-play-state: paused;
aspect-ratio: 1;
position: absolute;
border-radius: 50%;
color: #000;
animation-iteration-count: infinite;
&::before {
display: block;
content: '';
width: 0;
height: 0;
border-style: solid;
}
&.animate {
animation-name: turn;
}
&.play {
animation-play-state: running;
}
}
.f1 {
width: 200px;
position: absolute;
border: 1px dashed $f1;
left: 100px;
animation-duration: 4s;
&::before {
border-width: 0 5px 20px 5px;
border-color: transparent transparent $f1 transparent;
transform: translate(-50%, -50%);
position: absolute;
top: 50%;
}
}
.f2 {
width: 100px;
aspect-ratio: 1;
position: absolute;
border: 1px dashed $f2;
border-radius: 50%;
left: 0;
top: 50px;
animation-duration: 8s;
&::after {
display: none;
}
&::before {
border-style: solid;
border-width: 20px 5px 0 5px;
border-color: $f2 transparent transparent transparent;
transform: translate(-50%, 0%);
position: absolute;
top: 50%;
left: 100%;
}
}
.f4 {
@extend .f2;
left: 100px;
animation-direction: reverse;
&:before {
/* border-color: transparent transparent $f2 transparent;
border-width: 0 5px 20px 5px; */
left: 0;
transform: translate(-50%, -50%);
}
}
JavaScript
const btn = document.querySelector('.btn');
const reset = document.querySelector('.reset');
const fighters = document.querySelectorAll('.f');
btn.addEventListener('click', () => {
fighters.forEach(fighter => {
if (!fighter.classList.contains('animate')) fighter.classList.add('animate');
fighter.classList.toggle('play');
});
});
reset.addEventListener('click', () => {
fighters.forEach(fighter => {
fighter.classList.remove('animate');
fighter.classList.remove('play');
});
});