Draw Hilbert Fractal
Generate the Hilbert space-filling curve. A continuous fractal curve that fills a square. 100% client-side.
Generate a Hilbert space-filling curve at up to 7 iterations of detail, with custom line color, background, and stroke width. The L-system expands the curve, the Canvas API draws it, and you download the result as a PNG. And everything runs in your browser. Nothing gets uploaded.
How It Works
The tool starts with the axiom "A" and recursively applies two production rules to build the curve's instruction string. Each "F" in the result means draw forward, "+" means turn left 90°, and "-" means turn right 90°. After expansion, the string is walked step by step on an HTML canvas. More iterations = more segments = finer detail. At iteration 5 you get 1,024 line segments; at iteration 7, that jumps to 16,384.
Examples
Quick Visual for a Math Presentation
You need a clean Hilbert curve image for a slide deck on fractal geometry. Nothing fancy — just clear enough to show the pattern.
Iterations: 4
Line Color: #1e40af
Background: #ffffff
Line Width: 2
256 line segments
Clean blue curve on white
PNG download ready
Dark-Mode Poster Art
Making a generative art print. You want a neon green curve on black at high detail for a large-format poster.
Iterations: 6
Line Color: #22c55e
Background: #000000
Line Width: 1
4,096 segments
Neon green on black
Tightly packed space-filling pattern
Low-Iteration Diagram for a Blog Post
Writing an article explaining space-filling curves to beginners. You need a simple iteration-2 curve where each turn is easy to follow.
Iterations: 2
Line Color: #dc2626
Background: #fef2f2
Line Width: 4
16 segments
Bold red U-shaped pattern
Easy to trace each turn by eye
Who Uses This
Generate Hilbert curve images at different iterations to show students how fractals approach dimension 2.
Create high-iteration curve renders with custom color schemes for prints, album covers, and digital art.
Grab a quick reference image when implementing Hilbert curve indexing for spatial databases or texture mapping.
Visualize L-system expansion and understand how recursive rewriting rules produce complex geometry.
Common Mistakes
At iteration 7, 16,384 tiny segments are crammed into the canvas. A line width of 3+ turns the whole thing into a solid blob. Use line width 1 for iterations 6 and 7.
A dark gray curve on a black background renders fine technically, but you can barely see it. Pick colors with strong contrast — the curve detail gets lost fast without it.
The download is a rasterized PNG from the canvas. If you scale it up for a large poster, it will pixelate. For vector output, you'd need to trace the curve path in an SVG editor separately.
Technical Details
- Processing: 100% client-side (Canvas API)
- Algorithm: L-system with axiom "A" and two production rules
- Max Iterations: 7 (16,384 segments)
- Output Format: PNG image download
- Browser Support: Chrome, Firefox, Safari, Edge
Frequently Asked Questions
What is a Hilbert curve used for in computer science?
Hilbert curves map 1D data to 2D space while preserving locality — points near each other on the curve stay near each other in the plane. That property makes them useful for spatial database indexing, texture mapping in graphics, and load balancing in parallel computing. Google's S2 geometry library uses a Hilbert curve variant for geo-indexing.
How many segments does each iteration produce?
The segment count follows 4^n. So iteration 1 gives you 4 segments, iteration 2 gives 16, iteration 3 is 64, and it ramps up from there. By iteration 7, you're at 16,384 line segments packed into the canvas.
What is the difference between a Hilbert curve and a Peano curve?
Both are space-filling curves, but they use different construction rules. The Peano curve came first (1890) and uses a 3×3 grid subdivision — segments grow by 9^n. The Hilbert curve (1891) subdivides into a 2×2 grid, giving 4^n growth. Hilbert curves are more commonly used in practice because the 2×2 structure maps cleanly to binary and computer memory.
Can I get SVG output instead of PNG?
Not directly. This tool renders onto an HTML canvas, which outputs raster PNGs. If you need a vector version, download the PNG as a reference and recreate the path in Inkscape or Illustrator. For programmatic SVG generation, you'd write a script that traces the same L-system path as SVG line elements.
Why does my high-iteration curve look like a solid square?
That's actually the point — a Hilbert curve is meant to fill a square completely as iterations approach infinity. At iteration 6 or 7 with a wide line, the gaps between segments disappear and it looks solid. Try reducing the line width to 1 or dropping to iteration 5 to see the individual path clearly.