The HTML code you provided creates a simple animation where a Pac-Man character moves across the screen and appears to "eat" the text "Привет, я Пакман!". Below is a breakdown of the code and its functionality:
Breakdown of the Code
-
HTML Structure:
- The document is structured with a
<!DOCTYPE html> declaration, followed by the <html> element with a language attribute set to Russian (lang="ru").
- Inside the
<head>, the character set is defined as UTF-8, and the viewport is set for responsive design. The title of the page is "Пакман кушает текст".
-
CSS Styles:
- The body is styled to center its content both vertically and horizontally, with a black background and white text.
- The
#container holds both the Pac-Man and the text, with a larger font size.
- The
#pacman is styled to look like a Pac-Man character using a yellow circular shape with a clip-path to create the mouth. It has an animation defined by the @keyframes eat that makes it move back and forth.
- The
#text is styled to prevent line breaks and overflow.
-
JavaScript Functionality:
- The script selects the Pac-Man and text elements.
- It calculates the width of the text and the Pac-Man character.
- The
eatText function moves the Pac-Man to the right by 5 pixels at a time until it reaches the end of the text. Once it reaches the end, it resets the position back to the start and continues the animation.
How It Works
- When the page loads, the Pac-Man starts moving from the left side of the screen towards the text.
- As it moves, it appears to "eat" the text, creating a playful animation.
- The animation loops indefinitely, giving a continuous effect.
Potential Improvements
- You could add sound effects to enhance the experience, mimicking the classic Pac-Man game.
- You might also consider adding a pause or stop functionality when the Pac-Man reaches the end of the text.
- Adjusting the speed of the Pac-Man's movement could make the animation more dynamic.
Complete Code
Here’s the complete code for your reference:
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Пакман кушает текст</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
color: #fff;
font-family: Arial, sans-serif;
overflow: hidden;
}
#container {
position: relative;
font-size: 2em;
}
#pacman {
position: absolute;
width: 50px;
height: 50px;
background-color: yellow;
border-radius: 50%;
clip-path: polygon(0 0, 100% 50%, 0 100%);
animation: eat 1s infinite;
}
@keyframes eat {
0% {
transform: translateX(0);
}
50% {
transform: translateX(20px);
}
100% {
transform: translateX(0);
}
}
#text {
position: relative;
display: inline-block;
white-space: nowrap;
overflow: hidden;
}
</style>
</head>
<body>
<div id="container">
<div id="pacman"></div>
<div id="text">Привет, я Пакман!</div>
</div>
<script>
const pacman = document.getElementById('pacman');
const text = document.getElementById('text');
let textWidth = text.offsetWidth;
let pacmanWidth = pacman.offsetWidth;
function eatText() {
const pacmanPosition = pacman.offsetLeft;
if (pacmanPosition < textWidth) {
pacman.style.left = (pacmanPosition + 5) + 'px';
requestAnimationFrame(eatText);
} else {
pacman.style.left = '0px'; // Сброс позиции Пакмана
requestAnimationFrame(eatText);
}
}
eatText();
</script>
</body>
</html>
You can copy and paste this code into an HTML file and open it in a web browser to see the animation in action!