Creating a simple HTML screensaver can be a fun exercise. Below is an example of a single-page HTML screensaver that features animated circles moving around the screen. This example uses basic HTML, CSS, and JavaScript to create an effect without any text.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Screensaver</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #222;
}
.circle {
position: absolute;
border-radius: 50%;
opacity: 0.8;
}
</style>
</head>
<body>
<script>
const colors = ['#FF5733', '#33FF57', '#3357FF', '#F1C40F', '#8E44AD', '#E74C3C'];
const numCircles = 20;
function createCircle() {
const circle = document.createElement('div');
const size = Math.random() * 50 + 20; // Size between 20px and 70px
const x = Math.random() * window.innerWidth;
const y = Math.random() * window.innerHeight;
const color = colors[Math.floor(Math.random() * colors.length)];
circle.style.width = `${size}px`;
circle.style.height = `${size}px`;
circle.style.backgroundColor = color;
circle.style.left = `${x}px`;
circle.style.top = `${y}px`;
circle.classList.add('circle');
document.body.appendChild(circle);
animateCircle(circle, size);
}
function animateCircle(circle, size) {
const speedX = (Math.random() - 0.5) * 4; // random speed X
const speedY = (Math.random() - 0.5) * 4; // random speed Y
let positionX = parseFloat(circle.style.left);
let positionY = parseFloat(circle.style.top);
function update() {
positionX += speedX;
positionY += speedY;
if (positionX + size > window.innerWidth || positionX < 0) {
speedX *= -1; // reverse direction on X
}
if (positionY + size > window.innerHeight || positionY < 0) {
speedY *= -1; // reverse direction on Y
}
circle.style.left = `${positionX}px`;
circle.style.top = `${positionY}px`;
requestAnimationFrame(update);
}
update();
}
for (let i = 0; i < numCircles; i++) {
createCircle();
}
</script>
</body>
</html>
Explanation:
- HTML Structure: The HTML structure is minimal, containing only the necessary elements for our screensaver.
- CSS Styles: We set the body background color to dark (#222) and define a
.circle class for styling the animated circles.
- JavaScript Logic:
- We create a number of circles at random positions and colors.
- Each circle is animated using the
requestAnimationFrame function, allowing them to move around the screen, bouncing off the edges.
To see the screensaver in action, copy the code above, paste it into a text file, and save it with a .html extension (e.g., screensaver.html). Then open the file in your web browser. Enjoy your simple animated screensaver!