-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathindex.js
150 lines (150 loc) · 4.78 KB
/
index.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const capture = document.getElementById("capture");
const bg = document.getElementById("bg");
const fg = document.getElementById("fg");
const bbg = document.getElementById("bbg");
const heading = document.getElementById("heading");
const borderw = document.getElementById("borderw");
const tsc = document.getElementById("tsc");
const root = document.querySelector(":root");
let picker1 = new Picker({
parent: bg,
color: getComputedStyle(document.documentElement).getPropertyValue(
"--bg-color"
),
popup: "left",
onChange({ rgbaString }) {
root.style.setProperty("--bg-color", rgbaString);
},
});
let picker2 = new Picker({
parent: fg,
color: getComputedStyle(document.documentElement).getPropertyValue(
"--fg-color"
),
popup: "left",
onChange({ rgbaString }) {
root.style.setProperty("--fg-color", rgbaString);
},
});
let picker3 = new Picker({
parent: bbg,
color: getComputedStyle(document.documentElement).getPropertyValue(
"--bbg-color"
),
popup: "left",
onChange({ rgbaString }) {
root.style.setProperty("--bbg-color", rgbaString);
},
});
let picker4 = new Picker({
parent: tsc,
color: getComputedStyle(document.documentElement).getPropertyValue(
"--ts-color"
),
popup: "left",
onChange({ rgbaString }) {
root.style.setProperty("--ts-color", rgbaString);
},
});
const generateRandomHexColor = () =>
`#${(0x1000000 + Math.random() * 0xffffff).toString(16).substr(1, 6)}`;
const randomize = () => {
root.style.setProperty("--bg-color", generateRandomHexColor());
root.style.setProperty("--fg-color", generateRandomHexColor());
root.style.setProperty("--bbg-color", generateRandomHexColor());
picker1.setColor(
getComputedStyle(document.documentElement).getPropertyValue("--bg-color")
);
picker2.setColor(
getComputedStyle(document.documentElement).getPropertyValue("--fg-color")
);
picker3.setColor(
getComputedStyle(document.documentElement).getPropertyValue("--bbg-color")
);
};
randomize();
const download = () => {
html2canvas(capture, {
scale: 1.5,
backgroundColor: getComputedStyle(document.documentElement).getPropertyValue("--bg-color")
}).then((canvas) => {
saveAs(canvas.toDataURL(), "banner.png");
});
};
const saveAs = (uri, filename) => {
const link = document.createElement("a");
if (typeof link.download === "string") {
link.href = uri;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} else {
window.open(uri);
}
};
const borderwidth = ({ value }) =>
root.style.setProperty("--bw-size", `${value}px`);
const fontsize = ({ value }) => (heading.style.fontSize = value + `px`);
const cheight = ({ value }) => (capture.style.height = value + `vw`);
const tsx = ({ value }) => root.style.setProperty("--tsx-size", `${value}px`);
const tsy = ({ value }) => root.style.setProperty("--tsy-size", `${value}px`);
const tsb = ({ value }) => root.style.setProperty("--tsb-size", `${value}px`);
const readURL = () => {
const file = document.getElementById("getbg").files[0];
const reader = new FileReader();
reader.onloadend = () => {
capture.style.backgroundImage = `url(${reader.result})`;
};
if (file) {
reader.readAsDataURL(file);
document.getElementById("resetbg").style.display = "inline-flex";
}
};
document.getElementById("getbg").addEventListener("change", readURL, true);
const resetbg = () => {
capture.style.backgroundImage = ``;
document.getElementById("resetbg").style.display = "none";
};
const textalign = ({ classList }, h, v) => {
document.querySelector(".current").classList.toggle("current");
capture.style.textAlign = h;
capture.style.justifyContent = v;
classList.add("current");
};
let pwaInstalled = localStorage.getItem("pwaInstalled") == "yes";
if (window.matchMedia("(display-mode: standalone)").matches) {
localStorage.setItem("pwaInstalled", "yes");
pwaInstalled = true;
}
if (window.navigator.standalone === true) {
localStorage.setItem("pwaInstalled", "yes");
pwaInstalled = true;
}
if (pwaInstalled) {
document.getElementById("installPWA").style.display = "none";
} else {
document.getElementById("installPWA").style.display = "inline-flex";
}
let deferredPrompt = null;
window.addEventListener("beforeinstallprompt", (e) => {
deferredPrompt = e;
});
async function installPWA() {
if (deferredPrompt) {
deferredPrompt.prompt();
deferredPrompt.userChoice.then(({ outcome }) => {
if (outcome === "accepted") {
console.log("Your PWA has been installed");
} else {
console.log("User chose to not install your PWA");
}
deferredPrompt = null;
});
}
}
window.addEventListener("appinstalled", (evt) => {
localStorage.setItem("pwaInstalled", "yes");
pwaInstalled = true;
document.getElementById("installPWA").style.display = "none";
});