Skip to content

Commit

Permalink
factor out rust constants
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonRadtke committed Dec 3, 2023
1 parent fcd73cc commit a63970a
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions animation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;
use web_sys::CanvasRenderingContext2d;

const MIN_DELTA_PROP: f64 = 0.05;
const DELTA_PROP_RANGE_SIZE: f64 = 0.1;
const MOUSE_MAX_RADIUS: f64 = 25.;

#[wasm_bindgen]
pub struct Particle {
x: f64,
Expand All @@ -16,8 +20,8 @@ impl Particle {
fn tick(&mut self) {
let dx = self.home_x - self.x;
let dy = self.home_y - self.y;
self.x += 0.05 + rand::random::<f64>() * 0.1 * dx;
self.y += 0.05 + rand::random::<f64>() * 0.1 * dy;
self.x += MIN_DELTA_PROP + rand::random::<f64>() * DELTA_PROP_RANGE_SIZE * dx;
self.y += MIN_DELTA_PROP + rand::random::<f64>() * DELTA_PROP_RANGE_SIZE * dy;
}
}

Expand Down Expand Up @@ -53,7 +57,9 @@ impl System {
{
let dx = mouse_x - *x;
let dy = mouse_y - *y;
if dx * dx + dy * dy < rand::random::<f64>().powf(5.) * 25. * 25. {
if dx * dx + dy * dy
< rand::random::<f64>().powf(5.) * MOUSE_MAX_RADIUS * MOUSE_MAX_RADIUS
{
particle.x = self.width * rand::random::<f64>();
particle.y = self.height * rand::random::<f64>();
continue;
Expand Down

0 comments on commit a63970a

Please sign in to comment.