-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleEmitter.js
78 lines (54 loc) · 1.87 KB
/
ParticleEmitter.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
(function () {
'use strict';
var CanvasObject = NRD['CanvasObject'];
function ParticleEmitter (x, y, width) {
CanvasObject.call(this, x, y);
this.width = width;
this.particleSpeed = 7;
this.particlesPerFrame = 4;
this.particles = [];
}
ParticleEmitter.prototype = Object.create(CanvasObject.prototype);
ParticleEmitter.prototype.constructor = CanvasObject;
ParticleEmitter.prototype.render = function (scene) {
var i = 0;
var particle;
while ((particle = this.particles[i++]) !== undefined) {
particle.render(scene);
}
return this;
};
ParticleEmitter.prototype.update = function (scene) {
var i = 0;
var x;
var y;
var particle;
for (; i !== this.particlesPerFrame; i++) {
x = Math.random() * this.width + this.x - (this.width / 2);
y = this.y;
this.particles.push(new Particle(x, y));
}
i = this.particles.length;
while ((particle = this.particles[--i]) !== undefined) {
if (particle.y > scene.context.canvas.height) {
this.particles.splice(i, 1);
continue;
}
particle.y = particle.y + this.particleSpeed;
}
};
function Particle (x, y) {
CanvasObject.call(this, x, y);
this.width = 3;
this.height = 13;
this.color = 'rgba(30, 150, 240, 0.2)';
}
Particle.prototype = Object.create(CanvasObject.prototype);
Particle.prototype.constructor = Particle;
Particle.prototype.render = function (scene) {
scene.context.fillStyle = this.color;
scene.context.fillRect(this.x - this.width / 2, this.y - this.height / 2, this.width, this.height);
return this;
};
NRD['ParticleEmitter'] = ParticleEmitter;
}());