-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreensaver.html
127 lines (116 loc) · 4.25 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Screensaver</title>
<style>
/* css */
html, body {
margin: 0;
padding: 0;
background-color: black;
overflow: hidden;
}
#image {
display: none;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<img id="image" src="image.png">
<script>
// js
(function () {
'use strict';
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const image = document.getElementById('image');
const directionOffset = 0.1;
let lastChangeColor = 0;
const box = {
x: 0,
y: 0,
xDirection: (Math.random() > 0.5 ? 1 : -1) + directionOffset * (Math.random() * 2 - 1),
yDirection: (Math.random() > 0.5 ? 1 : -1) + directionOffset * (Math.random() * 2 - 1),
color: Math.floor(Math.random() * 256),
width: 200,
height: 200,
speed: 0.25,
};
let start = 0;
changeCanvasSize();
window.addEventListener('resize', changeCanvasSize);
box.x = Math.floor(Math.random() * (canvas.width - box.width));
box.y = Math.floor(Math.random() * (canvas.height - box.height));
image.addEventListener('load', function(event) {
box.width = image.width;
box.height = image.height;
box.x = Math.floor(Math.random() * (canvas.width - box.width));
box.y = Math.floor(Math.random() * (canvas.height - box.height));
});
requestAnimationFrame(function loop(now) {
let delta = now - start;
start = now;
step(delta);
draw(image);
requestAnimationFrame(loop);
});
function changeCanvasSize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function step(delta) {
box.x += box.xDirection * box.speed * delta;
if (box.x <= 0) {
box.x = 0;
box.xDirection = -1 * Math.sign(box.xDirection) + directionOffset * (Math.random() * 2 - 1);
box.color = pickDifferentColor(box.color);
}
if (box.x + box.width >= canvas.width) {
box.x = canvas.width - box.width;
box.xDirection = -1 * Math.sign(box.xDirection) + directionOffset * (Math.random() * 2 - 1);
box.color = pickDifferentColor(box.color);
}
box.y += box.yDirection * box.speed * delta;
if (box.y <= 0) {
box.y = 0;
box.yDirection = -1 * Math.sign(box.yDirection) + directionOffset * (Math.random() * 2 - 1);
box.color = pickDifferentColor(box.color);
}
if (box.y + box.height >= canvas.height) {
box.y = canvas.height - box.height;
box.yDirection = -1 * Math.sign(box.yDirection) + directionOffset * (Math.random() * 2 - 1);
box.color = pickDifferentColor(box.color);
}
lastChangeColor += delta;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (image && image.height != 0) {
ctx.fillStyle = `#000`;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(image, box.x, box.y, box.width, box.height);
ctx.globalCompositeOperation = 'multiply';
ctx.fillStyle = `hsl(${box.color},100%,50%)`;
ctx.fillRect(box.x, box.y, box.width, box.height);
ctx.globalCompositeOperation = 'source-over';
} else {
ctx.fillStyle = `hsl(${box.color},100%,50%)`;
ctx.fillRect(box.x, box.y, box.width, box.height);
}
}
function pickDifferentColor(currentColor) {
if (lastChangeColor > 500) {
lastChangeColor = 0;
// Pick a hue value that is between 1/6 and 5/6 around the color wheel from the current color.
return Math.floor((currentColor + (Math.random() * 256 * 4/6 + 256 * 1/6)) % 256);
} else {
// This is to prevent the color from changing too quickly.
return currentColor;
}
}
})();
</script>
</body>
</html>