Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More

interactjs

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

interactjs

Drag and drop, resizing and multi-touch gestures with inertia and snapping for modern browsers (and also IE9+)


Version published
Weekly downloads
207K
increased by7.22%
Maintainers
1
Weekly downloads
 
Created

What is interactjs?

Interact.js is a JavaScript library for drag and drop, resizing, and multi-touch gestures for modern browsers. It provides a simple and flexible API to create interactive user interfaces.

What are interactjs's main functionalities?

Drag and Drop

This feature allows elements to be draggable. The code sample demonstrates how to make elements with the class 'draggable' draggable within their parent container.

document.addEventListener('DOMContentLoaded', function () {
  interact('.draggable').draggable({
    inertia: true,
    modifiers: [
      interact.modifiers.restrictRect({
        restriction: 'parent',
        endOnly: true
      })
    ],
    autoScroll: true,
    listeners: {
      move: dragMoveListener
    }
  });

  function dragMoveListener(event) {
    var target = event.target;
    var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
    var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
    target.style.transform = 'translate(' + x + 'px, ' + y + 'px)';
    target.setAttribute('data-x', x);
    target.setAttribute('data-y', y);
  }
});

Resizable Elements

This feature allows elements to be resizable. The code sample demonstrates how to make elements with the class 'resizable' resizable within their parent container.

document.addEventListener('DOMContentLoaded', function () {
  interact('.resizable').resizable({
    edges: { left: true, right: true, bottom: true, top: true },
    listeners: {
      move(event) {
        let { x, y } = event.target.dataset;
        x = (parseFloat(x) || 0) + event.deltaRect.left;
        y = (parseFloat(y) || 0) + event.deltaRect.top;
        Object.assign(event.target.style, {
          width: `${event.rect.width}px`,
          height: `${event.rect.height}px`,
          transform: `translate(${x}px, ${y}px)`
        });
        Object.assign(event.target.dataset, { x, y });
      }
    },
    modifiers: [
      interact.modifiers.restrictEdges({
        outer: 'parent',
        endOnly: true
      }),
      interact.modifiers.restrictSize({
        min: { width: 100, height: 50 }
      })
    ],
    inertia: true
  });
});

Gestures

This feature allows elements to respond to multi-touch gestures like pinch and rotate. The code sample demonstrates how to make elements with the class 'gesture-area' respond to these gestures.

document.addEventListener('DOMContentLoaded', function () {
  interact('.gesture-area').gesturable({
    listeners: {
      start(event) {
        console.log(event.type, event.target);
      },
      move(event) {
        var target = event.target;
        var scale = (parseFloat(target.getAttribute('data-scale')) || 1) * (1 + event.ds);
        var angle = (parseFloat(target.getAttribute('data-angle')) || 0) + event.da;
        target.style.transform = 'scale(' + scale + ') rotate(' + angle + 'deg)';
        target.setAttribute('data-scale', scale);
        target.setAttribute('data-angle', angle);
      },
      end(event) {
        console.log(event.type, event.target);
      }
    }
  });
});

Other packages similar to interactjs

FAQs

Package last updated on 28 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts