-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparticle.js
52 lines (48 loc) · 2 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
/*==============================================================================
Init
==============================================================================*/
$.Particle = function( opt ) {
for( var k in opt ) {
this[k] = opt[k];
}
};
/*==============================================================================
Update
==============================================================================*/
$.Particle.prototype.update = function( i ) {
/*==============================================================================
Apply Forces
==============================================================================*/
this.x += Math.cos( this.direction ) * ( this.speed * $.dt );
this.y += Math.sin( this.direction ) * ( this.speed * $.dt );
this.ex = this.x - Math.cos( this.direction ) * this.speed;
this.ey = this.y - Math.sin( this.direction ) * this.speed;
this.speed *= this.friction;
/*==============================================================================
Lock Bounds
==============================================================================*/
if( !$.util.pointInRect( this.ex, this.ey, 0, 0, $.ww, $.wh ) || this.speed <= 0.05 ) {
this.parent.splice( i, 1 );
}
/*==============================================================================
Update View
==============================================================================*/
if( $.util.pointInRect( this.ex, this.ey, -$.screen.x, -$.screen.y, $.cw, $.ch ) ) {
this.inView = 1;
} else {
this.inView = 0;
}
};
/*==============================================================================
Render
==============================================================================*/
$.Particle.prototype.render = function( i ) {
if( this.inView ) {
$.ctxmg.beginPath();
$.ctxmg.moveTo( this.x, this.y );
$.ctxmg.lineTo( this.ex, this.ey );
$.ctxmg.lineWidth = this.lineWidth;
$.ctxmg.strokeStyle = 'hsla(' + this.hue + ', ' + this.saturation + '%, ' + $.util.rand( 50, 100 ) + '%, 1)';
$.ctxmg.stroke();
}
}