forked from jsoverson/JavaScript-Particle-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle.js
More file actions
117 lines (107 loc) · 3.94 KB
/
Copy pathParticle.js
File metadata and controls
117 lines (107 loc) · 3.94 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*jshint bitwise:false*/
/*global define:false,_*/
define(['lib/Vector'],function(Vector){
"use strict";
function Particle(point,velocity){
this.position = point;
this.velocity = velocity;
this.acceleration = new Vector(0,0);
this.ttl = -1;
this.lived = 0;
}
Particle.size = 2;
Particle.color = [66,167,222,255];
//Particle.GLOBAL_DRAW_COLOR = [166,67,0,255];
_.extend(Particle.prototype,{
submitToFields : function(fields) {
var totalAccelerationX = 0;
var totalAccelerationY = 0;
_(fields).each(function(field){
// inlining what should be Vector object methods for performance reasons
var vectorX = field.position.x - this.position.x;
var vectorY = field.position.y - this.position.y;
var force = field.mass / Math.pow((vectorX*vectorX+field.mass/2+vectorY*vectorY+field.mass/2),1.5);
totalAccelerationX += vectorX * force;
totalAccelerationY += vectorY * force;
},this);
this.acceleration = new Vector(totalAccelerationX,totalAccelerationY);
},
move : function() {
this.velocity.x += this.acceleration.x;
this.velocity.y += this.acceleration.y;
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
},
drawVariable : function(pixels,width,height) {
var baseIndex = 4 * (~~this.position.y * width + ~~this.position.x);
var velocity = this.velocity.getMagnitude();
var r = Particle.color[0] * velocity;
var g = Particle.color[1];
var b = Particle.color[2] * 0.5/velocity;
var a = Particle.color[3];
pixels[baseIndex] += r;
pixels[baseIndex + 1] += g;
pixels[baseIndex + 2] += b;
pixels[baseIndex + 3] = a;
},
drawBasic : function(pixels,width,height) {
var baseIndex = 4 * (~~this.position.y * width + ~~this.position.x);
var r = Particle.color[0];
var g = Particle.color[1];
var b = Particle.color[2];
var a = Particle.color[3];
pixels[baseIndex] += r;
pixels[baseIndex + 1] += g;
pixels[baseIndex + 2] += b;
pixels[baseIndex + 3] = a;
},
drawSoft : function(pixels,width,height) {
var baseIndex = 4 * (~~this.position.y * width + ~~this.position.x);
var r = Particle.color[0];
var g = Particle.color[1];
var b = Particle.color[2];
var a = Particle.color[3];
pixels[baseIndex - 4] += r*0.80;
pixels[baseIndex - 3] += g*0.80;
pixels[baseIndex - 2] += b*0.80;
pixels[baseIndex - 1] = a;
pixels[baseIndex] += r*0.80;
pixels[baseIndex + 1] += g*0.80;
pixels[baseIndex + 2] += b*0.80;
pixels[baseIndex + 3] = a;
pixels[baseIndex + 4] += r*0.80;
pixels[baseIndex + 5] += g*0.80;
pixels[baseIndex + 6] += b*0.80;
pixels[baseIndex + 7] = a;
baseIndex += width * 4;
pixels[baseIndex - 4] += r*0.80;
pixels[baseIndex - 3] += g*0.80;
pixels[baseIndex - 2] += b*0.80;
pixels[baseIndex - 1] = a;
pixels[baseIndex] += r;
pixels[baseIndex + 1] += g;
pixels[baseIndex + 2] += b;
pixels[baseIndex + 3] = a;
pixels[baseIndex + 4] += r*0.80;
pixels[baseIndex + 5] += g*0.80;
pixels[baseIndex + 6] += b*0.80;
pixels[baseIndex + 7] = a;
baseIndex += width * 4;
pixels[baseIndex - 4] += r*0.80;
pixels[baseIndex - 3] += g*0.80;
pixels[baseIndex - 2] += b*0.80;
pixels[baseIndex - 1] = a;
pixels[baseIndex] += r*0.80;
pixels[baseIndex + 1] += g*0.80;
pixels[baseIndex + 2] += b*0.80;
pixels[baseIndex + 3] = a;
pixels[baseIndex + 4] += r*0.80;
pixels[baseIndex + 5] += g*0.80;
pixels[baseIndex + 6] += b*0.80;
pixels[baseIndex + 7] = a;
}
});
Particle.prototype.draw = Particle.prototype.drawBasic;
Particle.drawFunctions = ['Basic','Soft','Variable'];
return Particle;
});