Every project / Art & Animation

🌌 Warp Speed

Fly through a 3D starfield. Move the mouse to steer, hold to go to lightspeed.

Warp Speed running
Language
HTML, CSS & JavaScript
Level
Intermediate
Category
Art & Animation
ā–¶ Try it & remix

What you'll learn

  • 3D perspective projection
  • Animation loops
  • Mouse position
  • Easing
  • Drawing lines

How it works

  • Each star has x, y and z, where z is how far away it is. Every frame z shrinks, so stars fly toward you.
  • Perspective: a star's screen position is x Ɨ (300 / z), so near stars spread out quickly.
  • Each star draws a line from where it was to where it is now. At high speed those lines become streaks.
  • Holding the mouse eases the speed up to lightspeed. Moving it shifts the point the stars fly out of.

Knobs to turn

These settings are near the top of the code. Remix it, change one, and watch what happens. The preview updates as you type.

SettingStarts atFileWhat it does
STAR_COUNT600stars.jshow many stars
DEPTH1000stars.jshow far away stars start
CRUISE4stars.jsnormal speed
LIGHTSPEED45stars.jsspeed while holding the mouse

Files

FileLines
index.html19the page: what shows up
stars.js79what it does

Challenges

Each one is a bit harder than the last. Teachers: these make good assignment instructions.

  1. Change the number of stars and their color.
  2. Make some stars bigger and brighter than others.
  3. Show your current speed on screen, like a spaceship dashboard.
  4. Add planets that fly past once in a while.
  5. Make pressing the arrow keys steer instead of the mouse.