0.8ms Canvas Snapping in Pure TypeScript: Phase 1 Asymmetric Separable Morphology

In web-based spatial canvas tools like architecture diagram authoring systems, whiteboard tools, and workflow graph builders, click-to-snap is a critical micro-interaction: when clicking on a diagram card, the engine must extract the exact geometric outer boundary of the card in under 1 millisecond to establish a rock-solid physical anchor for camera panning and connector docking.
In Episode 01, we tackled high-frequency glyph traps using adaptive parallel radar. But real-world production UI cards introduce a more severe structural dilemma: long centered titles that physically slice cards into disconnected multi-component fragments.
1. The Dilemma: Title Bar Punctures & False Multi-Component Fragments
In standard cloud architecture diagrams, card titles are often lengthy and centered across the card header. Text glyphs are composed of high-contrast discrete strokes separated by empty background gaps.
When standard Connected-Component Labeling (CCL) or flood-fill routines encounter such cards:
- Text glyphs form a dense barrier across the binarized mask;
- Letter gaps are mistaken for background regions, causing the card to be severed into disjoint upper and lower fragments;
- Snapping routines either lock onto only the title bar or only the lower payload section, failing to capture the full card perimeter.
Attempting to resolve this with isotropic circular dilation immediately creates an impossible trade-off: closing letter gaps requires a large kernel radius, but that large radius causes vertical leakage that welds adjacent cards together into a single blob.
2. The Solution: Exploiting Typography Anisotropy via Asymmetric Kernels
The root of the problem is that classical computer vision assumes spatial isotropy, whereas UI typography is intrinsically anisotropic:
- Horizontal Kerning: Letters in standard UI fonts are tightly packed, with inter-glyph gaps of only 2 to 4 pixels;
- Vertical Line-Height & Padding: Vertical spacing between text lines and card boundaries is generous, typically 10 to 18 pixels or more.
FocusFlow exploits this geometric reality by designing an anisotropic elliptic structuring element:
- Horizontal semi-axis: rx = 6 px
- Vertical semi-axis: ry = 14 px
This kernel bridges letter strokes horizontally across the title bar without leaking vertically into adjacent diagram elements.
3. Morphological Closing: Bridging Without Shape Distortion
To bridge internal glyph gaps while preserving the card's exact physical geometry, we employ classical morphological closing, which strictly cascades a dilation pass followed by an erosion pass:
- Step 1: Asymmetric Dilation (Bridging): Applies a maximum filter across the binary mask using the asymmetric elliptic kernel. Separate glyph strokes weld horizontally into a continuous solid band, healing the title void and reconnecting the disjoint card components into a single connected component.
- Step 2: Equal-Radius Erosion (Geometry Restoration): Applies a minimum filter using the identical structuring element. This cleanly strips away the excess outer perimeter added during dilation, snapping the external boundaries back to their exact original vector dimensions.
The result is 100% gap closure with zero dimensional dilation error.
4. Algorithmic Optimization: Separable 1D Filtering in O(2K)
On a 4K canvas (3840x2160), 2D spatial convolution scales with quadratic complexity O(K^2). With a 13x29 kernel, direct 2D convolution requires nearly 400 memory lookups per pixel, violating the 60fps real-time budget.
Because our asymmetric kernel is mathematically separable, FocusFlow decomposes the 2D closing operation into two orthogonal 1D passes:
- Pass 1: Horizontal 1D row filtering along the X-axis (width Kx = 13);
- Pass 2: Vertical 1D column filtering along the Y-axis (height Ky = 29).
This decomposition slashes computational complexity from O(Kx * Ky) down to O(Kx + Ky), cutting execution time by more than 90%.
Coupled with contiguous memory TypedArray pooling and sub-pixel edge interpolation in pure TypeScript, the entire pipeline executes in 0.79ms on standard CPU threads with zero garbage collection pauses.
5. 4K Video Demo & Open Source Code
Episode 02 interactive player and algorithm source code are fully open-sourced in FocusFlow:
- 4K Video: https://youtu.be/TZIhB3aPPOo
- Live Demo: https://focusflow.tumio.site
- GitHub Repo: https://github.com/tumio-ltd/focusflow