-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrectAngular.js
More file actions
304 lines (280 loc) · 9.16 KB
/
rectAngular.js
File metadata and controls
304 lines (280 loc) · 9.16 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
var rectAngular = function (elem, width, height, heading, colour, font) {
var rect = this;
rect.area = document.getElementById(elem);
rect.ctxt = rect.area.getContext("2d");
rect.draw = {
"x1" : null,
"y1" : null,
"x2" : null,
"y2" : null,
"tx" : null,
"ty" : null,
"left" : 10000,
"score" : 0,
"start" : null
};
rect.ball = {
"x" : 20,
"y" : 20,
"w" : 20,
"mx" : true,
"my" : true,
"speed" : 1,
"step" : 1,
"count" : 0
};
rect.set = {
draw : null,
ball : null
};
rect.init = function () {
/* Save Base Rect Settings */
if (!rect.set.draw) {
rect.set.draw = JSON.stringify(rect.draw);
}
/* Save Base Ball Settings */
if (!rect.set.ball) {
rect.set.ball = JSON.stringify(rect.ball);
}
rect.area.width = width ? width : window.innerWidth;
rect.area.height = height ? height : window.innerHeight;
rect.ctxt.fillStyle = colour ? colour : "DodgerBlue";
rect.ctxt.strokeStyle = colour ? colour : "DodgerBlue";
rect.ctxt.font = font ? font : "30px Verdana";
rect.screen();
};
rect.screen = function () {
rect.area.style.cursor = "pointer";
/* Clear Canvas */
rect.ctxt.clearRect(0, 0, rect.area.width, rect.area.height);
/* Get Dimensions */
var offsetX = rect.area.width / 20;
var offsetY = rect.area.height / 10;
var row = rect.area.height / 20;
var middle = rect.area.width / 2;
/* Write Title */
rect.ctxt.fillText((heading ? heading : "rectAngular"), middle - 100, 80);
/* Write Instructions */
rect.ctxt.fillText("Tap screen to start...", middle - 160, row * 4);
/* Write High Score If Any */
var highscore = rect.getHighScore();
for (let i = 0; i < highscore.length; i++) {
let d = new Date(highscore[i].date);
let thisDate = d.getDate() + "/" + (1 + d.getMonth()) + " " + (d.getYear() + 1900);
rect.ctxt.fillText(i + 1 + "- " + highscore[i].score + " - " + highscore[i].name + " - " + thisDate, middle - 300, row * (6 + i *2));
}
};
rect.start = function () {
/* Set Start Date */
if (!rect.draw.start) {
rect.draw.start = new Date();
}
/* Initialise Game Cyclus */
rect.anima = setInterval(rect.paint, 1);
rect.area.style.cursor = "none";
};
rect.stop = function () {
/* Stop Game Cyclus */
clearInterval(rect.anima);
/* Handle Highscores */
var highscore = rect.getHighScore();
if (highscore.length < 5 || rect.draw.score > highscore[4].score) {
var name = window.prompt("Enter Name for High Score");
highscore.push({"name" : name, "score" : rect.draw.score, "date" : rect.draw.start });
highscore = highscore.sort(function (x, y) {
return y.score - x.score;
});
while (highscore.length > 5) {
highscore.pop();
}
localStorage["RectScore"] = JSON.stringify(highscore);
}
/* Reset Game Objects */
rect.draw = JSON.parse(rect.set.draw);
rect.ball = JSON.parse(rect.set.ball);
/* Draw Start Screen */
rect.screen();
};
rect.getHighScore = function () {
var highscore = new Array();
var high = localStorage["RectScore"];
if (high) {
highscore = JSON.parse(high);
}
return highscore;
};
rect.area.onclick = function (e) {
/* Check if game has begun */
if (rect.draw.start !== null) {
/* Check if there is line left */
if (rect.draw.left > 0) {
let x = e.pageX - (rect.area.getBoundingClientRect().left + window.scrollX);
let y = e.pageY - (rect.area.getBoundingClientRect().top + window.scrollY);
/* Step One: Set x1 and y1 */
if ((!rect.draw.x1 && !rect.draw.y1 && !rect.draw.x1 && !rect.draw.y1) || (rect.draw.x1 && rect.draw.y1 && rect.draw.x2 && rect.draw.y2)) {
rect.draw.x1 = x;
rect.draw.y1 = y;
rect.draw.x2 = null;
rect.draw.y2 = null;
}
/* Step Two: Set x2 and y2 and draw rectangle */
else if (rect.draw.x1 && rect.draw.y1 && !rect.draw.x2 && !rect.draw.y2) {
rect.draw.x2 = x;
rect.draw.y2 = y;
/* Modify Line Left */
let x_1 = rect.draw.x1 > rect.draw.x2 ? rect.draw.x2 : rect.draw.x1;
let x_2 = rect.draw.x1 > rect.draw.x2 ? rect.draw.x1 : rect.draw.x2;
let y_1 = rect.draw.y1 > rect.draw.y2 ? rect.draw.y2 : rect.draw.y1;
let y_2 = rect.draw.y1 > rect.draw.y2 ? rect.draw.y1 : rect.draw.y2;
let x_L = x_2 - x_1;
let y_L = y_2 - y_1;
rect.draw.left -= (x_L + y_L);
}
}
}
else {
rect.start();
}
};
rect.area.onmousemove = function (e) {
/* Set Temporary Values */
rect.draw.tx = e.pageX - (rect.area.getBoundingClientRect().left + window.scrollX);
rect.draw.ty = e.pageY - (rect.area.getBoundingClientRect().top + window.scrollY);
};
rect.paint = function () {
/* Clear Canvas */
rect.ctxt.clearRect(0, 0, rect.area.width, rect.area.height);
/* Update Score */
rect.draw.score = rect.draw.start ? Math.abs((new Date().getTime() - rect.draw.start.getTime())) : 0;
/* Display Line Left */
rect.ctxt.fillText("Line: " + (rect.draw.left > 0 ? rect.draw.left : 0), rect.area.width - 190, 35);
/* Display Score */
rect.ctxt.fillText("Score: " + rect.draw.score, 10, 35);
/* Draw Rect (Mouse) */
if (rect.draw.tx && rect.draw.ty) {
if (!rect.draw.x1 && !rect.draw.y1 && !rect.draw.x1 && !rect.draw.y1) {
rect.ctxt.fillRect(rect.draw.tx, rect.draw.ty, 5, 5);
}
else if (rect.draw.x1 && rect.draw.y1 && !rect.draw.x2 && !rect.draw.y2) {
rect.ctxt.strokeRect(rect.draw.x1, rect.draw.y1, -(rect.draw.x1 - rect.draw.tx), -(rect.draw.y1 - rect.draw.ty));
}
else if (rect.draw.x1 && rect.draw.y1 && rect.draw.x2 && rect.draw.y2) {
rect.ctxt.fillRect(rect.draw.tx, rect.draw.ty, 5, 5);
rect.ctxt.fillRect(rect.draw.x1, rect.draw.y1, -(rect.draw.x1 - rect.draw.x2), -(rect.draw.y1 - rect.draw.y2));
}
}
/* Draw Rect (Touch) */
else {
if (rect.draw.x1 && rect.draw.y1 && !rect.draw.x2 && !rect.draw.y2) {
rect.ctxt.fillRect(rect.draw.x1, rect.draw.y1, 5, 5);
}
else if (rect.draw.x1 && rect.draw.y1 && rect.draw.x2 && rect.draw.y2) {
rect.ctxt.fillRect(rect.draw.x1, rect.draw.y1, -(rect.draw.x1 - rect.draw.x2), -(rect.draw.y1 - rect.draw.y2));
}
}
/* Draw Ball */
if (rect.ball.count % rect.ball.speed === 0) {
/* Direction Up-Left */
if (!rect.ball.mx && !rect.ball.my) {
rect.ball.x -= rect.ball.step;
rect.ball.y -= rect.ball.step;
}
/* Direction Up-Right */
else if (rect.ball.mx && !rect.ball.my) {
rect.ball.x += rect.ball.step;
rect.ball.y -= rect.ball.step;
}
/* Direction Down-Left */
else if (!rect.ball.mx && rect.ball.my) {
rect.ball.x -= rect.ball.step;
rect.ball.y += rect.ball.step;
}
/* Direction Down-Right */
else {
rect.ball.x += rect.ball.step;
rect.ball.y += rect.ball.step;
}
}
rect.ctxt.beginPath();
rect.ctxt.arc(rect.ball.x, rect.ball.y, rect.ball.w, 0, 2 * Math.PI);
rect.ctxt.closePath();
rect.ctxt.fill();
/* Check for Collision */
if (rect.draw.x1 && rect.draw.y1 && rect.draw.x2 && rect.draw.y2) {
let x_1 = rect.draw.x1 > rect.draw.x2 ? rect.draw.x2 : rect.draw.x1;
let x_2 = rect.draw.x1 > rect.draw.x2 ? rect.draw.x1 : rect.draw.x2;
let y_1 = rect.draw.y1 > rect.draw.y2 ? rect.draw.y2 : rect.draw.y1;
let y_2 = rect.draw.y1 > rect.draw.y2 ? rect.draw.y1 : rect.draw.y2;
let mx_1 = x_1 - rect.ball.w;
let mx_2 = x_2 - rect.ball.w;
let my_1 = y_1 - rect.ball.w;
let my_2 = y_2 - rect.ball.w;
let nx_1 = x_1 + rect.ball.w;
let nx_2 = x_2 + rect.ball.w;
let ny_1 = y_1 + rect.ball.w;
let ny_2 = y_2 + rect.ball.w;
/* Direction Up-Left */
if (!rect.ball.mx && !rect.ball.my) {
if (nx_1 <= rect.ball.x && nx_2 >= rect.ball.x && ny_1 <= rect.ball.y && ny_2 >= rect.ball.y) {
let fromX = rect.ball.x - nx_2;
let fromY = rect.ball.y - ny_2;
if (fromX < fromY) {
rect.ball.my = true;
}
else {
rect.ball.mx = true;
}
}
}
/* Direction Up-Right */
else if (rect.ball.mx && !rect.ball.my) {
if (mx_1 <= rect.ball.x && mx_2 >= rect.ball.x && ny_1 <= rect.ball.y && ny_2 >= rect.ball.y) {
let fromX = rect.ball.x - mx_1;
let fromY = rect.ball.y - my_1;
if (fromX < fromY) {
rect.ball.my = false;
rect.ball.mx = false;
}
else {
rect.ball.my = true;
rect.ball.mx = true;
}
}
}
/* Direction Down-Left */
else if (!rect.ball.mx && rect.ball.my) {
if (nx_1 <= rect.ball.x && nx_2 >= rect.ball.x && my_1 <= rect.ball.y && my_2 >= rect.ball.y) {
let fromX = rect.ball.x - mx_1;
let fromY = rect.ball.y - my_1;
if (fromX > fromY) {
rect.ball.my = false;
rect.ball.mx = false;
}
else {
rect.ball.my = true;
rect.ball.mx = true;
}
}
}
/* Direction Down-Right */
else if (rect.ball.mx && rect.ball.my) {
if (mx_1 <= rect.ball.x && mx_2 >= rect.ball.x && my_1 <= rect.ball.y && my_2 >= rect.ball.y) {
let fromX = rect.ball.x - mx_1;
let fromY = rect.ball.y - my_1;
if (fromX > fromY) {
rect.ball.my = false;
}
else {
rect.ball.mx = false;
}
}
}
}
/* Check for Ball Overflow */
if (rect.ball.x < 0 || rect.ball.y < 0 || rect.ball.x > rect.area.width || rect.ball.y > rect.area.height) {
rect.stop();
}
rect.ball.count++;
};
rect.init();
};