Commit | Line | Data |
---|---|---|
98344134 JC |
1 | const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); |
2 | ||
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)); | |
10 | return li; | |
11 | })); | |
12 | } | |
13 | ||
14 | const relativeToCtx = (x1, x2, y1, y2, ctx) => { | |
15 | const newX = x1 + x2; | |
16 | const newY = y1 + y2; | |
17 | ctx.lineTo(newX, newY); | |
18 | return [newX, newY]; | |
19 | } | |
20 | ||
1ffe1db0 JC |
21 | /** |
22 | * Return (x,y) of canvas | |
23 | */ | |
24 | const getDimensions = (canvas) => [canvas.width, canvas.height]; | |
25 | ||
26 | /** | |
27 | * Return (x,y) midpoint of canvas | |
28 | */ | |
29 | const getCenter = (canvas) => getDimensions(canvas).map((dim) => dim / 2); | |
30 | ||
31 | /** | |
32 | * Draw champion box and clear background. | |
33 | */ | |
34 | const drawWinner = (canvas) => { | |
98344134 | 35 | const ctx = canvas.getContext("2d"); |
1ffe1db0 JC |
36 | const [width, height] = getDimensions(canvas); |
37 | const [mid_x, mid_y] = getCenter(canvas); | |
98344134 JC |
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); | |
1ffe1db0 JC |
42 | } |
43 | ||
44 | /** | |
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] | |
51 | */ | |
52 | const drawBranchFrom = (ctx, x, y, xDist, yDist, left, up) => { | |
53 | const newX = x + (xDist * left) | |
54 | const newY = y + (yDist * up) | |
55 | ctx.lineTo(x, newY); | |
56 | ctx.lineTo(newX, newY); | |
57 | return [newX, newY]; | |
58 | } | |
59 | ||
60 | const drawArtistOnCtx = (ctx, artistName, x, y) => ctx.strokeText(artistName, x, y); | |
61 | ||
62 | /** | |
63 | * Draws paths to the terminal nodes of a round | |
64 | * @param x, y the point representation of the start of the branch | |
65 | */ | |
66 | const drawMatchup = (canvas, x, y, iter, left) => { | |
67 | const ctx = canvas.getContext("2d"); | |
68 | const [width, height] = getDimensions(canvas); | |
69 | ||
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); | |
98344134 | 73 | |
98344134 | 74 | ctx.beginPath(); |
1ffe1db0 JC |
75 | ctx.moveTo(x, y); |
76 | drawArtist( | |
77 | 'test', | |
78 | ...drawBranchUp(width / iter, height / iter) | |
79 | ); | |
80 | ctx.moveTo(x, y); | |
81 | drawArtist( | |
82 | 'test2', | |
83 | ...drawBranchDown(width / iter, height / iter) | |
84 | ); | |
98344134 JC |
85 | ctx.stroke(); |
86 | } | |
87 | ||
1ffe1db0 JC |
88 | const drawBracket = (canvas, ...artists) => { |
89 | drawWinner(canvas); | |
90 | const [mid_x, mid_y] = getCenter(canvas); | |
91 | drawMatchup(canvas, mid_x, mid_y, 2 * 3, -1); | |
92 | } | |
93 | ||
98344134 JC |
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"); | |
100 | ||
101 | const createGenreList = (genreList, genres) => { | |
102 | return createGenreListWithClickEvent(genreList, genres, (e) => { | |
103 | genreInput.value = e.target.innerText; | |
104 | genreForm.requestSubmit(); | |
105 | }) | |
106 | } | |
107 | let genres = JSON.parse(lStorage.getItem("genres")) | |
108 | if (genres === null) { | |
109 | fetch("http://localhost:8080/genre") | |
110 | .then((response) => response.text()) | |
111 | .then((text) => { | |
112 | window.localStorage.setItem("genres", text); | |
113 | genres = JSON.parse(text); | |
114 | createGenreList(genreList, genres); | |
115 | }); | |
116 | } else { | |
117 | createGenreList(genreList, genres); | |
118 | } | |
119 | ||
120 | genreForm.addEventListener("submit", (e) => { | |
121 | e.preventDefault(); | |
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, ...[]) | |
126 | }); | |
127 | ||
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"; | |
132 | } | |
133 | }); | |
134 | genreInput.addEventListener("focus", (e) => { | |
135 | genreList.style.display = "block"; | |
136 | }); | |
137 | genreInput.addEventListener("blur", (e) => { | |
138 | sleep(150).then(() => genreList.style.display = "none"); | |
139 | }); | |
140 | ||
141 | ||
142 | canvas.width = window.innerWidth; | |
143 | canvas.height = window.innerHeight; | |
144 | drawBracket(canvas); | |
145 | ||
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; | |
153 | ctx.drawImage( | |
154 | bgImg, | |
155 | 0, | |
156 | 0, | |
157 | (canvas.width / bgImg.width) * bgImg.width, | |
158 | (canvas.height / bgImg.height) * bgImg.height | |
159 | ); | |
160 | } | |
161 | ||
162 | const upload = document.getElementById("image-upload"); | |
163 | upload.addEventListener("change", (e) => { | |
164 | bgImg.src = URL.createObjectURL(e.target.files[0]); | |
165 | }); | |
166 | } |