Create a complete p5.js sketch that demonstrates: fractal tree Requirements: - Include…
- User
Create a complete p5.js sketch that demonstrates: fractal tree
Requirements:
- Include both setup() and draw() functions
- Use createCanvas(400, 400) to set canvas size
- Add comments explaining key concepts
- Make it visually engaging and educational
- Use appropriate colors and animations if relevant
- Keep code clean and readable
- Focus specifically on: fractal tree
IMPORTANT: Only output the JavaScript code, no explanations or markdown formatting. The code should be self-contained and runnable in the p5.js editor.
- Assistant
function setup() { createCanvas(400, 400); angleMode(DEGREES); // Use degrees for easier angle understanding frameRate(30); background(30, 30, 40); }
function draw() { background(30, 30, 40, 50); // Slight transparency for trailing effect
// Move origin to bottom center of canvas translate(width / 2, height);
// Animate the branching angle with a sine wave for dynamic effect let maxAngle = 30; let angle = maxAngle * sin(frameCount * 1.5);
stroke(120, 100, 40); // Brownish color for branches // Start drawing the fractal tree with initial length and depth drawBranch(100, angle); }
// Recursive function to draw branches of the fractal tree function drawBranch(len, branchAngle) { // Map branch length to stroke weight for natural tapering strokeWeight(map(len, 2, 100, 1, 8)); // Color changes from brown to green as branches get smaller let greenValue = map(len, 2, 100, 150, 50); stroke(50, greenValue, 30);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.