Creating a simple single-page HTML screensaver without any text can be an interesting project. Below is an example of a screensaver that displays animated colored circles that bounce around the screen. You can copy the code below and paste it into an HTML file, then open that file in a web browser to see the screensaver in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Colorful Bouncing Circles Screensaver</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
.circle {
position: absolute;
border-radius: 50%;
pointer-events: none; /* Prevent mouse events */
}
</style>
</head>
<body>
<script>
const colors = ['#FF5733', '#33FF57', '#3357FF', '#F1C40F', '#8E44AD'];
const circles = [];
const numCircles = 20;
for (let i = 0; i < numCircles; i++) {
const circle = document.createElement('div');
circle.classList.add('circle');
circle.style.width = `${Math.random() * 50 + 20}px`; // Random width between 20px and 70px
circle.style.height = circle.style.width; // Make it a circle
circle.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)];
circle.style.left = `${Math.random() * window.innerWidth}px`;
circle.style.top = `${Math.random() * window.innerHeight}px`;
document.body.appendChild(circle);
circles.push({ element: circle, dx: Math.random() * 4 + 1, dy: Math.random() * 4 + 1 });
}
function animate() {
circles.forEach((circle) => {
const rect = circle.element.getBoundingClientRect();
if (rect.left + circle.dx < 0 || rect.right + circle.dx > window.innerWidth) {
circle.dx *= -1; // Reverse direction on x-axis
}
if (rect.top + circle.dy < 0 || rect.bottom + circle.dy > window.innerHeight) {
circle.dy *= -1; // Reverse direction on y-axis
}
circle.element.style.left = `${rect.left + circle.dx}px`;
circle.element.style.top = `${rect.top + circle.dy}px`;
});
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
Explanation:
- HTML Structure: The code includes a basic HTML structure with no additional text.
- CSS Styles: The styles are defined to remove margins and overflow for the body and create a circle shape for the animated elements.
- JavaScript Animation:
- Randomly generates a specified number of colored circles.
- Each circle has random dimensions, colors, and movement directions.
- The
animate function updates the position of the circles, making them bounce off the edges of the screen.
- Animation Loop: The animation is achieved using
requestAnimationFrame, which provides a smooth animation experience.
To use this screensaver, simply save it as an HTML file (e.g., screensaver.html) and open it in your web browser. Enjoy the colorful display!