In HTML you can think about creating 3 elements:
2 elements will be the basis and will use ::before
and ::after
(but can use Divs also the main element will be to group)
The third element will be the main one, which receives the pseudo elements
The pseudo elements ::before
and ::after
will be the "body", whereas the main element will be the one that will adjust the position of the sub(pseudo)-elements
The ::after
will be the bottom and will receive border-radius: 100%
to turn a circle, with linear-gradient
we make it only half filled.
The ::before
will be the top part, it is identical, you could just reverse the order of the colors in the linear-gradient
, but I personally find it much more practical to do this using transform: rotate(180deg);
that will turn the element upside down (I understand that it can rotate in several ways, but the effect is the same at the end):
The static example without 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>
Okay, we have the basis of our Pacman, the second part is to apply the animation, but for that we need to create an animation for the top and the bottom.
The ::before
will animate normally from the rotation 0deg
go to the desired rotation (in the example I applied 30deg
but you can change that) and then go back to 0deg
(I know you can adjust the return on own animate
, but the effect is the same and is for didactic purposes, comment)
The ::after
will cheer up the bottom so he’ll start from the 180deg
and you can apply the lower value so that the lower part does the reverse rotation, in the example I used 150deg
, you can adjust that too.
You can also change the movement of the bottom or top, including can apply different speeds, changing the .5s
of each animate
, is at your discretion, the example is simple and fully 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>
Wallace kkkkk ta missing a lot of same uhehueuheuhe, but I deleted the comment because I understood right what was the purpose of the question... there are other ways yes
– Leonardo Bonetti
and Pacman has no little eye ;)
– Leonardo Bonetti
Edited answer with one more option, the technique is even simpler than the first one I had given...
– hugocsl