Motion Trails
Instead of clearing the picture each frame, paint over it with a see-through colour: old frames fade slowly and moving things leave trails.
How it works¶
The shapes and particles starters keep the picture between frames. clear(c) paints over all of it, so only this frame shows. fade(c, alpha) paints the same colour at an opacity of alpha out of 255, so every frame the old drawing moves a little towards the background colour: after n frames, what was drawn keeps about (1 − alpha / 255)ⁿ of its strength.
Alpha sets the length of the trail. At 15, a mark fades to half in about 11 frames; at 4 the trails are long; at 60 they are short.
A very faint fade never quite reaches the background colour, because colours are whole numbers from 0 to 255 and each step rounds: some "ghosts" stay. A stronger fade, or an occasional clear, gets rid of them.
The C is for the raylib shapes starter.
A dot on a Lissajous curve¶
static void draw(float t)
{
fade((Color){ 20, 20, 20, 255 }, 15);
float s = t * 0.6f;
DrawCircleV((Vector2){ W / 2 + 240 * sinf(3 * s), H / 2 + 240 * sinf(2 * s) }, 12, (Color){ 255, 200, 80, 255 });
}
function draw(t) {
fade(Color(20, 20, 20, 255), 15);
let s = t * 0.6;
DrawCircleV(Vector2(W / 2 + 240 * sinf(3 * s), H / 2 + 240 * sinf(2 * s)), 12, Color(255, 200, 80, 255));
}
A ring of dots, each its own colour¶
static void draw(float t)
{
fade((Color){ 12, 10, 20, 255 }, 20);
for (int i = 0; i < 12; i++) {
float a = i / 12.0f * 2 * PI + t * 0.8f;
float r = 150 + 90 * sinf(t * 1.3f + i);
DrawCircleV((Vector2){ W / 2 + r * cosf(a), H / 2 + r * sinf(a) }, 10, toColor(rainbow(i / 12.0f)));
}
}
function draw(t) {
fade(Color(12, 10, 20, 255), 20);
for (let i = 0; i < 12; i++) {
let a = i / 12 * 2 * PI + t * 0.8;
let r = 150 + 90 * sinf(t * 1.3 + i);
DrawCircleV(Vector2(W / 2 + r * cosf(a), H / 2 + r * sinf(a)), 10, toColor(rainbow(i / 12)));
}
}
Related¶
- Lissajous Curves: the path of the first dot.
- Flow Fields: particles with trails.
- Animating a Pattern with Time: other ways to move.