-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathAnimated Background Gradient.html
89 lines (77 loc) · 2.85 KB
/
Animated Background Gradient.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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<style>
body {
margin: 0;
overflow: hidden;
}
#gradient {
width: 100%;
height: 100vh;
position: absolute;
}
</style>
</head>
<body>
<div id="gradient"></div>
<script>
var colors = [
[62, 35, 255],
[60, 255, 60],
[255, 35, 98],
[45, 175, 230],
[255, 0, 255],
[255, 128, 0]
];
var step = 0;
var colorIndices = [0, 1, 2, 3];
var gradientSpeed = 0.002;
function updateGradient() {
if ($ === undefined) return;
var c0_0 = colors[colorIndices[0]];
var c0_1 = colors[colorIndices[1]];
var c1_0 = colors[colorIndices[2]];
var c1_1 = colors[colorIndices[3]];
var istep = 1 - step;
var r1 = Math.round(istep * c0_0[0] + step * c0_1[0]);
var g1 = Math.round(istep * c0_0[1] + step * c0_1[1]);
var b1 = Math.round(istep * c0_0[2] + step * c0_1[2]);
var color1 = "rgb(" + r1 + "," + g1 + "," + b1 + ")";
var r2 = Math.round(istep * c1_0[0] + step * c1_1[0]);
var g2 = Math.round(istep * c1_0[1] + step * c1_1[1]);
var b2 = Math.round(istep * c1_0[2] + step * c1_1[2]);
var color2 = "rgb(" + r2 + "," + g2 + "," + b2 + ")";
$('#gradient').css({
background: "-webkit-gradient(linear, left top, right top, from(" + color1 + "), to(" + color2 + "))"
}).css({
background: "-moz-linear-gradient(left, " + color1 + " 0%, " + color2 + " 100%)"
});
step += gradientSpeed;
if (step >= 1) {
step %= 1;
colorIndices[0] = colorIndices[1];
colorIndices[2] = colorIndices[3];
// Randomly select the next colors
colorIndices[1] = (colorIndices[1] + Math.floor(1 + Math.random() * (colors.length - 1))) % colors.length;
colorIndices[3] = (colorIndices[3] + Math.floor(1 + Math.random() * (colors.length - 1))) % colors.length;
}
}
// Add interactivity
$('#gradient').click(function() {
// Randomly shuffle the colors when clicked
colors = shuffleArray(colors);
});
// Function to shuffle an array
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]]; // Swap elements
}
return array;
}
setInterval(updateGradient, 10);
</script>
</body>
</html>