228

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

// 031 - L-System Fractal Tree (botany)
// 1:1 Original algorithm engine source
function createFractalTree() {
  function drawBranch(ctx, x, y, length, angle, depth, branchRatio, wind, time) {
    if (depth <= 0) {
      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);
    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;
    drawBranch(ctx, endX, endY, nextLen, angle - branchAngle + wind * 0.08, depth - 1, branchRatio, wind, time);
    drawBranch(ctx, endX, endY, nextLen, angle + branchAngle + wind * 0.08, depth - 1, branchRatio, wind, time);
  }
  return {
    setup() {
    },
    render(context, timeState, params) {
      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;
      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);
    }
  };
}

// Default parameters from content metadata
const defaultParams = [
  {
    "key": "branchDepth",
    "label": "Recursion Depth",
    "type": "range",
    "min": 4,
    "max": 9,
    "step": 1,
    "defaultValue": 8,
    "description": "Branching recursion levels"
  },
  {
    "key": "branchRatio",
    "label": "Branch Length Ratio",
    "type": "range",
    "min": 0.55,
    "max": 0.85,
    "step": 0.02,
    "defaultValue": 0.72,
    "description": "Child branch scaling factor"
  },
  {
    "key": "windSpeed",
    "label": "Wind Gust Turbulence",
    "type": "range",
    "min": 0.2,
    "max": 2.5,
    "step": 0.1,
    "defaultValue": 1.2,
    "description": "Harmonic wind velocity"
  }
];

if (!window.__art_instances) window.__art_instances = {};
if (!window.__art_instances['fractal-tree']) {
  const inst = typeof createFractalTree === 'function' ? createFractalTree() : null;
  if (inst && inst.setup) {
    inst.setup({ ctx, width, height, dpr: 1, aspectRatio: width / height }, defaultParams);
  }
  window.__art_instances['fractal-tree'] = inst;
}

const instance = window.__art_instances['fractal-tree'];
if (instance && instance.render) {
  instance.render(
    { ctx, width, height, dpr: 1, aspectRatio: width / height },
    { time, deltaTime: dt, frameCount: Math.floor(time * 60), fps: 60 },
    defaultParams
  );
}
Edit in Interactive Playground Zero Dependencies • Standalone Canvas 2D
ƒ

Mathematical Formulation

medium
Analytical Equation
Grammar: XF[+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
110 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! ✨