-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
97 lines (81 loc) · 2 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const URL = 'https://banana-get.herokuapp.com/';
const getButton = document.getElementById('get-button');
const loading = document.getElementById('loading');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.strokeStyle = '#42331c';
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.lineWidth = 6;
let bananas = [];
// Draw a line from a set of coordinates
function drawPath(stroke) {
const [x, y] = stroke;
ctx.beginPath();
ctx.moveTo(x[0], y[0]);
for (let i = 1; i < x.length; i++) {
ctx.lineTo(x[i], y[i]);
}
ctx.stroke();
}
// Draw all lines of a drawing
function drawBanana(drawing) {
for (let stroke of drawing) {
drawPath(stroke);
}
}
// Clear canvas
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
// Get 50 drawings from API
async function getBananas() {
const resp = await axios.get(URL);
return resp.data.drawings;
}
// Display load while getting bananas
async function loadBananas() {
try {
startLoad();
bananas = await getBananas();
endLoad();
} catch (e) {
displayError();
}
}
// Get banana button
async function handleClick(e) {
clearCanvas();
const drawing = bananas.pop();
drawBanana(drawing);
if (bananas.length <= 0) {
await loadBananas();
}
}
// Display loading message and hide canvas
function startLoad() {
loading.style.display = 'block';
canvas.style.display = 'none';
getButton.disabled = true;
}
// Hide loading message and display canvas
function endLoad() {
loading.style.display = 'none';
canvas.style.display = 'block';
getButton.disabled = false;
}
// Display error message on server error
function displayError() {
const error = document.getElementById('error');
error.style.display = 'block';
loading.style.display = 'none';
canvas.style.display = 'none';
getButton.disabled = true;
}
async function setUp() {
await loadBananas();
}
window.addEventListener('load', () => {
setUp();
getButton.addEventListener('click', handleClick)
});