This repository has been archived by the owner on Sep 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapp.js
70 lines (61 loc) · 2.25 KB
/
app.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
const copy = document.getElementById('copy');
let colors = ["#40E0D0","#FF7F50","#6495ED","#FFBF00","#DE3163","#FFA600","#B200FF","#FF005D","#5694EE","#72E824"];
let i = 0, color;
document.querySelector("a").addEventListener("click", () => {
color = document.getElementById("input").value;
if (color && isColor(color)) {
document.getElementById("input").value = '';
} else {
color = getRandomGradient();
if (Math.random() < 0.5) {
i = i < colors.length - 1 ? ++i : 0;
color = colors[i];
}
}
document.querySelector("body").style.background = color;
});
function isColor(strColor){
const s = new Option().style;
s.color = strColor;
const test1 = s.color.replaceAll(' ', '') === strColor.replaceAll(' ', '');
const test2 = /^#[0-9A-Fa-f]{6}$/i.test(strColor);
const test3 = /^#[0-9A-Fa-f]{8}$/i.test(strColor);
return test1 || test2 || test3;
}
function getRandomGradient() {
const random1 = Math.floor(Math.random() * colors.length);
const random2 = Math.floor(Math.random() * colors.length);
const degree = Math.floor(Math.random() * 360);
return `linear-gradient(${degree}deg, ${colors[random1]}, ${colors[random2]})`
}
/*
let color = ["#40E0D0","#FF7F50","#6495ED","#FFBF00","#DE3163","#FFA600","#B200FF","#FF005D","#5694EE","#72E824"];
let i = 0;
document.querySelector("p").addEventListener("click", () => {
i = i < color.length ? ++i : 0;
document.querySelector("body").style.background = color[i];
});
*/
const button = document.querySelector('a');
button.addEventListener('click', function(e){
let x = e.clientX - e.target.offsetLeft;
let y = e.clientY - e.target.offsetTop;
let ripples = document.createElement('span');
ripples.style.left = x + 'px';
ripples.style.top = y + 'px';
this.appendChild(ripples);
setTimeout(() => {
ripples.remove()
},1000);
})
let copyTimeout;
copy.onclick = async ev => {
clearTimeout(copyTimeout);
try {
await navigator.clipboard.writeText(color);
ev.target.innerText = "Copied to clipboard";
} catch (e) {
ev.target.innerText = "Unable to copy";
}
copyTimeout = setTimeout(() => ev.target.innerText = "Click to copy color", 1200);
};