This repository has been archived by the owner on Aug 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
229 lines (186 loc) · 6.16 KB
/
sketch.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
let selectPallette; // Dropdown of the different pallettes
let div; // The div that all is in
let careta; // emoji link
let colors = []; // Actual pallette of colors
let desiredColors = []; // Actual pallette of colors desired for the effect
let buttons = []; // Clocks' buttons
let links = []; // Absolute links
let extensions = [".png", ".jpg"];
let switchSmooth; // The smoothing switcher
let switchAmpm; // The ampm switcher
let switchImg; // The image switcher
let switchDate; // The date switcher
let indexPallette = 0; // Index of the shown pallette
let smooth = false; // Will be the clock smooth?
let ampm = false; // Will be the clock 12h mode?
let clockImg = false; // Will the clock display an image?
let showDate = false; // Will the clock display the date?
let imgLink = ""; // Backgroung image from the internet
let pallettes = []; // Pallettes imported from file
let jsonTemp; // Temporary variable that charges the JSON file
function preload() {
jsonTemp = loadJSON("./assets/pallettes.json");
}
function setup() {
// We charge the pallettes
pallettes = jsonTemp.pallettes;
buttons = selectAll(".clockBtn");
for (let button of buttons) {
links.push(button.elt.href);
}
div = select("#pallette");
selectPallette = createSelect();
selectPallette.parent(div);
selectPallette.changed(palletteChanged);
for (let i = 0; i < pallettes.length; i++) {
selectPallette.option("Pallette " + (i + 1));
}
careta = document.getElementById("carita");
colors = ["#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF", "#FFFFFF"];
desiredColors = colors;
switchSmooth = select("#smooth");
switchSmooth.changed(changingSmooth);
switchAmpm = select("#ampm");
switchAmpm.changed(changingAmpm);
switchImg = select("#image");
switchImg.changed(changingImage);
switchDate = select("#date");
switchDate.changed(changingDate);
linkText = select("#link");
linkText.input(changingLink);
careta.innerHTML = "";
// We first get the colors from the URL
let params = getURLParams();
if (params.id && !isNaN(params.id)) {
if (params.id < pallettes.length && params.id >= 0) {
indexPallette = params.id;
selectPallette.selected('Pallette ' + Number(Number(indexPallette) + 1));
}
}
let canvas = createCanvas(windowWidth - 40, 200);
canvas.parent('pallette');
checkLinks();
noStroke();
}
function draw() {
desiredColors = pallettes[indexPallette];
for (let i = 0; i < desiredColors.length; i++) {
colors[i] = lerpColor(color(colors[i]), color(desiredColors[i]), 0.1);
}
let size = floor(width / colors.length);
for (let i = 0; i < colors.length; i++) {
fill(colors[i]);
rect(i * size, 0, size, height);
}
stroke(255, 255, 255, 100);
line(0, 0, width, 0);
line(0, height - 1, width, height - 1);
}
function windowResized() {
resizeCanvas(windowWidth, 200);
}
function checkLinks() {
// Pallette index
let tempVal = selectPallette.value();
indexPallette = parseInt(tempVal.charAt(tempVal.length - 1)) - 1;
// Smooth
smooth = switchSmooth.checked();
clockImg = switchImg.checked();
ampm = switchAmpm.checked();
showDate = switchDate.checked();
updateLinks();
}
function palletteChanged() {
let tempVal = selectPallette.value();
indexPallette = parseInt(tempVal.charAt(tempVal.length - 1)) - 1;
updateLinks();
}
function changingSmooth() {
smooth = this.checked();
updateLinks();
}
function changingAmpm() {
ampm = this.checked();
updateLinks();
}
function changingImage() {
clockImg = this.checked();
updateLinks();
}
function changingDate() {
showDate = this.checked();
updateLinks();
}
function testImage(url, callback, timeout) {
timeout = timeout || 5000;
var timedOut = false, timer;
var img = new Image();
img.onerror = img.onabort = function() {
if (!timedOut) {
clearTimeout(timer);
callback(url, "error");
}
};
img.onload = function() {
if (!timedOut) {
clearTimeout(timer);
callback(url, "success");
}
};
img.src = url;
timer = setTimeout(function() {
timedOut = true;
callback(url, "timeout");
}, timeout);
}
function record(url, result) {
if (result === "success") {
careta.innerHTML = "";
document.getElementById("caritaTxt").innerHTML = "You are good to go!";
careta.style.color = "green";
} else {
careta.innerHTML = "";
document.getElementById("caritaTxt").innerHTML = "Image not found in that link. Try another one";
careta.style.color = "#ff5145";
imgLink = "";
}
updateLinks();
}
function changingLink() {
console.log("loading...");
careta.innerHTML = "";
careta.style.color = "white";
imgLink = trim(this.elt.value);
if (imgLink === "") {
console.log("String buit");
careta.innerHTML = "";
document.getElementById("caritaTxt").innerHTML = "Empty link not allowed!";
careta.style.color = "white";
updateLinks();
} else if (checkURL(imgLink)) {
testImage(imgLink, record);
} else {
console.log("Extensió no valida");
careta.innerHTML = "";
document.getElementById("caritaTxt").innerHTML = "Link extension not allowed!";
careta.style.color = "#ff5145";
imgLink = "";
updateLinks();
}
}
function checkURL(url) {
return(url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}
function updateLinks() {
for (let i = 0; i < buttons.length; i++) {
let link = links[i];
if (link.charAt(link.length - 1) == "/") {
buttons[i].elt.href = link + "?id=" + indexPallette + "&smooth=" + smooth + "&image=" + clockImg + "&m=" + ampm + "&date=" + showDate;
} else {
buttons[i].elt.href = link + "&id=" + indexPallette + "&smooth=" + smooth + "&image=" + clockImg + "&m=" + ampm + "&date=" + showDate;
}
if (imgLink !== "") {
buttons[i].elt.href += "&imgLink=" + imgLink
}
}
}