Struct firework_rs::fireworks::FireworkConfig
source · pub struct FireworkConfig {
pub gravity_scale: f32,
pub ar_scale: f32,
pub additional_force: Box<dyn Fn(&Particle) -> Vec2>,
pub gradient_scale: fn(_: f32) -> f32,
pub enable_gradient: bool,
}Expand description
Struct representing the configuration of a single Firework
This applies to all Particle in the Firework
Fields§
§gravity_scale: f32Larger gravity_scale tends to pull particles down
ar_scale: f32Air resistance scale
Warning: too large or too small ar_scale may lead to unexpected behavior of Particles
additional_force: Box<dyn Fn(&Particle) -> Vec2>§gradient_scale: fn(_: f32) -> f32This field is a function that takes a float between 0 and 1, returns a float representing all Particles’ gradient
Particles’ gradient changes according to its elapsed time and lifetime
The input f32 equals to time_elapsed/life_time, which returns a f32 affecting its color gradient
gradient_scale returns 1. meansParticle will have the same colors as defined all over its lifetime
enable_gradient: boolSet wheter or not firework has color gradient
§Notes
- It is recommanded that your terminal window is non-transparent and has black bg color to get better visual effects
- Otherwise set it to
false
Implementations§
source§impl FireworkConfig
impl FireworkConfig
sourcepub fn with_gradient_scale(self, f: fn(_: f32) -> f32) -> Self
pub fn with_gradient_scale(self, f: fn(_: f32) -> f32) -> Self
Set gradient_scale
Examples found in repository?
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
fn gen_fountain_firework(center: Vec2) -> Firework {
let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
let mut particles = Vec::new();
for v in gen_points_fan(
300.,
45,
5 as f32 / 12 as f32 * PI,
7 as f32 / 12 as f32 * PI,
)
.iter()
{
particles.push(ParticleConfig::new(
center,
*v,
thread_rng().gen_range(28..38),
Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.15)
.with_gravity_scale(0.5)
.with_gradient_scale(gradient);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(5),
time_interval: Duration::from_secs_f32(0.08),
timer: Duration::ZERO,
},
..Default::default()
}
}More examples
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
fn gen_vortex_firework(center: Vec2) -> Firework {
let colors = vec![
(6, 55, 63),
(24, 90, 96),
(47, 123, 119),
(92, 174, 166),
(200, 255, 255),
];
let mut particles = Vec::new();
for p in gen_points_circle(30, 45).iter() {
particles.push(ParticleConfig::new(
center + *p,
Vec2::new((*p).y, -(*p).x).normalize() * 15.,
thread_rng().gen_range(28..40),
Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.05)
.with_gravity_scale(0.)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| {
(center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
});
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(10),
time_interval: Duration::from_secs_f32(0.01),
timer: Duration::ZERO,
},
..Default::default()
}
}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
fn gen_heart_firework(center: Vec2) -> Firework {
let colors = vec![
(233, 232, 237),
(254, 142, 130),
(200, 27, 72),
(86, 18, 31),
];
let mut particles = Vec::new();
let trail_length = thread_rng().gen_range(100..105);
let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
let init_pos = center - Vec2::NEG_Y * 15.;
for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.1)
.with_gravity_scale(0.1)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| (center - particle.pos) * 2.);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Instant { used: false },
..Default::default()
}
}sourcepub fn with_gravity_scale(self, s: f32) -> Self
pub fn with_gravity_scale(self, s: f32) -> Self
Set gravity_scale
Examples found in repository?
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
fn gen_fountain_firework(center: Vec2) -> Firework {
let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
let mut particles = Vec::new();
for v in gen_points_fan(
300.,
45,
5 as f32 / 12 as f32 * PI,
7 as f32 / 12 as f32 * PI,
)
.iter()
{
particles.push(ParticleConfig::new(
center,
*v,
thread_rng().gen_range(28..38),
Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.15)
.with_gravity_scale(0.5)
.with_gradient_scale(gradient);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(5),
time_interval: Duration::from_secs_f32(0.08),
timer: Duration::ZERO,
},
..Default::default()
}
}More examples
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
fn gen_vortex_firework(center: Vec2) -> Firework {
let colors = vec![
(6, 55, 63),
(24, 90, 96),
(47, 123, 119),
(92, 174, 166),
(200, 255, 255),
];
let mut particles = Vec::new();
for p in gen_points_circle(30, 45).iter() {
particles.push(ParticleConfig::new(
center + *p,
Vec2::new((*p).y, -(*p).x).normalize() * 15.,
thread_rng().gen_range(28..40),
Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.05)
.with_gravity_scale(0.)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| {
(center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
});
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(10),
time_interval: Duration::from_secs_f32(0.01),
timer: Duration::ZERO,
},
..Default::default()
}
}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
fn gen_heart_firework(center: Vec2) -> Firework {
let colors = vec![
(233, 232, 237),
(254, 142, 130),
(200, 27, 72),
(86, 18, 31),
];
let mut particles = Vec::new();
let trail_length = thread_rng().gen_range(100..105);
let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
let init_pos = center - Vec2::NEG_Y * 15.;
for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.1)
.with_gravity_scale(0.1)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| (center - particle.pos) * 2.);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Instant { used: false },
..Default::default()
}
}sourcepub fn with_ar_scale(self, s: f32) -> Self
pub fn with_ar_scale(self, s: f32) -> Self
Set ar_scale
Examples found in repository?
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
fn gen_fountain_firework(center: Vec2) -> Firework {
let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
let mut particles = Vec::new();
for v in gen_points_fan(
300.,
45,
5 as f32 / 12 as f32 * PI,
7 as f32 / 12 as f32 * PI,
)
.iter()
{
particles.push(ParticleConfig::new(
center,
*v,
thread_rng().gen_range(28..38),
Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.15)
.with_gravity_scale(0.5)
.with_gradient_scale(gradient);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(5),
time_interval: Duration::from_secs_f32(0.08),
timer: Duration::ZERO,
},
..Default::default()
}
}More examples
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
fn gen_vortex_firework(center: Vec2) -> Firework {
let colors = vec![
(6, 55, 63),
(24, 90, 96),
(47, 123, 119),
(92, 174, 166),
(200, 255, 255),
];
let mut particles = Vec::new();
for p in gen_points_circle(30, 45).iter() {
particles.push(ParticleConfig::new(
center + *p,
Vec2::new((*p).y, -(*p).x).normalize() * 15.,
thread_rng().gen_range(28..40),
Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.05)
.with_gravity_scale(0.)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| {
(center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
});
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(10),
time_interval: Duration::from_secs_f32(0.01),
timer: Duration::ZERO,
},
..Default::default()
}
}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
fn gen_heart_firework(center: Vec2) -> Firework {
let colors = vec![
(233, 232, 237),
(254, 142, 130),
(200, 27, 72),
(86, 18, 31),
];
let mut particles = Vec::new();
let trail_length = thread_rng().gen_range(100..105);
let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
let init_pos = center - Vec2::NEG_Y * 15.;
for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.1)
.with_gravity_scale(0.1)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| (center - particle.pos) * 2.);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Instant { used: false },
..Default::default()
}
}sourcepub fn with_additional_force(
self,
af: impl Fn(&Particle) -> Vec2 + 'static
) -> Self
pub fn with_additional_force( self, af: impl Fn(&Particle) -> Vec2 + 'static ) -> Self
Set additional_force
Examples found in repository?
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
fn gen_vortex_firework(center: Vec2) -> Firework {
let colors = vec![
(6, 55, 63),
(24, 90, 96),
(47, 123, 119),
(92, 174, 166),
(200, 255, 255),
];
let mut particles = Vec::new();
for p in gen_points_circle(30, 45).iter() {
particles.push(ParticleConfig::new(
center + *p,
Vec2::new((*p).y, -(*p).x).normalize() * 15.,
thread_rng().gen_range(28..40),
Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.05)
.with_gravity_scale(0.)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| {
(center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
});
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(10),
time_interval: Duration::from_secs_f32(0.01),
timer: Duration::ZERO,
},
..Default::default()
}
}More examples
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
fn gen_heart_firework(center: Vec2) -> Firework {
let colors = vec![
(233, 232, 237),
(254, 142, 130),
(200, 27, 72),
(86, 18, 31),
];
let mut particles = Vec::new();
let trail_length = thread_rng().gen_range(100..105);
let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
let init_pos = center - Vec2::NEG_Y * 15.;
for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.1)
.with_gravity_scale(0.1)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| (center - particle.pos) * 2.);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Instant { used: false },
..Default::default()
}
}sourcepub fn set_enable_gradient(&mut self, enable_gradient: bool)
pub fn set_enable_gradient(&mut self, enable_gradient: bool)
Set enable_gradient
Examples found in repository?
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
fn gen_fountain_firework(center: Vec2) -> Firework {
let colors = vec![(226, 196, 136), (255, 245, 253), (208, 58, 99)];
let mut particles = Vec::new();
for v in gen_points_fan(
300.,
45,
5 as f32 / 12 as f32 * PI,
7 as f32 / 12 as f32 * PI,
)
.iter()
{
particles.push(ParticleConfig::new(
center,
*v,
thread_rng().gen_range(28..38),
Duration::from_secs_f32(thread_rng().gen_range(2.5..3.8)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.15)
.with_gravity_scale(0.5)
.with_gradient_scale(gradient);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(5),
time_interval: Duration::from_secs_f32(0.08),
timer: Duration::ZERO,
},
..Default::default()
}
}More examples
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
fn gen_vortex_firework(center: Vec2) -> Firework {
let colors = vec![
(6, 55, 63),
(24, 90, 96),
(47, 123, 119),
(92, 174, 166),
(200, 255, 255),
];
let mut particles = Vec::new();
for p in gen_points_circle(30, 45).iter() {
particles.push(ParticleConfig::new(
center + *p,
Vec2::new((*p).y, -(*p).x).normalize() * 15.,
thread_rng().gen_range(28..40),
Duration::from_secs_f32(thread_rng().gen_range(4.5..7.0)),
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.05)
.with_gravity_scale(0.)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| {
(center - particle.pos).normalize() * (1. / center.distance(particle.pos)) * 150.
});
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Sustained {
lasts: Duration::from_secs(10),
time_interval: Duration::from_secs_f32(0.01),
timer: Duration::ZERO,
},
..Default::default()
}
}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
fn gen_heart_firework(center: Vec2) -> Firework {
let colors = vec![
(233, 232, 237),
(254, 142, 130),
(200, 27, 72),
(86, 18, 31),
];
let mut particles = Vec::new();
let trail_length = thread_rng().gen_range(100..105);
let life_time = Duration::from_secs_f32(thread_rng().gen_range(3.0..3.2));
let init_pos = center - Vec2::NEG_Y * 15.;
for v in gen_points_fan(300., 45, 0.2 * PI, 0.3 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
for v in gen_points_fan(300., 45, 0.7 * PI, 0.8 * PI).iter() {
particles.push(ParticleConfig::new(
init_pos,
*v,
trail_length,
life_time,
*colors.iter().choose(&mut thread_rng()).unwrap(),
));
}
let mut config = FireworkConfig::default()
.with_ar_scale(0.1)
.with_gravity_scale(0.1)
.with_gradient_scale(gradient)
.with_additional_force(move |particle| (center - particle.pos) * 2.);
config.set_enable_gradient(true);
Firework {
init_time: SystemTime::now(),
spawn_after: Duration::ZERO,
center,
particles,
config,
form: ExplosionForm::Instant { used: false },
..Default::default()
}
}