Skip to content
JavaScript drag and drop, resizing and multi-touch gestures with inertia and snapping for modern browsers (and also IE9+)
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs docs: don't include .js files Feb 2, 2019
examples
guide docs(guide): add touch-action and user-select info Mar 24, 2019
img
packages feat: install plugin dependcies Mar 24, 2019
scripts chore: fix errors in release script Feb 11, 2019
test fix(test): fix file args Mar 10, 2019
.babelrc.js
.codeclimate.yml chore: fix codeclimate config Feb 25, 2019
.eslintignore
.eslintrc.yml chore: update dependencies Feb 8, 2019
.gitignore fix: improve types and convert tests to typescript Feb 8, 2019
.npmignore
.nycrc.json chore: bump test coverage requirements Feb 19, 2019
.tern-project
.travis.yml chore(travis): run tsc_lint_test Feb 13, 2019
CHANGELOG.md CHANGELOG: add 1.4.0-alpha changes Aug 18, 2018
ISSUE_TEMPLATE.md ISSUE_TEMPLATE: line wrapping Jan 31, 2017
LICENSE chore: update license Jan 28, 2019
PULL_REQUEST_TEMPLATE.md
README.md docs: link to homepage SVG demo Mar 11, 2019
babel-transform-for-of-array.js style: use standard/eslint-config-standard Jan 31, 2019
lerna.json *: move dev stuff to root; add packages/interactjs Aug 18, 2018
package-lock.json
package.json chore: fix dependencies Mar 20, 2019
tsconfig.json fix: improve types and convert tests to typescript Feb 8, 2019
tslint.json

README.md

interact.js

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

Gitter Build Status Test Coverage

Features include:

  • inertia and snapping
  • multi-touch, simultaneous interactions
  • cross browser and device, supporting the desktop and mobile versions of Chrome, Firefox and Opera as well as Internet Explorer 9+
  • interaction with SVG elements
  • being standalone and customizable
  • not modifying the DOM except to change the cursor (but you can disable that)

Installation

  • npm: npm install interactjs@next
  • jsDelivr CDN: <script src="https://cdn.jsdelivr.net/npm/interactjs@next/dist/interact.min.js"></script>
  • unpkg CDN: <script src="https://unpkg.com/interactjs@next/dist/interact.min.js"></script>
  • Webjars SBT/Play 2: libraryDependencies ++= Seq("org.webjars.npm" % "interactjs" % version)
  • Direct download the latest version: http://interactjs.io/#download
    • Rails 4 app development (using Rails Asset Pipeline)
      • Download the file interact.js (development version) into a new sub-directory: vendor/assets/javascripts/interact
      • Add //= require interact/interact in app/assets/javascripts/application.js (above //= require_tree .)
      • Restart the Rails server

Typescript definitions

npm install --save-dev @interactjs/types

Documentation

Visit http://interactjs.io/docs for the API documentation.

Example

var pixelSize = 16;

interact('.rainbow-pixel-canvas')
  .origin('self')
  .draggable({
    modifiers: [{
      // snap to the corners of a grid
      type: 'snap',
      targets: [
        interact.snappers.grid({ x: pixelSize, y: pixelSize }),
      ],
    }],
  })
  // draw colored squares on move
  .on('dragmove', function (event) {
    var context = event.target.getContext('2d'),
        // calculate the angle of the drag direction
        dragAngle = 180 * Math.atan2(event.dx, event.dy) / Math.PI;

    // set color based on drag angle and speed
    context.fillStyle = 'hsl(' + dragAngle + ', 86%, '
                        + (30 + Math.min(event.speed / 1000, 1) * 50) + '%)';

    // draw squares
    context.fillRect(event.pageX - pixelSize / 2, event.pageY - pixelSize / 2,
                     pixelSize, pixelSize);
  })
  // clear the canvas on doubletap
  .on('doubletap', function (event) {
    var context = event.target.getContext('2d');

    context.clearRect(0, 0, context.canvas.width, context.canvas.height);
  });

  function resizeCanvases () {
    [].forEach.call(document.querySelectorAll('.rainbow-pixel-canvas'), function (canvas) {
      canvas.width = document.body.clientWidth;
      canvas.height = window.innerHeight * 0.7;
    });
  }

  // interact.js can also add DOM event listeners
  interact(document).on('DOMContentLoaded', resizeCanvases);
  interact(window).on('resize', resizeCanvases);

See the above code in action at https://codepen.io/taye/pen/tCKAm

License

interact.js is released under the MIT License.

You can’t perform that action at this time.