Interface Frame


public interface Frame
Per-frame timing and frame-count information passed to Sketch.render(Graphics, Frame, Params, Input) on every tick.

This interface separates when (timing) from how to draw (Graphics) and input (Input).

What each value means

  • time() - seconds elapsed since the sketch started.
  • dt() - seconds elapsed since the previous frame, clamped to a sane upper bound to avoid temporal hitching (when a frame skip for resource loading, garbage collection, etc. causes a huge spike in dt, leading to stuttering). Use this for physics, animation, aging, and anything else that should advance "in real time".
  • index() - the frame counter. 0 on the first frame, 1 on the second and so on.
  • fps() - the harness's best estimate of the current frame rate, in frames per second. Useful for HUDs, throttling, or sanity checks.

Example


 public void render(Graphics g, Frame f, Params p, Input in) {
     g.background(20);
     // Position that advances smoothly with real time:
     double x = g.cx() + 200 * Math.cos(f.time());
     // Physics that integrate against elapsed frame time:
     vy += GRAVITY * f.dt();
     y  += vy      * f.dt();
     g.fill(255); g.circle(x, y, 20);
 }
 
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    dt()
    Seconds elapsed since the previous frame, clamped by the harness so that pauses and window drags cannot produce unbounded steps.
    double
    fps()
    An estimate of the current frame rate.
    int
    Zero-based frame counter.
    double
    Seconds elapsed since the start of the sketch.
  • Method Details

    • time

      double time()
      Seconds elapsed since the start of the sketch.
      Returns:
      the elapsed seconds since start
    • dt

      double dt()
      Seconds elapsed since the previous frame, clamped by the harness so that pauses and window drags cannot produce unbounded steps.

      On the very first frame this returns 0.

      Returns:
      the elapsed frame time in seconds
    • index

      int index()
      Zero-based frame counter. 0 on the first frame, 1 on the second, etc.
      Returns:
      the frame index
    • fps

      double fps()
      An estimate of the current frame rate.
      Returns:
      frames per second