1 const sleep
= (ms
) => new Promise(resolve
=> setTimeout(resolve
, ms
));
3 const createGenreListWithClickEvent
= (genreList
, genres
, callback
) => {
4 genreList
.append(...genres
.map((genre
) => {
5 const name
= genre
["name"];
6 const li
= document
.createElement("li");
7 li
.setAttribute("data-name", name
);
8 li
.addEventListener("click", callback
);
9 li
.appendChild(document
.createTextNode(name
));
14 const relativeToCtx
= (x1
, x2
, y1
, y2
, ctx
) => {
17 ctx
.lineTo(newX
, newY
);
22 * Return (x,y) of canvas
24 const getDimensions
= (canvas
) => [canvas
.width
, canvas
.height
];
27 * Return (x,y) midpoint of canvas
29 const getCenter
= (canvas
) => getDimensions(canvas
).map((dim
) => dim
/ 2);
32 * Draw champion box and clear background.
34 const drawWinner
= (canvas
) => {
35 const ctx
= canvas
.getContext("2d");
36 const [width
, height
] = getDimensions(canvas
);
37 const [mid_x
, mid_y
] = getCenter(canvas
);
38 const rect_width
= width
* .5;
39 const rect_height
= height
* .1;
40 ctx
.strokeRect(mid_x
- rect_width
/ 2, mid_y
- rect_height
/ 2, rect_width
, rect_height
);
41 ctx
.clearRect(mid_x
- rect_width
/ 2, mid_y
- rect_height
/ 2, rect_width
, rect_height
);
45 * Draws a path in context from the current location to a point xDist * left, yDist * up away from it.
46 * @param ctx RenderingContext
47 * @param x, y float current location
48 * @param x, y float distance away
49 * @param up, left (-1|1) directions
50 * @return [newNodeX, newNodeY]
52 const drawBranchFrom
= (ctx
, x
, y
, xDist
, yDist
, left
, up
) => {
53 const newX
= x
+ (xDist
* left
)
54 const newY
= y
+ (yDist
* up
)
56 ctx
.lineTo(newX
, newY
);
60 const drawArtistOnCtx
= (ctx
, artistName
, x
, y
) => ctx
.strokeText(artistName
, x
, y
);
63 * Draws paths to the terminal nodes of a round
64 * @param x, y the point representation of the start of the branch
66 const drawMatchup
= (canvas
, x
, y
, iter
, left
) => {
67 const ctx
= canvas
.getContext("2d");
68 const [width
, height
] = getDimensions(canvas
);
70 const drawBranchUp
= (xDist
, yDist
) => drawBranchFrom(ctx
, x
, y
, xDist
, yDist
, left
, 1);
71 const drawBranchDown
= (xDist
, yDist
) => drawBranchFrom(ctx
, x
, y
, xDist
, yDist
, left
, -1);
72 const drawArtist
= (artistName
, x
, y
) => drawArtistOnCtx(ctx
, artistName
, x
, y
);
78 ...drawBranchUp(width
/ iter
, height
/ iter
)
83 ...drawBranchDown(width
/ iter
, height
/ iter
)
88 const drawBracket
= (canvas
, ...artists
) => {
90 const [mid_x
, mid_y
] = getCenter(canvas
);
91 drawMatchup(canvas
, mid_x
, mid_y
, 2 * 3, -1);
94 window
.onload
= () => {
95 const lStorage
= window
.localStorage
;
96 const genreList
= document
.getElementById("genre-list");
97 const genreInput
= document
.getElementById("genre-input");
98 const genreForm
= document
.getElementById("genre-form");
99 const canvas
= document
.getElementById("bracket");
101 const createGenreList
= (genreList
, genres
) => {
102 return createGenreListWithClickEvent(genreList
, genres
, (e
) => {
103 genreInput
.value
= e
.target
.innerText
;
104 genreForm
.requestSubmit();
107 let genres
= JSON
.parse(lStorage
.getItem("genres"))
108 if (genres
=== null) {
109 fetch("http://localhost:8080/genre")
110 .then((response
) => response
.text())
112 window
.localStorage
.setItem("genres", text
);
113 genres
= JSON
.parse(text
);
114 createGenreList(genreList
, genres
);
117 createGenreList(genreList
, genres
);
120 genreForm
.addEventListener("submit", (e
) => {
122 fetch(encodeURI(`http://localhost:8080/artist/genre?genre_name=${genreInput.value}`))
123 .then((response
) => response
.json())
124 .then((data
) => console
.log(data
));
125 drawBracket(canvas
, ...[])
128 genreInput
.addEventListener("input", (e
) => {
129 const input
= e
.target
;
130 for (const item
of genreList
.children
) {
131 item
.style
.display
= item
.dataset
.name
.includes(input
.value
.toLowerCase()) ? "block" : "none";
134 genreInput
.addEventListener("focus", (e
) => {
135 genreList
.style
.display
= "block";
137 genreInput
.addEventListener("blur", (e
) => {
138 sleep(150).then(() => genreList
.style
.display
= "none");
142 canvas
.width
= window
.innerWidth
;
143 canvas
.height
= window
.innerHeight
;
146 const bgImg
= new Image();
147 bgImg
.onload
= () => {
148 const ctx
= canvas
.getContext("2d");
149 //ctx.mozImageSmoothingEnabled = false;
150 //ctx.webkitImageSmoothingEnabled = false;
151 //ctx.msImageSmoothingEnabled = false;
152 //ctx.imageSmoothingEnabled = false;
157 (canvas
.width
/ bgImg
.width
) * bgImg
.width
,
158 (canvas
.height
/ bgImg
.height
) * bgImg
.height
162 const upload
= document
.getElementById("image-upload");
163 upload
.addEventListener("change", (e
) => {
164 bgImg
.src
= URL
.createObjectURL(e
.target
.files
[0]);