-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
84 lines (80 loc) · 2.09 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
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let particleArray = [];
const numberOfParticles = 700;
let title1 = document.getElementById('title1');
let titleMeasure = title1.getBoundingClientRect();
let title = {
x:titleMeasure.left,
y:titleMeasure.top,
width:titleMeasure.width,
height:10
}
class Particle {
constructor(x,y){
this.x = x;
this.y = y;
this.size = 3;
this.weight = 2;
this.directionX = -2;
}
update(){
if(this.y >canvas.height){
this.size = Math.random()*3;
this.y = 0-this.size;
this.x = Math.random()*canvas.width+120;
this.weight = 2;
}
this.weight += 0.05;
this.y += this.weight;
this.x += this.directionX;
// check for collision between particle and title
if( this.x < title.x + title.width &&
this.x + this.size > title.x &&
this.y < title.y + title.height &&
this.y + this.size > title.y ){
this.y -= 3;
this.weight *= -0.2;
}
}
draw(){
ctx.fillStyle = 'rgba(150,150,150,0.1)';
ctx.beginPath();
ctx.arc(this.x,this.y,this.size,0,Math.PI*2);
ctx.closePath();
ctx.fill();
}
}
function init(){
particleArray = [];
for(let i=0; i<numberOfParticles; i++){
const x = Math.random()*canvas.width;
const y = Math.random()*canvas.height;
particleArray.push(new Particle(x,y));
}
}
init();
function animate(){
ctx.fillStyle = 'rgba(255,255,255,0.07)';
ctx.fillRect(0,0,canvas.width,canvas.height);
for(let i=0; i<particleArray.length; i++){
particleArray[i].update();
particleArray[i].draw();
}
requestAnimationFrame(animate);
}
animate();
window.addEventListener('resize', ()=>{
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
titleMeasure = title1.getBoundingClientRect();
title = {
x: titleMeasure.left,
y: titleMeasure.top,
width: titleMeasure.width,
height: 10
}
init();
})