LearnPatterns and noise

Lesson 18 of 30 · 45 min

Touch and response

Make works that respond to the viewer with mouse and attention

In this lesson you will

  • Know that mouse is the cursor position in the same coordinates as p, and change the image by distance from the cursor
  • Send ripples out from the cursor, and pull or push space around it
  • Use attention to heighten a work only while it is hovered in the Gallery, without making the image jump
  • Finish works that also look good in the still thumbnail (time 2, attention 0, mouse (0, 0))

01mouse is the cursor position

Of the four uniforms this site provides, we have used time and resolution so far. This lesson covers the other two, which carry what the viewer is doing: mouse and attention. First, mouse.

mouse is a vec2 holding the cursor's position over the preview. Conveniently, it comes in the same coordinates as p: (0, 0) at the center, y = 1 at the top edge and -1 at the bottom, and x scaled by the aspect ratio (around 1.33–1.78 at the right edge of a wide screen). So simply writing p - mouse gives you "the position as seen from the cursor". Before the cursor has been over the preview, mouse is (0, 0): the center of the screen.

1

2

3

4

5

6

7

8

9

10

11

12

13

Starts when it scrolls into view
Open in /edit ↗
▸Move the cursor over the preview. Where does the dot go if you change p - mouse to p + mouse?

The dot's formula is the same length(p - center) we used to move circles in Circles and distance; swapping mouse in for the center makes the shape follow the cursor. As we saw in Moving space, p - mouse shifts the coordinates by mouse, so anything drawn in the shifted coordinates follows the cursor.

Watch out

Touch devices like phones and tablets have no cursor. Dragging a finger often won't move mouse at all, so make sure your work still stands on its own without it. We look at this closely at the end of the lesson.

02Changing things by distance from the cursor

With the distance from the cursor, length(p - mouse), you can change things only near the cursor. Let's start with a flashlight: a pattern hidden in darkness, lit only around the cursor.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Starts when it scrolls into view
Open in /edit ↗
▸Change 6.0 to 20.0 for a smaller beam. Set 0.08 to 0.0 and everything outside the beam goes pitch black

exp(-d * d * 6.0) is a bell-shaped curve: 1 at the cursor, about 0.58 at 0.3 away and about 0.12 at 0.6 away, easing smoothly toward 0. Unlike exp(-d * k) from Light and finishing, it squares d, which gives a pool of light that is flat in the middle with soft edges.

Brightness isn't the only thing distance can drive. Below, dots grow larger in cells closer to the cursor. The important part is that we measure the distance from the cell's center to the cursor, not from the pixel position p. Every pixel in a cell uses the same distance, so the dot grows while staying perfectly round.

1

2

3

4

5

6

7

8

9

10

11

12

13

Starts when it scrolls into view
Open in /edit ↗
▸Replace center in length(center - mouse) with p and the dots warp into eggs leaning toward the cursor. Change 3.0 to 1.0 so distant cells grow too

(id + 0.5) / 6.0 turns the cell index back into p coordinates. We scaled up by 6 with g = p * 6.0, so dividing by 6.0 undoes it. The + 0.5 points at the middle of the cell rather than its lower-left corner.

03Ripples

Let's make ripples spread across water from the cursor. The traveling wave sin(d * k - time * s) from Time and motion, fed with the distance d from the cursor, becomes rings moving outward around it. Multiplying by exp(-d * 2.0) weakens the waves with distance.

Rather than only drawing the waves, use them to shift where the floor pattern shows through, and it looks much more like water. On a real pool, waves bend the light so the tiles on the bottom wobble (refraction). Here we shift the coordinates outward from the cursor, in proportion to the wave height.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

Starts when it scrolls into view
Open in /edit ↗
▸Set the 0.03 in wave * 0.03 to 0.0 and the wobble disappears. Change time * 5.0 to time * -5.0 and the waves flow inward to the cursor

dir / max(d, 0.001) is the unit direction pointing away from the cursor (the same as normalize(dir), with max to avoid dividing by 0 right under the cursor). pow(max(wave, 0.0), 3.0) keeps only the highest parts of the wave, so light sits just on the crests.

04Pulling and pushing space

The ripples shifted coordinates a little to warp the pattern. Shift them more and you can magnify the pattern under the cursor like a lens, or shrink it as if sucked in. The trick is to read the pattern from a spot moved toward or away from the cursor, depending on the distance.

1

2

3

4

5

6

7

8

9

10

11

12

Starts when it scrolls into view
Open in /edit ↗
▸Set strength to -0.8 and the dots get sucked toward the cursor. At 0.9 the spot under the cursor is hugely magnified

mouse + dir * f keeps the direction from the cursor but scales the distance by f. Right under the cursor, f = 1.0 - 0.5 = 0.5, so we read the pattern at half the real distance. The pattern near the cursor is stretched to twice its size: magnification. At 0.5 away, exp(-2.0) is about 0.14, so f is about 0.93 — nearly unchanged. A negative strength makes f larger than 1, packing distant parts of the pattern in close: pinching.

Watch out

A shader recomputes everything from scratch each frame and remembers nothing about the previous one. Effects that need the past — motion that trails behind the cursor, a trail where it has been, ripples from the spot you clicked — are not possible. mouse only tells you where the cursor is right now.

05What attention is

attention is a uniform unique to this site. When a viewer hovers over a work's card in the Gallery, the still image starts moving and attention rises from 0 to nearly 1 in about 0.3 seconds. When the cursor leaves, it falls back to 0 just as fast. It tells the work "someone is looking at you right now".

  • Gallery … rises to 1 only while hovered. Hovering changes attention; mouse stays at (0, 0)
  • Work page and editor … always 0
  • This course's previews … playgrounds that use attention show a slider below so you can set 0–1 yourself

The most straightforward use is mix(resting look, hovered look, attention). Below, calm blue-violet rings come alive with color and start to undulate as attention rises. Move the slider to try it.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

Starts when it scrolls into view
attention
0.00
Open in /edit ↗
▸Slide between 0 and 1. Change mix(calm, vivid, attention) to mix(calm, vivid, attention * attention) to make the color arrive later

The site already moves attention smoothly between 0 and 1, so you can use it directly as an eased progress value. Reshape it with attention * attention or smoothstep(0.0, 1.0, attention) to tune how the change feels, just like the easing in Time and motion.

06Using attention well

When you want "move faster on hover", it's tempting to write time * mix(0.5, 3.0, attention). But this makes the image jump. At time = 60 seconds, changing the speed from 0.5 to 3.0 takes time * speed from 30 straight to 180. That fast-forwards 150 seconds of motion into the 0.3 seconds attention takes to rise.

Use attention for things that don't accumulate over time: strength, color, size, blend amounts. To change speed, build a slow motion and a fast motion separately, then blend or add them with attention. Each one only multiplies time by a fixed number, so the image never jumps however attention changes.

1

2

3

4

5

6

7

8

9

10

11

Starts when it scrolls into view
attention
0.00
Open in /edit ↗
▸Move the slider and a fine, fast wave blends in smoothly. Remove the // from // y = …, then move the slider: the wave spins through a fast-forward

Another trick is staggering. attention finishes rising in about 0.3 seconds, but if you split its range, as in smoothstep(0.0, 0.5, attention) and smoothstep(0.5, 1.0, attention), things change in sequence: first this, then that. Below, each cell's range is offset by its distance from the center, so the tiles open in a wave from the middle outward.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

Starts when it scrolls into view
attention
0.00
Open in /edit ↗
▸Slide slowly to the right and the tiles open from the middle out. Set the 0.6 in r * 0.6 to 0.0 and every tile opens at once

Tip

Like mouse, attention has no memory. You can't tell how many seconds have passed since the hover began, so long sequences are out. Use attention for short transitions of about 0.3 seconds, and leave longer motion to time.

07Beautiful in every state

Viewers won't always move mouse or change attention. In fact, a work is seen most often with no interaction at all. In the Gallery especially, what appears first is the still thumbnail: a single frame drawn from your code, with these values:

  • time = 2 (the frame 2 seconds in)
  • attention = 0
  • mouse = (0, 0) (the center of the screen)

On hover, the animation starts from that same time = 2 frame and attention rises. So the picture with attention at 0 and mouse at the center is the face of your work. Think of what mouse and attention change as a bonus, and make the work stand on its own without them. Some tips for checking:

  • Look without hovering to see the picture with mouse at the center. If the cursor acts as a light, does a lit center make a natural composition?
  • With attention at 0, is it too dark, or empty? Even if hovering reveals something hidden, leave something worth seeing in the resting state
  • Does the time = 2 frame happen to land on an empty moment? (Pause the editor at 2 seconds to check)
  • On touch devices mouse often doesn't move. Is the work still enjoyable without it?

The piece below brings it all together. At rest, the lanterns flicker slowly as if breathing; lanterns near the cursor grow brighter and larger; and as attention rises, they all light up in many colors. Before you hover, mouse is at the center, so the middle lanterns glow brighter — designed to read as a natural focal point. It is finished with the tone mapping and gamma correction from Light and finishing.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

Starts when it scrolls into view
attention
0.00
Open in /edit ↗
▸With no cursor and the slider at 0, you see the still thumbnail. Change 0.9 * near to 0.0 * near to stop reacting to the cursor

Tip

On the streaming-background page (/stage), you can fix attention to a value through the URL. Exporting and streaming are covered in Finish and share your demo.

08Exercises

When you finish each exercise, check that it also looks good with no cursor over it and the slider at 0.

Exercise 1

Lantern light

Light this screen-filling maze of curves only around the cursor. Give the light a slightly warm tint, and keep the unlit areas faintly visible. With no cursor, the center is lit.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

Starts when it scrolls into view
Open in /edit ↗
Exercise 2

Dots that flee

Make the dots in this grid shy away from the cursor: dots near it slide outward, away from it. The closer a dot, the farther it moves and the more its color changes. Dots must not leave their cells.

1

2

3

4

5

6

7

8

9

10

11

12

13

Starts when it scrolls into view
Open in /edit ↗
Exercise 3

A flower that blooms on hover

Turn the green bud into a flower whose five petals open as attention rises. Make the petals pink, with a yellow center that appears slightly later. With the slider at 0, it should still look nice as a bud.

1

2

3

4

5

6

7

8

9

10

11

Starts when it scrolls into view
attention
0.00
Open in /edit ↗

09Recap

  • mouse is the cursor position in p coordinates. It's (0, 0) before the cursor arrives, and often doesn't move on touch devices
  • length(p - mouse) lets you change things near the cursor: light, size, ripples. Measure from the cell center for per-cell changes
  • Shifting coordinates with mouse + dir * f pulls or pushes space
  • attention rises to 1 in about 0.3 seconds only while hovered in the Gallery; it's 0 on the work page and in the editor
  • Don't scale time by attention. Change strength, color, size or blend amounts instead, and stagger with smoothstep
  • The still thumbnail uses time 2, attention 0, mouse (0, 0). That state is your work's face

That wraps up the Patterns and noise part. From the next lesson on, we step into 3D. Using rays — a line of sight cast from the eye through each pixel — we'll draw scenes with depth. The distance functions, light and finishing touches from this part carry straight over into 3D.

Finished lessons get a mark in the course contents