Skip to main content

oscillate_with_phase

Function oscillate_with_phase 

Source
pub fn oscillate_with_phase(
    elapsed: Duration,
    period: Duration,
    phase: f32,
) -> f32
Expand description

oscillate, offset by phase full cycles.

phase shifts where in the cycle sampling starts: 0.0 matches plain oscillate, 0.25 starts a quarter-cycle ahead, and so on. Only phase’s fractional part matters (a whole number of cycles is no offset at all), and negative values are fine. This is what lets several callers share one clock’s elapsed and period while still pulsing out of sync with each other, e.g. a row of status dots each given a different phase.

use core::time::Duration;
use retroglyph_ui::oscillate_with_phase;

let period = Duration::from_secs(2);
// A quarter cycle ahead of `elapsed = 0` is the same as `elapsed = period / 4` with no phase.
let a = oscillate_with_phase(Duration::ZERO, period, 0.25);
let b = oscillate_with_phase(Duration::from_millis(500), period, 0.0);
assert!((a - b).abs() < 1e-6);