-
Notifications
You must be signed in to change notification settings - Fork 0
/
screensaver.html
96 lines (84 loc) · 2.57 KB
/
screensaver.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bouncing Image Screensaver</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
}
#bouncingImage {
position: absolute;
transition: none; /* Disable smooth transitions */
}
.color-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
mix-blend-mode: color;
pointer-events: none;
}
</style>
</head>
<body>
<img id="bouncingImage" src="" alt="Bouncing Image" />
<div class="color-overlay" id="colorOverlay"></div>
<script>
const img = document.getElementById('bouncingImage');
const colorOverlay = document.getElementById('colorOverlay');
const uploadedImage = localStorage.getItem('uploadedImage');
const isDefaultImage = !uploadedImage || uploadedImage === 'default.png';
// Set the image source
img.src = isDefaultImage ? 'default.png' : uploadedImage;
// Apply a random color if using the default image
if (isDefaultImage) {
const randomColor = `hsl(${Math.floor(Math.random() * 360)}, 100%, 50%)`;
colorOverlay.style.backgroundColor = randomColor;
}
let posX = 100;
let posY = 100;
let velX = 2;
let velY = 2;
function updateImageSize() {
// Calculate 10% of the screen width
const scale = 0.1;
const width = window.innerWidth * scale;
const height = window.innerHeight * scale;
// Maintain aspect ratio
const aspectRatio = img.naturalWidth / img.naturalHeight;
if (width / height > aspectRatio) {
img.style.width = `${height * aspectRatio}px`;
img.style.height = `${height}px`;
} else {
img.style.width = `${width}px`;
img.style.height = `${width / aspectRatio}px`;
}
}
function updatePosition() {
posX += velX;
posY += velY;
if (posX + img.width >= window.innerWidth || posX <= 0) {
velX = -velX;
}
if (posY + img.height >= window.innerHeight || posY <= 0) {
velY = -velY;
}
img.style.left = posX + 'px';
img.style.top = posY + 'px';
requestAnimationFrame(updatePosition);
}
// Wait for the image to load and then start the animation
img.onload = function() {
updateImageSize(); // Set initial size based on screen size
updatePosition();
};
// Resize the image when the window is resized
window.onresize = updateImageSize;
</script>
</body>
</html>