The circle is the perfect shape because it never ends. In creative coding, the requestAnimationFrame loop is our heartbeat. It pulses 60 times a second (or 120, or 144), giving us a canvas that is never static, always becoming.
Writing a recursive function is a leap of faith. You define the base case (the exit), and then you call the function from within itself, trusting that it will eventually resolve.
const MAX_DEPTH = 6;
function drawBranch(x, y, len, angle, depth) {
if (depth > MAX_DEPTH) return;
// Calculate end point
const endX = x + len * Math.cos(angle);
const endY = y + len * Math.sin(angle);
// Draw the line
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(endX, endY);
ctx.stroke();
// Recursive calls
const newLen = len * 0.67;
drawBranch(endX, endY, newLen, angle + Math.PI / 4, depth + 1);
drawBranch(endX, endY, newLen, angle - Math.PI / 4, depth + 1);
}
There is a flow state that comes from watching these systems emerge. You tweak a variable—the angle, the length, the decay rate—and the entire structure transforms. It's gardening, not engineering.
The loop is not just a programming construct; it's a philosophy.
Structured Chaos / Reflection