Skip to content

Adding pinch event #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ steal("jquery",
"jquerypp/event/pause",
"jquerypp/event/resize",
"jquerypp/event/swipe",
"jquerypp/event/pinch",
function($) {
return $;
});
49 changes: 49 additions & 0 deletions event/pinch/pinch.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE HTML>
<html>
<head>
<title>pinch</title>
<style type='text/css'>
body {font-family: verdana}
.error {border: solid 1px red;}
.error_text { color: red; font-size: 10px;}
td {padding: 3px;}
#pinch {
width: 500px;
height: 100px;
background-color: green;
}
li {
border: solid 5px green;
width: 200px;
height: 200px;
margin-left: 400px;
}
</style>
</head>
<body>
<div id='pinch'>Pinch Me</div>
<div id='pinches'>Pinches
<ul>
<li>Pinch Me</li>
<li>Pinch Me</li>
<li>Pinch Me</li>
<li>Pinch Me</li>
</ul>
</div>
<script type='text/javascript' src='../../../steal/steal.js'></script>
<script>
steal('jquery', 'jquerypp/event/pinch', function($) {
$('#pinch').bind('pinch', function(){
console.log('pinch1')
}).bind("pinchin", function(){
console.log('in')
}).bind('pinchout', function(){
console.log('out')
})
$('#pinches').delegate('li','pinch', function(){
console.log('pinch2')
})
})
</script>
</body>
</html>
64 changes: 64 additions & 0 deletions event/pinch/pinch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
steal('jquery', 'jquerypp/event/livehack', function($) {

var isPhantom = /Phantom/.test(navigator.userAgent),
supportGestures = !isPhantom && "ongestureend" in document;

if(!supportGestures) {
return $;
}

/**
* @add jQuery.event.pinch
*/
var pinch = $.event.pinch = {
/**
* @attribute minScale
* minScale is the minimum amount of scale change required to trigger a pinch event. This defaults to 0.01.
*
* The scale of the gestureend event must differ by at least this much.
*/
minScale : 0.01,
};

$.event.setupHelper( [
/**
* @hide
* @attribute pinch
*/
"pinch",
/**
* @hide
* @attribute pinchin
*/
'pinchin',
/**
* @hide
* @attribute pinchout
*/
'pinchout'], "gesturestart", function(ev) {
var delegate = ev.delegateTarget || ev.currentTarget,
selector = ev.handleObj.selector,
entered = this;

$(document.documentElement).one('gestureend', function(event) {
var scale = event.originalEvent.scale,
deltaScale = Math.abs(1 - scale),
events = [];

if(deltaScale >= pinch.minScale) {
events.push('pinch');
if(scale > 1) {
events.push('pinchout')
} else if(scale < 1) {
events.push('pinchin')
}

$.each($.event.find(delegate, events, selector), function(){
this.call(entered, ev);
});
}
})
});

return $;
});
19 changes: 19 additions & 0 deletions event/pinch/pinch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@page jQuery.event.pinch
@parent jquerypp

`jQuery.event.pinch` provides `pinchout`, `pinchin` and a general `pinch` event in browsers that support multi-touch gesture events.
Detecting pinches is done via gesture events.

A pinch happens when two fingers move away or towards each other and change the scale of the document by more than [jQuery.event.pinch.minScale]. The pinch events are fired when the gesture ends, so pinching in and then out will result in a single `pinchout` event.

$('#swiper').on({
'pinch' : function(ev) {
console.log('Pinched')
},
'pinchin' : function(ev) {
console.log('Pinched In')
},
'pinchout' : function(ev) {
console.log('Pinched Out')
}
})