Git Product home page Git Product logo

touch-pinch's Introduction

touch-pinch

experimental

A low-level utility for two-finger pinch and panning gestures.

Install

npm install touch-pinch --save

Example

The following example scales by the delta difference in a two-finger pinch gesture.

var pinch = require('touch-pinch')

var scale = 1
pinch(window)
  .on('change', function (event) {
    scale += (event.distance - event.lastDistance)
    event.preventDefault()
  })

Usage

NPM

pinch = touchPinch([target])

Creates a new pinch emitter with the optional target element, which defaults to window.

events

Events are called with the original event object with additional pinch-specific properties assigned. This allows consumers to still do things like event.preventDefault(), for example, to prevent mobile window zooming.

pinch.on('start', fn)

Called when the pinch event begins; i.e. when two fingers are active on screen.

Called with fn({distance}), which is the initial Euclidean distance between these two points.

pinch.on('change', fn)

Called when the pinch changes; i.e. one or both of the fingers in the pinch have moved.

Called with fn({distance, lastDistance}), where distance is the new Euclidean distance, and lastDistance is the last recorded distance. Often, you will use this delta to compute a new scale:

scale += (distance - lastDistance)

pinch.on('end', fn)

Called when the pinch is finished; i.e. one or both of the active fingers have been lifted from the screen.

pinch.on('place', fn)

Called before the pinch has started, to indicate that a new finger has been placed on screen (with a maximum of two fingers).

Called with fn({newTouch, otherTouch}), where newTouch is the new TouchEvent. otherTouch is the touch event that represents the other finger on screen, or undefined if none exists.

pinch.on('lift', fn)

Called before the pinch has ended, to indicate that a previoulsy pinching finger has been lifted.

Called with fn({removedTouch, otherTouch}), where removedTouch is the TouchEvent that was removed from the screen. otherTouch is the touch event for the other finger on screen, or undefined if none exists.

members

pinch.pinching

A read-only boolean; true only if the user is currently pinching (two fingers on screen).

pinch.fingers

An array of two elements, which are initially both null (representing "no finger"). The elements are the two possible fingers in a pinch event.

When a finger is present on screen, the element in the array will contain:

{
  position: [x, y],  // the offset relative to target
  touch: TouchEvent  // the associated event
}

The order is maintained; so if you place a finger, then place a second, then remove the first finger, pinch.fingers will look like this:

[ null, { position, touch } ]

methods

pinch.indexOfTouch(touchEvent)

Returns the index of touchEvent within the pinch.fingers array. This can be used to determine

License

MIT, see LICENSE.md for details.

touch-pinch's People

Contributors

hughsk avatar mattdesl avatar zzmp avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.