📂 The Stage 2 Files
Stage 2 is a refactoring stage: the rocket looks exactly the same as Stage 1 (click “Stage 1” in the card footer to compare), but the code has been completely reorganized. Three files make up the Stage 2 build:
| File | What it does | Stage 2 changes |
|---|---|---|
rocketStg2.html |
HTML shell — loads p5.js, CSS, and the sketch | File header & title added; stylesheet pointer updated |
rocketStg2Sketch.js |
The p5.js sketch — all drawing logic | Full professional overhaul (see Laundry List) |
rocket2Styles.css |
Minimal CSS reset — canvas flush, no margin | Unchanged from Stage 1 (intentionally minimal) |
fetch(url).then(r => r.text()).
In Stage 1,
random(width) and
random(height) were called inside draw()
every frame. That means every star gets a new random position
60 times per second — producing both a twinkle effect
and an unintended slow drift across the screen.In Stage 2, star positions are calculated once in
setup() via generateStars() and stored in an
array of objects ({ x, y, size }).
Each frame, drawStars() reads the fixed
x / y from the array and only
re-randomizes the alpha (opacity) — so stars
twinkle but never move.Bonus: the spread operator (
fill(...CLR_STAR, alpha)) expands the
[255, 255, 255] array into three separate
arguments, letting us append a per-call alpha without writing
fill(CLR_STAR[0], CLR_STAR[1], CLR_STAR[2], alpha)
every time.
📜 Source Code
Loading rocketStg2.html…
Loading rocketStg2Sketch.js…
Loading rocket2Styles.css…
💪 Try It Yourself
Use the Copy button above to grab any file, then:
- Create a new project folder with
sketches/andstyles/sub-folders. - Paste each file into VS Code and save it with the matching filename.
- Open
rocketStg2.htmlin your browser — you should see Griffin’s rocket! -
Pick one constant to change — try setting
STAR_COUNT = 300or changingCLR_ACCENTto[100, 255, 100]for green fins — and observe the effect. -
Challenge: add a
ROCKET_SPEEDconstant and make the rocket climb slowly up the canvas. Hint: use alet rocketYvariable in the DATA section and decrement it indraw().
⚠️ The Copy button requires a secure context
(https:// or localhost). If you’re viewing this file directly
from your file system (file://), right-click the
Run link above and choose “View Page Source” instead.