-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
101 lines (80 loc) · 2.31 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
const canvas = document.getElementById("canvas");
let ctx = canvas.getContext("2d");
const particleArray = [];
window.addEventListener("resize",function()
{
canvas.width=window.innerWidth
canvas.height=window.innerHeight
ctx=canvas.getContext("2d")
})
console.log(particleArray)
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const mouse={
x: undefined,
y: undefined
}
canvas.addEventListener("click", make);
function make(e){
mouse.x = e.x;
mouse.y = e.y;
for (let i=0; i<10; i++){
particleArray.push(new Particle())
}
}
class Particle{
constructor(){
this.x = mouse.x;
this.y = mouse.y;
// this.x = canvas.width/2;
// this.y = canvas.height/2;
this.size = Math.random()*40 +5;
this.speedx = Math.random()*80 - 40;
this.speedy = Math.random()*80 - 40;
this.color_list = ["red", "orange", "white", "blue", "yellow"];
this.rand_color = this.color_list[Math.floor(Math.random()*5)];
}
update(){
this.x += this.speedx;
this.y += this.speedy;
this.size = this.size*0.998;
// if (this.size < 1){
// particleArray.(particleArray[i])
// }
if ((this.x + this.speedx > canvas.width-this.size) ||
(this.x + this.speedx < this.size)){
this.speedx = -this.speedx;
}
if ((this.y + this.speedy > canvas.height-this.size) ||
(this.y + this.speedy < this.size)){
this.speedy = -this.speedy;
}
}
draw(){
ctx.beginPath();
ctx.fillStyle = this.rand_color;
ctx.arc(this.x, this.y, this.size, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
}
function handel(){
for (let i=0; i<(particleArray.length); i++){
if(!particleArray[i])
continue;
if (particleArray[i].size < 1){
// particleArray.remove(particleArray[i]);
delete particleArray[i]
}
else{
particleArray[i].draw();
particleArray[i].update();
}
}
}
function animate(){
ctx.clearRect(0, 0, canvas.width, canvas.height)
handel();
requestAnimationFrame(animate);
}
animate();