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, 100); // Slight transparency for trailing effect
// Move origin to bottom center of canvas translate(width / 2, height);
// Animate branch angle with sine wave for dynamic effect let maxAngle = 30; let angle = maxAngle * sin(frameCount * 2);
// Start drawing the fractal tree with initial length and depth stroke(200, 150, 100); drawBranch(100, angle); }
// Recursive function to draw branches of the fractal tree function drawBranch(len, angle) { // Draw the current branch strokeWeight(map(len, 2, 100, 1, 6)); // Thicker branches at base stroke(120, 70, 20, map(len, 2, 100, 150, 255)); // Brownish color with opacity based on length line(0, 0, 0, -len);
// Move to the end of the branch translate(0, -len);
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.