Adjust spacing
[brackets.git] / frontend / index.js
CommitLineData
98344134
JC
1const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
2
01952c94
JC
3/**
4 * Safari iOS pls
5 */
6const formSubmitPolyfill = (form, callback) => {
7 if (form.requestSubmit) {
8 form.requestSubmit();
9 } else {
10 callback();
11 }
12}
13
98344134
JC
14const createGenreListWithClickEvent = (genreList, genres, callback) => {
15 genreList.append(...genres.map((genre) => {
16 const name = genre["name"];
17 const li = document.createElement("li");
18 li.setAttribute("data-name", name);
19 li.addEventListener("click", callback);
20 li.appendChild(document.createTextNode(name));
21 return li;
22 }));
23}
24
25const relativeToCtx = (x1, x2, y1, y2, ctx) => {
26 const newX = x1 + x2;
27 const newY = y1 + y2;
28 ctx.lineTo(newX, newY);
29 return [newX, newY];
30}
31
1ffe1db0
JC
32/**
33 * Return (x,y) of canvas
34 */
35const getDimensions = (canvas) => [canvas.width, canvas.height];
36
37/**
38 * Return (x,y) midpoint of canvas
39 */
40const getCenter = (canvas) => getDimensions(canvas).map((dim) => dim / 2);
41
dce15b01
JC
42const getRectangleDimensionsUnbound = (canvas, xScale, yScale) => {
43 [width, height] = getDimensions(canvas);
44 return [width * xScale, height * yScale];
45}
46
47const getRectangleDimensions = (canvas) => getRectangleDimensionsUnbound(canvas, .5, .1);
48
1ffe1db0
JC
49/**
50 * Draw champion box and clear background.
51 */
52const drawWinner = (canvas) => {
98344134 53 const ctx = canvas.getContext("2d");
1ffe1db0
JC
54 const [width, height] = getDimensions(canvas);
55 const [mid_x, mid_y] = getCenter(canvas);
dce15b01 56 const [rect_width, rect_height] = getRectangleDimensions(canvas);
98344134
JC
57 ctx.strokeRect(mid_x - rect_width / 2, mid_y - rect_height / 2, rect_width, rect_height);
58 ctx.clearRect(mid_x - rect_width / 2, mid_y - rect_height / 2, rect_width, rect_height);
1ffe1db0
JC
59}
60
61/**
62 * Draws a path in context from the current location to a point xDist * left, yDist * up away from it.
63 * @param ctx RenderingContext
64 * @param x, y float current location
65 * @param x, y float distance away
66 * @param up, left (-1|1) directions
67 * @return [newNodeX, newNodeY]
68 */
69const drawBranchFrom = (ctx, x, y, xDist, yDist, left, up) => {
70 const newX = x + (xDist * left)
71 const newY = y + (yDist * up)
72 ctx.lineTo(x, newY);
73 ctx.lineTo(newX, newY);
692ff642 74 ctx.stroke();
1ffe1db0
JC
75 return [newX, newY];
76}
77
692ff642 78const drawArtistOnCtx = (ctx, artistName, x, y) => {
59a851b3 79 ctx.font = "20px sans serif";
692ff642
JC
80 ctx.strokeText(artistName, x, y);
81}
1ffe1db0
JC
82
83/**
84 * Draws paths to the terminal nodes of a round
85 * @param x, y the point representation of the start of the branch
692ff642 86 * @param baseCallback a callback that needs the terminal x,y context
1ffe1db0 87 */
692ff642
JC
88const drawMatchup = (canvas, x, y, iter, maxIter, left, artists, baseCallback) => {
89 if (iter === maxIter) {
90 return baseCallback(x, y);
91 }
1ffe1db0 92 const ctx = canvas.getContext("2d");
0230aa11 93 ctx.direction = left === -1 ? "ltr" : "rtl";
1ffe1db0
JC
94 const [width, height] = getDimensions(canvas);
95
692ff642
JC
96 const drawBranchUp = (xDist, yDist) => drawBranchFrom(ctx, x, y, xDist, yDist, left, -1);
97 const drawBranchDown = (xDist, yDist) => drawBranchFrom(ctx, x, y, xDist, yDist, left, 1);
1ffe1db0 98 const drawArtist = (artistName, x, y) => drawArtistOnCtx(ctx, artistName, x, y);
692ff642
JC
99 const drawArtist1 = (x, y) => drawArtist(artists.shift()["name"], x, y);
100 const drawArtist2 = (x, y) => drawArtist(artists.pop()["name"], x, y);
98344134 101
98344134 102 ctx.beginPath();
1ffe1db0 103 ctx.moveTo(x, y);
692ff642
JC
104 const branchDistances = [width / (5 * (iter + 1)), height / (9 * (iter + 1))];
105 drawMatchup(
106 canvas,
107 ...drawBranchUp(...branchDistances),
108 iter + 1,
109 maxIter,
110 left,
111 artists,
112 drawArtist1,
1ffe1db0 113 );
692ff642 114
1ffe1db0 115 ctx.moveTo(x, y);
692ff642
JC
116 drawMatchup(
117 canvas,
118 ...drawBranchDown(...branchDistances),
119 iter + 1,
120 maxIter,
121 left,
122 artists,
123 drawArtist2,
1ffe1db0 124 );
98344134
JC
125}
126
a6490680
JC
127const drawBracket = (canvas, artists, genre) => {
128 const context = canvas.getContext("2d");
129 context.clearRect(0, 0, canvas.width, canvas.height);
1ffe1db0
JC
130 drawWinner(canvas);
131 const [mid_x, mid_y] = getCenter(canvas);
a6490680
JC
132 context.font = '28px sans serif'
133 context.strokeText(genre.toUpperCase(), mid_x - (28 * (genre.length / 3)), 40);
dce15b01 134 const [rect_width, rect_height] = getRectangleDimensions(canvas);
581e4442 135 const groups = 4;
692ff642 136 const rounds = Math.floor(Math.log2(artists.length / groups));
581e4442 137 for (let group = 1; group <= groups; group++) {
692ff642
JC
138 drawMatchup(
139 canvas,
59a851b3
JC
140 mid_x + (Math.pow(-1, group) * (rect_width / 6)),
141 mid_y + (Math.pow(-1, Math.floor(group / 2)) * (rect_height * 2.5)),
692ff642
JC
142 0,
143 rounds,
144 Math.pow(-1, group),
145 artists,
146 (x, y) => console.log("hello")
147 );
581e4442 148 }
1ffe1db0
JC
149}
150
98344134
JC
151window.onload = () => {
152 const lStorage = window.localStorage;
153 const genreList = document.getElementById("genre-list");
154 const genreInput = document.getElementById("genre-input");
155 const genreForm = document.getElementById("genre-form");
156 const canvas = document.getElementById("bracket");
157
01952c94 158 const formSubmitAction = () => {
1520458e 159 fetch(encodeURI(`http://api.brackets.jacobcasper.com/artist/genre?genre_name=${genreInput.value}`))
01952c94 160 .then((response) => response.json())
a6490680 161 .then((data) => drawBracket(canvas, data.slice(0, 33), genreInput.value));
01952c94
JC
162 }
163
98344134
JC
164 const createGenreList = (genreList, genres) => {
165 return createGenreListWithClickEvent(genreList, genres, (e) => {
166 genreInput.value = e.target.innerText;
01952c94 167 formSubmitPolyfill(genreForm, formSubmitAction);
98344134
JC
168 })
169 }
170 let genres = JSON.parse(lStorage.getItem("genres"))
171 if (genres === null) {
1520458e 172 fetch("http://api.brackets.jacobcasper.com/genre")
98344134
JC
173 .then((response) => response.text())
174 .then((text) => {
175 window.localStorage.setItem("genres", text);
176 genres = JSON.parse(text);
177 createGenreList(genreList, genres);
178 });
179 } else {
180 createGenreList(genreList, genres);
181 }
182
183 genreForm.addEventListener("submit", (e) => {
184 e.preventDefault();
01952c94 185 formSubmitAction()
98344134
JC
186 });
187
188 genreInput.addEventListener("input", (e) => {
189 const input = e.target;
6861bd5a 190 Array.from(genreList.children).forEach((item) => item.style.display= item.dataset.name.includes(input.value.toLowerCase()) ? "block" : "none");
98344134
JC
191 });
192 genreInput.addEventListener("focus", (e) => {
193 genreList.style.display = "block";
194 });
195 genreInput.addEventListener("blur", (e) => {
196 sleep(150).then(() => genreList.style.display = "none");
197 });
198
199
200 canvas.width = window.innerWidth;
201 canvas.height = window.innerHeight;
a6490680 202 drawBracket(canvas, ['dummy', 'dummy', 'dum' ,'dumhy'], "");
98344134
JC
203
204 const bgImg = new Image();
205 bgImg.onload = () => {
206 const ctx = canvas.getContext("2d");
207 //ctx.mozImageSmoothingEnabled = false;
208 //ctx.webkitImageSmoothingEnabled = false;
209 //ctx.msImageSmoothingEnabled = false;
210 //ctx.imageSmoothingEnabled = false;
211 ctx.drawImage(
212 bgImg,
213 0,
214 0,
215 (canvas.width / bgImg.width) * bgImg.width,
216 (canvas.height / bgImg.height) * bgImg.height
217 );
218 }
219
220 const upload = document.getElementById("image-upload");
221 upload.addEventListener("change", (e) => {
222 bgImg.src = URL.createObjectURL(e.target.files[0]);
223 });
224}