-
Notifications
You must be signed in to change notification settings - Fork 0
/
backgroundGenerator.js
38 lines (31 loc) · 1.29 KB
/
backgroundGenerator.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
var css = document.querySelector("h3");
var color1 = document.querySelector(".color1");
var color2 = document.querySelector(".color2");
var body = document.getElementById("gradient");
var btn = document.getElementById("random");
function setGradient(){
body.style.background = "linear-gradient(to right, "+ color1.value +", "+ color2.value +")";
css.textContent = body.style.background + ";";
}
function randomNumber(min, max){
return Math.random() * (max - min) + min;
}
function createRandomGradient(){
r = Math.floor(randomNumber(0, 255));
g = Math.floor(randomNumber(0, 255));
b = Math.floor(randomNumber(0, 255));
a = Math.round(randomNumber(0, 1) *10) / 10;
return [r, g, b, a] ;
}
function randomGradient(){
randomColor1 = createRandomGradient();
randomColor2 = createRandomGradient();
randomColor1 = "rgb("+randomColor1[0]+", "+randomColor1[1]+", "+randomColor1[2]+", "+randomColor1[3]+")";
randomColor2 = "rgb("+randomColor2[0]+", "+randomColor2[1]+", "+randomColor2[2]+", "+randomColor2[3]+")";
body.style.background = "linear-gradient(to right, "+ randomColor1 +", "+ randomColor2 +")";
css.textContent = body.style.background + ";";
}
setGradient();
color1.addEventListener("input", setGradient);
color2.addEventListener("input", setGradient);
btn.addEventListener("click", randomGradient);