How to build a Perlin noise particle flow field in p5.js

Build an animated particle flow field in the free p5.js web editor, starting from a blank canvas and ending with hundreds of particles leaving coloured trails. It suits beginners who know a little JavaScript and want to try creative coding. No installs are needed, just a browser.

YouTube video: Generative Art with p5.js: Build a Particle Flow Field from Scratch

Caption availability is unknown. Check the CC control in the YouTube player.

14 chapters. Tap a step to jump to it.Chapters from the creator
Choose a chapter (14)
Video length
12 min 3 s
Time to do it
45 minutes
Difficulty
Medium
Steps
10

What you need

Steps

  1. Open the p5.js web editor in your browser and start a new sketch. Press play to run it. Everything runs online, so there is nothing to install.

  2. In setup(), call createCanvas() to set the canvas size and colorMode(HSB) for hue based colour. The draw() function then repeats on every frame.

  3. Draw with ellipse(), rect() and line(), and set fill() and stroke() before each shape. Remember that 0,0 sits at the top left corner.

  4. Use map() to turn mouseX and mouseY into hue and brightness values, so the sketch reacts when you move the mouse.

  5. Place shapes in a grid with nested for loops, stepping across the canvas width and height by a fixed spacing.

  6. Compare random() with noise(). random() jumps between values, while noise() returns smooth values that change gradually across space.

  7. Animate the noise by adding frameCount to the noise coordinates, or call noise(x, y, frameCount) for smooth 3D motion over time.

  8. Write a Particle class that stores a position vector and a velocity vector, with update() and display() methods for moving and drawing each particle.

  9. Fade the background each frame with a low alpha instead of clearing it fully. Old particle positions stay faint, so each particle leaves a trail.

  10. Sample noise at each particle position to get an angle, then steer the velocity along that angle. Press S to save a PNG with saveCanvas().

Tips and common mistakes

Start with a few hundred particles, since thousands with long trails can drop the frame rate.

Lower the background alpha for longer trails and raise it for shorter, sharper ones.

Press C to clear the canvas and S to save your artwork as a PNG in the finished sketch.

Change SCALE, NUM and the speed limit to make your own variations of the flow field.

Questions

Do I need to install anything to follow along?

No. The p5.js web editor runs in your browser, and a free account lets you save and reopen your sketches.

Why do my particles sit still on the canvas?

Check that you add velocity to position inside update() every frame, and that your noise angle actually changes across the canvas.

What is the difference between random() and noise()?

Random() gives a new jumpy value each call, while noise() returns smooth values that vary gradually, which is what makes flow fields look organic.

The notes are drafted automatically from video details and general task knowledge. How the guide was prepared

Report a correction

More in Art & Design

See all