Skip to content

Interactable

The Interactable plugin enables advanced pointer-based drag-and-drop interactions. It uses unified Pointer Events to work flawlessly across mouse and touch screens, and bypasses standard tween engines to provide zero-latency 120hz tracking.


1. Dragging & Bounds

Making an element draggable is as simple as passing it to Interactable.create().

You can restrict movement by passing a bounds property. If you pass an element (like the parent container), the plugin calculates the bounding box perfectly. When the user releases the mouse, inertia applies momentum physics.

Try grabbing the box and violently throwing it against the edge of the container to see the edgeResistance and inertia in action! Check the console to see when dragging starts and ends.
Drag
60 FPS

Try grabbing the box and violently throwing it against the edge of the container to see the edgeResistance and inertia in action.

js
import { Interactable } from 'animx/plugins/Interactable'

Interactable.create('.box', {
  bounds: '.container',
  edgeResistance: 0.85, // How stiff the boundary feels (0-1)
  inertia: true         // Throw physics
})

2. Snapping & Grids

You can force a dragged element to snap to a specific grid or set of points. By default, snapping only occurs after you release the mouse. But if you enable liveSnap, the element will interpolate to the nearest snap point while you drag it.

Try grabbing the coin. Drag it around the grid and watch it snap to the nearest point!. Try throwing it!
60 FPS
js
Interactable.create('.coin', {
  snap: 50, // Snap to 50px increments
  liveSnap: {
    duration: 0.15,      // The tween duration pulling it to the point
    ease: 'cubic.out'
  }
})

You can pass a number (for a grid), an array of coordinate objects [{x: 0, y: 0}, {x: 100, y: 100}], or a custom function to the snap property.


3. Rotation Mode

You aren't limited to X/Y coordinates. By setting type: 'rotation', you can turn any element into a turnable dial or wheel.

Grab the dial and rotate it! Check the console to see the live rotation angle as you drag.
60 FPS
js
Interactable.create('.dial', {
  type: 'rotation',
  inertia: true,
  snap: 45 // Snap to 45 degree increments
})

API Reference

Constructor

js
const instance = Interactable.create(target, config)
Config PropertyTypeDefaultDescription
typestring'x,y'Restricts movement. Options: 'x', 'y', 'x,y', 'rotation'.
boundsstring|Element|ObjectnullElement selector, or coordinate object ({minX:0, maxX:100}).
edgeResistancenumber0.85How "stiff" the boundaries feel when dragging past them (0 = impossible to drag past, 1 = no resistance).
inertiabooleantrueApplies momentum/friction physics upon release based on flick velocity.
snapnumber|Array|FunctionnullSnap logic. Grid number, array of coordinates, or function.
liveSnapboolean|ObjectfalseSnaps during the drag. Can be true or a tween config object ({ duration: 0.15 }).
minimumMovementnumber2Pixels the pointer must move before initiating a drag (prevents accidental clicks).
cursorstring'grab'CSS cursor on hover.
activeCursorstring'grabbing'CSS cursor while dragging.

Event Callbacks

All callbacks receive the Interactable instance as the first argument, and the raw DOM PointerEvent as the second.

CallbackDescription
onPressFired the instant the pointer touches the element.
onDragStartFired once minimumMovement threshold is exceeded.
onDragFired on every pointer movement during an active drag.
onReleaseFired the instant the pointer leaves the screen or mouse is released.
onDragEndFired after the drag is complete (and after inertia/snapping settles).
onSnapFired when the element perfectly aligns with a snap point.

Instance Methods & Properties

NameTypeDescription
x / yPropertyThe live transform coordinates.
rotationPropertyThe live rotation angle (if type: 'rotation').
isDraggingPropertytrue if actively moving past minimumMovement.
isPressedPropertytrue if pointer is currently held down.
kill()MethodCompletely removes event listeners and restores original cursors.
applyBounds()MethodManually recalculates bounding box coordinates (useful on window resize).