pub struct Timer { /* private fields */ }Expand description
Timer — three.js’ successor to Clock, and the only one of the two any
graded example page uses (nine of the thirty-nine construct a
new THREE.Timer(); none construct a Clock).
Its point over Clock is that update is the one place
the clock is read, so get_delta and
get_elapsed can be called any number of times in a
simulation step and answer the same thing.
§What is left out
connect( document ) / disconnect() and their Page Visibility handling.
Five of the nine pages call timer.connect( document ); all it does is
register a visibilitychange listener that calls
reset when the tab comes back, so that a tab left in the
background does not produce one enormous delta. There is no document here —
the crate is host-agnostic and the browser shell drives frames from
requestAnimationFrame, which a hidden tab does not fire — so a host that
wants that behaviour calls reset itself. Nothing about
update/getDelta/getElapsed differs.
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn new() -> Self
pub fn new() -> Self
new Timer(). _startTime = performance.now(), so the first
update() after construction has a delta of “however long init took”,
exactly as on the page.
Sourcepub fn get_elapsed(&self) -> f64
pub fn get_elapsed(&self) -> f64
getElapsed() — the sum of every delta so far, in seconds.
Sourcepub fn get_timescale(&self) -> f64
pub fn get_timescale(&self) -> f64
getTimescale().
Sourcepub fn set_timescale(&mut self, timescale: f64) -> &mut Self
pub fn set_timescale(&mut self, timescale: f64) -> &mut Self
setTimescale( timescale ) — scales the delta update() computes.
Sourcepub fn reset(&mut self) -> &mut Self
pub fn reset(&mut self) -> &mut Self
reset() — forget the gap since the last update(), so the next step
is short rather than however long the timer was ignored for.
Sourcepub fn update_with(&mut self, timestamp: Option<f64>) -> &mut Self
pub fn update_with(&mut self, timestamp: Option<f64>) -> &mut Self
update( timestamp ) — the requestAnimationFrame argument, in
milliseconds on the same origin as performance.now().
A host that wants a fixed simulation step passes
start + n * step here; there is no separate fixed-delta mode in
r186’s Timer.