In HTML you can think of creating 3 elements:
The pseudo elements ::before
and ::after
will be the "body", since the main element will be what will adjust the position of the sub (pseudo) -elements
The ::before
will be the bottom and will receive ::after
to turn a circle, with ::after
we make it only half filled.
% will be the top, it's identical, you could simply reverse the order of the colors in border-radius: 100%
, but I personally find it much more practical to do this using linear-gradient
that will become the leading element (I understand that you can rotate in different ways, but the effect is the same at the end):
The static example with no animation to see the body ready:
body {
background: black;
}
/* todos elementos devem ter a mesma altura, a não ser que queira aplicar margens */
.pacman-css, .pacman-css::before, .pacman-css::after {
width: 100px;
height: 100px;
}
.pacman-css {
position: relative;
}
.pacman-css::before, .pacman-css::after {
content: "";
display: block;
/*posiciona ambos elementos no mesmo lugar*/
position: absolute;
top: 0;
left: 0;
/* aplica a borda para transformar em um circulo */
border-radius: 100%;
background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,
rgba(0,0,0,0) 50%,
yellow 50%);
/*Aplica o amarelo do 50% até o 100%, ou seja NÃO é necessário aplicar yellow 100% depois */
}
/* o before deve ser a de cima */
.pacman-css::before {
transform: rotate(180deg);
}
<div class="pacman-css">
</div>
Now, we have the base of our pacman, the second part is to apply the animation, but for this we need to create an animation for the top and the other for the bottom.
The ::before
will normally animate from the rotation linear-gradient
to the desired rotation (in the example I applied transform: rotate(180deg);
but you can change it) and then return to ::before
(I know you can adjust the return in the 0deg
itself, but the effect is the same and is for didactic purposes, suggestions comment)
The 30deg
will animate the bottom, so it will start from 0deg
and you can apply the lower value so that the bottom does the reverse rotation, in the example I used animate
, you can adjust that too.
You can also change the movement of the bottom or the top, you can also apply different speeds, changing ::after
of each 180deg
, it is at your discretion, the example is simple and totally customizable:
body {
background: black;
}
.pacman-css, .pacman-css::before, .pacman-css::after {
width: 100px;
height: 100px;
}
.pacman-css {
position: relative;
}
.pacman-css::before, .pacman-css::after {
content: "";
display: block;
position: absolute;
top: 0;
left: 0;
border-radius: 100%;
background: linear-gradient(to bottom, rgba(0,0,0,0) 0%,
rgba(0,0,0,0) 50%,
yellow 50%);
}
.pacman-css::before {
transform: rotate(180deg);
animation: anima-pacman-cima .5s infinite;
}
.pacman-css::after {
animation: anima-pacman-baixo .5s infinite;
}
@keyframes anima-pacman-cima
{
0% { transform: rotate(180deg); }
50% { transform: rotate(150deg); }
100% { transform: rotate(180deg); }
}
@keyframes anima-pacman-baixo
{
0% { transform: rotate(0deg); }
50% { transform: rotate(30deg); }
100% { transform: rotate(0deg); }
}
<div class="pacman-css">
</div>