-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.js
63 lines (54 loc) · 1.44 KB
/
particle.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
/* global Vector,Physics */
function Particle(x, y, bounds, pattern) {
this.position = new Vector(x, y);
this.pattern = pattern;
this.active = false;
this.deltaCenter = Math.floor(this.pattern.width/2);
this.physics = new Physics(
this.getPosition(),
this.getWidth(),
this.getHeight(),
bounds
);
}
Particle.prototype.getPosition = function() {
return this.position;
};
Particle.prototype.getHeight = function() {
return this.pattern.height;
};
Particle.prototype.getWidth = function() {
return this.pattern.width;
};
Particle.prototype.isActive = function() {
return this.active;
};
Particle.prototype.activate = function() {
this.active = true;
return this;
};
Particle.prototype.update = function() {
if (this.isActive()) {
this.position = this.physics.getNewPosition();
}
};
Particle.prototype.drawPixel = function(paper, x, y, pixel) {
var oldPixel = paper.pixel(x, y);
var newPixel = pixel.slice();
var a1 = oldPixel[3] / 255;
var a2 = pixel[3] / 255;
newPixel[3] = Math.round((Math.max(a1, a2) + (a1 * a2)) * 255);
paper.pixel(x, y, newPixel);
};
Particle.prototype.drawOn = function(paper) {
for (var y = -this.deltaCenter; y < this.getHeight() - this.deltaCenter; y++) {
for (var x = -this.deltaCenter; x < this.getWidth() - this.deltaCenter; x++) {
this.drawPixel(
paper,
this.position.x + x,
this.position.y + y,
this.pattern.data[y + this.deltaCenter][x + this.deltaCenter]
);
}
}
};