├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── demo.js ├── index.html ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Jam3 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # touch-pinch 2 | 3 | [](http://github.com/badges/stability-badges) 4 | 5 | A low-level utility for two-finger pinch and panning gestures. 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install touch-pinch --save 11 | ``` 12 | 13 | ## Example 14 | 15 | The following example scales by the delta difference in a two-finger pinch gesture. 16 | 17 | ```js 18 | var pinch = require('touch-pinch') 19 | 20 | var scale = 1 21 | pinch(window) 22 | .on('change', function (dist, prev) { 23 | scale += (dist - prev) 24 | }) 25 | ``` 26 | 27 | ## Usage 28 | 29 | [](https://www.npmjs.com/package/touch-pinch) 30 | 31 | #### `pinch = touchPinch([target])` 32 | 33 | Creates a new `pinch` emitter with the optional `target` element, which defaults to `window`. 34 | 35 | ### events 36 | 37 | #### `pinch.on('start', fn)` 38 | 39 | Called when the pinch event begins; i.e. when two fingers are active on screen. 40 | 41 | Called with `fn(distance)`, which is the initial Euclidean distance between these two points. 42 | 43 | #### `pinch.on('change', fn)` 44 | 45 | Called when the pinch changes; i.e. one or both of the fingers in the pinch have moved. 46 | 47 | Called with `fn(distance, prevDistance)`, where `distance` is the new Euclidean distance, and `prevDistance` is the last recorded distance. Often, you will use this delta to compute a new scale: 48 | 49 | ```js 50 | scale += (distance - prevDistance) 51 | ``` 52 | 53 | #### `pinch.on('end', fn)` 54 | 55 | Called when the pinch is finished; i.e. one or both of the active fingers have been lifted from the screen. 56 | 57 | #### `pinch.on('place', fn)` 58 | 59 | Called before the pinch has started, to indicate that a new finger has been placed on screen (with a maximum of two fingers). 60 | 61 | 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. 62 | 63 | #### `pinch.on('lift', fn)` 64 | 65 | Called before the pinch has ended, to indicate that a previoulsy pinching finger has been lifted. 66 | 67 | 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. 68 | 69 | ### members 70 | 71 | #### `pinch.pinching` 72 | 73 | A read-only boolean; `true` only if the user is currently pinching (two fingers on screen). 74 | 75 | #### `pinch.fingers` 76 | 77 | An array of two elements, which are initially both `null` (representing "no finger"). The elements are the two possible fingers in a pinch event. 78 | 79 | When a finger is present on screen, the element in the array will contain: 80 | 81 | ```js 82 | { 83 | position: [x, y], // the offset relative to target 84 | touch: TouchEvent // the associated event 85 | } 86 | ``` 87 | 88 | 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: 89 | 90 | ```js 91 | [ null, { position, touch } ] 92 | ``` 93 | 94 | ### methods 95 | 96 | #### `pinch.indexOfTouch(touchEvent)` 97 | 98 | Returns the index of `touchEvent` within the `pinch.fingers` array. This can be used to determine 99 | 100 | ## License 101 | 102 | MIT, see [LICENSE.md](http://github.com/Jam3/touch-pinch/blob/master/LICENSE.md) for details. 103 | -------------------------------------------------------------------------------- /demo.js: -------------------------------------------------------------------------------- 1 | var css = require('dom-css') 2 | 3 | var pxSize = 50 4 | var div = document.createElement('div') 5 | document.body.appendChild(div) 6 | css(div, { 7 | width: pxSize, 8 | height: pxSize, 9 | background: 'blue', 10 | position: 'absolute', 11 | left: (document.documentElement.clientWidth - pxSize) / 2, 12 | top: (document.documentElement.clientHeight - pxSize) / 2, 13 | }) 14 | 15 | var pinch = require('./')(window) 16 | 17 | var scale = 1 18 | 19 | window.addEventListener('touchstart', function (ev) { 20 | ev.preventDefault() // no scrolling 21 | }) 22 | 23 | pinch.on('start', function () { 24 | css(div, 'background', 'green') 25 | }) 26 | 27 | pinch.on('end', function () { 28 | css(div, 'background', 'blue') 29 | }) 30 | 31 | pinch.on('change', function (current, prev) { 32 | var delta = (current - prev) * 0.01 33 | scale += delta 34 | css(div, 'transform', 'scale(' + scale.toFixed(5) + ')') 35 | }) 36 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |