-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
66 lines (54 loc) · 1.49 KB
/
background.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
//background.js
var canvas = document.getElementById("background");
canvas.width = window.innerWidth
canvas.height = window.innerHeight;
var canvasW = canvas.width;
var canvasH = canvas.height;
var ctx = canvas.getContext("2d");
function Particle() {
this.x = Math.random() * canvasW;
this.y = Math.random() * canvasH;
this.radius = Math.random() * 8 + 2;
this.vx = (Math.random() - 0.5) * this.radius;
this.vy = (Math.random() - 0.5) * this.radius;
this.color = "#4b4b4b";
};
Particle.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true);
ctx.closePath();
ctx.fillStyle = this.color;
ctx.fill();
}
var particles = [];
function createParticles() {
particles = [];
for (var i = 0; i < 100; i++) {
particles.push(new Particle());
}
}
var frames = 400;
function draw() {
frames++;
if (frames > 400) {
frames = 0;
createParticles();
ctx.beginPath();
ctx.rect(0, 0, canvasW, canvasH);
ctx.fillStyle = "#555";
ctx.fill();
}
particles.map(function(particle) {
particle.draw();
particle.x += particle.vx;
particle.y += particle.vy;
if (particle.y > canvas.height || particle.y < 0) {
particle.vy = -particle.vy;
}
if (particle.x > canvas.width || particle.x < 0) {
particle.vx = -particle.vx;
}
})
raf = window.requestAnimationFrame(draw);
}
draw();