all repos — artbound-go @ main

The official administration panel for ArtBound, by EarthBound Café.

static/script.js (view raw)

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