-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.js
57 lines (47 loc) · 1.38 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
window.addEventListener('DOMContentLoaded',(e)=>{
var canvas = document.querySelector('#canvas')
var ctx = canvas.getContext('2d')
canvas.height=window.innerHeight
canvas.width=window.innerWidth
console.log(ctx)
let particlearray= []
class Particle{
constructor(){
this.size=Math.random() * 5+1,
this.x=Math.random() * (canvas.width),
this.y=Math.random() * (canvas.height),
this.speed = 0.3,
this.color = `rgb(${Math.random() * 255},${Math.random() * 255},${Math.random() * 255})`
}
update(){
ctx.clearRect(this.x,this.y,this.size * 2 ,this.size * 2)
this.y+=this.speed
this.x+=this.speed
}
draw(){
ctx.beginPath();
ctx.fillStyle=this.color
ctx.arc(this.x,this.y,this.size,0,Math.PI*2)
ctx.fill()
ctx.closePath()
}
}
function statusupdate(){
particlearray.forEach(part =>{
part.update()
part.draw()
})
// requestAnimationFrame(statusupdate)
}
function init(){
let num= Math.random()*25
for(let i=0;i<num;i++){
particlearray.push(new Particle())
}
particlearray.forEach(part =>{
part.draw()
})
statusupdate()
}
init()
})