FRENAR
N°06 · Canvas 2D · Generative

L-System

Formal grammar turned into trees, ferns and fractals. Three rewriting rules are enough to generate infinite morphogenetic complexity.

choose a preset · adjust angle and depth
// L-System — réécriture + interprétation turtle
const presets = [
  { axiom: 'F', rules: { F: 'F[-F]F[+F]F' }, angle: 25 },
  { axiom: 'X', rules: { X: 'F+[[X]-X]-F[-FX]+X', F: 'FF' }, angle: 25 },
  ...
];

function generate(axiom, rules, iterations) {
  let str = axiom;
  for (let i = 0; i < iterations; i++) {
    str = str.split('').map(c => rules[c] || c).join('');
  }
  return str;
}

function turtle(str, len, angle) {
  const stack = [];
  str.split('').forEach(c => {
    if (c === 'F') { ctx.lineTo(x += Math.cos(a)*len, y += Math.sin(a)*len); }
    else if (c === '+') a -= angle;
    else if (c === '-') a += angle;
    else if (c === '[') stack.push({x,y,a});
    else if (c === ']') ({x,y,a} = stack.pop());
  });
}
Your turn

At what depth does the tree become visually indistinguishable from a real plant? Try changing just the angle by 5° — how does the overall shape change?

Generate your own organic forms with L-Systems

Book a session