-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.js
executable file
·70 lines (54 loc) · 1.36 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
var particles = [];
var nparticles = 30;
var particleSize = 7;
//by default, particles can have only negative x-velocity
var xDirection = -1;
function initParticles(n){
nparticles = n;
for (i=0; i<nparticles; i++){
particles[i] = {x:0, y:0, xVel:0, yVel:0, alpha:0};
}
}
function setParticleDirection(n){
xDirection = n;
}
function shootParticle(p){
p.xVel = xDirection*(Math.random()*150)-75;
p.yVel = (Math.random()*150)-75;
p.x = square.x+square.height/2-particleSize/2;
p.y = square.y+square.height/2-particleSize/2;
p.alpha = 1;
}
function updateParticle(p, dt){
p.x += dt*p.xVel;
p.y += dt*p.yVel;
p.alpha -= dt;
if(p.alpha < 0) p.alpha = 0;
}
function drawParticle(p){
var prevAlpha = ctx.globalAlpha;
ctx.globalAlpha = p.alpha*ctx.globalAlpha;
ctx.beginPath();
ctx.rect(p.x, p.y, particleSize, particleSize);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
ctx.globalAlpha = prevAlpha;
}
//time since the last particle was shot
var lastParticle = 0;
function updateParticles(dt){
lastParticle += dt;
for (i=0; i<nparticles; i++){
if(particles[i].alpha < 0.1 && Math.random() < lastParticle){
shootParticle(particles[i]);
lastParticle = 0;
}
updateParticle(particles[i], dt);
}
}
function drawParticles(){
for (i=0; i<nparticles; i++){
drawParticle(particles[i]);
}
}