Blog · Frontend · · 4 min read
Filtering taxa by location with Path2D
Ptree can dim every clade on screen that does not occur near your browser's coordinates or inside any country, state, sea, or basin you pick on the world map. Click Madagascar on the primates and five families light up, every one of them a lemur; switch to the Amazon basin and five completely different monkey families answer, with zero overlap. Adding that entire feature took under 200 lines of client JavaScript and required no new database table, no spatial index on the server, and no polygon clipping library.
Why the data is already in the right shape
Ptree's world map renders both taxon ranges and selectable geographic regions as SVG <path> strings in the same equirectangular coordinate space. Every taxon's range arrives with its tile data as a compact SVG path string (combining expert ranges such as the Mammal Diversity Database with iNaturalist geomodel outlines). Every selectable country and province in the location picker is an SVG <path> inside the map's <svg> in the exact same projection.
Because both sides speak native SVG path syntax in one shared coordinate system, testing whether a taxon occurs at a point (x, y) needs no GeoJSON parsing, Turf, 500 KB H3 hexagon library, or backend query. The browser's own 2D canvas engine already implements exact point-in-path hit testing for SVG path strings.
Point testing with a 1×1 OffscreenCanvas
The location module allocates a single OffscreenCanvas(1, 1) and its 2D context once at startup:
const hit = new OffscreenCanvas(1, 1).getContext("2d");
const paths = new WeakMap();
When a taxon's range object is tested, Ptree compiles its SVG path string into a native Path2D once and memoizes it in the WeakMap keyed by the range object itself. Garbage collection frees the Path2D automatically if the range leaves memory. Testing whether a coordinate falls inside that range is a single native call:
hit.isPointInPath(path, x, y)
For browser geolocation or a map click, the (latitude, longitude) pair projects straight into the map's equirectangular (x, y) and tests against every visible taxon's Path2D in microseconds. The marker on the map is a single <use href="#point"> element moved to (x, y) and parked at -1000 when inactive so no DOM node is created or destroyed. Because the coordinates live on that one SVG symbol instead of on :root, moving the pin never triggers a document-wide CSS restyle.
Hover scrubbing on the sidebar world map
Because Path2D hit testing runs in microseconds, the sidebar thumbnail map previews any spot on Earth directly on hover without waiting for a click. On pointerenter, the location module caches mapSvg.getScreenCTM().inverse() once so each pointermove projects the pointer into SVG world units with plain multiplication, zero layout queries, and zero per-frame allocations. Coordinates quantize to 0.5 world units (about 0.18°), reposition the same #point crosshair, and trigger the lighting pass live.
CSS reveals the crosshair only while the map is hovered when no location is committed, with no :has() selector and no extra state class on <body>:
body:not(.Located) figure.map:not(:hover) .location { display: none; }
Leaving the map or pressing down to expand it restores the committed location marker and brings every tile and summary back to rest.
Testing whole countries and provinces
A country or province is a polygon, not a single point: a species living only in southern Chile should still match when you pick Chile, even if it misses the country's centroid. Rather than running polygon-polygon intersection in JavaScript, Ptree samples a grid of interior points inside the chosen region's own Path2D using the same hit.isPointInPath() call.
Sampling reads the region's bounding box, walks a grid across it, keeps the grid points that fall inside the region's path (rounded to tenths of a unit), and caches the resulting coordinate list directly on place.dataset.points in the DOM. If you pick France, switch to Japan, and return to France, the sampled interior points are already sitting on the <path> element. Multi-region selections (like US-TN|US-NC in the URL) simply pool the sampled points from each selected place.
Testing a taxon against a selected region then checks points.some(([x, y]) => hit.isPointInPath(rangePath, x, y)). The first hit short-circuits immediately.
Cascading the result onto the tree
Once Ptree knows which displayed taxa match the active point, hover coordinate, or region, it never rebuilds the tree or strips out colors. It sets data-out on non-matching tiles so CSS dims their names and icons while keeping the active property overlay readable, dims their connector stroke opacity in the same pass, and feeds the match mask into live client-side filtered summaries so every property readout and box-whisker plot updates under the cursor. Location composes cleanly with any active property filter: endemic, under five grams, and in the Sahara runs as three independent predicates on the exact same visible tree.
Nothing in the build pipeline is precomputed for every (taxon, country) pair, a cross-product of millions of taxa by thousands of provinces that would add gigabytes of storage and a new invalidation stage. Keeping range shapes and place outlines in one shared SVG projection lets the browser's built-in Path2D engine answer the query directly on the client.