interact.js README

The project's README from github.com/taye/interact.js. As one markdown file: README.md.

interact.js

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

Gitter jsDelivr Build Status

Features include:

Installation

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:

npm install --save-dev @interactjs/types

Documentation

http://interactjs.io/docs

Example

var pixelSize = 16;

interact('.rainbow-pixel-canvas')
  .origin('self')
  .draggable({
    modifiers: [
      interact.modifiers.snap({
        // snap to the corners of a grid
        targets: [
          interact.snappers.grid({ x: pixelSize, y: pixelSize }),
        ],
      })
    ],
    listeners: {
      // draw colored squares on move
      move: 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.

interact.js (taye/interact.js)

Searching Database…

这是什么?interact.js 是一个 JavaScript 库,可为网页元素添加拖放、调整大小和多点触控手势支持,并内置惯性和吸附效果。
它是框架还是库?它是一个独立的库,而不是框架:除了可选地更改光标外,它不会操作 DOM,并且可以与任何其他代码或框架一起使用。
我该如何把它添加到项目中?可以通过 npm 安装,运行 'npm install interactjs',也可以从 jsDelivr/unpkg CDN 引入;另外还有 Rails 5.1+ 的 gem 方式(yarn add interactjs 加上一行 require 语句),以及用于 SBT/Play 2 的 Webjars 依赖。
使用它需要 TypeScript 吗?不需要。它是用 TypeScript 编写的,npm 包中附带类型定义;不过如果你只是想在普通 JS 项目中使用这些类型,也可以单独安装类型声明。
它能在 IE 这类旧浏览器上运行吗?可以,它专门支持 IE9 及以上版本,同时也支持现代桌面版和移动版的 Chrome、Firefox 和 Opera。
它能用于触屏设备和 SVG 吗?可以,它支持多点触控,并能在各种设备上处理同时进行的交互,还可以直接操作 SVG 元素。
惯性和吸附功能到底有什么作用?惯性让被拖动或调整大小的元素在松开后像物理物体一样继续短暂移动;吸附则在交互过程中把元素锁定到网格或特定位置。
我能否在页面上同时进行多个拖拽或调整大小的操作?可以,它支持多点触控和同时进行的交互,因此可以同时处理多个手势。
有没有可以直接看效果的在线演示?有,README 中链接了一个可运行的示例页面,可以实时展示该库的代码运行效果。
Creator
taye
Size
29.7 MB
Stars
12.9K
Updated
CommitAug 01
Age
13y
Commits · 90 Days