Skip to content

Instantly share code, notes, and snippets.

@roipeker
Last active February 25, 2023 22:29
Show Gist options
  • Save roipeker/bd37139415b65fa6331891e8f9472b94 to your computer and use it in GitHub Desktop.
Save roipeker/bd37139415b65fa6331891e8f9472b94 to your computer and use it in GitHub Desktop.
Simple particles system in Graphx.
/// Author: roipeker 2023.
///
/// Particles in GraphX
///
/// video example:
/// https://giphy.com/gifs/VXZ0GvQSRh49AUYX9b
///
Future<void> addParticles() async {
/// creating a circle texture for quick demo... u can use any png.
var texture =
await GTextureUtils.createCircle(color: Colors.red, radius: 10);
var particles = GSimpleParticleSystem();
addChild(particles);
/// position emitter on screen.
particles.x = 100;
particles.y = 100;
/// how many particles to emit per second.
particles.emission = 40;
// how much variance in the emission rate.
// this might be between 40 and 50 particles (+10).
particles.emissionVariance = 10;
// for how long the particles will be emitted per cycle.
// 3 seconds.
particles.emissionTime = 3.0;
// 1 second delay before emitting particles cycle.
particles.emissionDelay = 1.0;
particles.blendMode = BlendMode.screen;
particles.particleBlendMode = BlendMode.modulate;
particles.texture = texture;
// particles will render WHITE by default...
// so you can tint them accordingly in transition.
particles.endColor = Colors.yellow.value;
particles.initialColor = Colors.blue.value;
// end scale is the scale the particle will have when it's dead.
particles.endScale = .1;
// "randomness" between .1 and .4.
particles.endScaleVariance = .3;
particles.endAlpha = 0;
/// This will take particle coordinates globally.
/// So the parent transform does not change the particles
/// after emission.
particles.useWorldSpace = true;
particles.initialAlpha = .7;
particles.initialAlphaVariance = .4;
particles.initialScale = .3;
particles.initialScaleVariance = .8;
particles.dispersionAngle = Math.PI / 2;
particles.dispersionAngleVariance = Math.PI / 4;
particles.initialVelocity = 2;
particles.initialVelocityVariance = 1;
particles.initialAcceleration = .5;
// for how "long" the particle will live.
// 1 seconds.
particles.energy = 1;
// extra time variation for the particle life.
particles.energyVariance = .5;
// changes the pivot point of each particle texture.
// this is useful to create a cool effect when the particles
// are rotating.
particles.particlePivotX = 20;
particles.particlePivotY = 20;
particles.initialAngularVelocityVariance = .005;
// coordinates where the particles are created.
particles.dispersionXVariance = 10;
particles.dispersionYVariance = 10;
/// create a BURST of particles.
stage!.onMouseDown.add((e) {
particles.burst = true;
/// stop bursting, and start emitting.
stage!.onMouseUp.addOnce((e) {
particles.burst = false;
particles.emit = true;
});
});
/// Move particles with mouse
/// (shows the useWorldSpace property).
/// try to rotate the emitter as well :)
stage!.onMouseMove.add((e) {
particles.x = mouseX;
particles.y = mouseY;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment