There’s something truly magical about watching mathematics come to life. Not in a dusty classroom or a dense textbook—but glowing, swirling, and evolving right on a digital canvas.
Recently, I stepped away from the LeetCode grind to build a generative animation using p5.js. It turns pure trigonometry into an organic, living form. Honestly? It’s way more rewarding than solving another "Invert a Binary Tree" problem.
🎨 The Vision: Code That Breathes
Instead of drawing static, predefined shapes, this animation generates thousands of points per frame. Each one is positioned using a cocktail of trigonometry, wave functions, and distance calculations.
The logic follows four simple pillars:
-
Oscillation: Using
cosandsinwaves to create movement. - Transformation: Using polar-style coordinates to rotate structures.
- Temporal Flow: Using a time variable ($t$) to animate the evolution.
- Atmosphere: Using HSB color mode to make the "organism" glow.
🧠The Mathematical Heart
At the center of this project is a single function. This is where the "DNA" of the organism lives.
function organism(x, y) {
// 1. Create layered wave fields
let k = 5 * cos(x / 14) * cos(y / 30);
let e = y / 8 - 13;
// 2. Calculate radial distance (The "Stretch")
let d = pow(mag(k, e), 2) / 59 + 4;
// 3. Create rotational symmetry
let angleTerm = atan2(k, e);
let q = 60 - 3 * sin(angleTerm * e);
// 4. Introduce the 'Pulse'
let wave = k * (3 + 4 / d * sin(d * d - t * 2));
let c = d / 2 + e / 99 - t / 18;
// 5. Map to Cartesian coordinates
let xCoord = (q + wave) * sin(c) + 200;
let yCoord = (q + d * 9) * cos(c) + 200;
// 6. Dynamic HSB Coloring
let hue = (200 + d * 15 + t * 40) % 360;
stroke(hue, 90, 80, 80);
point(xCoord, yCoord);
}
Why this works:
-
Wave Interference:
cos(x / 14)andcos(y / 30)create complex, overlapping patterns. - Distance Distortion: We calculate a radial distance $d$ using vector magnitude—this controls how far points stretch outward.
-
Angular Flow:
atan2(k, e)creates the swirling motion that makes it feel like a vortex. -
Time Evolution: By incrementing $t$ slightly every frame (
t += PI / 20), the entire structure shifts smoothly—it looks like it's breathing.
🌈 Why HSB is a Game Changer
Most of us start with RGB, but for generative art, HSB (Hue, Saturation, Brightness) is king.
colorMode(HSB, 360, 100, 100, 100);
By linking the hue to both distance ($d$) and time ($t$), the shape doesn't just move—it evolves. The colors cycle through the spectrum, making the animation look like a digital jellyfish or a cosmic mandala.
💻 Why This Is More Fun Than DSA
Data Structures and Algorithms are the foundation of our craft. But watching math create beauty in real-time hits differently.
When you experiment with this code, you aren't just "solving a problem"—you are discovering forms.
- Change the wave frequency? New species.
- Adjust the time speed? Different heartbeat.
- Modify the hue shift? New dimension.
The Lesson: Math isn't just calculation. It’s motion, rhythm, and symmetry. Sometimes, the most beautiful things in software come from a few lines of trigonometry and a blank canvas.
🚀 Try It Yourself
If you're feeling burned out by tutorials:
- Open the p5.js Web Editor.
- Paste the logic above into the
draw()loop. -
Break it. Change
10000points to20000. Swap asinfor atan.
What does your organism look like? Let me know in the comments!
Top comments (0)