-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
204 lines (157 loc) · 5.76 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext("2d");
var flowers_img = []
const FLOWER_SIZE = 32;
const FLOWER_COUNT = 12;
const API_URL = 'https://sood1cr8tc.execute-api.us-east-2.amazonaws.com/default/';
var flowers = [];
const FLOWER_BLOCK_TIME = 1000 * 60 * 1; // 1 minute
class Flower {
constructor(x, y, style, orientation = 1) {
this.x = x;
this.y = y;
this.style = style;
this.orientation = orientation;
}
}
function getFlowersFromDB() {
const http = new XMLHttpRequest();
const url = API_URL + 'flowers-get';
http.open("GET", url);
http.send();
http.onreadystatechange = (e) => {
console.log(http.responseText);
const flowers_json = JSON.parse(http.responseText);
flowers = flowers_json.map(f => new Flower(f.x, f.y, f.style, 1));
drawFlowers();
unblockCanvas();
}
// make a get from db
var testFlower = new Flower(40, 40, 2, 1);
return [testFlower];
}
function drawFlower(flower) {
ctx.drawImage(flowers_img[flower.style], flower.x - FLOWER_SIZE / 2, flower.y - FLOWER_SIZE, FLOWER_SIZE , FLOWER_SIZE);
}
function addFlower(x, y, flowers) {
var style = Math.floor(Math.random() * 11 + 1);
var orientation = Math.floor(Math.random() * 2 - 1);
var flower = new Flower(x, y, style, orientation);
saveFlowerInDB(flower);
// momentary thing
flowers.push(flower)
}
function saveFlowerInDB(flower) {
const http = new XMLHttpRequest();
const url = API_URL + 'flowers-post';
http.open("POST", url);
http.send(JSON.stringify(flower));
http.onreadystatechange = (e) => {
console.log(http.responseText);
const flowers_json = JSON.parse(http.responseText);
flowers = flowers_json.map(f => new Flower(f.x, f.y, f.style, 1));
drawFlowers();
}
// save into db
// update local flowers
return true;
}
function drawFlowers(){
// draw flowers
flowers.sort((a, b) => a.y - b.y);
for (let i = 0; i < flowers.length; i++) {
drawFlower(flowers[i]);
}
}
// from https://stackoverflow.com/questions/55677/how-do-i-get-the-coordinates-of-a-mouse-click-on-a-canvas-element
function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect();
const x = Math.floor(event.clientX - rect.left);
const y = Math.floor(event.clientY - rect.top);
return {x: x, y: y};
}
function mouseEventListener(e) {
const pos = getCursorPosition(canvas, e);
addFlower(pos.x, pos.y, flowers);
drawFlowers();
blockFlowers(FLOWER_BLOCK_TIME);
}
function blockFlowers(time) {
// block flowers for 5 minutes
canvas.removeEventListener('mousedown', mouseEventListener);
canvas.style.cursor = 'not-allowed';
const message = document.getElementById('message');
message.innerText = 'Wait...';
const id = progresBars(time);
setTimeout(() => {
canvas.addEventListener('mousedown', mouseEventListener);
canvas.style.cursor = 'crosshair';
message.innerText = 'Click the garden!';
progess_top.style.width = '0%';
progess_right.style.height = '0%';
progess_bottom.style.width = '0%';
progess_left.style.height = '0%';
clearInterval(id);
}, time);
}
function blockCanvas(){
canvas.removeEventListener('mousedown', mouseEventListener);
canvas.style.cursor = 'wait';
const message = document.getElementById('message');
message.innerText = 'Wait...';
}
function unblockCanvas(){
canvas.addEventListener('mousedown', mouseEventListener);
canvas.style.cursor = 'crosshair';
const message = document.getElementById('message');
message.innerText = 'Click the garden!';
}
function progresBars(time) {
const progess_top = document.getElementById('progress-top');
const progess_right = document.getElementById('progress-right');
const progess_bottom = document.getElementById('progress-bottom');
const progess_left = document.getElementById('progress-left');
progess_top.style.width = '0%';
progess_right.style.height = '100%';
progess_bottom.style.width = '100%';
progess_left.style.height = '100%';
time = time / 10;
var progress = 0;
var id = setInterval(frame, 10);
function frame() {
if (progress >= time) {
clearInterval(id);
} else {
progress++;
}
progess_top.style.width = `${Math.max(0,Math.min(100, ( 4*time/4 - progress ) / (time/4) * 100))}%`;
progess_right.style.height = `${Math.max(0,Math.min(100, ( 3*time/4 - progress ) / (time/4) * 100))}%`;
progess_bottom.style.width = `${Math.max(0,Math.min(100, ( 2*time/4 - progress ) / (time/4) * 100))}%`;
progess_left.style.height = `${Math.max(0,Math.min(100, ( 1*time/4 - progress ) / (time/4) * 100))}%`;
progess_bottom.style.left = `${Math.min(100,Math.max(0, ( progress - 1*time/4 ) / (time/4) * 100))}%`;
progess_left.style.top = `${Math.min(100,Math.max(0, ( progress - 0*time/4 ) / (time/4) * 100))}%`;
}
return id;
}
// calculate pos of flowers
blockCanvas()
getFlowersFromDB();
// first load after image load
flowers_loaded = Array(FLOWER_COUNT).fill(false);
for (let i = 0; i < FLOWER_COUNT; i++) {
flowers_img[i] = new Image();
flowers_img[i].src = `sprites/flowers/flower-Slice ${i + 1}.svg`;
flowers_img[i].onload = () => {
flowers_loaded[i] = true;
if (flowers_loaded.every(e => e)) drawFlowers();
}
}
// addEventListener("keydown", (event) => {
// console.log(event);
// console.log(event.key);
// if (event.isComposing || event.key === 'l') {
// progresBars(FLOWER_BLOCK_TIME);
// return;
// }
// // do something
// });