-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
89 lines (70 loc) · 1.98 KB
/
main.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
let anim1, anim2, canvas, ctx;
function stop() {
clearInterval(anim1);
clearInterval(anim2);
ctx.clearRect(0, 0, canvas.clientWidth, canvas.clientWidth);
}
function play() {
requestAnimationFrame(animate);
}
function animate() {
let score = document.getElementById('score');
let count = 1;
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
function Square (x, y, w, h) {
this.x = Math.random() * canvas.width;
this.y = y;
this.w = w;
this.h = h;
this.rgba = "#" + Math.round(Math.random() * 9) + Math.round(Math.random() * 9) + Math.round(Math.random() * 9);
this.draw = function() {
ctx.fillStyle = this.rgba;
ctx.fillRect(this.x, this.y, 20, 20);
this.update();
}
this.update = function() {
this.y += Math.random() * 3;
}
}
let squares = [];
function draw() {
ctx.clearRect(0, 0, canvas.clientWidth, canvas.clientWidth);
for(i = 0; i < squares.length; i++)
squares[i].draw();
update();
}
function update() {
for (var i = 0; i < squares.length; i++) {
squares[i].update();
}
}
anim1 = setInterval(function(){
squares.push(new Square(0, 0, 20, 20))
},1000);
anim2 = setInterval(draw, 20);
var isCursorInSquares = function(x, y, squares) {
return x > squares.x && x < squares.x + squares.w + 8 &&
y > squares.y && y < squares.y + squares.h + 20;
}
canvas.onclick = function(e) {
var x = e.pageX;
y = e.pageY;
for(var i = squares.length - 1; i >= 0; --i){
if(isCursorInSquares(x, y, squares[i])) {
delete squares.splice(i, 1);
score.innerHTML = count++;
}
}
}
let clearSquares = (function() {
score.innerHTML = 0;
count = 0;
})();
}
document.body.onload = () => {
let startBtn = document.getElementById('start');
let stopBtn = document.getElementById('stop');
startBtn.addEventListener('click', play);
stopBtn.addEventListener('click', stop);
}