Skip to content

devphilip21/cereb

User input handling and orchestration library, From low-level events (keyboard, wheel, pointer, touch, ...) to high-level gestures (pan, pinch, ...)

npm install --save cereb

Getting started

The 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;
    }
  });

High-level gestures packages

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

Pinch example

npm install --save cereb @cereb/pinch
import { 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})`;
  });

Documentation

Stream API

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

Operator API

Transform and compose streams with operators like filter, map, merge, throttle, debounce, and more.

See all operators →

The Problems Cereb Solves

  • 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

See detailed examples →

Contributing

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.

License

Cereb is MIT licensed.

About

User input handling and orchestration library, from low-level events to high-level gestures.

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •