interact.js

JavaScript drag and drop, resizing and multi-touch gestures with inertia an...

README

interact.js

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

GitterjsDelivrBuild Status

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](http://interactjs.io/#use_in_svg_files) elements
- being standalone and customizable
- not modifying the DOM except to change the cursor (but you can disable
  that)

Installation


- npm:npm install interactjs
- [jsDelivr CDN](https://cdn.jsdelivr.net/npm/interactjs/): ``- [unpkg CDN](https://unpkg.com/interactjs/): ``
  1. yarn add interactjs
  2. //= require interactjs/interact
- Webjars SBT/Play 2:libraryDependencies ++= Seq("org.webjars.npm" % "interactjs" % version)

Typescript definitions


The project is written in Typescript and the npm package includes the type
definitions, but if you need the typings alone, you can install them with:

  1. ```
  2. npm install --save-dev @interactjs/types
  3. ```

Documentation


http://interactjs.io/docs

Example


  1. ``` js
  2. var pixelSize = 16;

  3. interact('.rainbow-pixel-canvas')
  4.   .origin('self')
  5.   .draggable({
  6.     modifiers: [
  7.       interact.modifiers.snap({
  8.         // snap to the corners of a grid
  9.         targets: [
  10.           interact.snappers.grid({ x: pixelSize, y: pixelSize }),
  11.         ],
  12.       })
  13.     ],
  14.     listeners: {
  15.       // draw colored squares on move
  16.       move: function (event) {
  17.         var context = event.target.getContext('2d'),
  18.             // calculate the angle of the drag direction
  19.             dragAngle = 180 * Math.atan2(event.dx, event.dy) / Math.PI;

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

  23.         // draw squares
  24.         context.fillRect(event.pageX - pixelSize / 2, event.pageY - pixelSize / 2,
  25.                          pixelSize, pixelSize);
  26.       }
  27.     }
  28.   })
  29.   // clear the canvas on doubletap
  30.   .on('doubletap', function (event) {
  31.     var context = event.target.getContext('2d');

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

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

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

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

License


interact.js is released under the MIT License.

[ijs-twitter]: https://twitter.com/interactjs
[upcoming-changes]: https://github.com/taye/interact.js/blob/main/CHANGELOG.md#upcoming-changes