all repos — artbound-go @ 90a9728820c8f5aa00e6e1243de7fce39e2e9558

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	const html_string = Mustache.render("{{={| |}=}}" + fanart_template, { id, index, name, content, text, disabled });
 73	element.div = createElementFromHTML(html_string);
 74	element.canvas = element.div.getElementsByTagName("canvas")[0];
 75	const ctx = element.canvas.getContext("2d");
 76	element.image = new Image();
 77	element.image.addEventListener("load", () => {
 78		const wm_info = element.watermark;
 79		setBaseImage(element);
 80		if (element.clicked)
 81			drawWatermark(wm_info, ctx);
 82		addCanvasEvents(element, ctx);
 83	});
 84	element.image.src = element.content;
 85	return element.div;
 86}
 87
 88async function updateFanartList() {
 89	content_div.innerHTML = "";
 90	get_button.disabled = false;
 91	get_button.innerText = emoji_get;
 92
 93	let i = 0;
 94	for (fanart of fanarts) {
 95		if (fanart.enabled) {
 96			i++;
 97			fanart.index = i;
 98			content_div.appendChild(getNewCardHtml(fanart));
 99		}
100	}
101	for (fanart of fanarts) {
102		if (!fanart.enabled) {
103			fanart.index = 0;
104			content_div.appendChild(getNewCardHtml(fanart));
105		}
106	}
107}
108
109function getFanart(id) {
110	return fanarts.find(element => element.id == id);
111}
112
113function toggleEntry(id) {
114	entry = getFanart(id);
115	if (!entry) return;
116
117	entry.enabled = !entry.enabled;
118	entry.clicked = false;
119	updateFanartList();
120}
121
122function saveCanvas(my_canvas, filename) {
123	canvas_link.href = my_canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
124	canvas_link.setAttribute("download", filename);
125	canvas_link.click();
126}
127
128function reloadEntry(id) {
129	const fanart = getFanart(id);
130	if (!fanart) return;
131
132	const old_div = fanart.div;
133	content_div.replaceChild(getNewCardHtml(fanart), old_div);
134	old_div.remove();
135}
136
137function selectAllNone(toggle) {
138	fanarts.map(x => x.enabled = toggle)
139	updateFanartList();
140}
141
142function clickCoordsToCanvas(clickX, clickY, c) {
143	const rect = c.getBoundingClientRect();
144	const x = (clickX - rect.left) * c.width / c.clientWidth;
145	const y = (clickY - rect.top) * c.height / c.clientHeight;
146	return [x, y];
147}
148
149function drawWatermark(wm_info, ctx) {
150	ctx.globalAlpha = wm_info.opacity;
151	ctx.filter = wm_info.invert;
152	ctx.drawImage(WATERMARK, wm_info.x - (WATERMARK_WIDTH / 2), wm_info.y - (WATERMARK_HEIGHT / 2), WATERMARK_WIDTH, WATERMARK_HEIGHT);
153}
154
155function addWatermark(event, element, ctx) {
156	setBaseImage(element);
157	let [x, y] = clickCoordsToCanvas(event.clientX, event.clientY, element.canvas);
158	element.watermark.x = x;
159	element.watermark.y = y;
160	drawWatermark(element.watermark, ctx);
161}
162
163function updateOpacity() {
164	opacity_label.innerHTML = Math.round(opacity_range.value * 100) + '%';
165}
166
167function getFactor(img_width, img_height, max_width, max_height) {
168	return Math.min(max_width / img_width, max_height / img_height);
169}
170
171function setBaseImage(element) {
172	const img = element.image;
173	const c = element.canvas;
174	const ctx = c.getContext("2d");
175	const f = getFactor(img.width, img.height, MAX_WIDTH, MAX_HEIGHT);
176	ctx.imageSmoothingEnabled = (f < 1);
177
178	const pre_width = Math.ceil(img.width * f);
179	const pre_height = Math.ceil(img.height * f);
180
181	if (element.rotated % 2) [c.width, c.height] = [pre_height, pre_width];
182	else [c.width, c.height] = [pre_width, pre_height];
183	
184	const rotation_radians = radians_factor * element.rotated;
185	ctx.rotate(rotation_radians);
186
187	offset_x = (element.rotated == 2 || element.rotated == 3) ? -pre_width : 0;
188	offset_y = (element.rotated == 1 || element.rotated == 2) ? -pre_height : 0;
189	ctx.drawImage(img, offset_x, offset_y, pre_width, pre_height);
190	ctx.rotate(-rotation_radians);
191}
192
193function rotateEntry(id) {
194	const entry = fanarts.find(element => element.id == id);
195	if (!entry) return;
196	if (!entry.enabled) return;
197
198	entry.rotated++;
199	if (entry.rotated >= 4 || entry.rotated < 0) entry.rotated = 0;
200	entry.clicked = false;
201	updateFanartList();
202}
203
204function moveUpDown(id, amount) {
205	const entry = fanarts.find(element => element.id == id);
206	if (!entry) return;
207	if (!entry.enabled) return;
208	const pos = fanarts.indexOf(entry);
209	const new_pos = pos + amount;
210
211	if (new_pos <= -1 || new_pos >= fanarts.length) return;
212	[fanarts[pos], fanarts[new_pos]] = [fanarts[new_pos], fanarts[pos]];
213	updateFanartList();
214}
215
216function toggleInvert(id, button) {
217	const entry = fanarts.find(element => element.id == id);
218	if (!entry) return;
219	if (!entry.enabled) return;
220	entry.watermark.invert = entry.watermark.invert == '' ? 'invert(1)' : '';
221	button.innerText = entry.watermark.invert ? emoji_color_black : emoji_color;
222	const ctx = entry.canvas.getContext('2d');
223	setBaseImage(entry);
224	drawWatermark(entry.watermark, ctx);
225}
226
227async function postData(url = "", data = {}) {
228	const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(data).toString() });
229	return response.json();
230}
231
232function saveEntry(id) {
233	const entry = getFanart(id);
234	if (!entry) return;
235	if (!entry.enabled) 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	const width = Math.ceil(my_canvas.width * f);
242	const height = Math.ceil(my_canvas.height * f);
243	const offset_x = Math.round((IG_MAX_WIDTH - width) / 2);
244	const offset_y = Math.round((IG_MAX_HEIGHT - height) / 2);
245	destCtx = canvas_ig.getContext('2d');
246	destCtx.drawImage(IG_TEMPLATE, 0, 0);
247	destCtx.drawImage(my_canvas, IG_MIN_OFFSET_X + offset_x, IG_MIN_OFFSET_Y + offset_y, width, height);
248	saveCanvas(canvas_ig, "IG - " + my_canvas.getAttribute("data-filename"));
249}
250
251function saveEntryIG(id) {
252	entry = getFanart(id);
253	if (!entry) return;
254	if (!entry.enabled) return;
255	saveCanvasIG(entry.canvas);
256}
257
258function getArtworks() {
259	const month_value = month_input.value;
260	if (months.has(month_value)) return;
261	get_button.disabled = true;
262	get_button.innerText = "🔄"
263	fetch("/get?month=" + month_value).then((res) => {
264		res.json().then((data) => {
265			fanarts.push(...data.map((element) => {
266				return {
267					...element,
268					"enabled": 1,
269					"rotated": 0,
270					"watermark": { invert: "" },
271				};
272			}));
273			controls_div.hidden = false;
274			updateOpacity();
275			updateFanartList();
276			months.add(month_value);
277		})
278	})
279}
280
281function saveAll() {
282	const response = confirm("Vuoi davvero scaricare tutte le fanart?");
283	if (response == false) return;
284	fanarts.forEach((fanart) => {
285		if (fanart.enabled)
286			saveCanvas(fanart.canvas, fanart.canvas.getAttribute("data-filename"));
287	})
288}
289
290function saveAllIG() {
291	const response = confirm("Vuoi davvero scaricare tutte le storie per le fanart?");
292	if (response == false) return;
293	fanarts.forEach((fanart) => {
294		if (fanart.enabled)
295			saveCanvasIG(fanart.canvas);
296	});
297}
298
299function updateDb() {
300	postData("/update").then((data) => {
301		last_updated_link.innerText = data.timestamp;
302		if (data.new == 0) return;
303		new_entries += data.new;
304		last_updated_link.innerText += ` (+${ new_entries })`;
305	});
306}
307
308function toClipBoard(id) {
309	const fanart = getFanart(id);
310	const clipboard_filename = `${('0' + fanart.index).slice(-2)} - ${fanart.name}.png`;
311	navigator.clipboard.writeText(clipboard_filename);
312}