webdevpuneet.com

Scroll Canvas Starfield Warp Speed — Perspective-Projected Canvas2D Stars

Scroll Canvas Starfield Warp Speed · Scroll · Plain HTML, CSS & JS · Live preview

Copy & paste

Source Code

<section class="wsp-stage" id="wspStage">
  <div class="wsp-intro-overlay"><p>Scroll ↓ to jump to warp speed</p></div>
  <canvas id="wspCanvas"></canvas>
  <div class="wsp-hud"><span id="wspPct">0</span>% warp</div>
</section>
<section class="wsp-bottom"><p>A perspective-projected starfield, accelerating with your scroll.</p></section>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Hand-rolled perspective divide projects 3D star coordinates to 2D — no 3D engine
Motion streaks emerge from drawing a line between each star’s previous and current projected position
Fading background overlay leaves faint ambient trails instead of clearRect
Flat typed-array star state allocated once — zero per-frame allocation in the render loop
Stars recycle automatically when they pass the camera or leave the visible frame
Speed scales with warp progress squared for a gentle-drift-then-explosive-warp pacing curve
GSAP ScrollTrigger pins the stage and scrubs warp intensity from drift to lightspeed
Fully reversible — scrolling up decelerates smoothly back to a slow drift

About this UI Snippet

How to Build a Scroll-Driven Starfield Warp-Speed Effect With Canvas2D

Screenshot of the Scroll Canvas Starfield Warp Speed snippet rendered live

The Scroll Canvas Starfield Warp Speed snippet places several hundred stars in 3D space around a fixed camera, projects each one to 2D with a classic perspective divide, and streaks them into motion trails whose length grows with scroll-driven speed — the entire "jump to lightspeed" effect from a handful of Canvas2D primitives and no 3D engine.

3D coordinates, projected by hand

Each star has a fixed (x, y) position on a plane and a z depth that decreases every frame, simulating the camera flying forward through the field. The 2D screen position is computed with a textbook perspective-divide formula: screenX = centerX + x * (focalLength / z) — as z shrinks toward the camera, the same fixed x maps to an increasingly large offset from center, which is exactly why stars appear to radiate outward as they approach.

Streaks from cached previous positions, not a particle trail buffer

Rather than maintaining a history buffer of recent positions per star, each star simply remembers its *previous frame's* projected screen coordinates (prevScreenX/prevScreenY) and draws one line segment from there to its current position every frame. At low speed the segment is imperceptibly short and reads as a dot; at warp speed the segment stretches into a long streak — the streak length is an emergent property of how far the star moved between two frames, not something separately animated.

Fading background instead of clearRect for an ambient tail

Painting a low-alpha dark rectangle over the canvas each frame (rather than fully clearing it) leaves the faintest ghost of the previous few frames' streaks, reinforcing the sense of velocity without needing a second rendering pass — the same trail technique used in Scroll Canvas Generative Flow Field.

Recycling stars that pass the camera or leave frame

When a star's z drops below a small threshold (it has "passed" the camera) or its projected screen position leaves the visible canvas bounds, it is reseeded at a fresh random (x, y) and a far z, ready to fly in again — keeping the star count constant without ever resizing the underlying typed arrays.

Speed as a squared function of scroll progress

speed = 2 + warp * warp * 46 uses warp squared rather than linear, so the field stays a gentle, slow drift for the first portion of the scroll and only truly explodes into warp streaks in the final stretch — a deliberate pacing choice, not an accident of the perspective math.

Customizing it

Change focalLength for a wider or narrower field of view, adjust the speed formula's exponent and multiplier for a different acceleration curve, or tint the star and streak colors for a different palette. Pair it with Scroll Canvas Generative Flow Field for a related Canvas2D particle-motion technique.

Step by step

How to Use

  1. 1
    Load the CDN scriptsAdd gsap.min.js and ScrollTrigger.min.js from the CDN panel, in that order.
  2. 2
    Paste HTML, CSS, and JSA slow-drifting starfield renders inside a pinned canvas stage with a live "% warp" read-out.
  3. 3
    Scroll downStars accelerate and streak dramatically outward as scroll-driven speed climbs.
  4. 4
    Scroll back upThe field decelerates smoothly back to a slow drift exactly in reverse.
  5. 5
    Retune the acceleration curveAdjust the speed formula’s exponent and multiplier for a gentler or more explosive warp.
  6. 6
    Change the field of viewIncrease or decrease focalLength for a narrower or wider-feeling starfield.

Real-world uses

Common Use Cases

Space game and sci-fi landing pages
A classic "jump to warp" moment for game trailers, sci-fi product pages, or space-themed brands.
Generative art and creative-coding portfolios
Demonstrate perspective-projection technique as a standalone Canvas2D piece.
Loading or transition screens
A dramatic accelerating starfield suits a full-screen loading or chapter-transition moment.
Teaching perspective projection without a 3D engine
A compact real example of the classic focalLength / z projection formula in plain Canvas2D.
Product launch or countdown pages
Pair the warp acceleration with a countdown reveal for a launch-moment landing page.
Related: Scroll Canvas Generative Flow Field
See Scroll Canvas Generative Flow Field for a related Canvas2D scroll-driven particle technique.

Got questions?

Frequently Asked Questions

Each star has a fixed x/y position and a z depth. The screen position is computed with screenX = centerX + x * (focalLength / z) — a classic perspective-divide formula. As z shrinks (the star approaches the camera), the same x maps to a larger offset from center, which is exactly why stars radiate outward as they get closer.

Each star only remembers its previous frame’s projected screen position and draws a single line segment from there to its current position every frame. At low speed that segment is nearly a point; at warp speed it stretches into a long visible streak — the streak length is a natural byproduct of how far the star moved between two frames.

speed = 2 + warp * warp * 46 keeps the field a gentle, slow drift through most of the scroll range and reserves the dramatic streaking acceleration for the final stretch, since squaring a 0-to-1 value grows much faster near 1 than near 0 — a deliberate pacing choice for a more satisfying "jump to warp" moment.

When a star’s z depth drops below a small threshold, or its projected screen position leaves the visible canvas bounds, it is immediately reseeded at a fresh random x/y position and a far z depth, so the total star count stays constant and the field looks continuously replenished.

A low-alpha dark rectangle painted over the canvas each frame only partially erases the previous frame, leaving the faintest ghost of recent star streaks behind. This reinforces the sense of velocity at high warp speed without needing a second, separate trail-rendering pass.