FUN INTERACTIVE
PARTICLE SYSTEM

By Shane Brumback · · Updated · Free Three.js tutorial · Level 1

To make an interactive particle system in Three.js, give every particle a home position and a velocity, then each frame pull it toward home with a spring and push it away from the mouse. Here 160 glowing smiley sprites scatter from your cursor or finger, wiggle when they get scared, and drift back into a cloud.

A beginner walkthrough of Sprite particles, a smiley face drawn on a CanvasTexture, pointer raycasting and simple spring physics in Three.js. Move your mouse over this page: the smileys behind it are the live demo.

✓ FREE Level 1 · Beginner Sprites CanvasTexture Mouse & touch Spring physics
See it live

LIVE DEMO

The smiley cloud behind this page is a live Three.js scene. Launch the full-screen demo to chase the smileys with your mouse or finger and click or tap to blast them apart. Launch asks for a free Google sign-in first. Press Esc or Exit to return.

Free Google sign-in · Move to scatter · Click to blast · Esc to exit

What Is a Particle System?

A particle system is a crowd of small, simple things that move together to make one effect: sparks, snow, smoke, stars, or in this case a cloud of happy faces. Each particle is cheap on its own. The magic comes from giving every one of them the same few rules and letting them run.

Three.js is a JavaScript library that draws 3D graphics in the browser with WebGL. It gives you several ways to draw particles. This tutorial uses THREE.Sprite, a flat picture that always turns to face the camera, so a smiley never looks edge-on. For tens of thousands of dots you would switch to THREE.Points, which the simple particle system tutorial covers.

The Three Rules That Make It Feel Alive

1. Go home. Every smiley remembers where it was born. A spring pulls it back there, harder the farther away it is.

2. Run from the hand. We turn your mouse or finger into a point in the 3D world. Any smiley closer than 2.6 units gets pushed straight away from it, and the push gets stronger the closer you are.

3. Calm down. Damping slowly drains speed every frame, so smileys glide to a stop instead of bouncing forever. Add a little wobble and a scared wiggle, and a few lines of math feel like a living swarm. Want them to pop and score points instead? That is the interactive smiley particle game.

How It Works

  1. Set up the scene, camera and renderer. We make an empty dark 3D world, a camera looking at it and a renderer that draws it onto the page.
  2. Draw a smiley face texture on a canvas. Instead of loading an image file, we paint a white smiley with a soft glow on a tiny hidden canvas and use it as a picture.
  3. Create the smiley sprites with a home position. We make 160 smileys in seven bright colors, scatter them in a ball shape, and let each one remember its home spot.
  4. Turn the mouse or finger into a 3D point. We listen for the pointer and remember where it is on screen, so later we can shoot a ray from the camera through it.
  5. Click or tap to blast the cloud apart. A click finds the 3D spot under your finger and kicks every nearby smiley away from it.
  6. Spring home, flee the hand and wiggle. Each frame every smiley is pulled home, pushed away from your hand if it is close, slowed down a little, and wiggles more when it is scared.
  7. Run the render loop. Every frame we move time forward, sway the whole cloud, update the smileys and draw the picture.

The detailed code breakdown and the full copy-paste file are below. Every step is an exact excerpt of the full file, and the full file is the same code as the live demo.

When no pointer is over the page, the hand point takes a slow figure-eight path so the cloud keeps moving on its own. Next, try thousands of dots with a simple particle system, or bounce particles off a moving collider in exploring interactive visual particle systems.

Frequently Asked Questions

What is a particle system in Three.js?

A particle system is many small objects, such as sprites or points, that follow the same simple rules to create one effect like sparks, snow, stars or a swarm. In Three.js you can build one with Sprite objects, a Points cloud or an InstancedMesh, and update their positions every frame.

How do you make particles react to the mouse in Three.js?

Convert the pointer to normalized device coordinates, use Raycaster.setFromCamera to shoot a ray through it, and intersect that ray with a plane to get a 3D point. Each frame, push any particle closer than a set radius away from that point.

Why do the smileys spring back to their place?

Each particle stores a home position. Every frame a spring force proportional to the distance from home is added to its velocity, and damping removes a little speed, so it glides back and settles instead of flying away.

Should I use Sprite or Points for particles?

Sprites are easy and let every particle have its own color, size and rotation, which is great for a few hundred. Points draws thousands or millions in one draw call but shares one material, so use it for big clouds of dots.

Does the demo work on phones?

Yes. Pointer events cover mouse, pen and touch, so dragging a finger scatters the smileys and a tap blasts them. The demo also uses fewer particles on small screens to stay smooth.

Why do I need to sign in to launch the live demo?

The tutorial and its full code are free to read. The full-screen Live Demo asks for a free Google sign-in first, the same gate used across the site.

Play It and Keep Learning

Particles, sprites and pointer input like this power the effects in Super Soldier Battle, a free Three.js browser game. Browse every Three.js tutorial, read the articles, or search the site.

Need Three.js or WebGL work for your site or game? I take freelance and contract work: contact me.

Play Game