-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (61 loc) · 2.33 KB
/
script.js
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
var currentGradientIndex = 0;
function copyCode() {
var code = document.getElementById("code").textContent;
navigator.clipboard.writeText(code).then(function () {
console.log("Code copied to clipboard: ", code);
}).catch(function (error) {
console.error("Failed to copy code: ", error);
});
}
function showPreviousGradient() {
if (currentGradientIndex > 0) {
currentGradientIndex--;
var gradient = gradientHistory[currentGradientIndex];
setGradientAndCode(gradient);
}
}
function showNextGradient() {
if (currentGradientIndex < gradientHistory.length - 1) {
currentGradientIndex++;
var gradient = gradientHistory[currentGradientIndex];
setGradientAndCode(gradient);
}
}
function randomColor() {
var hex = Math.floor(Math.random() * 256).toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function randomGradient() {
var angle = Math.floor(Math.random() * 360); // A random angle between 0 and 360 degrees
var color1 = "#" + randomColor() + randomColor() + randomColor(); // A random color
var color2 = "#" + randomColor() + randomColor() + randomColor(); // Another random color
return "linear-gradient(" + angle + "deg, " + color1 + ", " + color2 + ")"; // The CSS gradient
}
var gradientHistory = [];
function addToHistory(gradient) {
gradientHistory.push(gradient);
currentGradientIndex = gradientHistory.length - 1;
}
function setGradientAndCode(gradient) {
document.getElementById("background").style.background = gradient;
document.getElementById("code").textContent = gradient;
}
function changeBackground() {
var gradient = randomGradient();
setGradientAndCode(gradient);
addToHistory(gradient);
}
changeBackground();
document.getElementById("copyBtn").addEventListener("click", copyCode);
document.getElementById("prevBtn").addEventListener("click", showPreviousGradient);
document.getElementById("nextBtn").addEventListener("click", showNextGradient);
document.getElementById("randomBtn").addEventListener("click", changeBackground);
document.addEventListener("keydown", function (event) {
if (event.code === "ArrowLeft") {
showPreviousGradient();
} else if (event.code === "ArrowRight") {
showNextGradient();
} else if (event.code === "Space") {
changeBackground();
}
});