Skip to content

Animating a Pattern with Time

Put time into the maths and the picture moves: inside a wave it slides, as a wave it pulses, in noise it drifts, and offset by position it travels.

How it works

The starters give every frame a time t that grows by 0.05 a frame (3 a second at 60 frames a second). There are a few ways to use it:

  • Add t to a wave's input. wave(x × 0.1 − t) slides sideways: the whole pattern moves, and its shape doesn't change.
  • Use sin(t) as a value. It goes up and down for ever between −1 and 1, which suits a size, a position or a brightness that pulses.
  • Use t as noise's third input. noise3(x, y, t × 0.6) changes shape smoothly, like clouds; how much t is multiplied by sets the speed.
  • Offset t by position. sin(t − d × 0.015) where d is a square's distance from the centre: squares further out are a little behind in time, so a wave moves outwards. This one trick gives a lot of good animations.

A perfect loop (for a GIF) repeats exactly. Use time only through sin(), cos() and wave(), and make one loop exactly one turn, 2π. With t growing 0.05 a frame, k = t / 6 × 2π makes one turn every 120 frames.

Give each moving part its own speed (0.7, 0.5, 1) rather than one shared one and the movement never quite repeats, as in the moving plasma on Plasma Effect.

The C is for the raylib pixels starter, or raylib shapes where it says draw.

Sliding: t inside the wave

static RGB shade(float x, float y, float t)
{
    return grey(wave(x * 0.1f - t) * wave(y * 0.1f));
}
function shade(x, y, t) {
  return grey(wave(x * 0.1 - t) * wave(y * 0.1));
}

Pulsing: sin(t) as a size

static void draw(float t)
{
    clear(PAPER);
    DrawCircle(W / 2, H / 2, 150 + 100 * sinf(t), INK);
}
function draw(t) {
  clear(PAPER);
  DrawCircle(W / 2, H / 2, 150 + 100 * sinf(t), INK);
}

Drifting: t as noise's third input

static RGB shade(float x, float y, float t)
{
    return sea(noise3(x * 0.015f, y * 0.015f, t * 0.6f));
}
function shade(x, y, t) {
  return sea(noise3(x * 0.015, y * 0.015, t * 0.6));
}

A wave through a grid: time offset by distance

static void draw(float t)
{
    clear(PAPER);
    for (int y = 30; y < H; y += 60)
        for (int x = 30; x < W; x += 60) {
            float d = dist(x, y, W / 2, H / 2);
            float s = remap(sinf(t - d * 0.015f), -1, 1, 8, 52);
            DrawRectanglePro((Rectangle){ x, y, s, s }, (Vector2){ s / 2, s / 2 },
                             (t * 0.5f - d * 0.005f) * RAD2DEG, INK);
        }
}
function draw(t) {
  clear(PAPER);
  for (let y = 30; y < H; y += 60)
    for (let x = 30; x < W; x += 60) {
      let d = dist(x, y, W / 2, H / 2);
      let s = remap(sinf(t - d * 0.015), -1, 1, 8, 52);
      DrawRectanglePro(Rectangle(x, y, s, s), Vector2(s / 2, s / 2),
                       (t * 0.5 - d * 0.005) * RAD2DEG, INK);
    }
}

A perfect loop: one turn every 120 frames

static RGB shade(float x, float y, float t)
{
    float k = t / 6 * 2 * PI;   /* t grows 6 in 120 frames */
    float d = dist(x, y, 150 + 40 * cosf(k), 150 + 40 * sinf(k));
    return rainbow(wave(d * 0.1f - k));
}
function shade(x, y, t) {
  let k = t / 6 * 2 * PI;   // t grows 6 in 120 frames
  let d = dist(x, y, 150 + 40 * cosf(k), 150 + 40 * sinf(k));
  return rainbow(wave(d * 0.1 - k));
}