Here’s a single‐page HTML “Tree Root Growth Visualiser” that lets you pick Willow, Redwood or Acacia and watch their roots grow fractally. Just save this as index.html and open in your browser:
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tree Root Growth Visualiser</title>
<style>
body {
margin: 0;
padding: 0;
background: #f0f4f8;
font-family: sans-serif;
display: flex;
flex-direction: column;
height: 100vh;
overflow: hidden;
}
header {
padding: 10px 20px;
background: #406882;
color: #fff;
display: flex;
align-items: center;
gap: 12px;
}
header select, header button {
font-size: 1rem;
padding: 4px 8px;
}
#canvas {
flex: 1;
display: block;
background: #f0f4f8;
}
</style>
</head>
<body>
<header>
<label for="treeSelect">Choose tree:</label>
<select id="treeSelect">
<option value="willow">Willow</option>
<option value="redwood">Redwood</option>
<option value="acacia">Acacia</option>
</select>
<button id="regen">Regenerate</button>
</header>
<canvas id="canvas"></canvas>
<script>
// --- Configuration for each tree type ---
const treeTypes = {
willow: {
trunkLength: 120,
trunkWidth: 6,
depth: 8,
branchCount: [3, 5],
angleRange: [20, 60],
lengthFactor: [0.6, 0.85],
rootColor: "#5A3E36"
},
redwood: {
trunkLength: 150,
trunkWidth: 12,
depth: 6,
branchCount: [2, 3],
angleRange: [15, 45],
lengthFactor: [0.7, 0.9],
rootColor: "#4B2E27"
},
acacia: {
trunkLength: 100,
trunkWidth: 5,
depth: 7,
branchCount: [3, 5],
angleRange: [10, 80],
lengthFactor: [0.5, 0.8],
rootColor: "#6D4C41"
}
};
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
let segments = [];
function resizeCanvas(){
canvas.width = window.innerWidth;
canvas.height = window.innerHeight - document.querySelector("header").offsetHeight;
}
window.addEventListener("resize", resizeCanvas);
resizeCanvas();
function randRange([min, max]){
return min + Math.random() * (max - min);
}
// Generate fractal root segments into `segments` array
function generate(x, y, length, angle, width, depth, cfg){
if(depth === 0) return;
// Compute end point
const x2 = x + Math.cos(angle) * length;
const y2 = y + Math.sin(angle) * length;
segments.push({ x, y, x2, y2, width, color: cfg.rootColor });
// branch
const count = Math.floor(randRange(cfg.branchCount));
for(let i=0; i<count; i++){
const newLen = length * randRange(cfg.lengthFactor);
const deltaDeg = randRange(cfg.angleRange);
const newAng = angle + (Math.random()<0.5 ? 1:-1) * deltaDeg * Math.PI/180;
const newWid = width * 0.7;
generate(x2, y2, newLen, newAng, newWid, depth-1, cfg);
}
}
function animateDraw(){
let i = 0;
function step(){
if(i >= segments.length) return;
const s = segments[i++];
ctx.beginPath();
ctx.moveTo(s.x, s.y);
ctx.lineTo(s.x2, s.y2);
ctx.lineWidth = s.width;
ctx.strokeStyle = s.color;
ctx.stroke();
requestAnimationFrame(step);
}
step();
}
function drawRoots(){
const choice = document.getElementById("treeSelect").value;
const cfg = treeTypes[choice];
ctx.clearRect(0, 0, canvas.width, canvas.height);
// starting point: middle top of soil line
const startX = canvas.width / 2;
const startY = 20;
segments = [];
generate(
startX, startY,
cfg.trunkLength,
Math.PI/2, // downwards
cfg.trunkWidth,
cfg.depth,
cfg
);
// shuffle for a more organic draw order
segments.sort(() => Math.random() - 0.5);
animateDraw();
}
// Initial draw
drawRoots();
document.getElementById("regen").onclick = drawRoots;
document.getElementById("treeSelect").onchange = drawRoots;
</script>
</body>
</html>