Back to Marketplace
Open Source

collide.js

IntersectionObserver for any two elements. collide.js fills the gap the browser leaves — proximity detection, collision events, and time-to-collision metrics between arbitrary DOM elements — with AABB or circle shapes, relative velocity, and a single shared requestAnimationFrame loop that batches reads and stops when idle. 2.1KB gzipped, zero dependencies, TypeScript-first, MIT.

TypeScriptIntersectionObserverCollision DetectionProximityTime to CollisionVelocity TrackingAABB + CircleShared RAFZero DependenciesESM + UMDOpen SourceMIT License

The Problem

Challenge

The browser ships `IntersectionObserver`, but it only compares an element against the viewport (or one ancestor). There's no built-in way to ask the far more common question: is *this element* near, overlapping, or on course to collide with *that other element*? Teams end up with ad-hoc `getBoundingClientRect` polling in every scroll/animation frame — duplicated per feature, unbatched, leaking performance on layout thrash. Drag-and-drop libraries, magnetic snap, collision-aware animations, and proximity-based UI all need the same primitive, and none was available under 2KB.

Our Approach

Strategy

We built collide.js around a single shared RAF loop that reads every observed pair once per frame, batches all DOM measurements, and stops automatically when no pair is active. The `Collide.watch(a, b, opts)` API emits `near` / `collide` / `separate` / `leave` / `tick` events with a rich metrics payload — distance, overlap, direction, angle, relative velocity, and estimated time-to-collision in seconds. Shapes support AABB (default) and circle for organic UI, with cross-shape (AABB ↔ circle) handled. `Collide.check(a, b)` and `Collide.shapes(sA, sB)` give instant one-off snapshots for imperative code paths. The landing page ships live demos: draggable balls that connect with lines when within 140px and flash on collision, drop zones that glow orange as a card approaches and turn green on overlap, and a magnetic snap demo where a node pulls toward a target within 120px with easing.

The Solution

Result

A 2.1KB gzipped, MIT-licensed collision and proximity library with zero runtime dependencies. One shared RAF loop, AABB + circle shapes, velocity + TTC metrics, and an event-driven API that scales from two elements to hundreds. Ships ESM, UMD, and full TypeScript declarations. Deployed on Vercel at collidejs.techzunction.com with an interactive docs site that lets visitors drag, drop, and collide shapes directly on the page.

Screenshots

Product Gallery

collide.js hero — v1.0.0 · 2.1KB gzip · 0 dependencies · TypeScript-first · MIT license with 5 draggable colored balls (A/B/C/D/E) connected by proximity lines within 140px
collide.js stats grid — 2.1 KB gzipped, 0 runtime deps, 1 shared RAF loop, 600 fps-capable with live draggable balls showing collision counter
Velocity + TTC, Shapes (AABB + circle), One shared RAF features — plus drag & drop with proximity zones that glow orange when near and turn green on collision
Magnetic snap demo — drag a purple node and collide.js feeds the direction vector into an easing loop that pulls it toward the target within 120px

Live Demos

See It in Action

Full Walkthrough

Key Metrics

0.1KB

Gzipped

0

Dependencies

0

Shared RAF

MIT

License

Get This Product

collide.js

Complete source code, assets, and deployment-ready build

$10USD / one-time

Exclusive Ownership

Secure payment via Razorpay. Instant delivery.

collide.js

$10