-
Notifications
You must be signed in to change notification settings - Fork 1
/
particles.js
89 lines (78 loc) · 1.99 KB
/
particles.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
const login = document.querySelector(".btn");
const container = document.querySelector(".container");
const canvas = document.getElementById('svgBlob');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let numParticles = 30;
let particles = [];
const colors = ["#1d1e22", "#7d8087", "#5f6988"];
const mouse = {
x: null
}
let user= {
login: false
}
class Particle {
constructor(){
this.x = Math.random() * canvas.width;
this.y = canvas.height + (Math.random() * 200);
this.radius = (Math.random() * 2) + 2;
this.speedX = (Math.random() * 2);
this.moveRight = this.x + this.speedX;
this.moveLeft = this.x - this.speedX;
this.speedY = Math.random() * 0.5;
this.color = colors[Math.floor(Math.random() * 3)];
}
draw(){
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
update(){
this.draw();
if(!user.login){
this.y -= this.speedY;
}else {
this.y -= 10;
}
if(this.y <= canvas.height){
if(mouse.x > canvas.width/2){
this.x = this.moveRight;
}else {
this.x = this.moveLeft;
}
}
}
}
function setup(){
for (let i = 0; i < numParticles; i++){
particles.push(new Particle());
}
}
window.addEventListener('mousemove', (e) => {
mouse.x = e.x;
})
function animate(){
requestAnimationFrame(animate);
ctx.clearRect(0, 0, canvas.width, canvas.height);
particles.forEach((particle, index) => {
particle.update();
if(particle.y + particle.radius < 0){
setTimeout(() => {
particles.splice(index, 1);
},0)
if(!user.login){
particles.push(new Particle());
}
}
})
}
setup();
animate();
window.addEventListener('resize', function(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
})