webdevpuneet.com

Quadtree Spatial Partitioning Visualizer — Free Interactive Canvas Demo

Quadtree Spatial Partitioning Visualizer · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

Copy & paste

Source Code

<div class="qt">
  <div class="qt-top">
    <div>
      <h2>Quadtree: find nearby points without checking all of them</h2>
      <p>Move the pointer (or drag on touch) to query the box around it. Click to add points.</p>
    </div>
    <div class="qt-ctrl">
      <label>Points <input type="range" id="qtN" min="50" max="2000" step="50" value="600"><output id="qtNOut"></output></label>
      <label>Capacity <input type="range" id="qtCap" min="1" max="16" value="4"><output id="qtCapOut"></output></label>
      <label><input type="checkbox" id="qtMove" checked> Moving</label>
    </div>
  </div>
  <canvas id="qtCanvas" width="900" height="480" aria-label="Quadtree of moving points"></canvas>
  <div class="qt-stats" id="qtStats" aria-live="off"></div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Live quadtree
Rebuilt every frame over moving points.
Adaptive subdivision
Small cells where data is dense.
Box range query
Skips non-overlapping nodes entirely.
Visited leaves shaded
See exactly what the query touched.
Work counters
Points tested versus brute force.
Capacity slider
Explore the depth versus leaf-size trade-off.
Clustered data
A realistic, non-uniform distribution.
Depth limit
Prevents runaway splitting.

About this UI Snippet

Quadtrees — Skipping Most of the Work in Spatial Queries

Screenshot of the Quadtree Spatial Partitioning Visualizer snippet rendered live

Many interactive programs keep asking the same question: which things are near this point? Games check collisions, maps find markers in the visible area, and drawing tools find shapes under the cursor. Checking every object each time is O(n) per query, and doing that for every object gives O(n²). A quadtree organises points by position so a query can skip whole regions at once.

How a quadtree is built

The root node covers the whole canvas. Each node holds up to a fixed number of points — the capacity. When one more point arrives, the node splits into four equal quadrants and pushes its points down into them. Dense areas therefore end up with many small cells, and empty areas stay as large cells. A depth limit stops endless splitting when many points share almost the same position.

How a query works

To find points inside a box, start at the root. If a node's rectangle doesn't overlap the box, skip it and everything below it. Otherwise test its own points and recurse into its children. The cyan-tinted cells are the leaves the query actually visited; everything else was skipped without looking at a single point.

Measuring the saving

The counters compare the number of points the quadtree tested with the number a brute-force scan would test. With clustered data and a small box, the quadtree typically tests a small fraction.

Capacity is a trade-off

A capacity of 1 creates a very deep tree with many nodes to visit; a large capacity makes each leaf a small brute-force scan. Try the slider and watch both the cell pattern and the percentage change.

Rebuilt every frame

The points move, so the tree is rebuilt on every animation frame. For thousands of points that is fast, and it avoids the complexity of moving points between nodes.

Step by step

How to Use

  1. 1
    Move the pointerThe yellow box queries the tree around it; found points turn yellow.
  2. 2
    Read the countersCompare quadtree tests with a brute-force scan.
  3. 3
    Change the capacitySee how the subdivision and the work change.
  4. 4
    Change the point countFrom 50 to 2,000 clustered points.
  5. 5
    Click to add pointsWatch cells split where points pile up.

Real-world uses

Common Use Cases

Game collision broad-phase
See why games partition space before testing collisions: the stats compare how many points the quadtree tests against a brute-force scan across hundreds of moving points.
Map marker lookups
Understand how map libraries find the markers inside the visible box without checking every pin, using the same range-query idea shown by the query box around the pointer.
Hit-testing in canvas editors
Learn how drawing tools find the shape under the cursor quickly. Move the pointer and watch the visited leaves shade in to see exactly what the query touched.
Neighbour lookups for flocking
Speed up systems where every particle asks who is nearby. Clustered data shows adaptive subdivision, with small cells wherever points are dense.
Data structures courses
Teach spatial indexing visually: the tree is rebuilt every frame, so students see the subdivision respond to movement instead of studying a static diagram.
Related: Canvas Boids Flocking
A simulation that benefits from spatial indexing: Canvas Boids Flocking Simulation.
Related: Leaflet Marker Clustering
Spatial grouping on a real map: Leaflet Marker Clustering at Scale.

Got questions?

Frequently Asked Questions

A tree in which each node covers a rectangle and has either up to a fixed number of points or four children covering the four quadrants of its rectangle. It adapts to the data, with small cells where points are dense.

A query can skip every node whose rectangle doesn't overlap the search area, so it tests only points in nearby cells. For clustered data and small search areas, that is a small fraction of all points.

Small capacities create deep trees with more nodes to traverse; large capacities make each leaf a longer brute-force list. Values between 4 and 16 are common; measure with your own data.

For moving objects and a few thousand points, rebuilding is simple and fast. For very large, mostly static datasets, update only the objects that move.

A grid uses fixed-size cells, which is simple and fast when data is evenly spread. A quadtree adapts cell size to density, which works better for clustered data.