-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleTest.js
90 lines (71 loc) · 2.21 KB
/
ParticleTest.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
var canvas = document.querySelector('canvas');
var ctx = canvas.getContext( '2d' );
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var particles = [];
var maxParticles = 2000;
var particlesPerSecond = 50;
var particleSize = 1.5;
var emitters = new Emitter( new Vector( 50, 50 ), Vector.fromPolar( -Math.PI/2, 15 ),0.1);
var fields = [];
fields[ 0 ] = new Field( new Vector( canvas.width/2 ,canvas.height/2 ), 10 );
fields[ 1 ] = new Field( new Vector( canvas.width/4 ,canvas.height/4 ), -20 );
function clearCanvas (){
ctx.clearRect( 0, 0, canvas.width, canvas.height );
}
function update (){
addNewParticles();
plotParticles( canvas.width, canvas.height );
}
function addNewParticles() {
// If there are too many particles
if( particles.length > maxParticles ){
return;
}
// Have each emitter release the particles.
for( var j = 0; j < particlesPerSecond; j++ ){
particles.push( emitters.emitParticle() );
}
}
function plotParticles( boundsX, boundsY ){
var currentParticles = [];
for ( var i = 0; i < particles.length; i++ ) {
var particle = particles[ i ];
var pos = particle.position;
particle.acceleration = new Vector( 0, 0);
var aceelVect;
for( var j = 0; j < fields.length; j++ ){
accelVect = fields[ j ].calcAccel( particle );
particle.acceleration.add( accelVect );
}
if( pos.x < 0 || pos.x > boundsX || pos.y < 0 || pos.y > boundsY ){
continue;
}
particle.move();
currentParticles.push( particle );
}
particles = currentParticles;
}
function drawParticles() {
ctx.fillStyle = "rgb(150,0,0)";
for( var i = 0; i < particles.length; i++ ){
var position = particles[ i ].position;
ctx.fillRect( position.x, position.y, particleSize, particleSize );
}
}
function queue (){
window.requestAnimationFrame( loop );
}
function loop (){
clearCanvas();
update();
drawParticles();
queue();
}
document.onmousemove = function ( e ){
var mouseX = e.clientX;
var mouseY = e.clientY;
var emitterPosition = new Vector( mouseX, mouseY );
emitters.position = emitterPosition;
}
loop();