UNPKG

3.97 kBMarkdownView Raw
1<a href="http://interactjs.io"><img alt="interact.js" src="https://c4d6f7d727e094887e93-4ea74b676357550bd514a6a5b344c625.ssl.cf2.rackcdn.com/ijs-anim.svg" height="131px" width="100%"></a>
2
3JavaScript drag and drop, resizing and multi-touch gestures with inertia and
4snapping for modern browsers (and also IE9+).
5
6[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/taye/interact.js)
7[![Build Status](https://travis-ci.org/taye/interact.js.svg?branch=master)](https://travis-ci.org/taye/interact.js)
8[![Test Coverage](https://codeclimate.com/github/taye/interact.js/badges/coverage.svg)](https://codeclimate.com/github/taye/interact.js/coverage)
9
10Features include:
11
12 - **inertia** and **snapping**
13 - **multiple interactions**
14 - cross browser and device, supporting the **desktop and mobile** versions of
15 Chrome, Firefox and Opera as well as **Internet Explorer 9+**
16 - interaction with [**SVG**](http://interactjs.io/images/star.svg) elements
17 - being **lightweight and standalone** (not _yet another_ jQuery plugin)
18 - **not modifying the DOM** except to change the cursor (but you can disable
19 that)
20
21Installation
22------------
23
24* [Bower](http://bower.io/): `bower install interactjs`
25* [npm](https://www.npmjs.org/): `npm install interactjs`
26* [Webjars SBT/Play 2](https://www.webjars.org/): `libraryDependencies ++= Seq("org.webjars.bower" % "interact.js" % "1.2.8")`
27* Direct download the latest version: http://interactjs.io/#download
28 * **Rails 4** app development (using Rails Asset Pipeline)
29 * Download the file interact.js (development version) into a new sub-directory: vendor/assets/javascripts/interact
30 * Add ```//= require interact/interact``` in app/assets/javascripts/application.js (above ```//= require_tree .```)
31 * Restart the Rails server
32* [jsDelivr CDN](http://www.jsdelivr.com/#!interact.js): `<script src="//cdn.jsdelivr.net/npm/interactjs@1.2.9/dist/interact.min.js"></script>`
33* [cdnjs CDN](https://cdnjs.com/libraries/interact.js): `<script src="//cdnjs.cloudflare.com/ajax/libs/interact.js/1.2.8/interact.min.js"></script>`
34
35Documentation
36-------------
37
38Visit http://interactjs.io/docs for the API documentation.
39
40Example
41-------
42
43```javascript
44var pixelSize = 16;
45
46interact('.rainbow-pixel-canvas')
47 .origin('self')
48 .draggable({
49 snap: {
50 targets: [ interact.createSnapGrid({
51 x: pixelSize, y: pixelSize
52 }) ]
53 },
54 // allow multiple drags on the same element
55 maxPerElement: Infinity
56 })
57 // draw colored squares on move
58 .on('dragmove', function (event) {
59 var context = event.target.getContext('2d'),
60 // calculate the angle of the drag direction
61 dragAngle = 180 * Math.atan2(event.dx, event.dy) / Math.PI;
62
63 // set color based on drag angle and speed
64 context.fillStyle = 'hsl(' + dragAngle + ', 86%, '
65 + (30 + Math.min(event.speed / 1000, 1) * 50) + '%)';
66
67 // draw squares
68 context.fillRect(event.pageX - pixelSize / 2, event.pageY - pixelSize / 2,
69 pixelSize, pixelSize);
70 })
71 // clear the canvas on doubletap
72 .on('doubletap', function (event) {
73 var context = event.target.getContext('2d');
74
75 context.clearRect(0, 0, context.canvas.width, context.canvas.height);
76 });
77
78 function resizeCanvases () {
79 [].forEach.call(document.querySelectorAll('.rainbow-pixel-canvas'), function (canvas) {
80 canvas.width = document.body.clientWidth;
81 canvas.height = window.innerHeight * 0.7;
82 });
83 }
84
85 // interact.js can also add DOM event listeners
86 interact(document).on('DOMContentLoaded', resizeCanvases);
87 interact(window).on('resize', resizeCanvases);
88```
89
90See the above code in action at http://codepen.io/taye/pen/YPyLxE
91
92License
93-------
94
95interact.js is released under the [MIT License](http://taye.mit-license.org).
96
97[ijs-twitter]: https://twitter.com/interactjs
98[upcoming-changes]: https://github.com/taye/interact.js/blob/master/CHANGELOG.md#upcoming-changes