Staaarter

Generate a Sierpinski Carpet

Generates the classic Sierpinski carpet, the two-dimensional generalization of the Cantor set, by recursively dividing a square into a 3x3 grid and removing the center cell at every level, rendering the result as downloadable SVG filled-shape art. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-08-05
By Staaarter Team
fractalgeneratorsvg

Overview

Introduction

The Sierpinski carpet is the classic square-grid counterpart to the Sierpinski triangle, a square that, when you recursively remove the center of every remaining 3x3 sub-grid, approaches a shape with zero area but infinite detail. This tool renders it as filled, downloadable SVG shapes.

Enter a depth and the tool recursively subdivides a starting square that many levels, discarding the center cell of every 3x3 sub-grid at each step, then draws every surviving smallest cell as a filled square.

What Is Generate a Sierpinski Carpet?

A real, classic fractal first described by Wacław Sierpiński, formed by dividing a square into a 3x3 grid, removing the single center cell, and repeating that same division-and-removal rule inside each of the eight remaining cells.

It's the direct two-dimensional generalization of the one-dimensional Cantor set, both fractals follow the same "divide into thirds, discard the middle, recurse into what's left" logic, just applied to a line versus a square.

How Generate a Sierpinski Carpet Works

Starting from one square, the generator divides it into a 3x3 grid of nine equal sub-squares.

The center sub-square (row 1, column 1 of the 3x3 grid) is discarded, and the surrounding eight sub-squares are recursed into using the same rule, down to the depth you enter.

At the deepest level, every surviving sub-square is emitted as a filled rectangle; those rectangles are then rescaled to fit the SVG viewbox and rendered together as the finished carpet.

When To Use Generate a Sierpinski Carpet

Use it whenever you want the classic filled Sierpinski carpet look, for generative art, educational diagrams, textile or tiling patterns, or any design needing a recognizable recursive-square motif.

It's a clear teaching example of a two-dimensional Cantor-set-style fractal, each level's rule is simple (keep eight cells, discard the center) but the compounding effect after several levels is what produces the fractal's self-similar detail.

If you want a triangular version of the same middle-removal idea instead, use the Sierpinski Triangle Generator.

Features

Advantages

  • A well-documented, classic fractal with a single, unambiguous construction, not an invented variant.
  • Filled-square rendering makes the fractal's self-similar structure immediately readable, even at low depths.
  • Downloadable as both SVG (for further vector editing) and PNG (for quick use anywhere).

Limitations

  • Square count multiplies by eight with every added depth level, so the range is capped at depth 4 to keep rendering and downloads responsive.
  • At higher depths the smallest squares become tiny relative to the overall shape, and browser rendering can slow down as the rectangle count grows into the thousands.

Examples

Zero depth

Input

Depth: 0

Output

One solid filled square, no subdivision applied yet.

At depth 0 the recursion hasn't run, so the output is just the starting square.

Default depth

Input

Depth: 3

Output

A dense grid of small filled squares clearly showing the classic Sierpinski carpet pattern with visible "holes" at multiple scales.

The default of 3 shows several levels of nested self-similar detail while staying quick to render and download.

Best Practices & Notes

Best Practices

  • Start at depth 1 or 2 to see the single removed center cell clearly before increasing toward the finer, more detailed higher end of the range.
  • Download the SVG rather than the PNG if you plan to recolor, scale, or further edit the shapes, vector output has no resolution limit.

Developer Notes

Implemented as direct recursive geometry, not an L-system: subdivideGrid() takes a square's origin and size, divides it into a gridSize-by-gridSize grid (3 for the carpet), and recurses into every cell where the supplied keep(row, col) predicate returns true, here `!(row === 1 && col === 1)` to drop only the center cell, until depth reaches 0, at which point the surviving cell is pushed as a Rect. This same subdivideGrid() helper (in recursive-fractal-generators.ts) also powers the Vicsek fractal and Cantor dust tools with different keep predicates and grid sizes, before normalizeGeometry() rescales everything to the shared SVG viewbox.

Generate a Sierpinski Carpet Use Cases

  • Educational diagrams demonstrating the Cantor set's two-dimensional generalization and self-similarity
  • Generative art, textile, and tiling patterns built from the classic Sierpinski carpet shape
  • Downloadable SVG shape art for design projects referencing a well-known fractal

Common Mistakes

  • Confusing this with the Sierpinski triangle; both remove a "middle" piece recursively, but the carpet works on a square 3x3 grid while the triangle works on triangular thirds, and they produce visibly different shapes.
  • Setting depth to the maximum right away; past depth 3 the individual squares become small enough that added detail is hard to see without zooming into the downloaded SVG.

Tips

  • Compare against the Sierpinski Triangle Generator to see the same middle-removal idea applied to a triangle instead of a square grid.
  • Zoom into the downloaded SVG in a vector editor to count how the eight surviving cells repeat inside each other at higher depths.

References

Frequently Asked Questions