Device agnostic touch handling.
Usage
Require this module doing either
angular.module('myApp', ['mobile-angular-ui.gestures']);
Or standalone
angular.module('myApp', ['mobile-angular-ui.gestures.touch']);
Then you will be able to use the $touch
service like that:
var unbindFn = $touch.bind(element, {
start: function(touchInfo, e);
move: function(touchInfo, e);
end: function(touchInfo, e);
cancel: function(touchInfo, e);
}, options);
Services
$touch service
- $touchProvider - class
- TouchInfo - class
- bind - method
$touch
is an abstraction of touch event handling that works with any kind of input devices.
It is intended for single touch only and provides extended infos about touch like: movement, direction, velocity, duration, and more. $touch service is intended as base to build any single-touch gesture handlers.
Usage
var unbindFn = $touch.bind(element, {
start: function(touchInfo, e);
move: function(touchInfo, e);
end: function(touchInfo, e);
cancel: function(touchInfo, e);
}, options);
Provider in $touch
$touchProvider
class in $touch
Configurable provider for $touch
service
setValid
method in $touchProvider
$touchProvider.setValid(validityFunction)
Set default validity function for a touch.
The default is defined as always true:
$touchProvider.setValid(function(touch, event) {
return true;
});
Param | Type | Description |
---|---|---|
validityFunction | function | The validity function. A function that takes two arguments: |
setMovementThreshold
method in $touchProvider
$touchProvider.setMovementThreshold(threshold)
Set default amount of pixels of movement before start to trigger touchmove
handlers.
Default is 1
.
ie.
$touchProvider.setMovementThreshold(120);
Param | Type | Description |
---|---|---|
threshold | integer | The new treeshold. |
setSensitiveArea
method in $touchProvider
$touchProvider.setSensitiveArea(sensitiveArea)
Set default sensitive area.
The sensitive area of a touch is the area of the screen inside what we consider a touch to be meaningful thus triggering its handlers.
NOTE: if movement goes out the sensitive area the touch event is not cancelled, instead its handler are just ignored.
By default sensitive area is defined as ownerDocument
bounding rectangle of the bound element.
ie.
$touchProvider.setSensitiveArea(function($element) {
return $element[0].ownerDocument.documentElement.getBoundingClientRect();
});
Param | Type | Description |
---|---|---|
sensitiveArea | function | Element | TextRectangle | The new default sensitive area, either static or as function taking an element and returning another element or a rectangle. |
Methods in $touch
bind
method in $touch
$touch.bind(element, eventHandlers, [options]) ⟶ function
Bind touch handlers for an element.
var unbind = $touch.bind(elem, {
end: function(touch) {
console.log('Avg Speed:', touch.averageVelocity);
unbind();
}
});
Param | Type | Description |
---|---|---|
element | Element | $element | The element to bound to. |
eventHandlers | object | An object with handlers for specific touch events. |
[eventHandlers.start] | function | The callback for |
[eventHandlers.end] | function | The callback for |
[eventHandlers.move] | function | The callback for |
[eventHandlers.cancel] | function | The callback for |
[options] | object | Options. |
[options.movementThreshold] | integer | Amount of pixels of movement before start to trigger |
[options.valid] | function | Validity function. A |
[options.sensitiveArea] | function | Element | TextRectangle | A Bounding Client Rect or an element or a function that takes the bound element and returns one of the previous. Sensitive area define bounduaries to release touch when movement is outside. |
[options.pointerTypes] | array | Pointer types to handle. An array of pointer types that is intended to be a subset of keys from default pointer events map (see |
Types in $touch
TouchInfo
class in $touch
TouchInfo
is an object containing the following extended informations about any touch event.
Prop | Type | Description |
---|---|---|
type | string | Normalized event type. Despite of pointer device is always one of |
timestamp | Date | The time object corresponding to the moment this touch event happened. |
duration | integer | The difference between this touch event and the corresponding |
startX | float | X coord of related |
startY | float | Y coord of related |
prevX | float | X coord of previous |
prevY | float | Y coord of previous |
x | float | X coord of this touch event. |
y | float | Y coord of this touch event. |
step | float | Distance between |
stepX | float | Distance between |
stepY | float | Distance between |
velocity | float | Instantaneous velocity of a touch event in pixels per second. |
averageVelocity | float | Average velocity of a touch event from its corresponding |
distance | float | Distance between |
distanceX | float | Distance between |
distanceY | float | Distance between |
total | float | Total number of pixels covered by movement, taking account of direction changes and turnarounds. |
totalX | float | Total number of pixels covered by horizontal movement, taking account of direction changes and turnarounds. |
totalY | float | Total number of pixels covered by vertical, taking account of direction changes and turnarounds. |
direction | string | The current prevalent direction for this touch, one of |
angle | float | Angle in degree between x axis and the vector |
comments powered by Disqus