228

Barnsley Fern

Michael Barnsley's classic affine IFS fractal simulating the self-similar frond morphology of the Asplenium adiantum-nigrum black spleenwort fern.

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

Full Executable Algorithm Code

// 032 - Barnsley Fern (botany)
// 1:1 Original algorithm engine source
function createBarnsleyFern() {
  const SAMPLES_PER_FRAME = 3500;
  return {
    setup() {
    },
    render(context, timeState, params) {
      const { ctx, width, height } = context;
      const speed = Number(params.swaySpeed || 0.6);
      const t = timeState.time * speed;
      ctx.fillStyle = "rgba(6, 9, 10, 0.2)";
      ctx.fillRect(0, 0, width, height);
      let x = 0;
      let y = 0;
      const scale = Math.min(width, height) * 0.088;
      const cx = width * 0.5 + Math.sin(t * 1.2) * 12;
      const cy = height * 0.95;
      const sway = Math.sin(t * 1.5) * 0.04;
      for (let i = 0; i < SAMPLES_PER_FRAME; i++) {
        const r = Math.random();
        let nextX = 0;
        let nextY = 0;
        if (r < 0.01) {
          nextX = 0;
          nextY = 0.16 * y;
        } else if (r < 0.86) {
          nextX = 0.85 * x + (0.04 + sway) * y;
          nextY = -0.04 * x + 0.85 * y + 1.6;
        } else if (r < 0.93) {
          nextX = 0.2 * x - 0.26 * y;
          nextY = 0.23 * x + 0.22 * y + 1.6;
        } else {
          nextX = -0.15 * x + 0.28 * y;
          nextY = 0.26 * x + 0.24 * y + 0.44;
        }
        x = nextX;
        y = nextY;
        const px = cx + x * scale;
        const py = cy - y * scale;
        const hue = (115 + y / 10 * 45 + t * 10) % 360;
        ctx.fillStyle = hsla(hue, 90, 60, 0.7);
        ctx.fillRect(px, py, 1.2, 1.2);
      }
    }
  };
}

// Default parameters from content metadata
const defaultParams = [
  {
    "key": "swaySpeed",
    "label": "Stem Sway Speed",
    "type": "range",
    "min": 0.2,
    "max": 2,
    "step": 0.1,
    "defaultValue": 0.6,
    "description": "Harmonic leaf sway rate"
  }
];

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

const instance = window.__art_instances['barnsley-fern'];
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
fi(x)=[aibicidi]x+[eifi],P(f1)=0.01,P(f2)=0.85,P(f3)=0.07,P(f4)=0.07f_i(\mathbf{x}) = \begin{bmatrix} a_i & b_i \\ c_i & d_i \end{bmatrix} \mathbf{x} + \begin{bmatrix} e_i \\ f_i \end{bmatrix}, \quad P(f_1)=0.01, P(f_2)=0.85, P(f_3)=0.07, P(f_4)=0.07
Click to expand
Compact Formula
nextX = 0.85x + 0.04y, nextY = -0.04x + 0.85y + 1.6 (p = 85%)

Mathematical Tags

#fern #barnsley #ifs #fractal #botany #leaves #nature
Author: Math Art Core Target: 60 FPS

Export & Embed: Barnsley Fern

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/barnsley-fern" 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! ✨