Skip to content

Interactable

The Interactable plugin makes it easy to add drag-and-drop interactions. It works perfectly on both mouse and touch devices, and is highly optimized to follow your finger or cursor instantly.


1. Dragging & Bounds

You create a drag interaction using Interactable.create().

You can limit where the element can go using the bounds property. If you give it another element, it will automatically stay inside that element. Turning on inertia gives it physics so it keeps sliding when you throw it.

Interact with the element to observe edgeResistance tension and inertia physics. Output is logged to the console.
Drag
60 FPS

In the demo above, try pulling the element past the edge to feel the edgeResistance, and throw it to see the inertia.

js
import { Interactable } from 'animx/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 make your element snap to a grid. Normally, it snaps after you let go. But you can turn on liveSnap to make it smoothly snap to the grid while you are still dragging it.

Interact with the element to observe live grid snapping interpolation.
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: 'power2.out'
  }
})

The snap property can be a simple number for a grid size, an array of exact {x, y} points, or a custom function.


3. Rotation Mode

You don't just have to drag things up and down. If you set type: 'rotation', you can spin the element like a dial or a wheel.

Rotate the element. Live rotation angles are logged to the console.
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 direction. Options: 'x', 'y', 'x,y', 'rotation'.
boundsstring|Element|ObjectnullElement selector, or coordinate object ({minX:0, maxX:100}).
edgeResistancenumber0.85How stiff the boundaries feel when pulling past them (0 = impossible, 1 = no resistance).
inertiabooleantrueAdds throwing physics so it keeps moving when you let go.
snapnumber|Array|FunctionnullSnap to a grid size, an array of points, or a function.
liveSnapboolean|ObjectfalseSnaps while dragging. Can be true or an animation config object ({ duration: 0.15 }).
minimumMovementnumber2How far the mouse/finger must move before the drag starts (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 native browser PointerEvent as the second.

CallbackDescription
onPressFired the instant you touch or click the element.
onDragStartFired once you move past the minimumMovement amount.
onDragFired every time the mouse/finger moves during the drag.
onReleaseFired the instant you let go.
onDragEndFired after the drag is fully complete (waits for inertia or snapping to finish).
onSnapFired when the element perfectly aligns with a snap point.

Instance Methods & Properties

NameTypeDescription
x / yPropertyThe live x and y position.
rotationPropertyThe live rotation angle (if type: 'rotation').
isDraggingPropertytrue if you are actively dragging past the minimumMovement.
isPressedPropertytrue if pointer is currently held down.
kill()MethodCompletely removes event listeners and restores original cursors.
applyBounds()MethodManually recalculates the bounds (useful if the screen changes size).