User input handling and orchestration library, From low-level events (keyboard, wheel, pointer, touch, ...) to high-level gestures (pan, pinch, ...)
npm install --save cerebThe example below moves an element by tracking pointer position:
import { singlePointer } from "cereb";
// Create a stream from pointer events
singlePointer(canvas)
// Listen to stream events
.on((signal) => {
// Receive signals from the stream
const { phase, x, y } = signal.value;
switch (phase){
case "move":
element.style.transform = `translate(${x}px, ${y}px)`;
break;
}
});For advanced gestures like pan or pinch, install dedicated packages that build on top of Cereb's core:
| Package | Description |
|---|---|
| @cereb/pan | Pan/drag gestures with velocity and direction tracking |
| @cereb/pinch | Pinch-to-zoom with distance and scale calculations |
npm install --save cereb @cereb/pinchimport { pipe } from "cereb";
import { zoom } from "cereb/operators";
import { pinch } from "@cereb/pinch";
// pipe creates a pipeline where signals flow through operators
// Each operator extends the signal (signals are immutable)
pinch(element)
// Operator: Determine scale value.
.pipe(zoom({ minScale: 0.5, maxScale: 3.0 })).on((signal) => {
// The scale property is extended from the value.
// - pinch emits distance → zoom calculates scale
// - zoom also works with other inputs (keyboard, wheel, etc.)
element.style.transform = `scale(${signal.value.scale})`;
});Create streams from various input sources:
| API | Description |
|---|---|
| pan | Pan gesture with velocity and direction |
| pinch | Pinch gesture with distance and center |
| singlePointer | Unified pointer (mouse/touch/pen) |
| multiPointer | Multi-touch tracking |
| keyboard | Keyboard events (keydown + keyup) |
| keydown | Keydown events only |
| keyheld | Track if a key is held |
| wheel | Wheel/scroll events |
| domEvent | Any DOM event |
Transform and compose streams with operators like filter, map, merge, throttle, debounce, and more.
- Spaghetti Event Code — Scattered handlers, shared mutable state, duplicated logic
- Lightweight Bundle — ~77% smaller than Hammer.js (1.73 KB gzipped for pan gesture)
- Resource Efficiency — Event listener reuse, single-responsibility operators
If you find Cereb useful, consider giving it a star — it helps others discover the project!
Contributions are welcome! Please read our Contributing Guide before submitting a Pull Request.
Cereb is MIT licensed.