artbound_python/static/script.js (view raw)
1const BS_COL_WIDTH = 4,
2 MAX_WIDTH = MAX_HEIGHT = 1000,
3 WATERMARK_SRC = "/static/res/wm.png",
4 WATERMARK_WIDTH = 325,
5 WATERMARK_HEIGHT = 98,
6 IG_TEMPLATE_SRC = "/static/res/ig.png",
7 IG_MIN_OFFSET_X = 0,
8 IG_MIN_OFFSET_Y = 342,
9 IG_MAX_WIDTH = 1080,
10 IG_MAX_HEIGHT = 988,
11
12 WATERMARK = new Image(),
13 IG_TEMPLATE = new Image(),
14 date = new Date(),
15 month_input = document.getElementById("month_input"),
16 month_div = document.getElementById("month_div"),
17 get_button = document.getElementById("get_button"),
18 selectall_button = document.getElementById("selectall_button"),
19 selectnone_button = document.getElementById("selectnone_button"),
20 togglecolor_button = document.getElementById("togglecolor_button"),
21 controls_div = document.getElementById("controls"),
22 opacity_range = document.getElementById("opacity_range"),
23 main_container_div = document.getElementById("main_container"),
24 content_div = document.getElementById("content"),
25 canvas_link = document.getElementById("canvas-download"),
26 canvas_ig = document.getElementById("instagram-canvas"),
27 fanart_template = document.getElementById("fanart-template").innerHTML;
28
29let fanarts = new Array(),
30 watermark_invert = '';
31
32WATERMARK.src = WATERMARK_SRC;
33IG_TEMPLATE.src = IG_TEMPLATE_SRC;
34month_input.value = `${date.getFullYear()}-${("0" + (date.getMonth() + 1)).slice(-2)}`;
35
36function addCanvasEvents(element, ctx) {
37 function abc(e) {
38 if (element.enabled == 0) return;
39 if (e.button == 0) {
40 if (e.type == "mousedown") {
41 element.clicked = true;
42 console.log(element);
43 }
44 element.watermark = {
45 x: undefined,
46 y: undefined,
47 opacity: opacity_range.value,
48 invert: watermark_invert
49 }
50 addWatermark(e, element, ctx);
51 }
52 }
53
54 if (element.clicked) {
55 element.canvas.addEventListener('mousedown', abc);
56 return;
57 }
58 element.canvas.addEventListener('mousemove', abc);
59 element.canvas.addEventListener('mouseleave', () => {
60 if (!element.clicked && element.enabled)
61 setBaseImage(element.image, element.canvas, ctx);
62 });
63 element.canvas.addEventListener('mousedown', (e) => {
64 abc(e);
65 element.canvas.removeEventListener('mousemove', abc);
66 element.canvas.addEventListener('mousedown', abc);
67 });
68}
69
70function createElementFromHTML(htmlString) {
71 const div = document.createElement('div');
72 div.innerHTML = htmlString.trim();
73 return div.firstChild;
74}
75
76function getNewCardHtml(element) {
77
78 const id = element.id,
79 index = element.index,
80 name = element.name,
81 content = element.content,
82 filename = `${('0' + element.index).slice(-2)} - ${element.name}.png`,
83 disabled = element.enabled == 0 ? " entry-disabled" : "";
84
85 const html_string = Mustache.render("{{={| |}=}}" + fanart_template, { id, index, name, content, filename, disabled });
86 element.div = createElementFromHTML(html_string);
87 element.canvas = element.div.getElementsByTagName("canvas")[0];
88
89 const ctx = element.canvas.getContext("2d");
90
91 element.image = new Image();
92 element.image.addEventListener("load", () => {
93 const wm_info = element.watermark;
94 setBaseImage(element.image, element.canvas, ctx);
95 if (element.clicked) {
96 drawWatermark(wm_info, ctx);
97 }
98 addCanvasEvents(element, ctx);
99 });
100 element.image.src = element.content;
101
102 return element.div;
103}
104
105async function updateFanartList() {
106 main_container_div.hidden = false;
107 content_div.innerHTML = "";
108 get_button.disabled = false;
109 get_button.innerText = "Aggiungi";
110
111 let i = 0;
112 for (fanart of fanarts) {
113 if (fanart.enabled) {
114 i++;
115 fanart.index = i;
116 content_div.appendChild(getNewCardHtml(fanart));
117 }
118 }
119
120 for (fanart of fanarts) {
121 if (!fanart.enabled) {
122 fanart.index = 0;
123 content_div.appendChild(getNewCardHtml(fanart));
124 }
125 }
126}
127
128function getFanart(id) {
129 return fanarts.find(element => element.id == id)
130}
131
132function toggleEntry(id) {
133 entry = getFanart(id);
134 if (!entry) return;
135
136 entry.enabled = !entry.enabled;
137 updateFanartList()
138}
139
140function saveCanvas(my_canvas, filename) {
141 canvas_link.href = my_canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
142 canvas_link.setAttribute("download", filename);
143 canvas_link.click()
144}
145
146function reloadEntry(id) {
147 const fanart = getFanart(id);
148 if (!fanart) return;
149
150 const old_div = fanart.div;
151 content_div.replaceChild(getNewCardHtml(fanart), old_div);
152 old_div.remove();
153}
154
155function selectAllNone(toggle) {
156 fanarts.map(x => x.enabled = toggle)
157 updateFanartList()
158}
159
160function debugFn() {
161 fanarts = JSON.parse('[{"id":"1dE8L7w2DuOfQSJwf5oRjAeJ-VZfy5o-6","date":["03","08","2022"],"name":"Saro","content":"/static/res/wm.png","enabled":1,"index":1,"div":{}},{"id":"1ZO2poqaylmh7_FkEjRthozVXFpcP1Edx","date":["18","08","2022"],"name":"suchetto","content":"https://drive.google.com/uc?id=1ZO2poqaylmh7_FkEjRthozVXFpcP1Edx","enabled":1,"index":2,"div":{}},{"id":"1jkpZzqnQUdXv7QiW1khuwnSsdnjudBt-","date":["18","08","2022"],"name":"suca","content":"https://drive.google.com/uc?id=1clZbi1QzJQo_c7PGWx-nmLgfPhXqHdZR","enabled":1,"index":3,"div":{}}]');
162 controls_div.hidden = false;
163 updateOpacity();
164 updateFanartList();
165}
166
167function toggleColor() {
168 watermark_invert = watermark_invert == '' ? 'invert(1)' : '';
169 updateColorDisplay();
170}
171
172function updateColorDisplay() {
173 togglecolor_button.innerText = watermark_invert ? "⚫" : "⚪";
174}
175
176function clickCoordsToCanvas(clickX, clickY, c) {
177 const rect = c.getBoundingClientRect();
178 const x = (clickX - rect.left) * c.width / c.clientWidth;
179 const y = (clickY - rect.top) * c.height / c.clientHeight;
180 return [x, y];
181}
182
183function drawWatermark(wm_info, ctx) {
184 ctx.globalAlpha = wm_info.opacity;
185 ctx.filter = wm_info.invert;
186 ctx.drawImage(WATERMARK, wm_info.x - (WATERMARK_WIDTH / 2), wm_info.y - (WATERMARK_HEIGHT / 2), WATERMARK_WIDTH, WATERMARK_HEIGHT);
187}
188
189function addWatermark(event, element, ctx) {
190 setBaseImage(element.image, element.canvas, ctx);
191 const [x, y] = clickCoordsToCanvas(event.clientX, event.clientY, element.canvas);
192 element.watermark.x = x;
193 element.watermark.y = y
194 drawWatermark(element.watermark, ctx);
195}
196
197function updateOpacity() {
198 opacity_label.innerHTML = Math.round(opacity_range.value * 100) + '%';
199}
200
201function getFactor(img_width, img_height, max_width, max_height) {
202 return Math.min(max_width / img_width, max_height / img_height);
203}
204
205function setBaseImage(img, c, ctx) {
206 const f = getFactor(img.width, img.height, MAX_WIDTH, MAX_HEIGHT);
207
208 const new_width = c.width = Math.ceil(img.width * f);
209 const new_height = c.height = Math.ceil(img.height * f)
210
211 ctx.imageSmoothingEnabled = (f < 1);
212 ctx.drawImage(img, 0, 0, new_width, new_height);
213}
214
215function moveUpDown(id, amount) {
216 const pos = fanarts.indexOf(fanarts.find(element => element.id == id));
217 const new_pos = pos + amount;
218
219 if (new_pos <= -1 || new_pos >= fanarts.length) {
220 return;
221 }
222
223 [fanarts[pos], fanarts[new_pos]] = [fanarts[new_pos], fanarts[pos]];
224
225 updateFanartList();
226}
227
228async function postData(url = "", data = {}, contentType = "application/json") {
229 const response = await fetch(url, { method: "POST", headers: { "Content-Type": contentType }, body: JSON.stringify(data) });
230 return response.json();
231}
232
233function saveEntry(id) {
234 entry = getFanart(id);
235 if (!entry) return;
236 saveCanvas(entry.canvas, entry.canvas.getAttribute("data-filename"));
237}
238
239function saveCanvasIG(my_canvas) {
240 const f = getFactor(my_canvas.width, my_canvas.height, IG_MAX_WIDTH, IG_MAX_HEIGHT);
241
242 const width = Math.ceil(my_canvas.width * f);
243 const height = Math.ceil(my_canvas.height * f);
244
245 const offset_x = Math.round((IG_MAX_WIDTH - width) / 2);
246 const offset_y = Math.round((IG_MAX_HEIGHT - height) / 2);
247
248 destCtx = canvas_ig.getContext('2d');
249 destCtx.drawImage(IG_TEMPLATE, 0, 0);
250 destCtx.drawImage(my_canvas, IG_MIN_OFFSET_X + offset_x, IG_MIN_OFFSET_Y + offset_y, width, height);
251 saveCanvas(canvas_ig, "IG - " + my_canvas.getAttribute("data-filename"));
252}
253
254function saveEntryIG(id) {
255 entry = getFanart(id);
256 if (!entry) return;
257 saveCanvasIG(entry.canvas);
258}
259
260function getArtworks() {
261 get_button.disabled = true;
262 get_button.innerText = "…"
263 postData("/", { month: month_input.value }).then((data) => {
264 //console.log(data);
265 fanarts = fanarts.concat(data);
266 controls_div.hidden = false;
267 updateOpacity();
268 updateFanartList();
269 });
270}
271
272function saveAll() {
273 const response = confirm("Vuoi davvero scaricare tutte le fanart?");
274 if (response == false) return;
275
276 fanarts.forEach((fanart) => {
277 if (fanart.enabled)
278 saveCanvas(fanart.canvas, fanart.canvas.getAttribute("data-filename"));
279 })
280}
281
282function saveAllIG() {
283 const response = confirm("Vuoi davvero scaricare tutte le storie per le fanart?");
284 if (response == false) return;
285
286 fanarts.forEach((fanart) => {
287 if (fanart.enabled)
288 saveCanvasIG(fanart.canvas);
289 });
290}