-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.js
96 lines (83 loc) · 3.01 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
90
91
92
93
94
//emitter to place at a location
var particleEmitter = function(context, type, position, velocity, particleVelocity, color, size, rate, maxParticles, centralParticle){
this.context = context;
this.type = type;
this.position = position;
this.velocity = velocity;
this.particleVelocity = particleVelocity;
this.color = color;
this.rate = rate;
this.size = size;
this.maxParticles = maxParticles;
this.centralParticle = (typeof(centralParticle) == 'undefined') ? true : centralParticle;
this.particles = new Array();
}
particleEmitter.prototype.makeParticle = function(){
this.particles.push(new particle(this.context, this.type, this.position, this.particleVelocity, this.color, this.size));
}
particleEmitter.prototype.run = function(time){
//update position first
this.position[0] += this.velocity[0];
this.position[1] += this.velocity[1];
this.particles.forEach( function(each){
each.run();
});
if(this.particles.length >= this.maxParticles){
this.particles.splice(0, 1);
};
//draw the projectile
if(this.centralParticle){
this.context.save();
this.context.fillStyle = this.color;
this.context.globalAlpha = 0.9;
this.context.lineWidth = 2;
this.context.fillRect(this.position[0], this.position[1], 20,10);
this.context.restore();
}
}
//individual particle
var particle = function(context, type, origin, velocity, color, size){
this.context = context;
this.type = type;
this.position = new Array();
this.position[0] = origin[0];
this.position[1] = origin[1];
this.rotation = 0;
this.velocity = new Array();
this.velocity[0] = Math.floor((Math.random()*velocity[0])-velocity[0]/2);
this.velocity[1] = Math.floor((Math.random()*velocity[1])-velocity[1]/2);
this.color = color;
this.opacity = 1.0;
this.size = size;
}
particle.prototype.update = function(){
this.position[0] += this.velocity[0];
this.position[1] += this.velocity[1];
this.rotation[0] += 0.1;
this.rotation[1] += 0.1;
this.opacity = Math.max(this.opacity - 0.1, 0);
}
particle.prototype.drawRect = function(){
this.context.save();
this.context.fillStyle = this.color;
this.context.globalAlpha = this.opacity;
this.context.fillRect(this.position[0], this.position[1], this.size[0], this.size[1]);
this.context.restore();
}
particle.prototype.drawEllipse = function(){
this.context.save();
this.context.beginPath();
this.context.strokeStyle = this.color;
this.context.globalAlpha = this.opacity;
this.context.lineWidth = 2;
this.context.ellipse(this.position[0], this.position[1], this.size[0], this.size[1], this.rotation, 0, 2*Math.PI, false);
this.context.closePath();
this.context.stroke();
this.context.restore();
}
particle.prototype.run = function(){
this.update();
//pick a rect or an ellipse
if(this.type=="ellipse") this.drawEllipse();
else if(this.type=="rect") this.drawRect();
}