Quadtree Spatial Partitioning Visualizer — Free Interactive Canvas Demo

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

CategoryVisualizers

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.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to explain why skipping non-overlapping nodes is safe. Ask it to add a circular radius query, a k-nearest-neighbours search, collision detection between points using the tree, or a uniform grid mode to compare against the quadtree. It can also help you measure when rebuilding every frame stops being cheap.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an interactive quadtree visualizer on an HTML canvas in plain HTML, CSS and JavaScript on a dark theme.

Requirements:
- Generate several hundred points in clusters, moving and bouncing off the edges (with a toggle to pause movement, off by default for reduced motion).
- Rebuild a quadtree every frame: each node covers a rectangle and holds up to a configurable capacity of points, splitting into four quadrants and redistributing its points when full, with a maximum depth.
- Draw every node's rectangle faintly and the points as small dots.
- Query a rectangular box that follows the pointer: skip nodes that don't overlap it, test points in the nodes that do, and highlight found points and the visited leaf cells.
- Show counters for points found, points tested by the quadtree, points a brute-force scan would test, and the percentage of work compared with brute force.
- Sliders for point count (50–2000) and capacity (1–16), and clicking adds a small cluster of points.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

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>

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 development
Broad-phase collision detection.
Maps and GIS
Find markers inside the viewport.
Canvas editors
Hit-test shapes under the cursor.
Particle systems
Neighbour lookups for flocking.
Data structures courses
A visual intro to spatial indexing.
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.