API Reference /Time

API Reference

Time

Delta time and elapsed time, the two clocks for frame-rate independent animation.

Import
import { deltaTime, elapsedTime } from "codetoy/time"
// Time lives on Screen in C#: Screen.DeltaTime.
from codetoy.time import deltaTime, elapsedTime

See the Animation & Time guide for patterns that use these.

#deltaTime

Seconds since the previous frame, typically about 0.016 at 60 FPS. Multiply speeds by it so movement is the same at any frame rate.

deltaTime(): f64

x += 200 * deltaTime()   // 200 pixels per second
Screen.DeltaTime // double field

x += 200 * Screen.DeltaTime;   // 200 pixels per second
deltaTime() -> float

x += 200 * deltaTime()   # 200 pixels per second

#elapsedTime

Total seconds since the program started. Great for waves, pulses and timers.

elapsedTime(): f64

const y = screen.centerY() + Math.sin(elapsedTime() * 2) * 50
// Not available in C# yet. Add up DeltaTime instead:
double elapsed = 0;
Screen.OnUpdate(() => {
    elapsed += Screen.DeltaTime;
    double y = Screen.CenterY + Math.Sin(elapsed * 2) * 50;
});
elapsedTime() -> float

y = centerY() + math.sin(elapsedTime() * 2) * 50
Esc