A particle emitter
The particle emitter allows you to achive many special effects like smoke, fire, rain and similar. It can emitt images or animations.
The emitter extends the gamvas.Actor class and can therefor be added with gamvas.State.addActor for automatic drawing
new gamvas.ParticleEmitter(name, x, y, img, anim);
name | a unique name |
x/y | the position of the emitter |
img | a gamvas.Image or gamvas.Animation instance |
anim | true/false, if img is a gamvas.Animation instead of gamvas.Image |
The emitter provides a number of function named set{value} with a corresponding set{value}Range function. The range specifies a range of possible values around the non range value. This allows you to bring variance in the particle emission and therefor realism.
For example, you could use emitter.setParticleSpeed(150); with emitter.setParticleSpeedRange(50); This would end up with the particles being generated with speeds between 125 to 175 pixels per second.
myEmitter = gamvas.ParticleEmitter.extend({ onParticleEnd: function(pos, rot, scale, vel) { concole.log('rest in peace, particle at '+pos.x+','+pos.y); } }); myState = gamvas.State.extend({ init: function() { var em = new myEmitter('smoke', 0, 0, new gamvas.Image(this.resource.getImage('smoke.png'))); em.setParticleRate(20); em.setRotationRange(Math.PI*0.1); this.addActor(em); }, });
gamvas. | A particle emitter |
Variables | |
name | The name of the particle emitter |
lifetime | The time the emitter is running |
emitted | The amount of emitted particles |
position | A gamvas.Vector2D object with the position information |
center | A gamvas.Vector2D object with the center offset of the particle emitter |
rotation | The rotation of the particle emitter |
scaleFactor | The scale factor of the object |
Functions | |
setImage | Set a gamvas.Image as Particle |
setAnimation | Set a gamvas.Animation as Particle |
setAnimationLifeTime | Set if the animation should be played once for the lifetime |
setRotation | Set the rotation of the nozzle |
setRotationRange | Set the angle around rotation how particles are emitted from the nozzle |
setParticleRate | Set the rate the particles are emitted |
getParticleRate | Get the current particle emission rate |
setParticleRateRange | Set the rate range particles are emitted |
setParticleLimit | Set the maximum number of particles that will be emitted |
alignParticleToPath | If true, align particle orientation along their movement |
setParticleRotation | Set the particles starting rotation |
setParticleRotationRange | Set the range around particles starting rotation |
setParticleRotationVelocity | The rotational velocity of created particles |
setParticleRotationVelocityRange | The range of the rotational velocity |
setParticleScale | Set the scale of newly created particles |
setParticleScaleRange | Set the range of scale |
setParticleSpeed | Set the initial speed of created particles |
setParticleSpeedRange | Set the range around the initial speed of created particles |
setParticleLifeTime | Set the lifetime of new particles |
setParticleLifeTimeRange | Set the range around the lifetime of new particles |
setParticleVelocityDamping | Set how much the velocity is slowed down over time |
setParticleVelocityDampingRange | Set the range around velocity damping |
setParticleRotationDamping | Set how much the rotation is slowed down over time |
setParticleRotationDampingRange | Set range around the rotation damping |
setParticleStartPositionRange | Sets the area where particles are created |
setGravity | Set the gravity that affects the particle emitter |
setScaleTable | Set the scale over lifetime |
setAlphaTable | Set the alpha (transparency) over lifetime |
setSpeedScaleTable | Set the scale depending on particle speed |
draw | Draw the particle emitter |
reset | Reset the particle emitter |
onParticleEnd | Overwrite this function to do something everytime a particle is removed |
setRotation | Set certain rotation of the particle emitter in radians |
rotate | Rotate the particle emitter |
setPosition | Set the position of a particle emitter |
move | Move the particle emitter |
setScale | Set a certain scale factor |
scale | Scale a object |
A gamvas.Vector2D object with the position information
gamvas.ParticleEmitter.setPosition gamvas.ParticleEmitter.move
A gamvas.Vector2D object with the center offset of the particle emitter
<gamvas.ParticleEmitter.setCenter>
The rotation of the particle emitter
gamvas.ParticleEmitter.setRotation gamvas.ParticleEmitter.rotate
setAnimationLifeTime: function( yesno )
Set if the animation should be played once for the lifetime
If set to true, the Animation plays once from start to end until the particle dies, otherwise the animation plays with whatever it has set as FPS
yesno | true/false, true = play once over lifetime, false = play as specified by FPS |
setParticleStartPositionRange: function( s )
Sets the area where particles are created
By default every particle is created exactly at the position of the particle emitter, by setting a start position range, you can define a rectangle where the particles are created
s | the starting position range as gamvas.Vector2D |
setGravity: function( g )
Set the gravity that affects the particle emitter
s | the gravity in pixels per second as gamvas.Vector2D |
Gravity is in pixels per second, other then in real life, if you want your particles to fall down, you specify a positive Y value
setScaleTable: function( scaleTable )
Set the scale over lifetime
scaleTable | a array with scale factors over the particles lifetime |
It has a index (first value) between 0 and 1 where 0 is when the particle is created and 1 is when it is destroyed. You can add values between them feely, but you have to ensure, that there is a 0 and 1 index.
The following would be a table that scales from 0 to original size at the half, and then back to 0 at end:
[ [0, 0], [0.5, 1], [1, 0] ]
setAlphaTable: function( alphaTable )
Set the alpha (transparency) over lifetime
alphaTable | a array with alpha values over the particles lifetime |
It has a index (first value) between 0 and 1 where 0 is when the particle is created and 1 is when it is destroyed. You can add values between them feely, but you have to ensure, that there is a 0 and 1 index.
The following would be a table that alpha from 0 to original size, then quite early it gets full opacity, and then fades out slowly to the end:
[ [0, 0], [0.1, 1], [1, 0] ]
setSpeedScaleTable: function( spdScaleTable )
Set the scale depending on particle speed
spdScaleTable | a array with scale factors for x/y depending on particle speed |
It has a index (first value) which is the speed of the particle in pixels per second, you should have at least a speed of 0 and some speed bigger then 0
Following the index, there are two values representing the x and y scale factor where 1 is original size, smaller then 1 is smaller and higher then 1 is bigger
The following would be a table that scales the particle very small when it is slow, then when moving at medium speed, it scales it to original size, wen moving fast it streches it by reducing the x scale and significantly increasing the y scale.
[ [0, 0.1, 0.1], [30, 1, 1], [100, 0.2, 3] ]
This is usually used with gamvas.ParticleEmitter.alignParticleToPath, as x and y are regarding to the particles original orientation
draw: function( t )
Draw the particle emitter
t | the time since last redraw in seconds |
Particle emitter extends gamvas.Actor and therefor can be added via gamvas.State.addActor for automatic drawing
onParticleEnd: function( pos, rot, scale, vel )
Overwrite this function to do something everytime a particle is removed
pos | the position of the destroyed particle as gamvas.Vector2D |
rot | the rotation in radians |
scale | the scale of the particle |
vel | the velocity of the particle as gamvas.Vector2D |
Set certain rotation of the particle emitter in radians
r | the rotation in radians |
gamvas.ParticleEmitter.rotate http://en.wikipedia.org/wiki/Radians
Rotate the particle emitter
r | the amount to rotate the particle emitter in radians |
gamvas.ParticleEmitter.setRotation http://en.wikipedia.org/wiki/Radians
The amount of emitted particles
this.emitted
Set a gamvas.Image as Particle
setImage: function( img )
Set a gamvas.Animation as Particle
setAnimation: function( anim )
Set if the animation should be played once for the lifetime
setAnimationLifeTime: function( yesno )
Set the angle around rotation how particles are emitted from the nozzle
setRotationRange: function( r )
Set the rate the particles are emitted
setParticleRate: function( r )
Get the current particle emission rate
getParticleRate: function()
Set the rate range particles are emitted
setParticleRateRange: function( r )
Set the maximum number of particles that will be emitted
setParticleLimit: function( l )
If true, align particle orientation along their movement
alignParticleToPath: function( yesno )
Set the particles starting rotation
setParticleRotation: function( r )
Set the range around particles starting rotation
setParticleRotationRange: function( r )
The rotational velocity of created particles
setParticleRotationVelocity: function( r )
The range of the rotational velocity
setParticleRotationVelocityRange: function( r )
Set the scale of newly created particles
setParticleScale: function( s )
Set the range of scale
setParticleScaleRange: function( s )
Set the initial speed of created particles
setParticleSpeed: function( s )
Set the range around the initial speed of created particles
setParticleSpeedRange: function( s )
Set the lifetime of new particles
setParticleLifeTime: function( l )
Set the range around the lifetime of new particles
setParticleLifeTimeRange: function( l )
Set how much the velocity is slowed down over time
setParticleVelocityDamping: function( v )
Set the range around velocity damping
setParticleVelocityDampingRange: function( v )
Set how much the rotation is slowed down over time
setParticleRotationDamping: function( r )
Set range around the rotation damping
setParticleRotationDampingRange: function( r )
Sets the area where particles are created
setParticleStartPositionRange: function( s )
Set the gravity that affects the particle emitter
setGravity: function( g )
Set the scale over lifetime
setScaleTable: function( scaleTable )
Set the alpha (transparency) over lifetime
setAlphaTable: function( alphaTable )
Set the scale depending on particle speed
setSpeedScaleTable: function( spdScaleTable )
Draw the particle emitter
draw: function( t )
Reset the particle emitter
reset: function( kill )
Overwrite this function to do something everytime a particle is removed
onParticleEnd: function( pos, rot, scale, vel )
Add a gamvas.Actor to the state.
addActor: function( act )