use rand::{rngs::StdRng, Rng, SeedableRng};
use serde::{Deserialize, Serialize};
use crate::choice::chance_with_rng;
mod samplers;
pub(crate) use samplers::{
bezier_samples, catmull_rom_samples, correction_samples, dwell_samples,
linear_jittered_samples, overshoot_samples,
};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum Motion {
Bezier {
p0: (f64, f64),
p1: (f64, f64),
p2: (f64, f64),
p3: (f64, f64),
samples: usize,
t_jitter: f64,
},
Spline {
points: Vec<(f64, f64)>,
samples: usize,
},
LinearJittered {
from: (f64, f64),
to: (f64, f64),
samples: usize,
jitter_px: f64,
},
Overshoot {
target: (f64, f64),
distance_px: f64,
recovery_ms: u64,
},
Correction {
from: (f64, f64),
to: (f64, f64),
samples: usize,
},
Dwell {
at: (f64, f64),
duration_ms: u64,
micro_jitter_px: f64,
},
Click {
at: (f64, f64),
hold_ms: u64,
},
}
#[derive(Debug, Clone, PartialEq)]
pub struct Sample {
pub x: f64,
pub y: f64,
pub dt_ms: u64,
}
impl Motion {
pub fn render(&self, rng: &mut StdRng) -> Vec<Sample> {
match self {
Motion::Bezier {
p0,
p1,
p2,
p3,
samples,
t_jitter,
} => bezier_samples(*p0, *p1, *p2, *p3, *samples, *t_jitter, rng),
Motion::Spline { points, samples } => catmull_rom_samples(points, *samples, rng),
Motion::LinearJittered {
from,
to,
samples,
jitter_px,
} => linear_jittered_samples(*from, *to, *samples, *jitter_px, rng),
Motion::Overshoot {
target,
distance_px,
recovery_ms,
} => overshoot_samples(*target, *distance_px, *recovery_ms, rng),
Motion::Correction { from, to, samples } => {
correction_samples(*from, *to, *samples, rng)
}
Motion::Dwell {
at,
duration_ms,
micro_jitter_px,
} => dwell_samples(*at, *duration_ms, *micro_jitter_px, rng),
Motion::Click { at, hold_ms } => vec![
Sample {
x: at.0,
y: at.1,
dt_ms: 0,
},
Sample {
x: at.0,
y: at.1,
dt_ms: *hold_ms,
},
],
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trajectory {
pub motions: Vec<Motion>,
}
impl Trajectory {
pub fn render(&self, rng: &mut StdRng) -> Vec<Sample> {
let mut out = Vec::new();
for m in &self.motions {
let mut samples = m.render(rng);
out.append(&mut samples);
}
out
}
}
#[derive(Debug, Clone, Copy)]
pub struct GrammarWeights {
pub approach_bezier: f64,
pub approach_spline: f64,
pub approach_linear: f64,
pub overshoot: f64,
pub correction: f64,
pub dwell: f64,
}
impl Default for GrammarWeights {
fn default() -> Self {
Self {
approach_bezier: 0.60,
approach_spline: 0.30,
approach_linear: 0.10,
overshoot: 0.35,
correction: 0.40,
dwell: 0.70,
}
}
}
pub fn sample_trajectory(
from: (f64, f64),
to: (f64, f64),
weights: GrammarWeights,
seed: u64,
) -> Trajectory {
let mut rng = StdRng::seed_from_u64(seed);
let mut motions = Vec::new();
let r: f64 = rng.gen();
let approach = if r < weights.approach_bezier {
let cx1 =
from.0 + (to.0 - from.0) * rng.gen_range(0.2..0.5) + (rng.gen::<f64>() - 0.5) * 60.0;
let cy1 =
from.1 + (to.1 - from.1) * rng.gen_range(0.2..0.5) + (rng.gen::<f64>() - 0.5) * 60.0;
let cx2 =
from.0 + (to.0 - from.0) * rng.gen_range(0.5..0.8) + (rng.gen::<f64>() - 0.5) * 60.0;
let cy2 =
from.1 + (to.1 - from.1) * rng.gen_range(0.5..0.8) + (rng.gen::<f64>() - 0.5) * 60.0;
Motion::Bezier {
p0: from,
p1: (cx1, cy1),
p2: (cx2, cy2),
p3: to,
samples: rng.gen_range(40..80),
t_jitter: 0.01,
}
} else if r < weights.approach_bezier + weights.approach_spline {
let k = rng.gen_range(4..=7);
let mut points = vec![from];
for _ in 0..k {
let t = (points.len() as f64) / (k as f64);
let x = from.0 + (to.0 - from.0) * t + (rng.gen::<f64>() - 0.5) * 80.0;
let y = from.1 + (to.1 - from.1) * t + (rng.gen::<f64>() - 0.5) * 80.0;
points.push((x, y));
}
points.push(to);
Motion::Spline {
points,
samples: rng.gen_range(60..120),
}
} else {
Motion::LinearJittered {
from,
to,
samples: rng.gen_range(10..30),
jitter_px: rng.gen_range(0.5..3.0),
}
};
motions.push(approach);
if chance_with_rng(weights.overshoot, &mut rng) {
motions.push(Motion::Overshoot {
target: to,
distance_px: rng.gen_range(8.0..40.0),
recovery_ms: rng.gen_range(20u64..80u64),
});
}
if chance_with_rng(weights.correction, &mut rng) {
let cx = to.0 + (rng.gen::<f64>() - 0.5) * 8.0;
let cy = to.1 + (rng.gen::<f64>() - 0.5) * 8.0;
motions.push(Motion::Correction {
from: (cx, cy),
to,
samples: rng.gen_range(5..15),
});
}
if chance_with_rng(weights.dwell, &mut rng) {
motions.push(Motion::Dwell {
at: to,
duration_ms: rng.gen_range(40u64..200u64),
micro_jitter_px: 1.0,
});
}
motions.push(Motion::Click {
at: to,
hold_ms: rng.gen_range(70u64..110u64),
});
Trajectory { motions }
}
pub fn render_to_js(samples: &[Sample]) -> String {
use std::fmt::Write;
let mut out = String::with_capacity(samples.len() * 80);
out.push_str("(async () => {\n");
for (i, s) in samples.iter().enumerate() {
let _ = writeln!(
out,
" await new Promise(r => setTimeout(r, {}));\n \
document.elementFromPoint({:.2}, {:.2})?.dispatchEvent(\
new MouseEvent('mousemove', {{ clientX: {:.2}, clientY: {:.2}, bubbles: true }}));",
if i == 0 { 0 } else { s.dt_ms },
s.x,
s.y,
s.x,
s.y
);
}
out.push_str("})();\n");
out
}
#[cfg(test)]
mod tests;