438

L-System Fractal Tree

Lindenmayer botanical growth grammar producing natural dendritic fractal trees with stochastic wind turbulence and harmonic terminal leaf clusters.

Playground
60 FPS • Canvas 2D
Click + Drag to interact with field
</>

Full Executable Algorithm Code


import type { ArtRenderer, ParameterState, RenderContext, TimeState } from '../../types/engine';
import { hsla } from '../common/color';

export function createFractalTree(): ArtRenderer {
  function drawBranch(
    ctx: CanvasRenderingContext2D,
    x: number,
    y: number,
    length: number,
    angle: number,
    depth: number,
    branchRatio: number,
    wind: number,
    time: number
  ) {
    if (depth <= 0) {
      // Draw terminal leaf cluster bud
      const leafHue = (110 + Math.sin(time + x * 0.01) * 30) % 360;
      ctx.fillStyle = hsla(leafHue, 85, 60, 0.85);
      ctx.beginPath();
      ctx.arc(x, y, 3.5, 0, Math.PI * 2);
      ctx.fill();
      return;
    }

    const endX = x + Math.cos(angle) * length;
    const endY = y + Math.sin(angle) * length;

    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(endX, endY);
    
    // Bark gradient from brown/amber at trunk to vibrant emerald at twigs
    const branchHue = depth > 5 ? 30 : 90 + depth * 8;
    ctx.strokeStyle = hsla(branchHue, 75, depth > 5 ? 35 : 55, 0.9);
    ctx.lineWidth = Math.max(1, depth * 1.5);
    ctx.stroke();

    const branchAngle = 0.42 + wind * 0.15;
    const nextLen = length * branchRatio;

    // Left branch
    drawBranch(ctx, endX, endY, nextLen, angle - branchAngle + wind * 0.08, depth - 1, branchRatio, wind, time);
    // Right branch
    drawBranch(ctx, endX, endY, nextLen, angle + branchAngle + wind * 0.08, depth - 1, branchRatio, wind, time);
  }

  return {
    setup() {},

    render(context: RenderContext, timeState: TimeState, params: ParameterState) {
      const { ctx, width, height } = context;
      const depth = Math.min(9, Number(params.branchDepth || 8));
      const branchRatio = Number(params.branchRatio || 0.72);
      const windSpeed = Number(params.windSpeed || 1.2);
      const t = timeState.time * windSpeed;

      ctx.fillStyle = '#06090c';
      ctx.fillRect(0, 0, width, height);

      const trunkX = width * 0.5;
      const trunkY = height * 0.92;
      const trunkLength = height * 0.22;

      // Harmonic wind gust oscillation
      const wind = Math.sin(t * 1.8) * 0.35 + Math.sin(t * 0.7) * 0.2;

      drawBranch(ctx, trunkX, trunkY, trunkLength, -Math.PI / 2, depth, branchRatio, wind, t);
    },
  };
}
Edit in Interactive Playground Zero Dependencies • Standalone Canvas 2D
ƒ

Mathematical Formulation

medium
Analytical Equation
Grammar: X→F[+X][−X]FX,pn+1=pn+Lrn[cos⁡(θ±Δθ+w)sin⁡(θ±Δθ+w)]\text{Grammar: } X \to F[+X][-X]FX, \quad \mathbf{p}_{n+1} = \mathbf{p}_n + L r^n \begin{bmatrix} \cos(\theta \pm \Delta\theta + w) \\ \sin(\theta \pm \Delta\theta + w) \end{bmatrix}
Click to expand
Compact Formula
branch(x, y, len * ratio, angle ± Δθ + wind, depth - 1)

Mathematical Tags

#tree #fractal #l-system #botany #nature #branching #leaves
Author: Math Art Core Target: 60 FPS

Export & Embed: L-System Fractal Tree

4K PNG Snapshot

High-resolution single frame render

WebM Video (5s Loop)

60 FPS browser-captured stream

Standalone JS Script

Complete executable Canvas 2D algorithm

HTML Iframe Embed

<iframe src="https://art.fazleyrabbi.xyz/embed/fractal-tree" width="500" height="500" frameborder="0" loading="lazy"></iframe>
ESC
↑↓ Navigate ↵ Select
113 Mathematical Artworks
Made by Fazley Rabbi
☕

Support the Project

Keep mathematical creative coding alive & open source

Or via Direct Payoneer ($0 Fee)
Payoneer Customer ID: $0 Fee Direct
24076084

💡 Payoneer app: Go to Pay → Pay to recipient → Enter ID 24076084.

Thank you for supporting generative mathematical animations! ✨