Skip to content

About

A headless motion engine for tactile UI controls.

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

tactile-motion

A headless motion engine for tactile UI controls.

Built for canvas controls, sliders, pickers, knobs, and timelines.

License: MIT npm version npm downloads bundle size

Gesture in → Physics → Value out
   drag → velocity → inertia → settle
  • Continuous motion: Precise exponential decay physics with tunable inertia.
  • Zero dependencies: Core has no runtime dependencies (~1.8 kB minified + gzip).
  • Headless: Decoupled from DOM events, rendering, and styling.
  • Framework agnostic: First-class core API with an optional React adapter.
  • Frame-rate independent: Consistent landing position across 30, 60, 120, and 144 Hz.

中文说明 · Examples

Install

npm install tactile-motion

To use the React adapter (tactile-motion/react), install react (>= 16.8.0).

Core API

import { createTactileMotion } from 'tactile-motion';

const motion = createTactileMotion({
  value: 0,
  min: 0,
  max: 1000,
  friction: 0.94,
  velocityMultiplier: 1,
  maxVelocity: 3,
  threshold: 0.01,
  onChange(value) {
    render(value);
  },
  onStateChange(state) {
    console.log(state); // 'idle' | 'dragging' | 'animating'
  },
  onRest(value) {
    console.log('settled', value);
  },
});

// Bind to your gesture lifecycle:
motion.start(); // Call on pointer down
motion.drag(12.5); // Call on pointer move with delta
motion.release(); // Call on pointer up (inertia takes over)

motion.stop(); // Stop immediately at current position
motion.set(500); // Stop and jump to value

Options

Option Default Description
value 0 Initial position (clamped to [min, max]).
min / max -Infinity / Infinity Hard boundaries.
friction 0.94 Velocity retained per 16.67ms (0 <= friction < 1). Lower stops faster.
velocityMultiplier 1 Multiplier applied to release velocity.
maxVelocity Infinity Speed cap in units/ms after multiplier.
threshold 0.01 Speed threshold in units/ms to consider resting.
scheduler RAF / timer Clock and frame scheduling adapter.

Methods

Method Description
getValue() Returns the current value.
getSnapshot() Returns immutable { value, velocity, state }.
start(time?) Enters dragging and resets velocity tracking.
drag(delta, time?) Applies movement delta and updates velocity estimate.
release(time?) Releases drag and starts inertia animation (or settles).
stop() Cancels motion and transitions to idle at current value.
set(value) Cancels motion, clamps value, and updates position.
configure(options) Dynamically updates options.
subscribe(listener) Subscribes to snapshot changes; returns unsubscribe fn.

React

import { useTactileMotion } from 'tactile-motion/react';

function Slider() {
  const motion = useTactileMotion({
    min: 0,
    max: 100,
    maxVelocity: 2,
    onChange(val) {
      // optional callback
    },
  });

  return (
    <div>
      <span>{motion.value.toFixed(1)}</span>
      {/* Wire gestures to motion.dragStart(), motion.dragMove(delta), motion.dragEnd() */}
    </div>
  );
}

The hook returns the current snapshot (value, velocity, state), all core motion methods, and convenience aliases: dragStart, dragMove, and dragEnd. Fully compatible with SSR and React StrictMode.

Snapping

The engine provides continuous physical motion. Snap logic belongs in your component or onRest:

const step = 10;
const motion = createTactileMotion({
  onRest(position) {
    motion.set(Math.round(position / step) * step);
  },
});

Development

# Clone and run the demo lab
npm install
npm run dev

# Run tests and type checks
npm run check

License

MIT

About

A headless motion engine for tactile UI controls.

Topics

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages