-
Notifications
You must be signed in to change notification settings - Fork 3
/
smoke.js
62 lines (44 loc) · 1.24 KB
/
smoke.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
/**
* smoke Particle Mass
*/
var SmokeParticle = function (x, y, v, r = 1, mass = 0.001) {
this.name = Math.random().toString(36).substr(2, 9);
var smokeColors = ['rgba(255,255,255,0.2)', 'rgba(200,200,200,0.2)', 'rgba(230,230,230,0.2)'];
v = v || { x: 0, y: 0 }
this.x = x;
this.y = y;
this.vx = v.x;
this.vy = v.y;
this.radius = r * 5;
this.mass = mass;
this.life = 100;
this.strokeColor = smokeColors[randNum(0, smokeColors.length - 1)];
this.fillColor = smokeColors[randNum(0, smokeColors.length - 1)];
}
SmokeParticle.prototype.step = function (dt) {
this.vy += gravity * this.mass;
this.vx += game.wind.x;
this.vy += game.wind.y;
// apply forces
this.x += dt * this.vx;
this.y += dt * this.vy;
if (this.y > groundPoint) {
this.y = groundPoint;
this.vy *= -1;
}
this.life -= 1;
if (this.radius < 30) {
this.radius += 0.1;
}
}
SmokeParticle.prototype.draw = function (ctx) {
ctx.save();
ctx.beginPath();
ctx.lineWidth = 0;
ctx.strokeStyle = this.strokeColor;
ctx.fillStyle = this.fillColor;
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI, false);
ctx.fill();
ctx.stroke();
ctx.restore();
}