Fractals are self-similar patterns — meaning if you zoom in on a fractal, you will keep seeing similar shapes repeating infinitely.
To create fractals, we use recursion and a drawing tool — usually HTML Canvas.
<canvas>✅ HTML
<canvas id="canvas" width="600" height="500"></canvas> ✅ JavaScript
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
function drawTree(x, y, length, angle, branchWidth) {
ctx.lineWidth = branchWidth;
ctx.beginPath();
ctx.moveTo(x, y);
const x2 = x + length * Math.cos(angle);
const y2 = y + length * Math.sin(angle);
ctx.lineTo(x2, y2);
ctx.stroke();
if (length < 10) return; // stop recursion
// branches
drawTree(x2, y2, length * 0.75, angle + 0.5, branchWidth * 0.7);
drawTree(x2, y2, length * 0.75, angle - 0.5, branchWidth * 0.7);
}
// draw
ctx.strokeStyle = "brown";
drawTree(300, 500, 100, -Math.PI / 2, 10);
| Fractal | Technique |
|---|---|
| Mandelbrot set | Complex numbers + iteration |
| Koch snowflake | Triangles + recursion |
| Sierpinski triangle | Triangles subdividing |
| Fractal clouds | Noise algorithms |
Fractals are not just math — they’re art, nature, and computer graphics blended together. With JavaScript and a little recursion, you can generate infinite beauty from simple rules.
Latest tech news and coding tips.
What is Rate Limiting? Download this article as a PDF on the Codeflare Mobile App…
Learn on the Go. Download the Codeflare Mobile from iOS App Store. 1. What is…
Download the Codeflare iOS app and learn on the Go 1. What UI and UX…
1. Running Everything as Root One of the biggest beginner errors. Many new users log…
A keylogger is a type of surveillance software or hardware that records every keystroke made…
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…