blinc_canvas_kit
Part of the Blinc UI Framework
This crate is a component of Blinc, a GPU-accelerated UI framework for Rust. For full documentation and guides, visit the Blinc documentation.
Interactive canvas toolkit for game editors and node graphs — pan, zoom, spatial indexing, multi-select, marquee selection, and snap-to-grid.
Overview
blinc_canvas_kit provides everything needed to build interactive infinite canvas applications:
- Viewport Management: Pan (with momentum), zoom (scroll + pinch), coordinate conversion between screen-space and content-space
- Spatial Indexing: Uniform-grid spatial hash for O(1) hit testing and fast range queries, scaling to thousands of elements
- Multi-Select: Shift+click to add, Cmd/Ctrl+click to toggle, with selection change callbacks
- Marquee Selection: Box-select via tool mode or Shift+drag, with live preview and additive mode
- Snap-to-Grid: Round content-space positions to configurable grid spacing
- Background Patterns: Infinite viewport-aware dot grid, line grid, and crosshatch patterns with zoom-adaptive density
- Viewport Culling: Skip rendering off-screen elements with a simple visibility check
- Hit Regions: Register interactive bounding boxes during drawing, with click/hover/drag callbacks
Quick Start
use *;
let mut kit = new
.with_background
.with_snap;
kit.on_element_click;
kit.on_element_drag;
kit.on_selection_change;
// Returns a fully-wired Div with canvas + event handlers
kit.element
Features
Tool Modes
// Pan mode (default): background drag pans, Shift+drag for marquee
kit.set_tool;
// Select mode: background drag draws marquee
kit.set_tool;
Selection
// Query selection state
let sel = kit.selection;
for id in &sel.selected
// Check individual items (useful in render callbacks for visual feedback)
if kit.is_selected
// Programmatic selection
kit.set_selection;
kit.clear_selection;
Snap-to-Grid
// Enable snapping
let kit = new.with_snap;
// Use in drag callbacks for precise positioning
kit.on_element_drag;
Viewport Culling
kit.element;
Background Patterns
// Dot grid (default)
dots
// Line grid
grid.with_spacing
// Crosshatch (diagonal lines)
crosshatch
// Customized with zoom-adaptive density
dots
.with_spacing
.with_color
.with_size
.with_zoom_adaptive // Below 30% zoom, show every 5th dot
Architecture
All state persists across UI rebuilds via BlincContextState keyed storage. The spatial index is rebuilt each frame during the render callback (via hit_rect() calls), while selection, viewport, and interaction state persist.
| Module | Purpose |
|---|---|
viewport |
Pan/zoom state, coordinate conversion, visibility testing |
pan |
Momentum panning with EMA velocity tracking |
zoom |
Scroll and pinch zoom handlers |
spatial |
Uniform-grid spatial hash for hit testing and range queries |
selection |
Multi-select state, marquee drag, tool modes |
snap |
Grid snapping for content-space coordinates |
background |
Infinite viewport-aware pattern rendering |
hit |
Hit region types and event structs |