p5.js
Ember Tree
A tree drawn by one rule, repeated a thousand times: branch, split, branch again. Embers glow at the tips and the whole canopy sways in a slow wind. Drag Depth to grow it older, Spread to open it up.
Licence: CC BY-NC-SA 4.0: remix with credit, non-commercial, share alike
View the code
// Ember Tree — a recursive fractal tree in p5.js
// One trunk, thousands of branches, embers glowing at the tips.
// The whole tree breathes in a slow wind. Palette: charcoal, bone, signal orange.
// Click the canvas for a new tree · Press S to save a frame
// Live sliders: Spread (branch angle) · Depth (recursion) · Wind · Embers
const BONE = [233, 229, 218];
const ORANGE = [255, 180, 84];
const BG = [13, 13, 18];
let seedJitter = 0;
let sSpread, sDepth, sWind, sEmber, uiBox;
function setup() {
createCanvas(windowWidth, windowHeight);
buildControls();
}
function buildControls() {
uiBox = createDiv();
uiBox.style('position', 'absolute');
uiBox.style('left', '14px');
uiBox.style('background', 'rgba(13,13,18,0.55)');
uiBox.style('padding', '10px 12px');
uiBox.style('border-radius', '10px');
uiBox.style('font-family', "'JetBrains Mono', monospace");
uiBox.style('color', '#e9e5da');
uiBox.style('z-index', '5');
const row = (label, slider) => {
const r = createDiv();
r.parent(uiBox);
r.style('display', 'flex');
r.style('align-items', 'center');
r.style('gap', '10px');
r.style('margin', '3px 0');
const lab = createSpan(label);
lab.parent(r);
lab.style('font-size', '11px');
lab.style('width', '52px');
lab.style('letter-spacing', '0.05em');
slider.parent(r);
slider.style('width', '130px');
};
sSpread = createSlider(10, 42, 19, 0.5); row('Spread', sSpread);
sDepth = createSlider(6, 12, 10, 1); row('Depth', sDepth);
sWind = createSlider(0, 3, 1, 0.1); row('Wind', sWind);
sEmber = createSlider(0, 10, 5, 0.5); row('Embers', sEmber);
placeControls();
}
function placeControls() {
if (uiBox) uiBox.position(14, height - 138);
}
function draw() {
background(...BG);
const maxDepth = sDepth.value();
const spread = radians(sSpread.value()) + seedJitter;
const windBase = (noise(frameCount * 0.002) - 0.5) * 0.22 * sWind.value();
noStroke();
fill(ORANGE[0], ORANGE[1], ORANGE[2], 10);
ellipse(width / 2, height, width * 0.9, height * 0.22);
push();
translate(width / 2, height);
branch(height * 0.20, maxDepth, maxDepth, spread, windBase);
pop();
}
function branch(len, depth, maxDepth, spread, windBase) {
const frac = depth / maxDepth;
const cr = lerp(ORANGE[0], BONE[0], frac * 0.75);
const cg = lerp(ORANGE[1], BONE[1], frac * 0.75);
const cb = lerp(ORANGE[2], BONE[2], frac * 0.75);
stroke(cr, cg, cb, 235);
strokeWeight(map(depth, 0, maxDepth, 0.7, 5.2));
line(0, 0, 0, -len);
translate(0, -len);
if (depth === 0) {
const e = sEmber.value();
if (e > 0) {
noStroke();
const flicker = 0.75 + 0.25 * sin(frameCount * 0.11 + len * 12.9898);
fill(ORANGE[0], ORANGE[1], ORANGE[2], 60 * flicker);
ellipse(0, 0, e * 3.4);
fill(255, 214, 150, 230 * flicker);
ellipse(0, 0, e * 1.15);
}
return;
}
const sway = windBase * (1.15 - frac) * sin(frameCount * 0.012 + depth * 1.7);
push();
rotate(spread + sway);
branch(len * 0.75, depth - 1, maxDepth, spread, windBase);
pop();
push();
rotate(-spread * 0.92 + sway);
branch(len * 0.75, depth - 1, maxDepth, spread, windBase);
pop();
}
function mousePressed(e) {
if (e && e.target && e.target.tagName !== 'CANVAS') return;
seedJitter = random(-0.06, 0.06);
}
function keyPressed() {
if (key === 's' || key === 'S') saveCanvas('ember-tree', 'png');
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
placeControls();
}