-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
128 lines (112 loc) · 3.08 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
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
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
let particleArray;
let mouseDraw = false;
let clickedX;
let clickedY;
let paintedBall = 0;
//click to destroy INPUT
canvas.addEventListener('mousedown',
function(e){
clickedX = e.clientX;
clickedY = e.clientY;
mouseDraw = true;
console.log("CLICKED " + e.clientX + " " + e.clientY);
})
canvas.addEventListener('mouseup', function(){
mouseDraw = false;
clickedX = 0;
clickedY = 0;
console.log("zeroed");
})
canvas.addEventListener('touchstart',
function(e){
clickedX = e.clientX;
clickedY = e.clientY;
mouseDraw = true;
console.log("CLICKED " + e.clientX + " " + e.clientY);
})
canvas.addEventListener('touchend', function(){
mouseDraw = false;
clickedX = 0;
clickedY = 0;
console.log("zeroed");
})
// Particle Constructor function
function Particle(x, y, directionX, directionY, size, color){
this.x = x;
this.y = y;
this.directionX = directionX;
this.directionY = directionY;
this.size = size;
this.color = color;
this.isDead = false;
}
//Add draw method to particle prototype
Particle.prototype.draw = function(){
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
}
//add update method to particle prototype
Particle.prototype.update = function() {
if(this.isDead){return;}
if(this.x + this.size > canvas.width || this.x - this.size < 0){
this.directionX = -this.directionY;
}
if(this.y + this.size > canvas.height || this.y - this.size <0){
this.directionY = -this.directionY;
}
this.x += this.directionX;
this.y += this.directionY;
this.draw();
// console.log("update called");
}
//create particle particleArray
function init(){
particleArray = [];
for (var i = 0; i < 100; i++) {
let size = Math.random() * 50;
let x = Math.random() * (innerWidth - size * 2);
let y = Math.random() * (innerHeight - size * 2) ;
let directionX = (Math.random() * .4 ) - .2;
let directionY = (Math.random() * .4 ) - .2;
let color = 'white';
particleArray.push(new Particle(x, y, directionX, directionY, size, color));
}
}
//Animation loop
function animate(){
requestAnimationFrame(animate);
ctx.clearRect(0,0,innerWidth, innerHeight);
paintedBall = 0;
for (let i=0; i<particleArray.length; i++){
if(mouseDraw){
if((clickedX + 50 >= particleArray[i].x) &&
(clickedX - 50 <= particleArray[i].x) &&
(clickedY + 50 >= particleArray[i].y) &&
(clickedY - 50 <= particleArray[i].y)) {
particleArray[i].color = 'red';
}
}
if(particleArray[i].color == 'red') {paintedBall += 1};
particleArray[i].update();
}
ctx.font = "100px Verdana";
ctx.fillStyle = 'blue';
ctx.fillText(paintedBall, 100, 100);
}
//Global Function Call
init();
animate();
//Window resize fix
window.addEventListener('resize',
function(){
canvas.width = innerWidth;
canvas.height = innerHeight;
// mouseClick = 0;
init();
})