Grayson’s Song Animation  •  Stage 2  •  10/18/2026
TNT Processing — p5.js Creative Coding & Professional Practices

Stage 2 — Professional Refactoring

Same animation. Better code.
Five developer goals applied to Grayson’s original sketch — a model for how JavaScript should be organized, documented, and maintained.

✨  Stage 2 — Professional Refactoring

Five Goals Every Developer Should Have

Stage 2 applies five professional practices to Grayson’s original animation. The canvas output is identical to Stage 1 — the moon still drifts, the stars still twinkle, the central star still rotates and pulses. What changed is the code: every bare number has a name, every visual element has its own function, and every function has a document block explaining what it does. This is what production-quality code looks like.

  • 1
    Name Your Constants. Replace every bare number with a descriptive const. TWINKLE_SPEED = 0.05 explains itself; bare 0.05 does not. Anyone reading the code — including you, three months from now — immediately knows what to change to make the stars twinkle faster or slower.
  • 2
    One Function, One Job. draw() is now a six-line scene recipe. Each scene element (drawStars(), drawMoon(), etc.) lives in its own function. You can comment out drawMoon() and the moon disappears without touching any other code. That is testability in its simplest form.
  • 3
    Separate Constants From State. Constants (values that never change) and state variables (values that change every frame) are in clearly labeled sections. CW = 400 never changes. moonX changes 60 times per second. Keeping them separate makes both easier to reason about.
  • 4
    Document Every Function. A brief JSDoc block above each function answers: What does this do? and Why does it do it this way? The drawStars() comment explains the randomSeed() trick and the phase-offset technique — two ideas that are invisible without documentation.
  • 5
    Fix Your Bugs First. The typo "twinle" is corrected. The undeclared ndx in drawStar() is declared. The leftover comments are removed. These are small fixes — until the code grows, and they become invisible landmines. Make clean code a habit, not a cleanup task.
Compare with the original: Open Stage 1 in another tab and compare the greenbar code side by side. The animation is identical. The code is not.

    Live Animation — Stage 2

    Same output as Stage 1.
    The code is what changed.

    Original by Grayson •  Stage 2 refactoring by TNT •  p5.js •  ~220 lines including documentation Canvas output identical to Stage 1

    Why These Five Goals — and What Comes After

    The five goals in Stage 2 address the most common structural problems in novice code: magic numbers, monolithic functions, mixed-up concerns, missing documentation, and deferred bug fixes. They are not style preferences — they are the habits that separate code that is easy to change from code that is dangerous to touch. The S.P.A.R.K. Chat Log documents every decision: what was changed, why it was changed, and what each goal unlocks for the developer who makes it a habit.

    Stage 3 preview: Interactive controls — sliders for TWINKLE_SPEED, STAR_COUNT, and MOON_SPEED — so students can see the constants they named become parameters they can tune in real time. When a constant has a name, adding a slider is one line of code. When it is a bare number buried in a loop, it is a search-and-hope exercise.

    Creative Cross Training: Stage 2 is the foundation of the Grayson’s Song App Comparison — the same professionally organized sketch translated into Java Processing and Python Processing, side by side. The Stage 2 structure makes the three-language comparison meaningful: when every value is a named constant and every layer is a named function, exactly what changes between dialects becomes obvious — push()/pop() → pushMatrix()/popMatrix(), const → final type, let → global in Python. The Processing vocabulary — colorMode(), randomSeed(), sin(), map() — is identical across all three.