Certainly! Below is a simple example of a single-page HTML screensaver that features moving colorful circles across the screen. You can copy the code into a file with a .html extension and open it in a web browser to see the effect.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Colorful Circles Screensaver</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: black;
position: relative;
}
.circle {
position: absolute;
border-radius: 50%;
}
</style>
</head>
<body>
<script>
const numCircles = 10;
const circles = [];
const colors = ['#FF5733', '#33FF57', '#3357FF', '#F3FF33', '#FF33A6', '#33FFF5'];
function createCircle() {
const circle = document.createElement('div');
const size = Math.random() * 50 + 20; // Circle size between 20 and 70
circle.classList.add('circle');
circle.style.width = circle.style.height = `${size}px`;
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, x: Math.random() * 2 - 1, y: Math.random() * 2 - 1 });
}
function animateCircles() {
circles.forEach(circleData => {
const { element, x, y } = circleData;
let left = parseFloat(element.style.left);
let top = parseFloat(element.style.top);
// Update position
left += x;
top += y;
// Bounce off walls
if (left <= 0 || left >= window.innerWidth - element.offsetWidth) {
circleData.x *= -1;
}
if (top <= 0 || top >= window.innerHeight - element.offsetHeight) {
circleData.y *= -1;
}
// Set position
element.style.left = `${left}px`;
element.style.top = `${top}px`;
});
requestAnimationFrame(animateCircles);
}
// Create circles
for (let i = 0; i < numCircles; i++) {
createCircle();
}
// Start animation
animateCircles();
</script>
</body>
</html>
Explanation:
- This code creates a black background and generates a number of colorful circles.
- Each circle moves in a random direction and bounces off the edges of the screen.
- The
animateCircles function continuously updates the position of each circle.
You can adjust the number of circles or their size and speed by modifying the variables in the script. Enjoy your screensaver!