LearnFirst steps
Lesson 6 of 30 · 50 min
Time and motion
Animate with time and sin, controlling period, speed and easing
In this lesson you will
- Use
timeto change a shape's position, size and color over time - Choose the amplitude, period and phase of
sinto make back-and-forth and circular motion - Repeat a motion at a fixed interval with
fract(time) - Add easing (speeding up and slowing down) with
smoothsteport * t - Offset time by position or index to make traveling waves and rows that move in sequence
Contents
Contents
- 01Time is a number that keeps growing
- 02Back and forth with sin
- 03Going around a circle
- 04Blinking and pulsing
- 05Repeating with fract
- 06Easing: giving speed some character
- 07There and back (ping-pong)
- 08Offsetting time by position: traveling waves
- 09Lining things up with a loop and staggering the phase
- 10Exercises
- 11Recap
01Time is a number that keeps growing
time, which we touched on in Your first shader, is the number of seconds since the work started. It is 0.0 at the very start, 2.0 two seconds later, 60.0 after a minute, and it keeps growing. Within a single frame, every pixel gets the same value.
A shader recomputes every pixel about 60 times per second. If time is part of the math, the answer changes a little from frame to frame, and that reads as motion — just like a flip book. Let's start by making the x of a circle's center grow with time.
1
2
3
4
5
6
7
8
9
10
11
0.3 to 1.0 to speed it up. If -1.2 becomes 0.0, where does it start?The center's x is -1.2 + time * 0.3. At 0 seconds it is -1.2 (toward the left), at 2 seconds -0.6, at 4 seconds 0.0 (the middle): it moves right by 0.3 every second. 0.3 is the speed. But after about 10 seconds it leaves the screen on the right and never comes back, because time never stops growing. For a work that people keep watching, you need motion that goes back and forth or repeats.
Tip
Still thumbnails on this site are drawn from a single frame at time = 2.0. Even for an animated work, it pays to check that the picture looks good at the 2-second mark (more on this in Touch and response).
02Back and forth with sin
sin is a function whose answer swings between -1.0 and 1.0 no matter how large its input gets. sin(0.0) is 0.0, sin(1.57) is 1.0, sin(3.14) is 0.0, sin(4.71) is -1.0, and sin(6.28) is back to 0.0. Feed it time and the ever-growing clock turns into a round trip.
1
2
3
4
5
6
7
8
9
10
11
0.8 to 0.3 for a smaller swing. What does sin(time * 3.0) do?As we saw in Shaping functions, the sin wave repeats every time its input advances by about 6.28; that length is its period. Feed it time and the period becomes a number of seconds: sin(time) takes about 6.28 seconds per round trip. To get exactly the motion you want, think of it in this form:
1
float x = 0.8 * sin(time * 2.0 + 1.0);
0.8… the amplitude: how far it swings. The result goes from -0.8 to 0.82.0… the speed: bigger is faster. The period is 6.28 ÷ 2.0, about 3.14 seconds1.0… the phase: where in the wave it starts. It moves the motion a little earlier or later
When you want to set the period in seconds — say, one round trip every 2 seconds — write sin(time * TAU / 2.0). TAU is twice pi (about 6.28), exactly the length of one sin repetition. As in Shaping functions, GLSL doesn't provide it, so you define it yourself with #define TAU 6.28318531 at the top of the code. When time advances by 2 seconds, the inside advances by exactly TAU, so you get one round trip every 2 seconds.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1.5 to 0.0 and the two move together. At 3.14 they always move in opposite directions. What about period = 5.0?The blue circle at the bottom moves with the same period as the orange one on top, but its phase is 1.5 ahead. Shift it by 3.14 (half a cycle) and when one is at the right end, the other is at the left. Phase is the knob for when the same motion starts. We will use this idea at the end of the lesson to move many shapes one after another.
03Going around a circle
Use cos(time) for x and sin(time) for y and the point travels around a circle. cos is the same wave as sin, just a quarter cycle (about 1.57) ahead. At 0 seconds, (cos, sin) is (1.0, 0.0), on the right; at 1.57 seconds it is (0.0, 1.0), at the top; at 3.14 seconds it is (-1.0, 0.0), on the left. Its distance from the center stays 1.0 the whole time, so the point moves counterclockwise around the circle.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vec2(sin(time), cos(time)) and it goes clockwise. What about vec2(1.5 * cos(time), 0.5 * sin(time))?Give x and y different amplitudes and the point travels around a wide or tall ellipse. Give them different speeds (say cos(time) and sin(time * 2.0)) and you get a figure-eight and other intricate paths. Combinations of sin and cos alone make a whole family of orbits.
04Blinking and pulsing
Position isn't the only thing you can animate. Add a sin to the radius and the circle grows and shrinks, as if pulsing. 0.35 + 0.08 * sin(time * 3.0) sways ±0.08 around 0.35, between 0.27 and 0.43. Drive the color with time and it slowly drifts. Here we pass time * 0.1 to the palette from Mixing colors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
beat equal abs(sin(time * 3.0)) for a thumping heartbeat. With step(0.0, sin(time * 3.0)) it blinks between two sizesabs(sin(…)) folds the negative half of the sine up, giving 0 to 1. Its direction flips sharply at 0, so it feels more like a beat than the smooth sine does. step(0.0, sin(…)) is 1.0 when the sine is positive and 0.0 when it is negative, so it snaps every half period: a blink. Multiply a brightness by it and you get a lamp switching on and off.
05Repeating with fract
sin gives you there-and-back motion. For motion that moves in one direction and then jumps back to the start, use fract, which we glimpsed in Shaping functions. fract(x) keeps only the fractional part of x: fract(2.3) is 0.3, fract(5.75) is 0.75. fract(time) climbs from 0 to 1, drops back to 0 the moment it reaches 1, and climbs again. Its graph looks like the teeth of a saw, so it is called a sawtooth wave.
In fract(time * 0.4) the inside takes 1 ÷ 0.4 = 2.5 seconds to advance by 1, so it repeats every 2.5 seconds. fract(time / 3.0) repeats every 3 seconds. Once you have a 0-to-1 value, mix(a, b, t) from Mixing colors stretches it to any range you like.
1
2
3
4
5
6
7
8
9
10
11
0.4 to 1.0 and it repeats every second. What happens with mix(2.2, -2.2, t) instead of mix(-2.2, 2.2, t)?The circle travels from off-screen to off-screen, so you never see the warp back to 0. Hiding the seam of a loop outside the screen is a common trick. With the same idea, let fract grow a circle's radius and you get a ring that spreads and vanishes like a ripple on water. Fade it as it grows and the jump back to 0 goes unnoticed too.
1
2
3
4
5
6
7
8
9
10
11
12
fade to 1.0 and the moment the ring disappears becomes obvious. Copy the four lines from float t to col = mix(…) once more, using fract(time / 3.0 + 0.5) in the copy, to add a second ring half a cycle apart (give the copy's variables new names)06Easing: giving speed some character
A 0-to-1 value from fract grows at a constant speed. Used directly as a position, it starts and stops at full speed and looks mechanical. Real objects ease into motion and ease out of it. Passing the 0-to-1 value through another 0-to-1 curve to shape how the speed changes is called easing.
t… unchanged: constant speed (0.5 when t is 0.5)smoothstep(0.0, 1.0, t)… slow, fast, slow: about 0.03 at t = 0.1, 0.5 at t = 0.5, about 0.97 at t = 0.9t * t… speeding up (ease-in): at t = 0.5 it has only covered 0.251.0 - (1.0 - t) * (1.0 - t)… slowing down (ease-out): at t = 0.5 it has already covered 0.75
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
e3 to 1.0 - (1.0 - t) * (1.0 - t) and it slows down instead. t * t * t * t gives an even sharper accelerationAll three circles leave at the same moment and arrive at the same moment, but their speeds along the way are completely different. The orange one (smoothstep) is slow at both ends, so it looks the most natural. t * t uses the fact that multiplying numbers between 0 and 1 makes them smaller: 0.5 × 0.5 = 0.25, so it stays small for the first half and races to 1 near the end. With pow, as in pow(t, 3.0), you can tune how strong the curve is with a number.
07There and back (ping-pong)
fract jumps from 1 back to 0, but run it through 1.0 - abs(t * 2.0 - 1.0) and you get a triangle wave that goes there and back: 0 → 1 → 0. When t is 0, t * 2.0 - 1.0 is -1; abs makes it 1; subtracting from 1 gives 0. At t = 0.25: -0.5 → 0.5 → 0.5. At t = 0.5: 0 → 0 → 1. At t = 0.75 it is 0.5 again, and at t = 1 it is back to 0. It starts and ends at the same 0, so it loops without a seam.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
smoothstep(0.0, 1.0, tri) to smoothstep(0.2, 0.8, tri) and it pauses at each end before moving. Set e to tri for a stiff, bounce-off-the-wall motionYou could make a round trip with sin, but splitting it into a triangle wave plus easing lets you build in pauses. smoothstep(0.2, 0.8, tri) stays at 0 while tri is below 0.2 and at 1 while it is above 0.8, so the circle rests at each end for that long. A rhythm of move, pause, return gives a work a sense of timing.
08Offsetting time by position: traveling waves
So far every pixel has used the same moment in time. Now we shift time depending on position. sin(p.x * 4.0) is a wave laid out across the screen, standing still. Subtract time to get sin(p.x * 4.0 - time * 2.0) and the wave travels to the right.
1
2
3
4
5
6
7
8
9
10
- time * 2.0 to + time * 2.0 and it travels left. Append + 0.1 * sin(p.x * 9.0 + time * 3.0) to the y line for a more ocean-like surfaceWhy does it move right? A crest sits where the inside of sin equals 1.57. Solving p.x * 4.0 - time * 2.0 = 1.57 for p.x gives p.x = (1.57 + time * 2.0) / 4.0. Each second, the crest's p.x shifts right by 2.0 ÷ 4.0 = 0.5. In other words, subtracting time moves the pattern in the positive direction. Its speed is the number multiplying time divided by the number multiplying p.x.
Tip
"Subtract to move in the positive direction" is an important rule that keeps coming back. In Moving space you will see that, for the same reason, writing p - c moves a shape toward +c.
09Lining things up with a loop and staggering the phase
Let's apply the traveling-wave idea to a row of shapes. Line up nine circles and delay each one's up-and-down motion a little more than the last, and a wave seems to ripple down the row. Instead of writing the circle nine times, we use a for loop.
1
2
3
for (int i = 0; i < 9; i++) {
// this runs 9 times, with i = 0, 1, 2, …, 8
}
int i = 0… start a counterias the integer 0i < 9… keep repeating the{ }block while i is less than 9i++… add 1 to i after each passfloat(i)… i is an integer, so convert it tofloatbefore using it in decimal math (as in Your first shader, int and float don't mix directly)
Watch out
In works you post on this site, loops must take the form for (int i = 0; i < 9; i++), with the limit written as a literal number of 64 or less (not a variable like i < n). while is not allowed. A loop body runs for every pixel on every frame, so more passes means more work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fi * 0.5 to fi * 0.0 and they all bob in unison. What about fi * 1.2? Flip - fi to + fi and the wave travels the other wayCircle number i moves with sin(time * 3.0 - fi * 0.5). Each one's phase lags 0.5 behind the previous, so the leftmost rises first, its right neighbor a moment later, and so on: the motion travels right. Since each circle's x is proportional to its index, this is the sin(p.x * k - time) wave from the previous section, sampled at separate spots. The color also changes per circle, by passing fi / 9.0 to the palette.
10Exercises
Combine these ways of using time to build some motion of your own. Decide "once every how many seconds" and "from where to where" first, then turn that into a formula — it keeps you from getting lost.
A breathing circle
Make the circle in the center slowly grow and shrink exactly once every 4 seconds. Vary the radius between 0.2 and 0.5, and make the color brighter the bigger it gets.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
A bouncing ball
Make the ball keep bouncing on the floor. If it changes direction sharply the instant it touches the floor and moves slowly near the top, it will look like it bounces. Once that works, make its shadow on the floor smaller and fainter the higher the ball is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
A loading spinner
Of the eight dots arranged in a circle, make a bright one travel all the way around once per second, followed by a tail of dots that get darker and smaller. It's the familiar spinning "loading" indicator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
11Recap
timeis an ever-growing count of seconds; put it in the math and the answer changes each frame, which reads as motionA * sin(time * speed + phase)makes a round trip; to set the period in seconds, usesin(time * TAU / seconds)vec2(cos(time), sin(time))moves counterclockwise around a circlefract(time * k)is a sawtooth that repeats 0 → 1;1.0 - abs(t * 2.0 - 1.0)turns it into a there-and-back triangle wave- Passing a 0-to-1 value through
smoothsteport * tadds acceleration and deceleration (easing) - Offsetting time by position or index gives traveling waves and staggered rows; subtracting time moves things in the positive direction
Next time we add boxes and line segments to our circles, and combine shapes by adding and subtracting them to build more complex forms.
Finished lessons get a mark in the course contents